/* Java code generated by "translate.java.pss" */ import java.io.*; import java.util.regex.*; import java.util.*; // contains stack public class markhtml { // using int instead of char so that all unicode code points are // available instead of just utf16. (emojis cant fit into utf16) private int accumulator; // counter for anything private int peep; // next char in input stream private int charsRead; // No. of chars read so far private int linesRead; // No. of lines read so far public StringBuffer workspace; // text accumulator private Stack stack; // parse token stack private int LENGTH; // tape initial length // use ArrayLists instead with .add() .get(n) and .set(n, E) // ArrayList al=new ArrayList(); private List tape; // array of token attributes private List marks; // tape marks private int tapePointer; // pointer to current cell private Reader input; // text input stream private boolean eof; // end of stream reached? private boolean flag; // not used here private StringBuffer escape; // char used to "escape" others "\" private StringBuffer delimiter; // push/pop delimiter (default is "*") private boolean markFound; // if the mark was found in tape /** make a new machine with a character stream reader */ public markhtml(Reader reader) { this.markFound = false; this.LENGTH = 100; this.input = reader; this.eof = false; this.flag = false; this.charsRead = 0; this.linesRead = 1; this.escape = new StringBuffer("\\"); this.delimiter = new StringBuffer("*"); this.accumulator = 0; this.workspace = new StringBuffer(""); this.stack = new Stack(); this.tapePointer = 0; this.tape = new ArrayList(); this.marks = new ArrayList(); for (int ii = 0; ii < this.LENGTH; ii++) { this.tape.add(new StringBuffer("")); this.marks.add(new StringBuffer("")); } try { this.peep = this.input.read(); } catch (java.io.IOException ex) { System.out.println("read error"); System.exit(-1); } } /** read one character from the input stream and update the machine. */ public void read() { int iChar; try { if (this.eof) { System.exit(0); } this.charsRead++; // increment lines if ((char)this.peep == '\n') { this.linesRead++; } this.workspace.append(Character.toChars(this.peep)); this.peep = this.input.read(); if (this.peep == -1) { this.eof = true; } } catch (IOException ex) { System.out.println("Error reading input stream" + ex); System.exit(-1); } } /** increment tape pointer by one */ public void increment() { this.tapePointer++; if (this.tapePointer >= this.LENGTH) { this.tape.add(new StringBuffer("")); this.marks.add(new StringBuffer("")); this.LENGTH++; } } /** remove escape character */ public void unescapeChar(char c) { if (workspace.length() > 0) { String s = this.workspace.toString().replace("\\"+c, c+""); this.workspace.setLength(0); workspace.append(s); } } /** add escape character */ public void escapeChar(char c) { if (workspace.length() > 0) { String s = this.workspace.toString().replace(c+"", "\\"+c); workspace.setLength(0); workspace.append(s); } } /** whether trailing escapes \\ are even or odd */ // untested code. check! eg try: add "x \\"; print; etc public boolean isEscaped(String ss, String sSuffix) { int count = 0; if (ss.length() < 2) return false; if (ss.length() <= sSuffix.length()) return false; if (ss.indexOf(this.escape.toString().charAt(0)) == -1) { return false; } int pos = ss.length()-sSuffix.length(); while ((pos > -1) && (ss.charAt(pos) == this.escape.toString().charAt(0))) { count++; pos--; } if (count % 2 == 0) return false; return true; } /* a helper to see how many trailing \\ escape chars */ private int countEscaped(String sSuffix) { String s = ""; int count = 0; int index = this.workspace.toString().lastIndexOf(sSuffix); // remove suffix if it exists if (index > 0) { s = this.workspace.toString().substring(0, index); } while (s.endsWith(this.escape.toString())) { count++; s = s.substring(0, s.lastIndexOf(this.escape.toString())); } return count; } /** reads the input stream until the workspace end with text */ // can test this with public void until(String sSuffix) { // read at least one character if (this.eof) return; this.read(); while (true) { if (this.eof) return; if (this.workspace.toString().endsWith(sSuffix)) { if (this.countEscaped(sSuffix) % 2 == 0) { return; } } this.read(); } } /** pop the first token from the stack into the workspace */ public Boolean pop() { if (this.stack.isEmpty()) return false; this.workspace.insert(0, this.stack.pop()); if (this.tapePointer > 0) this.tapePointer--; return true; } /** push the first token from the workspace to the stack */ public Boolean push() { String sItem; // dont increment the tape pointer on an empty push if (this.workspace.length() == 0) return false; // need to get this from this.delim not "*" int iFirstStar = this.workspace.indexOf(this.delimiter.toString()); if (iFirstStar != -1) { sItem = this.workspace.toString().substring(0, iFirstStar + 1); this.workspace.delete(0, iFirstStar + 1); } else { sItem = this.workspace.toString(); this.workspace.setLength(0); } this.stack.push(sItem); this.increment(); return true; } /** swap current tape cell with the workspace */ public void swap() { String s = new String(this.workspace); this.workspace.setLength(0); this.workspace.append(this.tape.get(this.tapePointer).toString()); this.tape.get(this.tapePointer).setLength(0); this.tape.get(this.tapePointer).append(s); } /** save the workspace to file "sav.pp" */ public void writeToFile() { try { File file = new File("sav.pp"); Writer out = new BufferedWriter(new OutputStreamWriter( new FileOutputStream(file), "UTF8")); out.append(this.workspace.toString()); out.flush(); out.close(); } catch (Exception e) { System.out.println(e.getMessage()); } } public void goToMark(String mark) { this.markFound = false; for (var ii = 0; ii < this.marks.size(); ii++) { if (this.marks.get(ii).toString().equals(mark)) { this.tapePointer = ii; this.markFound = true; } } if (this.markFound == false) { System.out.print("badmark '" + mark + "'!"); System.exit(1); } } /** parse/check/compile the input */ public void parse(InputStreamReader input) { //this is where the actual parsing/compiling code should go //but this means that all generated code must use //"this." not "mm." } public static void main(String[] args) throws Exception { String temp = ""; markhtml mm = new markhtml(new InputStreamReader(System.in)); // make room for document title, and table of contents // mark "toc"; add "toc*"; push; // mark "title"; add "title*"; push; /* nop: no-operation eliminated */ script: while (!mm.eof) { lex: { mm.read(); /* read */ if (mm.workspace.toString().matches("^[\n]+$")) { mm.tape.get(mm.tapePointer).setLength(0); /* put */ mm.tape.get(mm.tapePointer).append(mm.workspace); mm.workspace.setLength(0); /* clear */ // just for debugging. // add "line: "; lines; add "\n"; print; clear; mm.workspace.append(mm.accumulator); /* count */ // check counter as flag. If set, then dont generate newline // tokens. if (mm.workspace.toString().equals("0")) { mm.workspace.setLength(0); /* clear */ mm.workspace.append("nl*"); /* add */ mm.push(); break lex; } } if (mm.workspace.toString().matches("^[\r]+$")) { mm.workspace.setLength(0); /* clear */ continue script; } // space includes \n\r so we can't use the [:space:] class if (mm.workspace.toString().matches("^[ \t]+$")) { /* while */ while (Character.toString((char)mm.peep).matches("^[ \t]+$")) { if (mm.eof) { break; } mm.read(); } mm.workspace.setLength(0); /* clear */ break lex; } // cant really use ' because then we can't write "can't" for example if (mm.workspace.toString().equals("\"")) { // check for multiline syntax """ /* while */ while (Character.toString((char)mm.peep).matches("^[\"]+$")) { if (mm.eof) { break; } mm.read(); } if (!mm.workspace.toString().equals("\"")) { mm.tape.get(mm.tapePointer).setLength(0); /* put */ mm.tape.get(mm.tapePointer).append(mm.workspace); mm.workspace.setLength(0); /* clear */ mm.workspace.append("word*"); /* add */ mm.push(); break lex; } /* whilenot */ while (!Character.toString((char)mm.peep).matches("^[\"\n]+$")) { if (mm.eof) { break; } mm.read(); } // check for multiple """ for multiline quotes if (mm.eof) { mm.tape.get(mm.tapePointer).setLength(0); /* put */ mm.tape.get(mm.tapePointer).append(mm.workspace); mm.workspace.setLength(0); /* clear */ mm.workspace.append("text*"); /* add */ mm.push(); break lex; } mm.read(); /* read */ // one double quote on line. if (mm.workspace.toString().matches("^[\n]+$")) { mm.tape.get(mm.tapePointer).setLength(0); /* put */ mm.tape.get(mm.tapePointer).append(mm.workspace); mm.workspace.setLength(0); /* clear */ mm.workspace.append("text*"); /* add */ mm.push(); break lex; } // closing double quote. mm.tape.get(mm.tapePointer).setLength(0); /* put */ mm.tape.get(mm.tapePointer).append(mm.workspace); mm.workspace.setLength(0); /* clear */ mm.workspace.append("quoted*"); /* add */ mm.push(); break lex; } // [[ ]] >> << are parse as words (space delimited) // everything else is a word // all the logic in the word* block could just be here. if (!mm.workspace.toString().equals("")) { /* whilenot */ while (!Character.toString((char)mm.peep).matches("^\\p{Space}+$")) { if (mm.eof) { break; } mm.read(); } mm.tape.get(mm.tapePointer).setLength(0); /* put */ mm.tape.get(mm.tapePointer).append(mm.workspace); // the subsection marker if (mm.workspace.toString().equals("....")) { mm.workspace.setLength(0); /* clear */ mm.workspace.append("4dots*"); /* add */ mm.push(); break lex; } mm.workspace.setLength(0); /* clear */ mm.workspace.append("word*"); /* add */ mm.push(); break lex; } // end of the lexing phase of the script // start of the parse/compile/translate phase } parse: while (true) { // The parse/compile/translate/transform phase involves // recognising series of tokens on the stack and "reducing" them // according to the required bnf grammar rules. //----------------- // 1 token mm.pop(); //(eof).!"end*" { //} if (mm.workspace.toString().equals("word*")) { mm.workspace.setLength(0); /* clear */ mm.workspace.append(mm.tape.get(mm.tapePointer)); /* get */ // no numbers in headings! if (mm.workspace.toString().matches("^[A-Z]+$")) { mm.workspace.setLength(0); /* clear */ mm.workspace.append("utext*"); /* add */ mm.push(); continue parse; } // the subheading marker //"...." { clear; add "4dots*"; push; .reparse } // emphasis or explanation line marker //"*" { clear; add "star*"; push; .reparse } // image markers if (mm.workspace.toString().equals("[[")) { mm.workspace.append("*"); /* add */ mm.push(); continue parse; } if (mm.workspace.toString().equals("]]")) { mm.workspace.append("*"); /* add */ mm.push(); continue parse; } // the code line marker, and float right marker if (mm.workspace.toString().equals(">>")) { // convert to html entities mm.workspace.setLength(0); /* clear */ mm.workspace.append(">> "); /* add */ mm.tape.get(mm.tapePointer).setLength(0); /* put */ mm.tape.get(mm.tapePointer).append(mm.workspace); mm.workspace.setLength(0); /* clear */ mm.workspace.append(">>*"); /* add */ mm.push(); continue parse; } // the float left marker if (mm.workspace.toString().equals("<<")) { mm.workspace.setLength(0); /* clear */ mm.workspace.append("<< "); /* add */ mm.tape.get(mm.tapePointer).setLength(0); /* put */ mm.tape.get(mm.tapePointer).append(mm.workspace); mm.workspace.setLength(0); /* clear */ mm.workspace.append("<<*"); /* add */ mm.push(); continue parse; } // multiline quotes if (mm.workspace.toString().equals("\"\"\"")) { mm.workspace.setLength(0); /* clear */ mm.until("\"\"\""); if (!mm.workspace.toString().endsWith("\"\"\"")) { mm.tape.get(mm.tapePointer).setLength(0); /* put */ mm.tape.get(mm.tapePointer).append(mm.workspace); mm.workspace.setLength(0); /* clear */ mm.workspace.append("text*"); /* add */ mm.push(); continue parse; } if (mm.workspace.length() > 0) { /* clip */ mm.workspace.delete(mm.workspace.length() - 1, mm.workspace.length()); } if (mm.workspace.length() > 0) { /* clip */ mm.workspace.delete(mm.workspace.length() - 1, mm.workspace.length()); } if (mm.workspace.length() > 0) { /* clip */ mm.workspace.delete(mm.workspace.length() - 1, mm.workspace.length()); } mm.tape.get(mm.tapePointer).setLength(0); /* put */ mm.tape.get(mm.tapePointer).append(mm.workspace); mm.workspace.setLength(0); /* clear */ mm.workspace.append("quoted*"); /* add */ mm.push(); continue parse; } // multiline codeblocks start with --- on a newline if (mm.workspace.toString().startsWith("---") && mm.workspace.toString().matches("^[\\-]+$")) { mm.workspace.setLength(0); /* clear */ mm.pop(); if (mm.workspace.toString().equals("nl*")) { mm.workspace.setLength(0); /* clear */ mm.until(",,,"); if (!mm.workspace.toString().endsWith(",,,")) { mm.tape.get(mm.tapePointer).setLength(0); /* put */ mm.tape.get(mm.tapePointer).append(mm.workspace); mm.workspace.setLength(0); /* clear */ mm.workspace.append("text*"); /* add */ mm.push(); continue parse; } if (mm.workspace.length() > 0) { /* clip */ mm.workspace.delete(mm.workspace.length() - 1, mm.workspace.length()); } if (mm.workspace.length() > 0) { /* clip */ mm.workspace.delete(mm.workspace.length() - 1, mm.workspace.length()); } if (mm.workspace.length() > 0) { /* clip */ mm.workspace.delete(mm.workspace.length() - 1, mm.workspace.length()); } /* replace */ if (mm.workspace.length() > 0) { temp = mm.workspace.toString().replace(">", ">"); mm.workspace.setLength(0); mm.workspace.append(temp); } /* replace */ if (mm.workspace.length() > 0) { temp = mm.workspace.toString().replace("<", "<"); mm.workspace.setLength(0); mm.workspace.append(temp); } mm.tape.get(mm.tapePointer).setLength(0); /* put */ mm.tape.get(mm.tapePointer).append(mm.workspace); mm.workspace.setLength(0); /* clear */ mm.workspace.append("
"); /* add */
              mm.workspace.append(mm.tape.get(mm.tapePointer)); /* get */
              mm.workspace.append("
"); /* add */ mm.tape.get(mm.tapePointer).setLength(0); /* put */ mm.tape.get(mm.tapePointer).append(mm.workspace); mm.workspace.setLength(0); /* clear */ // discard extra ,,,, /* while */ while (Character.toString((char)mm.peep).matches("^[,]+$")) { if (mm.eof) { break; } mm.read(); } mm.workspace.setLength(0); /* clear */ mm.workspace.append("codeline*"); /* add */ mm.push(); continue parse; } mm.push(); mm.workspace.append("word*"); /* add */ mm.push(); continue parse; } // starline starts with a star if (mm.workspace.toString().equals("*")) { mm.workspace.setLength(0); /* clear */ mm.workspace.append("⊗ "); /* add */ mm.tape.get(mm.tapePointer).setLength(0); /* put */ mm.tape.get(mm.tapePointer).append(mm.workspace); mm.workspace.setLength(0); /* clear */ mm.pop(); if (mm.workspace.toString().equals("nl*")) { mm.workspace.setLength(0); /* clear */ // clear leading whitespace /* while */ while (Character.toString((char)mm.peep).matches("^[ \t]+$")) { if (mm.eof) { break; } mm.read(); } mm.workspace.setLength(0); /* clear */ mm.workspace.append(""); /* add */ /* whilenot */ while (!Character.toString((char)mm.peep).matches("^[\n]+$")) { if (mm.eof) { break; } mm.read(); } mm.workspace.append(""); /* add */ mm.tape.get(mm.tapePointer).setLength(0); /* put */ mm.tape.get(mm.tapePointer).append(mm.workspace); mm.workspace.setLength(0); /* clear */ mm.workspace.append("emline*"); /* add */ mm.push(); continue parse; } mm.push(); mm.workspace.append("word*"); /* add */ mm.push(); continue parse; } // document title is marked up by ==* at start of line if (mm.workspace.toString().equals("==*")) { mm.workspace.setLength(0); /* clear */ mm.pop(); if (mm.workspace.toString().equals("nl*")) { mm.workspace.setLength(0); /* clear */ // clear leading whitespace /* while */ while (Character.toString((char)mm.peep).matches("^[ \t]+$")) { if (mm.eof) { break; } mm.read(); } mm.workspace.setLength(0); /* clear */ mm.workspace.append("

"); /* add */ /* whilenot */ while (!Character.toString((char)mm.peep).matches("^[\n]+$")) { if (mm.eof) { break; } mm.read(); } mm.workspace.append("

"); /* add */ mm.tape.get(mm.tapePointer).setLength(0); /* put */ mm.tape.get(mm.tapePointer).append(mm.workspace); mm.workspace.setLength(0); /* clear */ mm.workspace.append("text*"); /* add */ mm.push(); continue parse; } mm.push(); mm.workspace.append("word*"); /* add */ mm.push(); continue parse; } // the code block begin marker. can't read straight to end marker //B"---".[-] { clear; put; add "---*"; push; .reparse } if (mm.workspace.toString().startsWith("http://") || mm.workspace.toString().startsWith("https://") || mm.workspace.toString().startsWith("www.") || mm.workspace.toString().startsWith("ftp://") || mm.workspace.toString().startsWith("sftp://")) { mm.workspace.setLength(0); /* clear */ mm.workspace.append("link*"); /* add */ mm.push(); continue parse; } // file names if (!mm.workspace.toString().equals("/")) { if (mm.workspace.toString().startsWith("/") || mm.workspace.toString().startsWith("../") || mm.workspace.toString().startsWith("img/")) { if (mm.workspace.toString().endsWith("/") || mm.workspace.toString().endsWith(".c") || mm.workspace.toString().endsWith(".txt") || mm.workspace.toString().endsWith(".html") || mm.workspace.toString().endsWith(".pss") || mm.workspace.toString().endsWith(".pp") || mm.workspace.toString().endsWith(".js") || mm.workspace.toString().endsWith(".java") || mm.workspace.toString().endsWith(".tcl") || mm.workspace.toString().endsWith(".py") || mm.workspace.toString().endsWith(".pl") || mm.workspace.toString().endsWith(".jpeg") || mm.workspace.toString().endsWith(".jpg") || mm.workspace.toString().endsWith(".png")) { mm.workspace.setLength(0); /* clear */ mm.workspace.append("file*"); /* add */ mm.push(); continue parse; } } } // file names in single quotes, eg 'asm.pp' if (mm.workspace.toString().startsWith("'") && mm.workspace.toString().endsWith("'") && !mm.workspace.toString().equals("'") && !mm.workspace.toString().equals("''") && !mm.workspace.toString().equals("'/'")) { if (mm.workspace.length() > 0) { /* clip */ mm.workspace.delete(mm.workspace.length() - 1, mm.workspace.length()); } if (mm.workspace.length() > 0) { /* clop */ mm.workspace.delete(0, 1); } /* clop */ if (mm.workspace.toString().endsWith("/") || mm.workspace.toString().endsWith(".c") || mm.workspace.toString().endsWith(".txt") || mm.workspace.toString().endsWith(".html") || mm.workspace.toString().endsWith(".pss") || mm.workspace.toString().endsWith(".pp") || mm.workspace.toString().endsWith(".js") || mm.workspace.toString().endsWith(".java") || mm.workspace.toString().endsWith(".tcl") || mm.workspace.toString().endsWith(".py") || mm.workspace.toString().endsWith(".pl") || mm.workspace.toString().endsWith(".jpeg") || mm.workspace.toString().endsWith(".jpg") || mm.workspace.toString().endsWith(".png")) { mm.tape.get(mm.tapePointer).setLength(0); /* put */ mm.tape.get(mm.tapePointer).append(mm.workspace); mm.workspace.setLength(0); /* clear */ mm.workspace.append("file*"); /* add */ mm.push(); continue parse; } } mm.workspace.setLength(0); /* clear */ mm.workspace.append("word*"); /* add */ // leave the wordtoken on the workspace. } // get rid of insignificant tokens at the end of the document if (mm.workspace.toString().equals("[[*") || mm.workspace.toString().equals("<<*") || mm.workspace.toString().equals(">>*") || mm.workspace.toString().equals("quoted*")) { if (mm.eof) { mm.workspace.setLength(0); /* clear */ mm.workspace.append("word*"); /* add */ } } // resolve links at the end of the document if (mm.workspace.toString().equals("link*")) { if (mm.eof) { mm.workspace.setLength(0); /* clear */ mm.workspace.append(""); /* add */ mm.workspace.append(mm.tape.get(mm.tapePointer)); /* get */ mm.workspace.append(""); /* add */ mm.tape.get(mm.tapePointer).setLength(0); /* put */ mm.tape.get(mm.tapePointer).append(mm.workspace); mm.workspace.setLength(0); /* clear */ mm.workspace.append("text*"); /* add */ mm.push(); continue parse; } } // resolve file links at the end of the document if (mm.workspace.toString().equals("file*")) { if (mm.eof) { mm.workspace.setLength(0); /* clear */ mm.workspace.append(""); /* add */ mm.workspace.append(mm.tape.get(mm.tapePointer)); /* get */ mm.workspace.append(""); /* add */ mm.tape.get(mm.tapePointer).setLength(0); /* put */ mm.tape.get(mm.tapePointer).append(mm.workspace); mm.workspace.setLength(0); /* clear */ mm.workspace.append("text*"); /* add */ mm.push(); continue parse; } } //----------------- // 2 tokens mm.pop(); // eliminate insignificant newlines and ellide words // and upper case text if (mm.workspace.toString().equals("nl*word*") || mm.workspace.toString().equals("nl*text*") || mm.workspace.toString().equals("emline*text*") || mm.workspace.toString().equals("emline*word*") || mm.workspace.toString().equals("text*utext*") || mm.workspace.toString().equals("word*utext*") || mm.workspace.toString().equals("utext*text*") || mm.workspace.toString().equals("utext*word*") || mm.workspace.toString().equals("word*word*") || mm.workspace.toString().equals("text*word*") || mm.workspace.toString().equals("text*text*") || mm.workspace.toString().equals("word*text*") || mm.workspace.toString().equals("quoted*text*") || mm.workspace.toString().equals("quoted*word*")) { // check for pattern "file 'asm.pp' or folder '/books/' mm.workspace.setLength(0); /* clear */ mm.workspace.append(mm.tape.get(mm.tapePointer)); /* get */ mm.workspace.append(" "); /* add */ mm.increment(); /* ++ */ mm.workspace.append(mm.tape.get(mm.tapePointer)); /* get */ if (mm.tapePointer > 0) mm.tapePointer--; /* -- */ mm.tape.get(mm.tapePointer).setLength(0); /* put */ mm.tape.get(mm.tapePointer).append(mm.workspace); mm.workspace.setLength(0); /* clear */ mm.workspace.append("text*"); /* add */ mm.push(); continue parse; } // ellide upper case text if (mm.workspace.toString().equals("utext*utext*")) { mm.workspace.setLength(0); /* clear */ mm.workspace.append(mm.tape.get(mm.tapePointer)); /* get */ mm.workspace.append(" "); /* add */ mm.increment(); /* ++ */ mm.workspace.append(mm.tape.get(mm.tapePointer)); /* get */ if (mm.tapePointer > 0) mm.tapePointer--; /* -- */ mm.tape.get(mm.tapePointer).setLength(0); /* put */ mm.tape.get(mm.tapePointer).append(mm.workspace); mm.workspace.setLength(0); /* clear */ mm.workspace.append("utext*"); /* add */ mm.push(); continue parse; } // ellide as text insignificant "]]" image end tokens if (mm.workspace.toString().equals("word*]]*") || mm.workspace.toString().equals("text*]]*")) { mm.workspace.setLength(0); /* clear */ mm.workspace.append(mm.tape.get(mm.tapePointer)); /* get */ mm.workspace.append(" "); /* add */ mm.increment(); /* ++ */ mm.workspace.append(mm.tape.get(mm.tapePointer)); /* get */ if (mm.tapePointer > 0) mm.tapePointer--; /* -- */ mm.tape.get(mm.tapePointer).setLength(0); /* put */ mm.tape.get(mm.tapePointer).append(mm.workspace); mm.workspace.setLength(0); /* clear */ mm.workspace.append("text*"); /* add */ mm.push(); continue parse; } // ellide multiple newlines if (mm.workspace.toString().equals("nl*nl*")) { mm.workspace.setLength(0); /* clear */ mm.workspace.append(mm.tape.get(mm.tapePointer)); /* get */ mm.increment(); /* ++ */ mm.workspace.append(mm.tape.get(mm.tapePointer)); /* get */ if (mm.tapePointer > 0) mm.tapePointer--; /* -- */ mm.workspace.append("

\n"); /* add */ mm.tape.get(mm.tapePointer).setLength(0); /* put */ mm.tape.get(mm.tapePointer).append(mm.workspace); mm.workspace.setLength(0); /* clear */ mm.workspace.append("nl*"); /* add */ mm.push(); continue parse; } // codelines. nl*>>* should not occur in image markup if (mm.workspace.toString().equals("nl*>>*")) { mm.workspace.setLength(0); /* clear */ // clear leading whitespace /* while */ while (Character.toString((char)mm.peep).matches("^[ \t]+$")) { if (mm.eof) { break; } mm.read(); } mm.workspace.setLength(0); /* clear */ /* whilenot */ while (!Character.toString((char)mm.peep).matches("^[\n]+$")) { if (mm.eof) { break; } mm.read(); } /* replace */ if (mm.workspace.length() > 0) { temp = mm.workspace.toString().replace(">", ">"); mm.workspace.setLength(0); mm.workspace.append(temp); } /* replace */ if (mm.workspace.length() > 0) { temp = mm.workspace.toString().replace("<", "<"); mm.workspace.setLength(0); mm.workspace.append(temp); } mm.tape.get(mm.tapePointer).setLength(0); /* put */ mm.tape.get(mm.tapePointer).append(mm.workspace); mm.workspace.setLength(0); /* clear */ mm.workspace.append("

"); /* add */
          mm.workspace.append(mm.tape.get(mm.tapePointer)); /* get */
          mm.workspace.append("
"); /* add */ mm.tape.get(mm.tapePointer).setLength(0); /* put */ mm.tape.get(mm.tapePointer).append(mm.workspace); mm.workspace.setLength(0); /* clear */ mm.workspace.append("codeline*"); /* add */ mm.push(); continue parse; } // eliminate insignificant newlines at end of document if (mm.workspace.toString().equals("word*nl*") || mm.workspace.toString().equals("text*nl*")) { if (mm.eof) { mm.workspace.setLength(0); /* clear */ mm.workspace.append(mm.tape.get(mm.tapePointer)); /* get */ mm.workspace.append(" "); /* add */ mm.increment(); /* ++ */ mm.workspace.append(mm.tape.get(mm.tapePointer)); /* get */ if (mm.tapePointer > 0) mm.tapePointer--; /* -- */ mm.tape.get(mm.tapePointer).setLength(0); /* put */ mm.tape.get(mm.tapePointer).append(mm.workspace); mm.workspace.setLength(0); /* clear */ mm.workspace.append("text*"); /* add */ mm.push(); continue parse; } } // mark this up as a "recipe". // sample: // * description // >> sh code.to.exec if (mm.workspace.toString().equals("emline*codeline*")) { mm.workspace.setLength(0); /* clear */ mm.workspace.append(""); mm.workspace.append("\n

"); mm.workspace.append("\n "); mm.workspace.append("\n \n"); /* add */ mm.workspace.append("\n
"); /* add */ mm.workspace.append(mm.tape.get(mm.tapePointer)); /* get */ mm.workspace.append("
"); /* add */ mm.increment(); /* ++ */ mm.workspace.append(mm.tape.get(mm.tapePointer)); /* get */ mm.workspace.append("
\n"); /* add */ if (mm.tapePointer > 0) mm.tapePointer--; /* -- */ mm.tape.get(mm.tapePointer).setLength(0); /* put */ mm.tape.get(mm.tapePointer).append(mm.workspace); mm.workspace.setLength(0); /* clear */ mm.workspace.append("text*"); /* add */ mm.push(); continue parse; } if (mm.workspace.toString().equals("word*codeline*") || mm.workspace.toString().equals("text*codeline*") || mm.workspace.toString().equals("quoted*codeline*")) { mm.workspace.setLength(0); /* clear */ mm.workspace.append(mm.tape.get(mm.tapePointer)); /* get */ mm.workspace.append(" "); /* add */ mm.workspace.append("\n
"); /* add */ mm.increment(); /* ++ */ mm.workspace.append(mm.tape.get(mm.tapePointer)); /* get */ mm.workspace.append("
\n"); /* add */ if (mm.tapePointer > 0) mm.tapePointer--; /* -- */ mm.tape.get(mm.tapePointer).setLength(0); /* put */ mm.tape.get(mm.tapePointer).append(mm.workspace); mm.workspace.setLength(0); /* clear */ mm.workspace.append("text*"); /* add */ mm.push(); continue parse; } // a line of code at the start of the document if (mm.workspace.toString().equals("codeline*")) { mm.workspace.setLength(0); /* clear */ mm.workspace.append("
"); /* add */ mm.workspace.append(mm.tape.get(mm.tapePointer)); /* get */ mm.workspace.append("
\n"); /* add */ mm.tape.get(mm.tapePointer).setLength(0); /* put */ mm.tape.get(mm.tapePointer).append(mm.workspace); mm.workspace.setLength(0); /* clear */ mm.workspace.append("text*"); /* add */ mm.push(); continue parse; } // sample: tree www.abc.org (also at the start of document) if (mm.workspace.toString().equals("word*link*") || mm.workspace.toString().equals("text*link*") || mm.workspace.toString().equals("nl*link*")) { mm.workspace.setLength(0); /* clear */ mm.workspace.append(mm.tape.get(mm.tapePointer)); /* get */ mm.workspace.append(" "); /* add */ mm.workspace.append(""); /* add */ mm.workspace.append(mm.tape.get(mm.tapePointer)); /* get */ if (mm.tapePointer > 0) mm.tapePointer--; /* -- */ mm.workspace.append(""); /* add */ mm.tape.get(mm.tapePointer).setLength(0); /* put */ mm.tape.get(mm.tapePointer).append(mm.workspace); mm.workspace.setLength(0); /* clear */ mm.workspace.append("text*"); /* add */ mm.push(); continue parse; } // link at the start of document (only 1 token) if (mm.workspace.toString().equals("link*")) { mm.workspace.setLength(0); /* clear */ mm.workspace.append(""); /* add */ mm.workspace.append(mm.tape.get(mm.tapePointer)); /* get */ mm.workspace.append(""); /* add */ mm.tape.get(mm.tapePointer).setLength(0); /* put */ mm.tape.get(mm.tapePointer).append(mm.workspace); mm.workspace.setLength(0); /* clear */ mm.workspace.append("text*"); /* add */ mm.push(); continue parse; } // sample: condor /file.txt if (mm.workspace.toString().equals("word*file*") || mm.workspace.toString().equals("text*file*") || mm.workspace.toString().equals("nl*file*")) { mm.workspace.setLength(0); /* clear */ mm.workspace.append(mm.tape.get(mm.tapePointer)); /* get */ mm.workspace.append(" "); /* add */ mm.workspace.append(""); /* add */ mm.workspace.append(mm.tape.get(mm.tapePointer)); /* get */ if (mm.tapePointer > 0) mm.tapePointer--; /* -- */ mm.workspace.append(""); /* add */ mm.tape.get(mm.tapePointer).setLength(0); /* put */ mm.tape.get(mm.tapePointer).append(mm.workspace); mm.workspace.setLength(0); /* clear */ mm.workspace.append("text*"); /* add */ mm.push(); continue parse; } // file link at start of document if (mm.workspace.toString().equals("file*")) { mm.workspace.setLength(0); /* clear */ mm.workspace.append(""); /* add */ mm.workspace.append(mm.tape.get(mm.tapePointer)); /* get */ mm.workspace.append(""); /* add */ mm.tape.get(mm.tapePointer).setLength(0); /* put */ mm.tape.get(mm.tapePointer).append(mm.workspace); mm.workspace.setLength(0); /* clear */ mm.workspace.append("text*"); /* add */ mm.push(); continue parse; } if (mm.workspace.toString().equals("quoted*file*") || mm.workspace.toString().equals("quoted*link*")) { mm.workspace.setLength(0); /* clear */ // remove quotes from quoted text mm.workspace.append(mm.tape.get(mm.tapePointer)); /* get */ if (mm.workspace.length() > 0) { /* clip */ mm.workspace.delete(mm.workspace.length() - 1, mm.workspace.length()); } if (mm.workspace.length() > 0) { /* clop */ mm.workspace.delete(0, 1); } /* clop */ mm.tape.get(mm.tapePointer).setLength(0); /* put */ mm.tape.get(mm.tapePointer).append(mm.workspace); mm.workspace.setLength(0); /* clear */ mm.workspace.append(""); /* add */ mm.workspace.append(mm.tape.get(mm.tapePointer)); /* get */ mm.workspace.append(""); /* add */ mm.tape.get(mm.tapePointer).setLength(0); /* put */ mm.tape.get(mm.tapePointer).append(mm.workspace); mm.workspace.setLength(0); /* clear */ mm.workspace.append("text*"); /* add */ mm.push(); continue parse; } // get rid of irrelevant ">>" tokens (ie not in image, nor at // start of code line). // image format: [[ /file.txt "caption" >> ]] if (mm.workspace.toString().endsWith(">>*")) { if (!mm.workspace.toString().startsWith("nl*") && !mm.workspace.toString().startsWith("quoted*") && !mm.workspace.toString().startsWith("file*") && !mm.workspace.toString().startsWith("link*")) { mm.workspace.setLength(0); /* clear */ mm.workspace.append(mm.tape.get(mm.tapePointer)); /* get */ mm.workspace.append(" "); /* add */ mm.increment(); /* ++ */ mm.workspace.append(mm.tape.get(mm.tapePointer)); /* get */ if (mm.tapePointer > 0) mm.tapePointer--; /* -- */ mm.tape.get(mm.tapePointer).setLength(0); /* put */ mm.tape.get(mm.tapePointer).append(mm.workspace); mm.workspace.setLength(0); /* clear */ mm.workspace.append("text*"); /* add */ mm.push(); continue parse; } } // ellide insignificant "<<" tokens (ie not in image markup) if (mm.workspace.toString().startsWith("<<*") && !mm.workspace.toString().endsWith("]]*")) { /* replace */ if (mm.workspace.length() > 0) { temp = mm.workspace.toString().replace("<<*", "word*"); mm.workspace.setLength(0); mm.workspace.append(temp); } mm.push(); mm.push(); continue parse; } // ellide insignificant "...." tokens (ie not in subsection heading) if (mm.workspace.toString().startsWith("4dots*") && !mm.workspace.toString().endsWith("nl*") && !mm.workspace.toString().equals("4dots*")) { /* replace */ if (mm.workspace.length() > 0) { temp = mm.workspace.toString().replace("4dots*", "word*"); mm.workspace.setLength(0); mm.workspace.append(temp); } mm.push(); mm.push(); continue parse; } // ellide insignificant "...." tokens (ie not in subsection heading) if (mm.workspace.toString().endsWith("4dots*") && !mm.workspace.toString().startsWith("utext*") && !mm.workspace.toString().startsWith("uword*") && !mm.workspace.toString().equals("4dots*")) { /* replace */ if (mm.workspace.length() > 0) { temp = mm.workspace.toString().replace("4dots*", "word*"); mm.workspace.setLength(0); mm.workspace.append(temp); } mm.push(); mm.push(); continue parse; } // eliminate newlines in image markup if (mm.workspace.toString().equals("[[*nl*")) { mm.workspace.setLength(0); /* clear */ mm.workspace.append(mm.tape.get(mm.tapePointer)); /* get */ mm.increment(); /* ++ */ mm.workspace.append(mm.tape.get(mm.tapePointer)); /* get */ if (mm.tapePointer > 0) mm.tapePointer--; /* -- */ mm.tape.get(mm.tapePointer).setLength(0); /* put */ mm.tape.get(mm.tapePointer).append(mm.workspace); mm.workspace.setLength(0); /* clear */ mm.workspace.append("[[*"); /* add */ mm.push(); continue parse; } if (mm.workspace.toString().equals("nl*]]*")) { mm.workspace.setLength(0); /* clear */ mm.workspace.append(mm.tape.get(mm.tapePointer)); /* get */ mm.increment(); /* ++ */ mm.workspace.append(mm.tape.get(mm.tapePointer)); /* get */ if (mm.tapePointer > 0) mm.tapePointer--; /* -- */ mm.tape.get(mm.tapePointer).setLength(0); /* put */ mm.tape.get(mm.tapePointer).append(mm.workspace); mm.workspace.setLength(0); /* clear */ mm.workspace.append("]]*"); /* add */ mm.push(); continue parse; } // get rid of insignificant "[[" image start tokens // image format: [[ /file.txt "caption" >> ]] if (mm.workspace.toString().startsWith("[[*") && !mm.workspace.toString().equals("[[*")) { if (!mm.workspace.toString().endsWith("file*") && !mm.workspace.toString().endsWith("link*")) { mm.workspace.setLength(0); /* clear */ mm.workspace.append(mm.tape.get(mm.tapePointer)); /* get */ mm.workspace.append(" "); /* add */ mm.increment(); /* ++ */ mm.workspace.append(mm.tape.get(mm.tapePointer)); /* get */ if (mm.tapePointer > 0) mm.tapePointer--; /* -- */ mm.tape.get(mm.tapePointer).setLength(0); /* put */ mm.tape.get(mm.tapePointer).append(mm.workspace); mm.workspace.setLength(0); /* clear */ mm.workspace.append("text*"); /* add */ mm.push(); continue parse; } } //---------------------- // 3 tokens mm.pop(); // top level headings, all upper case on the line in the source document if (mm.workspace.toString().equals("nl*utext*nl*") || mm.workspace.toString().equals("nl*uword*nl*")) { mm.workspace.setLength(0); /* clear */ mm.workspace.append(mm.tape.get(mm.tapePointer)); /* get */ // add a link so that table-of-contents works mm.workspace.append("\n"); /* add */ mm.workspace.append("\n"); /* add */ mm.workspace.append("

"); /* add */ mm.workspace.append(mm.tape.get(mm.tapePointer)); /* get */ mm.workspace.append(" (↑)"); /* add */ mm.workspace.append("

\n"); /* add */ if (mm.tapePointer > 0) mm.tapePointer--; /* -- */ mm.tape.get(mm.tapePointer).setLength(0); /* put */ mm.tape.get(mm.tapePointer).append(mm.workspace); mm.workspace.setLength(0); /* clear */ // transfer nl value mm.increment(); /* ++ */ mm.increment(); /* ++ */ mm.workspace.append(mm.tape.get(mm.tapePointer)); /* get */ if (mm.tapePointer > 0) mm.tapePointer--; /* -- */ mm.tape.get(mm.tapePointer).setLength(0); /* put */ mm.tape.get(mm.tapePointer).append(mm.workspace); mm.workspace.setLength(0); /* clear */ if (mm.tapePointer > 0) mm.tapePointer--; /* -- */ mm.workspace.append("text*nl*"); /* add */ mm.push(); mm.push(); continue parse; } // eliminate newlines within image markup // this is important because nl*>>* is considered the // start of a "codeline". if (mm.workspace.toString().equals("[[*file*nl*") || mm.workspace.toString().equals("[[*link*nl*") || mm.workspace.toString().equals("link*quoted*nl*") || mm.workspace.toString().equals("file*quoted*nl*")) { if (mm.workspace.length() > 0) { /* clip */ mm.workspace.delete(mm.workspace.length() - 1, mm.workspace.length()); } if (mm.workspace.length() > 0) { /* clip */ mm.workspace.delete(mm.workspace.length() - 1, mm.workspace.length()); } if (mm.workspace.length() > 0) { /* clip */ mm.workspace.delete(mm.workspace.length() - 1, mm.workspace.length()); } mm.push(); mm.push(); continue parse; } // simple image format: [[ /path/file.jpg ]] if (mm.workspace.toString().equals("[[*file*]]*") || mm.workspace.toString().equals("[[*link*]]*")) { mm.workspace.setLength(0); /* clear */ mm.increment(); /* ++ */ mm.workspace.append("\n"); /* add */ if (mm.tapePointer > 0) mm.tapePointer--; /* -- */ mm.tape.get(mm.tapePointer).setLength(0); /* put */ mm.tape.get(mm.tapePointer).append(mm.workspace); mm.workspace.setLength(0); /* clear */ mm.workspace.append("text*"); /* add */ mm.push(); continue parse; } // incorrect image format: [[ /path/file.jpg word // just becomes text. I probably should hyperlink the links // but wont for now. if (mm.workspace.toString().equals("[[*file*word*") || mm.workspace.toString().equals("[[*link*word*") || mm.workspace.toString().equals("[[*file*text*") || mm.workspace.toString().equals("[[*link*text*")) { mm.workspace.setLength(0); /* clear */ mm.workspace.append(mm.tape.get(mm.tapePointer)); /* get */ mm.workspace.append(" "); /* add */ mm.increment(); /* ++ */ mm.workspace.append(mm.tape.get(mm.tapePointer)); /* get */ mm.workspace.append(" "); /* add */ mm.increment(); /* ++ */ mm.workspace.append(mm.tape.get(mm.tapePointer)); /* get */ if (mm.tapePointer > 0) mm.tapePointer--; /* -- */ if (mm.tapePointer > 0) mm.tapePointer--; /* -- */ mm.tape.get(mm.tapePointer).setLength(0); /* put */ mm.tape.get(mm.tapePointer).append(mm.workspace); mm.workspace.setLength(0); /* clear */ mm.workspace.append("text*"); /* add */ mm.push(); continue parse; } //---------------------- // 4 tokens mm.pop(); // second level headings, eg SUBSECTION .... if (mm.workspace.toString().equals("nl*utext*4dots*nl*") || mm.workspace.toString().equals("nl*uword*4dots*nl*")) { mm.workspace.setLength(0); /* clear */ mm.workspace.append(mm.tape.get(mm.tapePointer)); /* get */ // add a link so that table-of-contents works mm.workspace.append("\n"); /* add */ mm.workspace.append("\n"); /* add */ mm.workspace.append("

"); /* add */ mm.workspace.append(mm.tape.get(mm.tapePointer)); /* get */ mm.workspace.append(" (↑)"); /* add */ mm.workspace.append("

\n"); /* add */ if (mm.tapePointer > 0) mm.tapePointer--; /* -- */ mm.tape.get(mm.tapePointer).setLength(0); /* put */ mm.tape.get(mm.tapePointer).append(mm.workspace); mm.workspace.setLength(0); /* clear */ // transfer nl value mm.increment(); /* ++ */ mm.increment(); /* ++ */ mm.increment(); /* ++ */ mm.workspace.append(mm.tape.get(mm.tapePointer)); /* get */ if (mm.tapePointer > 0) mm.tapePointer--; /* -- */ if (mm.tapePointer > 0) mm.tapePointer--; /* -- */ mm.tape.get(mm.tapePointer).setLength(0); /* put */ mm.tape.get(mm.tapePointer).append(mm.workspace); mm.workspace.setLength(0); /* clear */ if (mm.tapePointer > 0) mm.tapePointer--; /* -- */ mm.workspace.append("text*nl*"); /* add */ mm.push(); mm.push(); continue parse; } // image format with caption: [[ /path/file.jpg "caption" ]] if (mm.workspace.toString().equals("[[*file*quoted*]]*") || mm.workspace.toString().equals("[[*link*quoted*]]*")) { mm.workspace.setLength(0); /* clear */ mm.workspace.append("

"); /* add */ mm.increment(); /* ++ */ mm.workspace.append(mm.tape.get(mm.tapePointer)); /* get */ mm.workspace.append("

\n"); /* add */ if (mm.tapePointer > 0) mm.tapePointer--; /* -- */ if (mm.tapePointer > 0) mm.tapePointer--; /* -- */ mm.tape.get(mm.tapePointer).setLength(0); /* put */ mm.tape.get(mm.tapePointer).append(mm.workspace); mm.workspace.setLength(0); /* clear */ mm.workspace.append("text*"); /* add */ mm.push(); continue parse; } // image format with float: [[ /path/file.jpg >> ]] if (mm.workspace.toString().equals("[[*file*>>*]]*") || mm.workspace.toString().equals("[[*link*>>*]]*")) { mm.workspace.setLength(0); /* clear */ mm.workspace.append("\n"); /* add */ if (mm.tapePointer > 0) mm.tapePointer--; /* -- */ mm.tape.get(mm.tapePointer).setLength(0); /* put */ mm.tape.get(mm.tapePointer).append(mm.workspace); mm.workspace.setLength(0); /* clear */ mm.workspace.append("text*"); /* add */ mm.push(); continue parse; } // image format with float: [[ /path/file.jpg >> ]] if (mm.workspace.toString().equals("[[*file*<<*]]*") || mm.workspace.toString().equals("[[*link*<<*]]*")) { mm.workspace.setLength(0); /* clear */ mm.workspace.append("\n"); /* add */ if (mm.tapePointer > 0) mm.tapePointer--; /* -- */ mm.tape.get(mm.tapePointer).setLength(0); /* put */ mm.tape.get(mm.tapePointer).append(mm.workspace); mm.workspace.setLength(0); /* clear */ mm.workspace.append("text*"); /* add */ mm.push(); continue parse; } //---------------------- // 5 tokens mm.pop(); // image format with caption and float: [[ /path/file.jpg "caption" >> ]] if (mm.workspace.toString().equals("[[*file*quoted*>>*]]*") || mm.workspace.toString().equals("[[*link*quoted*>>*]]*")) { mm.workspace.setLength(0); /* clear */ mm.workspace.append("

"); /* add */ mm.increment(); /* ++ */ mm.workspace.append(mm.tape.get(mm.tapePointer)); /* get */ mm.workspace.append("

\n"); /* add */ if (mm.tapePointer > 0) mm.tapePointer--; /* -- */ if (mm.tapePointer > 0) mm.tapePointer--; /* -- */ mm.tape.get(mm.tapePointer).setLength(0); /* put */ mm.tape.get(mm.tapePointer).append(mm.workspace); mm.workspace.setLength(0); /* clear */ mm.workspace.append("text*"); /* add */ mm.push(); continue parse; } // image format with caption and float: [[ /path/file.jpg "caption" >> ]] if (mm.workspace.toString().equals("[[*file*quoted*<<*]]*") || mm.workspace.toString().equals("[[*link*quoted*<<*]]*")) { mm.workspace.setLength(0); /* clear */ mm.workspace.append("

"); /* add */ mm.increment(); /* ++ */ mm.workspace.append(mm.tape.get(mm.tapePointer)); /* get */ mm.workspace.append("

\n"); /* add */ if (mm.tapePointer > 0) mm.tapePointer--; /* -- */ if (mm.tapePointer > 0) mm.tapePointer--; /* -- */ mm.tape.get(mm.tapePointer).setLength(0); /* put */ mm.tape.get(mm.tapePointer).append(mm.workspace); mm.workspace.setLength(0); /* clear */ mm.workspace.append("text*"); /* add */ mm.push(); continue parse; } mm.push(); mm.push(); mm.push(); mm.push(); mm.push(); if (mm.eof) { mm.workspace.append("\n \n"); /* add */ System.out.print(mm.workspace); /* print */ mm.workspace.setLength(0); /* clear */ // workspace should be empty if (!mm.workspace.toString().equals("")) { mm.tape.get(mm.tapePointer).setLength(0); /* put */ mm.tape.get(mm.tapePointer).append(mm.workspace); mm.workspace.setLength(0); /* clear */ mm.workspace.append("\n"); /* add */ System.out.print(mm.workspace); /* print */ } mm.workspace.append("\n"); /* add */ System.out.print(mm.workspace); /* print */ mm.workspace.setLength(0); /* clear */ // pop; mm.pop(); if (mm.workspace.toString().equals("word*") || mm.workspace.toString().equals("text*") || mm.workspace.toString().equals("link*") || mm.workspace.toString().equals("file*") || mm.workspace.toString().equals("quoted*") || mm.workspace.toString().equals("emline*") || mm.workspace.toString().equals("nl*")) { mm.workspace.setLength(0); /* clear */ mm.workspace.append(""); mm.workspace.append("\n"); mm.workspace.append("\n "); mm.workspace.append("\n "); mm.workspace.append("\n "); mm.workspace.append("\n"); mm.workspace.append("\n "); mm.workspace.append("\n "); mm.workspace.append("\n "); mm.workspace.append("\n "); mm.workspace.append("\n ..."); mm.workspace.append("\n"); mm.workspace.append("\n "); mm.workspace.append("\n "); mm.workspace.append("\n "); mm.workspace.append("\n "); mm.workspace.append("\n "); mm.workspace.append("\n"); mm.workspace.append("\n "); /* add */ mm.workspace.append(mm.tape.get(mm.tapePointer)); /* get */ mm.workspace.append("\n\n"); /* add */ mm.workspace.append("\n"); /* add */ System.out.print(mm.workspace); /* print */ } } break parse; } } } }