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

	<<12/20/01; 1:23:24 AM by JES
		<<Use file.writeTextFile to write files. This also eliminates a bunch of code which calculated the mac type/creator codes, and line-endings.
	<<12/13/01; 4:47:50 PM by JES
		<<On MacOS X, write text files with a type of '    ' and a creator of 'text'.
	<<12/9/01; 3:08:56 PM by JES
		<<Fixed the check to see if is defined.
	<<11/30/01; 12:53:33 PM by JES
		<<Write files with the line-ending native to the platform. Check, instead of
	<<11/16/01; 2:05:32 PM by JES
		<<Only patch macro delimiters in templates once. This prevents cascading style sheets which are embedded in the #template and #homeTemplate from getting mucked up.
	<<11/1/01; 12:46:57 PM by JES
		<<Fixed an error if weblogData.template, weblogData.dayTemplate, or weblogData.itemTemplate is not defined. These are deprecated in Radio 7.1.
	<<10/18/01; 9:44:16 PM by JES
		<<Don't fail when upgrading files, if the file doesn't exist on disk.
		<<Still to do: make sure that this code respects the last applied theme.
	<<10/9/01; 5:55:45 PM by JES
		<<Only install the home folder text files if is true. This prevents writing out unwanted files which may mess up a Theme.
		<<To cause the home folder text files to be re-written, set to true, and check it in, before checking in
	<<10/6/01; 11:05:43 PM by JES
		<<Make sure the path to a file exists before attempting to write it to disk.
local (pc = file.getPathChar ());
local (adrtable =;
local (homefolder =;
local (adr);
if not defined ( { = false};
for adr in adrtable {
	local (objectname = nameOf (adr^));
	local (f = homefolder + string.replaceAll (objectname, "/", pc));
	<<local (macCreator, macType)
	<<if system.environment.isCarbon //on MacOS X, creator is TextEdit, type is '    '
		<<macCreator = 'text'
		<<macType = '    '
	<<else //for Classic, creator is SimpleText, type is 'TEXT'
		<<macCreator = 'ttxt'
		<<macType = 'TEXT'
	bundle { //set the mac type and creator
		local (extension = string.lower (string.nthField (objectname, '.', string.countFields (objectname, '.'))));
		case extension {
			"opml" {
				macCreator =;
				macType = 'OPML'};
			"html" {
				macCreator = user.html.prefs.textFileCreator}}};
	if {
		<<local (eol = "\r\n")
		<<if system.environment.isMac //eol is either \r or \n
			<<if system.environment.isCarbon
				<<eol = "\n"
			<<else //classic
				<<eol = "\r"
		<<local (filetext = string (adr^))
		<<bundle //convert to the platform-native line ending style
			<<filetext = string.replaceAll (filetext, "\r\n", "\r")
			<<filetext = string.replaceAll (filetext, "\n", "\r")
			<<filetext = string.replaceAll (filetext, "\r", eol)
		file.sureFilePath (f);
		<<file.writewholefile (f, string (adr^), macType, macCreator)
		file.writeTextFile (f, string (adr^));
		file.setcreated (f, timecreated (adr));
		file.setmodified (f, timemodified (adr))}
	else { //do upgrades: set type/creator on MacOS, change { & } to <% & %>, change {text} to <%itemText%>
		if file.exists (f) { //the file may not exist if a Theme has been applied, or if the user has deleted the file
			bundle { //patch templates -- this is bootstrap code which will go away
				local (s = file.readWholeFile (f));
				local (lowername = string.lower (file.fileFromPath (f)));
				if (lowername endsWith ".opml") or (lowername endsWith ".txt") { //upgrade all text and opml files
					if not (lowername beginsWith "#cascadingstylesheet.") { //don't touch the css
						local (flRewriteFile = false);
						if not (lowername beginsWith "#javascript.") { //don't touch the JavaScript
							local (flOutline = false);
							if (lowername endsWith ".opml") {
								local (localOutline);
								op.xmlToOutline (s, @localOutline);
								s = string (localOutline);
								flOutline = true};
							if (s contains "{") and (s contains "}") { //upgrade macro delimiters
								if not (s contains "<%" and s contains "%>") { //already patched -- don't do it again
									s = string.replaceAll (s, "{", "<%");
									s = string.replaceAll (s, "}", "%>");
									flRewriteFile = true}};
							if lowername beginsWith "#itemtemplate" { //change <%text%> to <%itemText%>
								s = string.replaceAll (s, "<%text%>", "<%itemText%>", false);
								flRewriteFile = true};
							if flRewriteFile {
								local (createDate = file.created (f));
								if flOutline {
									local (localOutline);
									op.newOutlineObject (s, @localOutline);
									s = op.outlineToXml (@localOutline)};
								<<file.writeWholeFile (f, s, macType, macCreator, createDate)
								file.writeTextFile (f, s)}}}}}}}};
			<<bundle //on the mac, make sure the type and creator code are correct
				<<if system.environment.isMac
					<<file.setType (f, macType)
					<<file.setCreator (f, macCreator)
			<<bundle //patch the weblog templates in weblogData.root, if defined
				<<local (adrweblog = radio.weblog.init ())
				<<local (adrprefs = @adrweblog^.prefs)
				<<on patchTemplate (adrtemplate)
					<<if not defined (adrtemplate^)
					<<local (s = string (adrtemplate^))
					<<if (s contains "{") and (s contains "}") //upgrade macro delimiters
						<<s = string.replaceAll (s, "{", "<%")
						<<s = string.replaceAll (s, "}", "%>")
						<<flRewriteFile = true
					<<op.newOutlineObject (s, adrtemplate)
				<<patchTemplate (@adrprefs^.template)
				<<patchTemplate (@adrprefs^.dayTemplate)
				<<patchTemplate (@adrprefs^.itemTemplate) = false

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.