<commit_before>package plugins.Sharesite.webui;

import java.io.IOException;
import java.net.URI;
import java.util.Calendar;
import java.util.TimeZone;
import java.util.Locale;
import java.util.regex.Pattern;
import java.util.regex.Matcher;
import java.io.BufferedReader;
import java.io.StringReader;

import plugins.Sharesite.Freesite;
import plugins.Sharesite.Plugin;
import freenet.clients.http.InfoboxNode;
import freenet.clients.http.PageMaker;
import freenet.clients.http.PageNode;
import freenet.clients.http.Toadlet;
import freenet.clients.http.ToadletContext;
import freenet.clients.http.ToadletContextClosedException;
import freenet.l10n.BaseL10n;
import freenet.pluginmanager.PluginRespirator;
import freenet.support.HTMLNode;
import freenet.support.api.HTTPRequest;

public class EditToadlet extends Toadlet {
	private PluginRespirator pr;
	private PageMaker pageMaker;
	private BaseL10n l10n;

	protected EditToadlet() {
		super(null);

		pr = Plugin.instance.pluginRespirator;
		pageMaker = pr.getPageMaker();
		l10n = Plugin.instance.l10n;
	}

	@Override
	public String path() {
		return "/Sharesite/Edit/";
	}

	// Gets called by the freenet node
	public void handleMethodGET(URI uri, HTTPRequest req, ToadletContext ctx) throws ToadletContextClosedException, IOException {
		// Get freesite, or return to the menu
		String[] split = uri.getPath().split("/");
		int siteId = -1;
		try {
			siteId = Integer.parseInt(split[3]);
		} catch (Exception e) {
		}

		Freesite c = Plugin.instance.database.getFreesiteWithUniqueKey(siteId);
		if (c == null) {
			writeTemporaryRedirect(ctx, "Redirecting...", "/Sharesite/");
			return;
		}

		PageNode pageNode = pageMaker.getPageNode(l10n.getString("Sharesite.Menu.Name"), ctx);
		HTMLNode editForm = pr.addFormChild(pageNode.content,"/Sharesite/Edit/" + siteId, "editForm");
		String iHour = Integer.toString(c.getInsertHour());
		addNodes(editForm,c.getName(),c.getPath(),c.getDescription(),c.getText(),c.getCSS(), c.getActivelinkUri(),c.getRequestSSK(),c.getInsertSSK(),iHour);
		String ret = pageNode.outer.generate();
		writeHTMLReply(ctx, 200, "OK", ret);
	}


