src/Pure/Thy/html.ML
changeset 6338 bf0d22535e47
parent 6324 3b7111b360b1
child 6347 ce7ab97a8e15
equal deleted inserted replaced
6337:150182bcb7da 6338:bf0d22535e47
     1 (*  Title:      Pure/Thy/HTML.ML
     1 (*  Title:      Pure/Thy/HTML.ML
     2     ID:         $Id$
     2     ID:         $Id$
     3     Author:     Markus Wenzel, TU Muenchen
     3     Author:     Markus Wenzel, TU Muenchen
     4 
     4 
     5 HTML markup elements.
     5 HTML presentation primitives.
     6 *)
     6 *)
     7 
     7 
     8 signature HTML =
     8 signature HTML =
     9 sig
     9 sig
    10   val output: string -> string
    10   val output: string -> string
    20   val href_path: Path.T -> text -> text
    20   val href_path: Path.T -> text -> text
    21   val preform: text -> text
    21   val preform: text -> text
    22   val verbatim: string -> text
    22   val verbatim: string -> text
    23   val begin_document: string -> text
    23   val begin_document: string -> text
    24   val end_document: text
    24   val end_document: text
    25   val insert_here: text
    25   val begin_index: Path.T * string -> Path.T * string -> Path.T option -> text
       
    26   val end_index: text
       
    27   val theory_entry: Path.T * string -> text
       
    28   val session_entries: (Path.T * string) list -> text
    26   val source: (string, 'a) Source.source -> text
    29   val source: (string, 'a) Source.source -> text
    27   val begin_theory: Path.T * string -> string -> string list -> ((Path.T * Path.T) * bool) list
    30   val begin_theory: Path.T * string -> string -> string list -> ((Path.T * Path.T) * bool) list
    28     -> text -> text
    31     -> text -> text
    29   val end_theory: text
    32   val end_theory: text
    30   val ml_file: Path.T -> string -> text
    33   val ml_file: Path.T -> string -> text
    31   val conclude_theory: text
       
    32   val theorem: string -> thm -> text
    34   val theorem: string -> thm -> text
    33   val section: string -> text
    35   val section: string -> text
    34   val setup: (theory -> theory) list
    36   val setup: (theory -> theory) list
    35 end;
    37 end;
    36 
    38 
    46 fun html_mode f x = setmp print_mode [htmlN] f x;
    48 fun html_mode f x = setmp print_mode [htmlN] f x;
    47 
    49 
    48 
    50 
    49 (* symbol output *)
    51 (* symbol output *)
    50 
    52 
    51 val escape = fn "<" => "&lt;" | ">" => "&gt;" | "&" => "&amp;" | c => c;
    53 local
    52 
    54   val escape = fn "<" => "&lt;" | ">" => "&gt;" | "&" => "&amp;" | c => c;
    53 val output_chars = implode o map escape;
    55 
    54 
    56   val output_sym =
    55 fun output s =
    57     fn "\\<not>" => (1.0, "&not;")
    56   if not (exists_string (equal "<" orf equal ">" orf equal "&") s) then s
    58      | "\\<hyphen>" => (1.0, "&shy;")
    57   else implode (map escape (explode s));        (*sic!*)
    59      | "\\<cdot>" => (1.0, "&middot;")
    58 
    60      | "\\<times>" => (1.0, "&times;")
    59 fun output_width s = (output s, real (size s));
    61      | "\\<copyright>" => (1.0, "&copy;")
       
    62      | "\\<mu>" => (1.0, "&micro;")
       
    63      | s => (real (size s), implode (map escape (explode s)));
       
    64 
       
    65   fun add_sym (width, (w: real, s)) = (width + w, s);
       
    66 in
       
    67 
       
    68 fun output_width str =
       
    69   if not (exists_string (equal "<" orf equal ">" orf equal "&") str) then (str, real (size str))
       
    70   else
       
    71     let val (width, syms) = foldl_map add_sym (0.0, map output_sym (Symbol.explode str))
       
    72     in (implode syms, width) end;
       
    73 
       
    74 val output = #1 o output_width;
       
    75 val output_symbols = map (#2 o output_sym);
       
    76 
       
    77 end;
       
    78 
    60 
    79 
    61 val _ = Symbol.add_mode htmlN output_width;
    80 val _ = Symbol.add_mode htmlN output_width;
    62 
    81 
    63 
    82 
    64 (* token translations *)
    83 (* token translations *)
   116 
   135 
   117 val end_document =
   136 val end_document =
   118   "</body>\n\
   137   "</body>\n\
   119   \</html>\n";
   138   \</html>\n";
   120 
   139 
   121 val insert_here = "<!--insert--here-->";
   140 fun back_link (back_path, back_name) =
       
   141   para (href_path back_path "Back" ^ " to index of " ^ plain back_name);
       
   142 
       
   143 
       
   144 (* session index *)
       
   145 
       
   146 fun begin_index back (index_path, session) opt_readme =
       
   147   begin_document ("Index of " ^ session) ^ back_link back ^
       
   148   (case opt_readme of None => "" | Some p => para (href_path p "README" ^ " file")) ^
       
   149   "\n<hr>\n\n<h2>Theories</h2>\n";
       
   150 
       
   151 val end_index = end_document;
       
   152 
       
   153 fun entry (p, s) = href_path p (plain s) ^ "<br>\n";
       
   154 val theory_entry = entry;
       
   155 
       
   156 fun session_entries [] = ""
       
   157   | session_entries es = "\n<hr>\n\n<h2>Sessions</h2>\n" ^ cat_lines (map entry es);
   122 
   158 
   123 
   159 
   124 (* theory *)
   160 (* theory *)
   125 
   161 
   126 fun source src =
   162 fun output_source src = implode (output_symbols (Source.exhaust (Symbol.source false src)));
   127   "\n<hr>\n<pre>" ^ output_chars (Source.exhaust src) ^ "</pre>\n<hr>\n";
   163 fun source src = "\n<hr>\n<pre>" ^ output_source src ^ "</pre>\n<hr>\n";
   128 
   164 
   129 
   165 
   130 local
   166 local
   131   fun file ((p, p'), loadit) =
   167   fun file ((p, p'), loadit) =
   132     href_path p' ((if not loadit then enclose "(" ")" else I) (file_path p));
   168     href_path p' ((if not loadit then enclose "(" ")" else I) (file_path p));
   134   fun suffix_last _ [] = []
   170   fun suffix_last _ [] = []
   135     | suffix_last s lst = let val (xs, x) = split_last lst in xs @ [x ^ s] end;
   171     | suffix_last s lst = let val (xs, x) = split_last lst in xs @ [x ^ s] end;
   136 
   172 
   137   val plus_names = space_implode " + " o map name;
   173   val plus_names = space_implode " + " o map name;
   138 
   174 
   139   fun theory (back_path, back_name) A =
   175   fun theory back A =
   140     begin_document ("Theory " ^ A) ^ "\n" ^
   176     begin_document ("Theory " ^ A) ^ "\n" ^ back_link back ^
   141     href_path back_path "Back" ^ " to the index of " ^ plain back_name ^ "\n<p>\n" ^
       
   142     keyword "theory" ^ " " ^ name A ^ " = ";
   177     keyword "theory" ^ " " ^ name A ^ " = ";
   143 in
   178 in
   144 
   179 
   145 fun begin_theory back A Bs [] body = theory back A ^ plus_names (suffix_last ":" Bs) ^ "\n" ^ body
   180 fun begin_theory back A Bs [] body = theory back A ^ plus_names (suffix_last ":" Bs) ^ "\n" ^ body
   146   | begin_theory back A Bs Ps body =
   181   | begin_theory back A Bs Ps body =
   147       theory back A ^ plus_names Bs ^
   182       theory back A ^ plus_names Bs ^
   148       "<br>" ^ keyword "files" ^ " " ^ space_implode " + " (map file Ps) ^ ":" ^ "\n" ^ body;
   183       "<br>" ^ keyword "files" ^ " " ^ space_implode " + " (map file Ps) ^ ":" ^ "\n" ^ body;
   149 end;
   184 end;
   150 
   185 
   151 val end_theory = "";
   186 val end_theory = end_document;
   152 val conclude_theory = end_document;
       
   153 
   187 
   154 
   188 
   155 (* ML file *)
   189 (* ML file *)
   156 
   190 
   157 fun ml_file path str =
   191 fun ml_file path str =