// // A simple uLpc script to do some simple markup of references and // make things look a little prettyer. // // ROOTDIR is the directory where this file reside. #define ROOTDIR "/usr/www/html/svmud/doc" #define URLROOT "/svmud/" #define THIS "doc.ulpc?" #define INFRAME() THIS + "inframe=yes" #define DIRINFRAME(arg) THIS + "dir=" + arg + "&inframe=yes" #define DIRLINK(arg) THIS + "dir=" + arg #define FILELINK(dir, fil) DIRLINK(dir) + "&file=" + fil #define FRAMEHELP() FILELINK("annat", "framereadingwithwww") #define SURROUND(str, tag) "<" + tag + ">\n" + str + "\n\n" #define HEAD(str) SURROUND(str, "head") #define TITLE(str) SURROUND(str, "title") #define H1(str) SURROUND(str, "h1") #define UL(str) SURROUND(str, "ul") #define PRE(str) SURROUND(str, "pre") #define LINUS() "Linus" #define HEADER(str) HEAD(TITLE(str)) \ + "\"\"" + str + "\n" \ + "
\n\"\"\n
\n" \ + "\n" #define FRAMEHEADER(str) "\"\"" + str + "\n
\n" #define FOOTER() "
\nAnsvarig för att den här informationen är rätt är " \ + LINUS() + ". Hör genast av dig om något är fel\n" \ + "eller om du har förslag på förbättringar.\n" \ + "" #define FRAMEFOOTER() "" string * directories = sort(get_dir(ROOTDIR) - ({ "doc.ulpc" }) || ({ })); mapping dir_contents = mkmapping(directories, map_array(directories, lambda(string dir) { return sort(get_dir(ROOTDIR + "/" + dir) || ({ })); })); mapping file_place = sum(@map_array(directories, lambda (string dir, mapping contents) { if (contents[dir]) return mkmapping( contents[dir], map_array(contents[dir], lambda(string file, string dir) { return dir; }, dir)); else return ([ ]); }, dir_contents)); // frameset() returns true if this is the frameset page to be generate. int frameset(object request) { if (request->variables->frameset && request->variables->frameset == "yes") { return 1; } return 0; } // inframe() returns true if we are to generate the contents in a frame. // This is relevant only for the left page. int inframe(object request) { if (request->variables->inframe) { return 1; } return 0; } // Ouch, a global variable. Very ugly. // It keeps the "state" while parsing through the record. int state; #define STATE_NONE 0 #define STATE_REF 1 #define STATE_SAMPLE 2 mixed parse(object request) { string dir = request->variables->dir; string file = request->variables->file; if (!stringp(file)) { if (!stringp(dir)) { // If we don't give any info lets list the directories if (frameset(request)) { return (["type" : "text/html", "data" : HEAD(TITLE("Svenskmuds dokumentation - ramvarianten")) + "\n" + "\n" + "\n" + "\n" + "\n" + H1("Men, du har ju inga ramar.\n") + FOOTER() + "\n", ]); } else { return (["type" : "text/html", "data" : (inframe(request) ? FRAMEHEADER("") : HEADER("Svenskmuds olika dokumentationsbibliotek")) + H1((inframe(request) ? "Bibliotek:" : "Du kan titta i något av följande bibliotek:")) + (inframe(request) ? "" : "") + "
\n" + (inframe(request) ? "Slå av ramarna" : "Du kan även prova att läsa med ramar (frames). (Detta fungerar nog bara med Netscape tror jag men om du provar med något annat och det fungerar så berätta det för mig.)") + (inframe(request) ? FRAMEFOOTER() : FOOTER()), ]); } } // If we give the directory but not the file, list the directory string * files = dir_contents[dir]; return (["type" : "text/html", "data" : (inframe(request) ? "\n" : "") + (inframe(request) ? FRAMEHEADER("") : HEADER("Svenskmuds dokumentationsbibliotek " + dir)) + (inframe(request) ? "Tillbaka" : "Tillbaka") + H1((inframe(request) ? dir + ": " : "Innehållet i " + dir + ":")) + (inframe(request) ? "" : "") + (inframe(request) ? FRAMEFOOTER() : FOOTER()), ]); } string filename = ROOTDIR + "/" + dir + "/" + file; string data = read_bytes(filename); // Here we will try to match the info in the file and do good. // Look at the first word of the file. string firstword; state = STATE_NONE; sscanf(data, "%*[ \n\r\t]%s%*[ \n\r\t]", firstword); switch (firstword) { case "NAME": case "NAMN": // This we can do. // Lines that start at the first column are headers. // Lines that are indented with 8 spaces are text. // Lines that are indented with more is examples.

	return ([ "type" : "text/html",
	"data" : HEADER("Svenskmuds dokumentationsfil " + dir + "/" + file)
		+ sum(@map_array(explode(data, "\n"),
		lambda(string line) {
			if (line == "")
			    if (state == STATE_SAMPLE)
				return "\n";
			    else
				return "

\n"; if (line[0..8] == " " || line[0..1] == "\t " || line[0..1] == "\t\t") // Nine spaces if (state == STATE_SAMPLE) return line + "\n"; else { state = STATE_SAMPLE; return "

\n" + line + "\n";
			    }

			if (line[0..7] == "        "
			    || line[0..0] == "\t") // Eight spaces
			{
			    switch (state)
			    {
			    case STATE_SAMPLE:
				state = STATE_NONE;
				return "
\n" + line + "\n"; case STATE_REF: // Get the words, and try to match them against // file_place. string * words = explode(replace(line, ({",", "\t", "/",}), ({" ", " "," ",})), " ") - ({ " " }); return sum(@map_array(words, lambda(string word) { if (file_place[word]) return "" + word + "\n"; else return word + "\n"; })); default: ; } return line + "\n"; } // Now it is a header string extra = ""; if (state == STATE_SAMPLE) extra = "
\n"; switch (line) { case "SE ÄVEN": case "SEE ALSO": state = STATE_REF; break; default: state = STATE_NONE; break; } return extra + H1(line); })) + FOOTER(), ]); } // This is a fallback if nothing else works. return ([ "type" : "text/html", "data" : HEADER("Svenskmuds dokumentationsfil " + dir + "/" + file) + PRE(data) + FOOTER(), ]); }