| author | wenzelm | 
| Sun, 01 Mar 2009 16:21:33 +0100 | |
| changeset 30187 | b92b3375e919 | 
| parent 29606 | fedb8be05f24 | 
| child 30573 | 49899f26fbd1 | 
| permissions | -rw-r--r-- | 
| 7726 
2c7fc0ba1e12
Simple LaTeX presentation primitives (based on outer lexical syntax).
 wenzelm parents: diff
changeset | 1 | (* Title: Pure/Thy/latex.ML | 
| 
2c7fc0ba1e12
Simple LaTeX presentation primitives (based on outer lexical syntax).
 wenzelm parents: diff
changeset | 2 | Author: Markus Wenzel, TU Muenchen | 
| 
2c7fc0ba1e12
Simple LaTeX presentation primitives (based on outer lexical syntax).
 wenzelm parents: diff
changeset | 3 | |
| 15801 | 4 | LaTeX presentation elements -- based on outer lexical syntax. | 
| 7726 
2c7fc0ba1e12
Simple LaTeX presentation primitives (based on outer lexical syntax).
 wenzelm parents: diff
changeset | 5 | *) | 
| 
2c7fc0ba1e12
Simple LaTeX presentation primitives (based on outer lexical syntax).
 wenzelm parents: diff
changeset | 6 | |
| 
2c7fc0ba1e12
Simple LaTeX presentation primitives (based on outer lexical syntax).
 wenzelm parents: diff
changeset | 7 | signature LATEX = | 
| 
2c7fc0ba1e12
Simple LaTeX presentation primitives (based on outer lexical syntax).
 wenzelm parents: diff
changeset | 8 | sig | 
| 14924 | 9 | val output_known_symbols: (string -> bool) * (string -> bool) -> | 
| 10 | Symbol.symbol list -> string | |
| 11719 | 11 | val output_symbols: Symbol.symbol list -> string | 
| 17081 | 12 | val output_basic: OuterLex.token -> string | 
| 13 | val output_markup: string -> string -> string | |
| 14 | val output_markup_env: string -> string -> string | |
| 15 | val output_verbatim: string -> string | |
| 16 | val markup_true: string | |
| 17 | val markup_false: string | |
| 18 | val begin_delim: string -> string | |
| 19 | val end_delim: string -> string | |
| 20 | val begin_tag: string -> string | |
| 21 | val end_tag: string -> string | |
| 9707 | 22 | val tex_trailer: string | 
| 9916 | 23 | val isabelle_file: string -> string -> string | 
| 14924 | 24 | val symbol_source: (string -> bool) * (string -> bool) -> | 
| 25 | string -> Symbol.symbol list -> string | |
| 7726 
2c7fc0ba1e12
Simple LaTeX presentation primitives (based on outer lexical syntax).
 wenzelm parents: diff
changeset | 26 | val theory_entry: string -> string | 
| 9135 | 27 | val modes: string list | 
| 7726 
2c7fc0ba1e12
Simple LaTeX presentation primitives (based on outer lexical syntax).
 wenzelm parents: diff
changeset | 28 | end; | 
| 
2c7fc0ba1e12
Simple LaTeX presentation primitives (based on outer lexical syntax).
 wenzelm parents: diff
changeset | 29 | |
| 
2c7fc0ba1e12
Simple LaTeX presentation primitives (based on outer lexical syntax).
 wenzelm parents: diff
changeset | 30 | structure Latex: LATEX = | 
| 
2c7fc0ba1e12
Simple LaTeX presentation primitives (based on outer lexical syntax).
 wenzelm parents: diff
changeset | 31 | struct | 
| 
2c7fc0ba1e12
Simple LaTeX presentation primitives (based on outer lexical syntax).
 wenzelm parents: diff
changeset | 32 | |
| 
2c7fc0ba1e12
Simple LaTeX presentation primitives (based on outer lexical syntax).
 wenzelm parents: diff
changeset | 33 | |
| 
2c7fc0ba1e12
Simple LaTeX presentation primitives (based on outer lexical syntax).
 wenzelm parents: diff
changeset | 34 | (* symbol output *) | 
| 
2c7fc0ba1e12
Simple LaTeX presentation primitives (based on outer lexical syntax).
 wenzelm parents: diff
changeset | 35 | |
| 7900 | 36 | local | 
| 37 | ||
| 7726 
2c7fc0ba1e12
Simple LaTeX presentation primitives (based on outer lexical syntax).
 wenzelm parents: diff
changeset | 38 | val output_chr = fn | 
| 9505 | 39 | " " => "\\ " | | 
| 7726 
2c7fc0ba1e12
Simple LaTeX presentation primitives (based on outer lexical syntax).
 wenzelm parents: diff
