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

system.verbs.builtins.radio.weblog.oldStuff.publish

on publish (adrblog=radio.weblog.init (), catname=nil, d=nil, adrconnection=nil) {
	<<Changes:
		<<1/19/01; 3:24:14 PM by DW
			<<If an item has an enclosure, link to it through the {enclosure} macro. If it has no enclosure, the icon isn't displayed.
		<<1/24/01; 5:54:25 PM by DW
			<<If an item has a source, link to it through the {enclosure} macro. If it has no source, the icon isn't displayed.
		<<1/25/01; 10:53:08 AM by DW
			<<Added alt attributes for images, working towards validation.
			<<Reorganize code.
			<<Do notification in a thread. (So it can wait for an ignored response while we're FTPing.)
			<<Add optional param that allows the rendering to be saved in a different file but in the same folder as the home page. 
		<<1/25/01; 2:49:06 PM by DW
			<<Set the htmlUrl for categories. Give precedence to the FTP url over the upstreaming url.
		<<1/25/01; 11:46:14 PM by JES
			<<Implement mirrroring on Manila sites.
		<<2/21/01; 4:26:46 PM by PBS
			<<New macro: rssLink gets you an XML button that links to the RSS version of your weblog.
		<<2/22/01; 11:49:14 AM by PBS
			<<d is an optional parameter, a date in an archive day that you want to publish.
			<<archiveLink macro points to the archive page for a day.
		<<2/22/01; 5:06:08 PM by JES
			<<adrconnection is a new optional parameter, the connection table for an FTP session already opened. This is passed to radio.weblog.uploadFile.
		<<2/22/01; 9:32:15 PM by JES
			<<Convert accented characters in the HTML rendering to entities. If this is a Mac, convert text to Latin for Manila mirroring.
		<<2/23/01; 3:27:05 PM by PBS
			<<Use xml.gif and dailyLinkIcon.gif on UserLand servers -- don't attempt to upload or upstream.
		<<2/23/01; 6:12:56 PM by PBS
			<<New macro: {radioBadge} adds a Radio badge.
		<<2/24/01; 1:57:57 AM by JES
			<<Fixed terminology mismatch; changed Blog Publish to Weblog Publish.
		<<2/27/01; 4:44:07 PM by PBS
			<<Get the Radio badge from the global glossary.
		<<3/1/01; 11:22:56 PM by JES
			<<Respect user.html.prefs.textFileCreator when writing out the file.
		<<3/2/01; 12:35:00 PM by JES
			<<Use the same file extension for writing archive files as was specified for the home page.
		<<3/18/01; 2:50:03 PM by PBS
			<<The default minimum and maximum date strings didn't work on some international systems -- there was an error coercing to date. So use date.set instead.
		<<3/18/01; 4:07:28 PM by PBS
			<<Fixed the incorrect archive link URL on category pages.
		<<8/15/01; 6:50:56 PM by JES
			<<Changed macro delimiters from { and } to <% and %>.
	local (startticks = clock.ticks ());
	local (filepath = adrblog^.prefs.homePageFilePath, adrcat);
	local (flArchivePage = (d != nil), archiveFtpPath);
	local (minDate = date.set (1, 1, 1904, 0, 0, 0), maxDate = date.set (1, 1, 2040, 0, 0, 0));
	bundle { //set filepath, adrcat
		if flArchivePage { //PBS 02/22/01: set the filepath for an archive day
			local (extension = "");
			bundle { //get the extension from the home page filename
				if filepath endsWith "/" { //default to .html
					extension = ".html"}
				else {
					local (homePageFileName = string.nthField (filepath, "/", string.countFields (filepath, "/")));
					if homePageFileName contains "." {
						extension = "." + string.nthField (homePageFileName, ".", string.countFields (homePageFileName, "."))}}};
			filepath = string.popSuffix (filepath, '/');
			local (day, month, year, hour, minute, second);
			date.get (d, @day, @month, @year, @hour, @minute, @second);
			archiveFtpPath = year + "/" + string.padWithZeros (month, 2) + "/" + string.padWithZeros (day, 2) + extension;
			filepath = filepath + "/" + archiveFtpPath;
			minDate = date.set (day, month, year, 0, 0, 0); //12 a.m.
			maxDate = date.set (day, month, year, 23, 59, 59)}; //11:59:59 p.m.
		if catname != nil {
			local (folderpath = string.popTrailingFilename (filepath));
			adrcat = @adrblog^.categories.[catname];
			filepath = folderpath + "categories/" + adrcat^.filename + ".html";
			if catname != nil { //set the htmlUrl
				adrcat^.htmlUrl = user.playlist.prefs.wwwUpstreamFolderUrl + filepath}}};
	local (f); //the local file to write to
	bundle { //set f
		local (pc = file.getpathchar ());
		local (partialpath = string.replaceall (filepath, "/", pc));
		if (partialpath beginswith pc) and (user.radio.prefs.wwwfolder endswith pc) {
			partialpath = string.delete (partialpath, 1, 1)};
		f = user.radio.prefs.wwwfolder + partialpath};
	local (ctdays = 0, maxdays = adrblog^.prefs.ctDaysToDisplay, lastday = -1, itemstext = "", lastitemdate);
	
	local (dateList = {}, textList = {});
	local (bodytext);
	bundle { //set bodytext
		on addDay () {
			if adrblog^.prefs.manila.enabled { //01/25/01 JES: build homepage text for sending to a Manila site
				dateList = dateList + lastitemdate;
				if system.environment.isMac { //02/22/2001 JES: convert to Latin text
					textList = textList + latinToMac.macToLatin (itemstext)}
				else {
					textList = textList + itemstext}};
			local (s = string (adrblog^.prefs.daytemplate));
			s = string.replaceall (s, "<%longDate%>", date.longstring (lastitemdate), false);
			s = string.replaceall (s, "<%shortDate%>", date.shortstring (lastitemdate), false);
			s = string.replaceall (s, "<%dayOfWeek%>", date.dayOfWeekToString (date.dayOfWeek (lastitemdate)), false);
			s = string.replaceall (s, "<%items%>", itemstext, false); itemstext = "";
			bundle { //PBS 02/22/01: archive link
				local (day, month, year, hour, minute, second);
				date.get (lastitemdate, @day, @month, @year, @hour, @minute, @second);
				local (dateString = string.mid (year, 3, 2) + "/" + string.padWithZeros (month, 2) + "/" + string.padWithZeros (day, 2));
				local (imgTag = "<img src=\"http://scripting.com/images/dailyLinkIcon.gif\" height=\"19\" width=\"13\" alt=\"Permanent link to archive for " + dateString + ".\" border=\"0\">");
				local (archiveUrl = "");
				if not flArchivePage {
					archiveUrl = year + "/" + string.padWithZeros (month, 2) + "/" + string.padWithZeros (day, 2) + ".html"};
				if catName != nil { //PBS 03/18/01: the archive URL for category pages needs to pop up a level
					archiveUrl = "../" + archiveUrl};
				local (archiveLink = html.getLink (imgTag, archiveUrl));
				s = string.replaceAll (s, "<%archiveLink%>", archiveLink, false)};
			bodytext = bodytext + s + "\r\n"};
		bodytext = "\r\n";
		local (i, adrpost, s, enclosurelink);
		for i = sizeof (adrblog^.posts) downto 1 {
			adrpost = @adrblog^.posts [i];
			bundle { //PBS 02/22/01: check min and max dates
				if adrpost^.when < minDate {
					break};
				if adrpost^.when > maxDate {
					continue}};
			bundle { //only include if in category, if catname was specified
				if catname != nil {
					local (flskip = true);
					if defined (adrpost^.categories) {
						local (adrthiscat = @adrpost^.categories.[catname]);
						if defined (adrthiscat^) {
							if adrthiscat^ {
								flskip = false}}};
					if flskip {
						continue}}};
			bundle { //check date rollover
				local (when = adrpost^.when, day, month, year, hour, minute, second);
				date.get (when, @day, @month, @year, @hour, @minute, @second);
				if lastday == -1 { //first time through loop
					lastday = day}
				else {
					if day != lastday {
						ctdays++;
						if ctdays == maxdays {
							break};
						lastday = day;
						addDay ()}}};
			bundle { //set enclosurelink
				if defined (adrpost^.enclosure) {
					enclosurelink = "<a href=\"" + adrpost^.enclosure.url + "\">" + radio.data.strings.enclosureImg + "</a>"}
				else {
					enclosurelink = ""}};
			local (sourcelink = "");
			bundle { //set sourcelink
				if defined (adrpost^.sourceUrl) {
					local (adrdata = xml.aggregator.init ());
					local (adrsource = @adrdata^.services.[adrpost^.sourceUrl]);
					if defined (adrsource^) {
						sourcelink = "<a href=\"" + adrsource^.compilation.channellink + "\" title=\"Source: " + adrsource^.compilation.channeltitle + ".\">" + adrblog^.prefs.sourceImgTag + "</a>"}}};
			s = string (adrblog^.prefs.itemtemplate);
			s = string.replaceall (s, "<%text%>", string (adrpost^.text), false);
			s = string.replaceall (s, "<%enclosure%>", enclosurelink, false);
			s = string.replaceall (s, "<%source%>", sourcelink, false);
			s = string.replaceall (s, "<%when%>", date.timestring (adrpost^.when), false);
			itemstext = itemstext + s + "\r\n";
			lastitemdate = adrpost^.when};
		if itemstext != "" { //there was some text left over when loop finished
			addDay ()}};
	
	local (s = string (adrblog^.prefs.template), description = string (adrblog^.prefs.description));
	bundle { //set description if it's a category
		if catname != nil {
			description = adrcat^.description}};
	bundle { //do macro substitutions
		s = string.replaceall (s, "<%bodytext%>", bodytext, false);
		s = string.replaceall (s, "<%title%>", adrblog^.prefs.title, false);
		s = string.replaceall (s, "<%description%>", description, false);
		s = string.replaceall (s, "<%authorname%>", adrblog^.prefs.authorname, false);
		s = string.replaceall (s, "<%authormailaddress%>", adrblog^.prefs.authormailaddress, false);
		s = string.replaceall (s, "<%year%>", date.year (), false);
		s = string.replaceall (s, "<%now%>", clock.now (), false);
		bundle { //PBS 02/21/01: RSS link
			local (imgTag = "<img src=\"http://scripting.com/images/xml.gif\" height=\"14\" width=\"36\" alt=\"Click here to see the XML version of this weblog.\" border=\"0\">");
			local (rssLink = html.getLink (imgTag, radio.weblog.getRssUrl (adrblog)));
			s = string.replaceAll (s, "<%rssLink%>", rssLink, false)};
		bundle { //PBS 02/23/01: Radio badge
			local (imgTag);
			if defined (user.html.glossary.radioBadge) {
				imgTag = user.html.glossary.radioBadge}
			else { //hard-code, just in case a beta user doesn't have the glossary entry
				imgTag = "<img src=\"http://scripting.com/images/radioUserlandBadge2.gif\" width=\"88\" height=\"31\" border=\"0\" alt=\"This site is edited with Radio UserLand, the first personal Web Application server for Windows and Macintosh.\">"};
			local (link = html.getLink (imgTag, "http://radiodiscuss.userland.com/"));
			s = string.replaceAll (s, "<%radioBadge%>", link)}};
	
	if system.environment.isMac { //02/22/2001 JES: iso8859 encode the HTML rendering
		s = string.iso8859encode (s, @html.data.iso8859.mac)}
	else {
		s = string.iso8859encode (s, @html.data.iso8859.win)};
	file.surefilepath (f);
	file.writewholefile (f, s, 'TEXT', user.html.prefs.textFileCreator); //02/22/2001 JES: added type/creator
	
	if adrblog^.prefs.flPublicBlog {
		if not flArchivePage {
			thread.callScript (@radio.weblog.notify, {adrblog})}};
	bundle { //do FTP and Manila mirroring
		if adrblog^.prefs.ftp.enabled {
			local (fname = filepath);
			fname = string.nthfield (fname, "/", string.countfields (fname, "/"));
			if flArchivePage {
				fname = archiveFtpPath};
			if catname != nil {
				fname = "categories/" + fname;
				adrcat^.htmlUrl = adrblog^.prefs.ftp.url  + "categories/" + adrcat^.filename + ".html"};
			radio.weblog.uploadFile (adrblog, fname, s, adrconnection)};
		if adrblog^.prefs.manila.enabled { //01/25/01 JES: send new homepage(s) text to Manila site
			<<if not flArchivePage
			if sizeOf (dateList) != 0 {
				if catname == nil {
					radio.weblog.uploadManila (adrblog, dateList, textList)}}}};
	if user.radio.prefs.flLogWeblogPublish {
		local (url = filepath);
		if not (url beginswith "/") {
			url = "/" + url};
		local (htmltext = "<a href=\"" + url + "\">" + adrblog^.prefs.title + "</a>.");
		radio.log.add ("Weblog publish", htmltext, startticks)}};
bundle { //test code
	bundle { //publish home page
		publish ()}}
	<<bundle //publish today
		<<publish (d:clock.now ())
	<<bundle //publish all categories
		<<local (adr)
		<<for adr in @weblogData.categories
			<<msg (nameof (adr^))
			<<publish (catname:nameof (adr^))



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.