	private void addNodes( HTMLNode form, String name, String path, String desc, String text, String css, String aUri, String rkey, String ikey, String iHour) {
		String[] attrs;
		String[] vals;


		InfoboxNode editBox = pageMaker.getInfobox(l10n.getString("Sharesite.Edit.Header"));
		form.addChild(editBox.outer);

		// Menu link
		HTMLNode quickLinks = editBox.content.addChild("p");
		quickLinks.addChild("a", "href", "/Sharesite/",	l10n.getString("Sharesite.Edit.MenuLink"));

		// Buttons above
		HTMLNode topBtnDiv = editBox.content.addChild("p");
		attrs = new String[] { "type", "name", "value" };
		vals = new String[] { "submit", "saveBtn",l10n.getString("Sharesite.Edit.SaveBtn") };
		topBtnDiv.addChild("input", attrs, vals);

		attrs = new String[] { "type", "name", "value" };
		vals = new String[] { "submit", "previewBtn",	l10n.getString("Sharesite.Edit.PreviewBtn") };
		topBtnDiv.addChild("input", attrs, vals);

		attrs = new String[] { "type", "name", "value" };
		vals = new String[] { "submit", "preprocessBtn",l10n.getString("Sharesite.Edit.PreprocessBtn") };
		topBtnDiv.addChild("input", attrs, vals);

		// Edit boxes
		HTMLNode nameDiv = editBox.content.addChild("p");
		HTMLNode nameSpan = nameDiv.addChild("span");
		nameSpan.addChild("span",l10n.getString("Sharesite.Edit.Name"));
		nameSpan.addChild("br");
		attrs = new String[] { "type", "size", "name", "value" };
		vals = new String[] { "text", "80", "nameInput", name };
		nameDiv.addChild("input", attrs, vals);

		HTMLNode pathDiv = editBox.content.addChild("p");
		HTMLNode pathSpan = pathDiv.addChild("span");
		pathSpan.addChild("span",l10n.getString("Sharesite.Edit.Path"));
		pathSpan.addChild("br");
		attrs = new String[] { "type", "size", "name", "value" };
		vals = new String[] { "text", "80", "pathInput", path };
		pathDiv.addChild("input", attrs, vals);

		HTMLNode descDiv = editBox.content.addChild("p");
		HTMLNode descSpan = descDiv.addChild("span");
		descSpan.addChild("span",
		                  l10n.getString("Sharesite.Edit.Description"));
		descSpan.addChild("br");
		attrs = new String[] { "name", "rows", "cols", "style" };
		vals = new String[] { "descInput", "3", "80", "font-size: medium;" };
		descDiv.addChild("textarea", attrs, vals, desc);

		HTMLNode textDiv = editBox.content.addChild("p");
		HTMLNode textSpan = textDiv.addChild("span");
		textSpan.addChild("span",l10n.getString("Sharesite.Edit.Text"));
		textSpan.addChild("br");
		attrs = new String[] { "name", "rows", "cols", "style" };
		vals = new String[] { "textInput", "20", "80", "font-size: medium;" };
		textDiv.addChild("textarea", attrs, vals, text);

		HTMLNode insertHour = editBox.content.addChild("p");
		insertHour.addChild("span",l10n.getString("Sharesite.Edit.InsertHour"));
		insertHour.addChild("br");

		attrs = new String[] { "type", "size", "name", "value" };
		vals = new String[] { "text",  "2", "insertHourInput",	iHour };
		insertHour.addChild("input", attrs, vals);
		Integer currentHour = Calendar.getInstance(TimeZone.getTimeZone("UTC"), Locale.US)
				.get(Calendar.HOUR_OF_DAY); // 0-23
		Integer currentMinute = Calendar.getInstance(TimeZone.getTimeZone("UTC"), Locale.US)
				.get(Calendar.MINUTE); // 0-59
		insertHour.addChild("span",l10n.getString("Sharesite.Edit.InsertHourDescription",
							  "utchour", Integer.toString(currentHour)));

		// Syntax
		HTMLNode syntaxHelpNode = editBox.content.addChild("p",l10n.getString("Sharesite.Edit.TextSyntax"));
		HTMLNode syntaxTable = syntaxHelpNode.addChild("table");
		
		HTMLNode syntaxHelp = syntaxTable.addChild("tr");
		HTMLNode syntaxHelpTextile = syntaxHelp.addChild("td", "h2. headline (or h3, h4, ..., followed by an empty line)");
		HTMLNode syntaxHelpHtml = syntaxHelp.addChild("td");
		HTMLNode syntaxHelpHtmlContent = syntaxHelpHtml.addChild("h2", "headline (or h3, h4, ...)");
		
		syntaxHelp = syntaxTable.addChild("tr");
		syntaxHelpTextile = syntaxHelp.addChild("td", "{toc}");
		syntaxHelpHtml = syntaxHelp.addChild("td", "(as the only element in a line inserts table of contents for h2,h3,h4)");
		
		syntaxHelp = syntaxTable.addChild("tr");
		syntaxHelpTextile = syntaxHelp.addChild("td", "bc. code block");
		syntaxHelpTextile.addChild("br");
		syntaxHelpTextile.addChild("span", "continuing (end with empty line)");
		syntaxHelpHtml = syntaxHelp.addChild("td");
		syntaxHelpHtmlContent = syntaxHelpHtml.addChild("pre").addChild("code", "code block\ncontinuing");<commit_msg>remove unused variable, thanks to Bombe<commit_after>package plugins.Sharesite.webui;

import java.io.IOException;
import java.net.URI;
import java.util.Calendar;
import java.util.TimeZone;
import java.util.Locale;
import java.util.regex.Pattern;
import java.util.regex.Matcher;
import java.io.BufferedReader;
import java.io.StringReader;

import plugins.Sharesite.Freesite;
import plugins.Sharesite.Plugin;
import freenet.clients.http.InfoboxNode;
import freenet.clients.http.PageMaker;
import freenet.clients.http.PageNode;
import freenet.clients.http.Toadlet;
import freenet.clients.http.ToadletContext;
import freenet.clients.http.ToadletContextClosedException;
import freenet.l10n.BaseL10n;
import freenet.pluginmanager.PluginRespirator;
import freenet.support.HTMLNode;
import freenet.support.api.HTTPRequest;

public class EditToadlet extends Toadlet {
	private PluginRespirator pr;
	private PageMaker pageMaker;
	private BaseL10n l10n;

	protected EditToadlet() {
		super(null);

		pr = Plugin.instance.pluginRespirator;
		pageMaker = pr.getPageMaker();
		l10n = Plugin.instance.l10n;
	}

	@Override
	public String path() {
		return "/Sharesite/Edit/";
	}

	// Gets called by the freenet node
	public void handleMethodGET(URI uri, HTTPRequest req, ToadletContext ctx) throws ToadletContextClosedException, IOException {
		// Get freesite, or return to the menu
		String[] split = uri.getPath().split("/");
		int siteId = -1;
		try {
			siteId = Integer.parseInt(split[3]);
		} catch (Exception e) {
		}

		Freesite c = Plugin.instance.database.getFreesiteWithUniqueKey(siteId);
		if (c == null) {
			writeTemporaryRedirect(ctx, "Redirecting...", "/Sharesite/");
			return;
		}

		PageNode pageNode = pageMaker.getPageNode(l10n.getString("Sharesite.Menu.Name"), ctx);
		HTMLNode editForm = pr.addFormChild(pageNode.content,"/Sharesite/Edit/" + siteId, "editForm");
		String iHour = Integer.toString(c.getInsertHour());
		addNodes(editForm,c.getName(),c.getPath(),c.getDescription(),c.getText(),c.getCSS(), c.getActivelinkUri(),c.getRequestSSK(),c.getInsertSSK(),iHour);
		String ret = pageNode.outer.generate();
		writeHTMLReply(ctx, 200, "OK", ret);
	}


	private void addNodes( HTMLNode form, String name, String path, String desc, String text, String css, String aUri, String rkey, String ikey, String iHour) {
		String[] attrs;
		String[] vals;


		InfoboxNode editBox = pageMaker.getInfobox(l10n.getString("Sharesite.Edit.Header"));
		form.addChild(editBox.outer);

		// Menu link
		HTMLNode quickLinks = editBox.content.addChild("p");
		quickLinks.addChild("a", "href", "/Sharesite/",	l10n.getString("Sharesite.Edit.MenuLink"));

		// Buttons above
		HTMLNode topBtnDiv = editBox.content.addChild("p");
		attrs = new String[] { "type", "name", "value" };
		vals = new String[] { "submit", "saveBtn",l10n.getString("Sharesite.Edit.SaveBtn") };
		topBtnDiv.addChild("input", attrs, vals);

		attrs = new String[] { "type", "name", "value" };
		vals = new String[] { "submit", "previewBtn",	l10n.getString("Sharesite.Edit.PreviewBtn") };
		topBtnDiv.addChild("input", attrs, vals);

		attrs = new String[] { "type", "name", "value" };
		vals = new String[] { "submit", "preprocessBtn",l10n.getString("Sharesite.Edit.PreprocessBtn") };
		topBtnDiv.addChild("input", attrs, vals);

		// Edit boxes
		HTMLNode nameDiv = editBox.content.addChild("p");
		HTMLNode nameSpan = nameDiv.addChild("span");
		nameSpan.addChild("span",l10n.getString("Sharesite.Edit.Name"));
		nameSpan.addChild("br");
		attrs = new String[] { "type", "size", "name", "value" };
		vals = new String[] { "text", "80", "nameInput", name };
		nameDiv.addChild("input", attrs, vals);

		HTMLNode pathDiv = editBox.content.addChild("p");
		HTMLNode pathSpan = pathDiv.addChild("span");
		pathSpan.addChild("span",l10n.getString("Sharesite.Edit.Path"));
		pathSpan.addChild("br");
		attrs = new String[] { "type", "size", "name", "value" };
		vals = new String[] { "text", "80", "pathInput", path };
		pathDiv.addChild("input", attrs, vals);

		HTMLNode descDiv = editBox.content.addChild("p");
		HTMLNode descSpan = descDiv.addChild("span");
		descSpan.addChild("span",
		                  l10n.getString("Sharesite.Edit.Description"));
		descSpan.addChild("br");
		attrs = new String[] { "name", "rows", "cols", "style" };
		vals = new String[] { "descInput", "3", "80", "font-size: medium;" };
		descDiv.addChild("textarea", attrs, vals, desc);

		HTMLNode textDiv = editBox.content.addChild("p");
		HTMLNode textSpan = textDiv.addChild("span");
		textSpan.addChild("span",l10n.getString("Sharesite.Edit.Text"));
		textSpan.addChild("br");
		attrs = new String[] { "name", "rows", "cols", "style" };
		vals = new String[] { "textInput", "20", "80", "font-size: medium;" };
		textDiv.addChild("textarea", attrs, vals, text);

		HTMLNode insertHour = editBox.content.addChild("p");
		insertHour.addChild("span",l10n.getString("Sharesite.Edit.InsertHour"));
		insertHour.addChild("br");

		attrs = new String[] { "type", "size", "name", "value" };
		vals = new String[] { "text",  "2", "insertHourInput",	iHour };
		insertHour.addChild("input", attrs, vals);
		Integer currentHour = Calendar.getInstance(TimeZone.getTimeZone("UTC"), Locale.US)
				.get(Calendar.HOUR_OF_DAY); // 0-23
		insertHour.addChild("span",l10n.getString("Sharesite.Edit.InsertHourDescription",
							  "utchour", Integer.toString(currentHour)));

		// Syntax
		HTMLNode syntaxHelpNode = editBox.content.addChild("p",l10n.getString("Sharesite.Edit.TextSyntax"));
		HTMLNode syntaxTable = syntaxHelpNode.addChild("table");
		
		HTMLNode syntaxHelp = syntaxTable.addChild("tr");
		HTMLNode syntaxHelpTextile = syntaxHelp.addChild("td", "h2. headline (or h3, h4, ..., followed by an empty line)");
		HTMLNode syntaxHelpHtml = syntaxHelp.addChild("td");
		HTMLNode syntaxHelpHtmlContent = syntaxHelpHtml.addChild("h2", "headline (or h3, h4, ...)");
		
		syntaxHelp = syntaxTable.addChild("tr");
		syntaxHelpTextile = syntaxHelp.addChild("td", "{toc}");
		syntaxHelpHtml = syntaxHelp.addChild("td", "(as the only element in a line inserts table of contents for h2,h3,h4)");
		
		syntaxHelp = syntaxTable.addChild("tr");
		syntaxHelpTextile = syntaxHelp.addChild("td", "bc. code block");
		syntaxHelpTextile.addChild("br");
		syntaxHelpTextile.addChild("span", "continuing (end with empty line)");
		syntaxHelpHtml = syntaxHelp.addChild("td");
		syntaxHelpHtmlContent = syntaxHelpHtml.addChild("pre").addChild("code", "code block\ncontinuing");
{'fraction_non_alphanumeric': 0.16327467482785005, 'fraction_numerical': 0.010482019892884468, 'mean_word_length': 5.19478672985782, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4882747', 'n_tokens_mistral': 4759, 'n_tokens_neox': 4384, 'n_words': 888}