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

system.verbs.builtins.mainResponder.folderToHTML

on folderToHTML (folder, adrparamtable) {
	<<Changes:
		<<Thu, 04 Mar 1999 02:48:30 GMT by AR
			<<Changed one occurence of config.mainResponder.urls to adrparamtable^.responderAttributes.urls^
		<<01/28/00; 12:01:48 AM by PBS
			<<Ensure that either resources.root exists, or there is an address at config.mainResponder.sites.resources that is the address of mainResponder.resources.
		<<08/31/00; 8:14:58 PM by PBS
			<<Matt's fixes for icons not appearing. See http://drmatt.userland.com/mrIntermediateStuff/walkstuff/foldertohtmlbugs.html
	local (htmltext = "", indentlevel = 0);
	bundle { //PBS 01/28/00: mainResponder.resources
		<<This fixes the 6.1 bug that left folder and table listings without icons, since resources.root didn't ship with Frontier anymore. Now the contents of resources.root are stored at mainResponder.root. If resources.root is not present on the system, we create a pointer in config.mainResponder.sites.resources to point to mainResponder.resources.
		local (pathToResourcesRoot = Frontier.getSubFolder ("www") + "resources.root");
		if not (file.exists (pathToResourcesRoot)) {
			<<Create an entry at config.mainResponder.sites.resources that's the address of mainResponder.resources.
			if not defined (config.mainResponder.sites) {
				new (tableType, @config.mainResponder.sites)};
			if not defined (config.mainResponder.sites.resources) {
				config.mainResponder.sites.resources = @mainResponder.resources}}};
	on add (s) {
		htmltext = htmltext + string.filledString ("\t", indentlevel) + s + "\r"};
	add ("<html>"); indentlevel++;
	add ("<head>"); indentlevel++;
	local (title = "Index of " + adrparamtable^.path);
	add ("<title>" + title + "</title>");
	add ("</head>"); indentlevel--;
	add ("<body>"); indentlevel++;
	add ("<h2>" + title + "</h2>");
	add ("<table cellpadding=\"4\" width=\"100%\" border=\"0\">"); indentlevel++;
	bundle { //add two rows at top of table
		add ("<tr><td> </td><td><b>Name</b></td><td><b>Size</b></td><td><b>Kind</b></td><td><b>Last Modified</b></td></tr>");
		add ("<tr><td colspan=\"5\"><hr noshade></td></tr>")};
	local (f);
	fileloop (f in folder) {
		local (flfolder = file.isfolder (f), name = file.fileFromPath (f), iconname = "unknown", iconlink);
		local (mimetype);
		bundle { //set mimetype
			if flfolder {
				mimeType = "folder";
				iconname = "folder";
				objectType = '    ';
				iconlink = "<img src=\"" + adrparamtable^.responderAttributes.urls^.icons + iconname + "\">"}
			else {
				if name endswith ".root" {
					mimetype = "database";
					iconname = "skull";
					iconlink =  "<img src=\"/resources/userlandIcons/" + iconname + "\" border=\"0\">";
					iconlink = "<a href=\"http://www.scripting.com/frontier5/\">" + iconlink + "</a>"}
				else {
					local (objectType = file.type (f));
					case sys.os () { // decide what the MIME type is
						"Win95";
						"WinNT" {
							local (extension = objectType);
							extension = string.replaceAll (extension, " ", ""); // strip spaces
							if defined (user.webserver.prefs.ext2MIME.[extension]) {
								mimeType= user.webserver.prefs.ext2MIME.[extension]}
							else {
								mimeType = "text/plain"}};
						"MacOS" {
							if defined (user.webserver.prefs.type2MIME.[objectType]) {
								mimeType = user.webserver.prefs.type2MIME.[objectType]}
							else {
								mimeType = "text/html"}}}}}};
		
		local (size);
		bundle { //set size
			if flfolder {
				size = "-"}
			else {
				size = string.megabyteString (file.size (f))}};
		
		try {
			iconname = user.webserver.prefs.MIME2icon.[mimetype];
			<<iconlink = "<img src=\"/resources/icons/" + iconname + "\">" //PBS 08/31/00: commented out -- the line below is the replacement
			iconlink = "<img src=\"" + adrparamtable^.responderAttributes.urls^.icons + iconname + "\">"};
		
		local (itemurl = adrparamtable^.path);
		if flfolder {name = string.delete (name, sizeof (name), 1)}; //pop off trailing path char
		if not (itemurl endswith "/") {
			itemurl = itemurl + "/"};
		itemurl = itemurl + string.urlEncode (name);
		if flfolder == tabletype {
			itemurl = itemurl + "/"};
		
		add ("<tr>"); indentlevel++;
		add ("<td>" + iconlink + "</td>");
		add ("<td><a href=\"" + itemurl + "\">" + name + "</a></td>");
		add ("<td>" + size + "</td>");
		add ("<td>" + mimetype + "</td>");
		add ("<td>" + file.modified (f) + "</td>");
		add ("</tr>"); indentlevel--};
	add ("<tr><td colspan=\"5\"><hr noshade></td></tr>");
	add ("</table>"); indentlevel--;
	bundle { //add a comment if one is present
		local (f = folder + "#comment");
		if file.exists (f) {
			add ("Comment: " + string (file.readwholefile (f)))}};
	add ("</body>"); indentlevel--;
	add ("</html>"); indentlevel--;
	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.