changeset | 40 | "\n" => "\\isanewline\n" | | 
| 9668 | 41 |   "!" => "{\\isacharbang}" |
 | 
| 42 |   "\"" => "{\\isachardoublequote}" |
 | |
| 9663 | 43 |   "#" => "{\\isacharhash}" |
 | 
| 44 |   "$" => "{\\isachardollar}" |
 | |
| 45 |   "%" => "{\\isacharpercent}" |
 | |
| 46 |   "&" => "{\\isacharampersand}" |
 | |
| 47 |   "'" => "{\\isacharprime}" |
 | |
| 48 |   "(" => "{\\isacharparenleft}" |
 | |
| 49 |   ")" => "{\\isacharparenright}" |
 | |
| 50 |   "*" => "{\\isacharasterisk}" |
 | |
| 9668 | 51 |   "+" => "{\\isacharplus}" |
 | 
| 52 |   "," => "{\\isacharcomma}" |
 | |
| 9663 | 53 |   "-" => "{\\isacharminus}" |
 | 
| 9668 | 54 |   "." => "{\\isachardot}" |
 | 
| 55 |   "/" => "{\\isacharslash}" |
 | |
| 56 |   ":" => "{\\isacharcolon}" |
 | |
| 57 |   ";" => "{\\isacharsemicolon}" |
 | |
| 9663 | 58 |   "<" => "{\\isacharless}" |
 | 
| 9668 | 59 |   "=" => "{\\isacharequal}" |
 | 
| 9663 | 60 |   ">" => "{\\isachargreater}" |
 | 
| 9668 | 61 |   "?" => "{\\isacharquery}" |
 | 
| 62 |   "@" => "{\\isacharat}" |
 | |
| 9663 | 63 |   "[" => "{\\isacharbrackleft}" |
 | 
| 64 |   "\\" => "{\\isacharbackslash}" |
 | |
| 65 |   "]" => "{\\isacharbrackright}" |
 | |
| 66 |   "^" => "{\\isacharcircum}" |
 | |
| 67 |   "_" => "{\\isacharunderscore}" |
 | |
| 9668 | 68 |   "`" => "{\\isacharbackquote}" |
 | 
| 9663 | 69 |   "{" => "{\\isacharbraceleft}" |
 | 
| 70 |   "|" => "{\\isacharbar}" |
 | |
| 71 |   "}" => "{\\isacharbraceright}" |
 | |
| 72 |   "~" => "{\\isachartilde}" |
 | |
| 14992 | 73 |   c => if Symbol.is_ascii_digit c then enclose "{\\isadigit{" "}}" c else c;
 | 
| 7726 
2c7fc0ba1e12
Simple LaTeX presentation primitives (based on outer lexical syntax).
 wenzelm parents: diff
changeset | 74 | |
| 14924 | 75 | val output_chrs = translate_string output_chr; | 
| 76 | ||
| 77 | fun output_known_sym (known_sym, known_ctrl) sym = | |
| 14874 | 78 | (case Symbol.decode sym of | 
| 79 | Symbol.Char s => output_chr s | |
| 14924 | 80 |   | Symbol.Sym s => if known_sym s then enclose "{\\isasym" "}" s else output_chrs sym
 | 
| 81 | | Symbol.Ctrl s => if known_ctrl s then enclose "\\isactrl" " " s else output_chrs sym | |
| 14874 | 82 | | Symbol.Raw s => s); | 
| 7900 | 83 | |
| 84 | in | |
| 85 | ||
| 14924 | 86 | val output_known_symbols = implode oo (map o output_known_sym); | 
| 87 | val output_symbols = output_known_symbols (K true, K true); | |
| 8192 | 88 | val output_syms = output_symbols o Symbol.explode; | 
| 7900 | 89 | |
| 27874 
f0364f1c0ecf
antiquotes: proper SymbolPos decoding, adapted Antiquote.read/Antiq;
 wenzelm parents: 
27809diff
changeset | 90 | val output_syms_antiq = | 
| 22648 
8c6b4f7548e3
output_basic: added isaantiq markup (only inside verbatim tokens);
 wenzelm parents: 
19265diff
changeset | 91 | (fn Antiquote.Text s => output_syms s | 
| 27874 
f0364f1c0ecf
antiquotes: proper SymbolPos decoding, adapted Antiquote.read/Antiq;
 wenzelm parents: 
27809diff
changeset | 92 | | Antiquote.Antiq (ss, _) => | 
| 
f0364f1c0ecf
antiquotes: proper SymbolPos decoding, adapted Antiquote.read/Antiq;
 wenzelm parents: 
27809diff
changeset | 93 | enclose "%\n\\isaantiq\n" "%\n\\endisaantiq\n" (output_symbols (map SymbolPos.symbol ss)) | 
| 27344 | 94 |     | Antiquote.Open _ => "{\\isaantiqopen}"
 | 
| 27874 
f0364f1c0ecf
antiquotes: proper SymbolPos decoding, adapted Antiquote.read/Antiq;
 wenzelm parents: 
27809diff
changeset | 95 |     | Antiquote.Close _ => "{\\isaantiqclose}");
 | 
