program oldstyle, textpatterns; val sectionNumber = new Stack (); var inAppendix : boolean = false; var copyright : String = ""; var bibliography : String = ""; val infoPart : TextPattern = anyText upto (/"::" | /"`"); var email : String = ""; var where : String = "TOP"; print ("\n" "\n" "
\n"); with ruleSet = "main" do sendToMatch (System.in); endSection (0); print ("References\n" + bibliography + "\n") when bibliography != ""; print ("
\n"); ruleSet "main": match /".article" & [" \t"]+ & (anyText upto /"::") => "title" & /"::" & anyText* => "copyright" { print ("" + => "title" + "\n" "\n"); copyright = => "copyright"; } match /".conf" & [" \t"]+ & (anyText upto /"::") => "name" & /"::" & anyText* => "date" { print ("\n" (=> "name", => "date")); } match /".abstract" { endInfoPart ("ABSTRACT"); print ("\n"); } match /".author" & [" \t"]+ & \[":"]* => "first" & /":" & \[":"]* => "middle" & /":" & \[":"]* => "last" & /":" & anyText* => "email" { endInfoPart ("AUTHOR"); email = => "email"; print ("\n" ""); print ("" + => "first" + "") when => "first" != ""; print ("" + => "middle" + "") when => "middle" != ""; print ("" + => "last" + "") when => "last" != ""; print ("\n" "\n"); where = "AUTHOR"; } match /".keyword" & [" \t"]+ { endInfoPart ("KEYWORD"); print ("\n"); selectAll * { match (anyText upto ",") => "keyword" & /"," & /" "* | anyText+ => "keyword": print ("" + => "keyword" + "\n"); } print ("\n"); } match /".appendix" & [" \t"] & ((anyText upto "::") => "title" & /"::" & anyText* => "subtitle" | anyText* => "title" & /"" => "subtitle") { endInfoPart ("BODY"); startSection (1, => "title", true, => "subtitle"); } match /".h" & ["01234"] => "level" & [" \t"]+ & ((anyText upto "::") => "title" & /"::" & anyText* => "subtitle" | anyText* => "title" & /"" => "subtitle") { endInfoPart ("BODY"); startSection (=> "level" into integer, => "title", false, => "subtitle"); } match /".endFootnote" &? /"\n" { return; } match ? anyText { print (""); processParagraph (*); select * { match /"\n"* &? /"=": print ("\n"); selectAll currentInput { match /"\n"* & /"=" & [" \t"]*: print (""); processParagraph (*); print ("\n"); } print (""); match /"\n"* & /":" & /" "?: print (""); selectAll currentInput { match \["<>&\n"]+ => "asis": print (=> "asis"); match /"<": print ("<"); match /">": print (">"); match /"&": print ("&"); match /"\n"+ & /":" & /" "?: print ("\n"); match /"\n": break; } print (""); } print ("\n"); } def endInfoPart (newWhere : String) { select where { case "ABSTRACT": print ("\n"); case "AUTHOR": print ("\n"); print ("" + email + "\n") when email != ""; print ("\n"); print ("\n") when copyright != ""; } print ("\n") when where != "BODY" and newWhere == "BODY"; where = newWhere; } def processParagraph (source : String) { processParagraph (new MatchableInput (source)); } def processParagraph (source : MatchableInput) { selectAll source { match /"``": print ("`"); match /"`footnote`\n": print (""); with ruleSet = "main" do sendToMatch (currentInput); print (" "); match /"`BJ`": print ("%s" (languageName)); match /"`" & (/"bold" | /"ital" | /"bital" | /"rom" | /"under") => "role" & /":" & \["`"]* => "words" & /"`"?: print ("%s" (=> "role", => "words")); match /"`" & infoPart => "key" & /"::"? & infoPart => "label" & /"::"? & infoPart => "title" & /"::"? & infoPart => "url" & /"`": var http : String = (if => "url" matches (/"http" & /"s"? & /":" | -|) then "" else "http://") + => "url"; print (" "key" + "\"/>"); bibliography += "%s" (=> "key", => "label", if => "title" == "" then => "label" else => "title"); bibliography += "%s" "" (http, http) when http != ""; bibliography += "\n"; match /"`" & ? anyText [0, 20] => "following": System.err.printf ("ERROR: unpaired ` prior to \"%s\"\n" (=> "following")); match \["`<>&\n"]+ => "other": print (=> "other"); match /"<": print ("<"); match /">": print (">"); match /"&": print ("&"); match /"\n" &! [".=:\n"]: print ("\n"); match /"\n": break; } } def endSection (level : integer) { while length sectionNumber > 0 and length sectionNumber >= level do { print ("" (length sectionNumber, level, inAppendix)); println (if inAppendix and length sectionNumber == 1 then "" else ""); sectionNumber.pop (); } if length sectionNumber > 0 then with thisSectionNumber : Integer = sectionNumber.pop () do sectionNumber.push (thisSectionNumber.intValue () + 1); } def startSection (level : integer, title : String, startsAppendix : boolean, subtitle : String = "") { endSection (level); inAppendix or= startsAppendix; var sectionStart : String = if inAppendix and length sectionNumber == 0 then "" else "
"; print ("" (length sectionNumber, level, inAppendix)); while length sectionNumber < level - 1 do { sectionNumber.push (1); println ("%sSection %s\n" (sectionStart, getSectionNumber ())); sectionStart = "
"; } print ("%s" (sectionStart)); processParagraph (title); print (""); if subtitle != "" then { print (""); processParagraph (subtitle); print (""); } println (); sectionNumber.push (1); } def getSectionNumber () : String { val parts = new ArrayList (); until sectionNumber.empty () do parts.add (0, sectionNumber.pop ()); var result : String = ""; for i = length parts - 1 by -1 to 1 do result += "%s." (parts [i]); result += "%s" (parts [0]); for p = parts do sectionNumber.push (p); return result; }