Monday, April 04, 2011 at 1:08 AM.

radio2Suite.buildHtmlArchive

on buildHtmlArchive (adruser, adrfeed) {
	<<Changes
		<<3/17/11; 12:58:12 AM by DW
			<<Add item-level permalinks.
		<<3/10/11; 9:04:12 AM by DW
			<<Get the template from the user's prefs table.
		<<3/3/11; 10:56:55 PM by DW
			<<Properly handle items with links in the HTML rendering. Example: http://static.reallysimple.org/users/dave/wikileaks.html.
		<<3/1/11; 9:54:24 PM by DW
			<<Account for the fact that string.urlsplit can fail if the link isn't properly formatted.
		<<3/1/11; 4:25:08 PM by DW
			<<Fixed bug where it would fail if an item had an empty link.
		<<2/28/11; 7:00:19 PM by DW
			<<Build the HTML archive.
	local (t, now = clock.now (), templatetext, imgPermalink);
	bundle { //set up images
		imgPermalink = string (radio2Suite.data.html.permalink)};
	bundle { //setup t
		new (tabletype, @t);
		t.title = adrfeed^.prefs.htmlArchive.pagetitle;
		t.feedUrl = adrfeed^.stats.feedUrl;
		t.xmlIcon = string (radio2Suite.data.html.xmlFull); //3/10/11 by DW
		t.now = now};
	bundle { //generate t.reportText
		local (htmltext = "", indentlevel = 0, lastday = date (0));
		local (htmlarray, ct=0, maxct = adrfeed^.prefs.htmlArchive.maxct, currentdaytext = "", flfirstday = true);
		local (imgleftarrow = string (radio2Suite.data.html.blueLeftArrow));
		new (tabletype, @htmlarray);
		on add (s) {
			if sizeof (htmltext) > 50000 {
				htmlarray.[string.padwithzeros (sizeof (htmlarray), 5)] = htmltext;
				htmltext = ""};
			htmltext = htmltext + string.filledstring ("\t", indentlevel) + s + "\r\n";};
		on visit (adritem) {
			local (when = adritem^.when, domain = "");
			if not date.sameday (lastday, when) {
				local (aname, leftarrow);
				bundle { //set aname, leftarrow
					local (day, month, year, hour, minute, second);
					date.get (when, @day, @month, @year, @hour, @minute, @second);
					local (label = "linksFor" + day + date.monthtostring (month) + year);
					aname = "<a name=\"" + label + "\"></a>";
					leftarrow = " <a href=\"#" + label + "\">" + imgleftarrow + "</a>"};
				add ("<h4>" + aname + date.longstring (when) + leftarrow + "</h4>"); add ("");
				<<msg (lastday)
				<<bundle //handle callback on first day's text
					<<if flfirstday and (sizeof (currentdaytext) > 0)
						<<local (adrscript)
						<<for adrscript in @adrdata^.callbacks.buildArchiveDay
							<<while typeof (adrscript^) == addresstype
								<<adrscript = adrscript^
							<<try
								<<adrscript^ (currentdaytext, lastday)
							<<else
								<<scratchpad.lastbuilderror = tryerror
						<<flfirstday = false
					<<currentdaytext = ""
				lastday = when};
			bundle { //set domain
				if sizeof (adritem^.link) > 0 {
					local (link = adritem^.link);
					try { //string.urlsplit can fail if the link isn't properly formatted.
						local (ul = string.urlsplit (link));
						domain = string.lower (ul [2]);
						if domain beginswith "www." {
							domain = string.delete (domain, 1, 4)}}}};
						<<if domain endswith ".co.uk" //do an extra popsuffix
							<<domain = string.popsuffix (domain)
						<<domain = string.popsuffix (domain) //get rid of .com, .net, .org
						<<if domain contains "."
							<<domain = string.lastfield (domain, ".")
						<<adritem^.domainFull = domain
			local (s, title = string.trimwhitespace (adritem^.title), permalink = "p" + number (nameof (adritem^)));
			bundle { //set s
				local (i);
				s = "<a name=\"" + permalink + "\"></a>" + string.trimwhitespace (adritem^.description);
				if s endswith "</a>" { //if it ends with a link, pop it off
					for i = sizeof (s) downto 1 {
						if s [i] == ' ' {
							s = string.mid (s, 1, i-3);
							break}}}};
			if sizeof (title) == 0 {
				if sizeof (domain) > 0 {
					s = s + " <a href=\"" + adritem^.link + "\">" + domain + "</a>"}}
			else {
				s = "<a href=\"" + adritem^.link + "\">" + title + "</a> " + s};
			s = s + " <a href=\"#" + permalink + "\">" + imgPermalink + "</a><br /><br />";
			add (s);
			currentdaytext = currentdaytext + s; //12/6/10 by DW
			return (ct++ < maxct)};
		mainresponder.calendar.visitReverseChronologic (@adrfeed^.calendar, @visit);
		bundle { //build t.reportText from htmlarray
			local (i);
			t.reportText = "";
			for i = 1 to sizeof (htmlarray) {
				t.reportText = t.reportText + htmlarray [i]};
			t.reportText = t.reportText + htmltext}};
	bundle { //save file
		local (s = string.multiplereplaceall (string (adruser^.prefs.htmlArchiveTemplate), @t, false, "<%", "%>"));
		adrfeed^.stats.htmlUrl = radio2Suite.writeStaticFile (adruser, adrfeed^.prefs.htmlArchive.filename, s);
		adrfeed^.stats.ctArchiveBuilds++;
		adrfeed^.stats.whenLastArchiveBuild = clock.now ()}};
bundle { //test code
	buildhtmlarchive (@config.radio2.users.dave, @config.radio2.users.dave.feeds.["linkblog.xml"])}



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.