| 22648 
8c6b4f7548e3
output_basic: added isaantiq markup (only inside verbatim tokens);
 wenzelm parents: 
19265diff
changeset | 96 | |
| 7900 | 97 | end; | 
| 7726 
2c7fc0ba1e12
Simple LaTeX presentation primitives (based on outer lexical syntax).
 wenzelm parents: diff
changeset | 98 | |
| 
2c7fc0ba1e12
Simple LaTeX presentation primitives (based on outer lexical syntax).
 wenzelm parents: diff
changeset | 99 | |
| 
2c7fc0ba1e12
Simple LaTeX presentation primitives (based on outer lexical syntax).
 wenzelm parents: diff
changeset | 100 | (* token output *) | 
| 
2c7fc0ba1e12
Simple LaTeX presentation primitives (based on outer lexical syntax).
 wenzelm parents: diff
changeset | 101 | |
| 
2c7fc0ba1e12
Simple LaTeX presentation primitives (based on outer lexical syntax).
 wenzelm parents: diff
changeset | 102 | structure T = OuterLex; | 
| 
2c7fc0ba1e12
Simple LaTeX presentation primitives (based on outer lexical syntax).
 wenzelm parents: diff
changeset | 103 | |
| 28375 | 104 | val invisible_token = T.keyword_with (fn s => s = ";") orf T.is_kind T.Comment; | 
| 7756 | 105 | |
| 17081 | 106 | fun output_basic tok = | 
| 27809 
a1e409db516b
unified Args.T with OuterLex.token, renamed some operations;
 wenzelm parents: 
27781diff
changeset | 107 | let val s = T.content_of tok in | 
| 17081 | 108 | if invisible_token tok then "" | 
| 109 | else if T.is_kind T.Command tok then | |
| 110 |       "\\isacommand{" ^ output_syms s ^ "}"
 | |
| 111 | else if T.is_kind T.Keyword tok andalso Syntax.is_ascii_identifier s then | |
| 112 |       "\\isakeyword{" ^ output_syms s ^ "}"
 | |
| 17169 | 113 | else if T.is_kind T.String tok then | 
| 114 |       enclose "{\\isachardoublequoteopen}" "{\\isachardoublequoteclose}" (output_syms s)
 | |
| 115 | else if T.is_kind T.AltString tok then | |
| 116 |       enclose "{\\isacharbackquoteopen}" "{\\isacharbackquoteclose}" (output_syms s)
 | |
| 17186 | 117 | else if T.is_kind T.Verbatim tok then | 
| 27874 
f0364f1c0ecf
antiquotes: proper SymbolPos decoding, adapted Antiquote.read/Antiq;
 wenzelm parents: 
27809diff
changeset | 118 | let | 
| 27885 | 119 | val (txt, pos) = T.source_position_of tok; | 
| 27874 
f0364f1c0ecf
antiquotes: proper SymbolPos decoding, adapted Antiquote.read/Antiq;
 wenzelm parents: 
27809diff
changeset | 120 | val ants = Antiquote.read (SymbolPos.explode (txt, pos), pos); | 
| 
f0364f1c0ecf
antiquotes: proper SymbolPos decoding, adapted Antiquote.read/Antiq;
 wenzelm parents: 
27809diff
changeset | 121 | val out = implode (map output_syms_antiq ants); | 
| 
f0364f1c0ecf
antiquotes: proper SymbolPos decoding, adapted Antiquote.read/Antiq;
 wenzelm parents: 
27809diff
changeset | 122 |       in enclose "{\\isacharverbatimopen}" "{\\isacharverbatimclose}" out end
 | 
| 17081 | 123 | else output_syms s | 
| 124 | end; | |
| 125 | ||
| 126 | fun output_markup cmd txt = "%\n\\isamarkup" ^ cmd ^ "{" ^ Symbol.strip_blanks txt ^ "%\n}\n";
 | |
| 7745 | 127 | |
| 17081 | 128 | fun output_markup_env cmd txt = | 
| 129 |   "%\n\\begin{isamarkup" ^ cmd ^ "}%\n" ^
 | |
| 130 | Symbol.strip_blanks txt ^ | |
| 131 |   "%\n\\end{isamarkup" ^ cmd ^ "}%\n";
 | |
