Monday, November 08, 2010 at 12:05 AM.

on editTextFile () {
		<<9/17/04; 1:53:28 PM by SK
			<<Bug fix to name the form and allow better compatibility with the WYSIWYG text editor
		<<8/11/03; 4:02:09 AM by JES
			<<Added support for the Mozilla rich text editor.
		<<1/28/02; 6:27:11 PM by JES
			<<On MacOS, convert from Mac to Latin when sending file text to the browser for editing, and convert back to Mac text before writing the file to disk.
		<<1/26/02; 2:59:03 AM by JES
			<<Make sure that the editor has a minimum vertical size.
		<<1/25/02; 6:41:48 PM by JES
			<<Handles editing of text files. Takes two getArgs from an Edit This Page button: f, and referer. The user edits the file in a textarea on the page. When done, they click a Post Changes button, the file is written to disk, and the browser is redirected to the referer.
			<<Referer deafults to the home page, if not present.
			<<If f is not present, an (innocuous) error message appears in place of the editing form.
	local (pta = html.getPageTableAddress ());
	local (adrblog = radio.weblog.init ());
	pta^.editingToolName = adrblog^.prefs.editingToolName;
	local (f, referer = "http://" + pta^.host + pta^.path);
	if pta^.method == "POST" { //write changes, redirect to referer
		local (adrargs = @pta^.radioResponder.postArgs);
		if defined (adrargs^.postChanges) {
			f = radio.file.getAbsolutePath (adrargs^.f);
			if defined (adrargs^.referer) {
				referer = adrargs^.referer};
			local (filetext = adrargs^.itemtext);
			if system.environment.isMac {
				filetext = string.latinToMac (filetext)};
			bundle { //add directives to the begining of filetext -- this only happens when using the wysiwyg editor
				local (adr);
				for adr in adrargs {
					local (name = nameOf (adr^));
					local (val = adr^);
					if system.environment.isMac {
						val = string.latinToMac (val)};
					if name beginsWith "#" {
						filetext = (name + " " + val + "\r\n") + filetext}}};
			file.writeTextFile (f, filetext);
			radio.webserver.redirect (referer);
			return ("")}};
	local (htmltext);
	on add (s) {
		htmltext = htmltext + s + "\r"};
	local (adrargs = @pta^.radioResponder.getArgs);
	if defined (adrargs^.referer) {
		referer = adrargs^.referer};
	if defined (adrargs^.f) { //render the editing form
		f = radio.file.getAbsolutePath (adrargs^.f);
		local (title, adrfile);
		radio.file.getFileAttributes (f, @adrfile);
		title = adrfile^.title;
		pta^.title = radio.string.getlocalizedstring ("editTextFile.titlePrefix") + title;
		add (html.editor.formStart ("itemtext", name:"editPostForm", pta:pta));
		local (ctrows);
		bundle { //get filetext, and possibly create hidden fields for directives
			filetext = file.readWholeFile (f);
			filetext = radio.html.translateToEntities (filetext);
			filetext = string.replaceAll (filetext, "\r\n", "\r");
			filetext = string.replaceAll (filetext, "\n", "\r");
			if html.editor.getEditingTool (pta) contains "wysiwyg" { //put directives in hidden fields if using the WYSIWYG editor
				loop {
					if sizeOf (filetext) == 0 { // account for files that contain nothing but directives (or are empty)
					if filetext[1] == "#" { //read in a directive, and add a hidden field
						local (directive = string.nthField (filetext, "\r", 1));
						local (directiveName = string.nthField (directive, " ", 1));
						local (ixOperand = string.patternMatch (" ", directive) + 1);
						local (operand = string.mid (directive, ixOperand, infinity));
						add ("<input type=\"hidden\" name=\"" + directiveName + "\" value=\"" + operand + "\">");
						filetext = string.delete (filetext, 1, sizeOf (directive) + 1)}
					else { //done reading directives
			ctrows = string.countFields (filetext, "\r") * 2;
			if system.environment.isMac {
				filetext = string.macToLatin (filetext)};
			case html.editor.getEditingTool (pta) {
				"wysiwygEditorMozilla" { //pre-process for Mozilla's rich text editor
					}}//do nothing
					<<if filetext contains "\r\r"
						<<filetext = string.replaceAll (filetext, "\r\r", "<br><br>")
			else { //other editors
				filetext = string.replaceAll (filetext, "\r", "
		if ctrows < 15 {ctrows = 15};
		add (radio.userInterface.editorBox (filetext, ctrows, 70));
		add ("<input type=\"hidden\" name=\"f\" value=\"" + radio.file.getRelativePath (f) + "\">");
		add ("<input type=\"hidden\" name=\"referer\" value=\"" + referer + "\"><br><br>");
		add ("<input type=\"submit\" name=\"postChanges\" value=\"" + radio.string.getlocalizedstring ("editTextFile.postChangesButton") + "\">");
		add ("</form>");
		add ("<br><br><br>")} //add some white-space below the Post Changes button
	else { //no file specified -- display an error message
		add (radio.string.getlocalizedstring ("editTextFile.noFileSpecifiedError"))};
	return (htmltext)}

This listing is for code that runs in the OPML Editor environment. I created these listings because I wanted the search engines to index it, so that when I want to look up something in my codebase I don't have to use the much slower search functionality in my object database. Dave Winer.