| 7756 | 132 | |
| 17081 | 133 | fun output_verbatim txt = "%\n" ^ Symbol.strip_blanks txt ^ "\n"; | 
| 7726 
2c7fc0ba1e12
Simple LaTeX presentation primitives (based on outer lexical syntax).
 wenzelm parents: diff
changeset | 134 | |
| 17081 | 135 | val markup_true = "\\isamarkuptrue%\n"; | 
| 136 | val markup_false = "\\isamarkupfalse%\n"; | |
| 7726 
2c7fc0ba1e12
Simple LaTeX presentation primitives (based on outer lexical syntax).
 wenzelm parents: diff
changeset | 137 | |
| 17081 | 138 | val begin_delim = enclose "%\n\\isadelim" "\n"; | 
| 139 | val end_delim = enclose "%\n\\endisadelim" "\n"; | |
| 140 | val begin_tag = enclose "%\n\\isatag" "\n"; | |
| 141 | fun end_tag tg = enclose "%\n\\endisatag" "\n" tg ^ enclose "{\\isafold" "}%\n" tg;
 | |
| 11860 | 142 | |
| 143 | ||
| 7726 
2c7fc0ba1e12
Simple LaTeX presentation primitives (based on outer lexical syntax).
 wenzelm parents: diff
changeset | 144 | (* theory presentation *) | 
| 
2c7fc0ba1e12
Simple LaTeX presentation primitives (based on outer lexical syntax).
 wenzelm parents: diff
changeset | 145 | |
| 9707 | 146 | val tex_trailer = | 
| 147 | "%%% Local Variables:\n\ | |
| 9135 | 148 | \%%% mode: latex\n\ | 
| 149 | \%%% TeX-master: \"root\"\n\ | |
| 9144 | 150 | \%%% End:\n"; | 
| 8192 | 151 | |
| 9916 | 152 | fun isabelle_file name txt = | 
| 153 |   "%\n\\begin{isabellebody}%\n\
 | |
| 10247 | 154 |   \\\def\\isabellecontext{" ^ output_syms name ^ "}%\n" ^ txt ^
 | 
| 9916 | 155 |   "\\end{isabellebody}%\n" ^ tex_trailer;
 | 
| 9707 | 156 | |
| 14924 | 157 | fun symbol_source known name syms = isabelle_file name | 
| 158 |   ("\\isamarkupheader{" ^ output_known_symbols known (Symbol.explode name) ^ "}%\n" ^
 | |
| 159 | output_known_symbols known syms); | |
| 7726 
2c7fc0ba1e12
Simple LaTeX presentation primitives (based on outer lexical syntax).
 wenzelm parents: diff
changeset | 160 | |
| 9038 
63d20536971f
session.tex: nsert blank lines in order to guarantee new paragraphs
 wenzelm parents: 
8965diff
changeset | 161 | fun theory_entry name = "\\input{" ^ name ^ ".tex}\n\n";
 | 
| 7726 
2c7fc0ba1e12
Simple LaTeX presentation primitives (based on outer lexical syntax).
 wenzelm parents: diff
changeset | 162 | |
| 
2c7fc0ba1e12
Simple LaTeX presentation primitives (based on outer lexical syntax).
 wenzelm parents: diff
changeset | 163 | |
| 8460 | 164 | (* print mode *) | 
| 165 | ||
| 8965 | 166 | val latexN = "latex"; | 
| 17218 | 167 | val modes = [latexN, Symbol.xsymbolsN]; | 
| 8965 | 168 | |
| 8460 | 169 | fun latex_output str = | 
| 170 | let val syms = Symbol.explode str | |
| 23621 | 171 | in (output_symbols syms, Symbol.length syms) end; | 
| 19265 | 172 | |
| 23621 | 173 | fun latex_markup (s, _) = | 
| 174 |   if s = Markup.keywordN then ("\\isakeyword{", "}")
 | |
| 175 |   else if s = Markup.commandN then ("\\isacommand{", "}")
 | |
| 29325 | 176 | else Markup.no_output; | 
| 10955 | 177 | |
| 23621 | 178 | fun latex_indent "" _ = "" | 
| 179 |   | latex_indent s _ = enclose "\\isaindent{" "}" s;
 | |
| 180 | ||
| 181 | val _ = Output.add_mode latexN latex_output Symbol.encode_raw; | |
| 23703 | 182 | val _ = Markup.add_mode latexN latex_markup; | 
| 183 | val _ = Pretty.add_mode latexN latex_indent; | |
| 8460 | 184 | |
| 7726 
2c7fc0ba1e12
Simple LaTeX presentation primitives (based on outer lexical syntax).
 wenzelm parents: diff
changeset | 185 | end; |