src/Pure/Thy/thy_info.ML
changeset 72651 52cb065aa916
parent 72638 2a7fc87495e0
child 72669 5e7916535860
equal deleted inserted replaced
72650:787ba1d19d3a 72651:52cb065aa916
     1 (*  Title:      Pure/Thy/thy_info.ML
     1 (*  Title:      Pure/Thy/thy_info.ML
     2     Author:     Markus Wenzel, TU Muenchen
     2     Author:     Makarius
     3 
     3 
     4 Global theory info database, with auto-loading according to theory and
     4 Global theory info database, with auto-loading according to theory and
     5 file dependencies.
     5 file dependencies, and presentation of theory documents.
     6 *)
     6 *)
     7 
     7 
     8 signature THY_INFO =
     8 signature THY_INFO =
     9 sig
     9 sig
    10   type presentation_context =
    10   type presentation_context =
    26 end;
    26 end;
    27 
    27 
    28 structure Thy_Info: THY_INFO =
    28 structure Thy_Info: THY_INFO =
    29 struct
    29 struct
    30 
    30 
    31 (** presentation of consolidated theory **)
    31 (** theory presentation **)
       
    32 
       
    33 (* artefact names *)
       
    34 
       
    35 fun document_name ext thy =
       
    36   Path.explode_binding0 ("document/" ^ Context.theory_name thy ^ "." ^ ext);
       
    37 
       
    38 val document_html_name = document_name "html";
       
    39 val document_tex_name = document_name "tex";
       
    40 
       
    41 fun html_name thy = Path.basic (Context.theory_name thy ^ ".html");
       
    42 
       
    43 
       
    44 (* theory as HTML *)
       
    45 
       
    46 local
       
    47 
       
    48 fun get_session_chapter thy =
       
    49   let
       
    50     val session = Resources.theory_qualifier (Context.theory_long_name thy);
       
    51     val chapter = Resources.session_chapter session;
       
    52   in (session, chapter) end;
       
    53 
       
    54 fun theory_link thy thy' =
       
    55   let
       
    56     val ((session, chapter), (session', chapter')) = apply2 get_session_chapter (thy, thy');
       
    57     val link = html_name thy';
       
    58   in
       
    59     if session = session' then link
       
    60     else if chapter = chapter' then Path.parent + Path.basic session' + link
       
    61     else Path.parent + Path.parent + Path.basic chapter' + Path.basic session' + link
       
    62   end;
       
    63 
       
    64 fun theory_document symbols A Bs body =
       
    65   HTML.begin_document symbols ("Theory " ^ A) ^ "\n" ^
       
    66   HTML.command symbols "theory" ^ " " ^ HTML.name symbols A ^ "<br/>\n" ^
       
    67   (if null Bs then ""
       
    68    else
       
    69     HTML.keyword symbols "imports" ^ " " ^
       
    70       space_implode " " (map (uncurry HTML.href_path o apsnd (HTML.name symbols)) Bs) ^ "<br/>\n") ^
       
    71   "\n</div>\n<div class=\"source\">\n<pre class=\"source\">" ::
       
    72   body @ ["</pre>\n", HTML.end_document];
       
    73 
       
    74 in
       
    75 
       
    76 fun export_html thy spans =
       
    77   let
       
    78     val name = Context.theory_name thy;
       
    79     val parents =
       
    80       Theory.parents_of thy |> map (fn thy' =>
       
    81         (Url.File (theory_link thy thy'), Context.theory_name thy'));
       
    82 
       
    83     val symbols = Resources.html_symbols ();
       
    84     val keywords = Thy_Header.get_keywords thy;
       
    85     val body = map (HTML.present_span symbols keywords) spans;
       
    86     val html = XML.blob (theory_document symbols name parents body);
       
    87   in Export.export thy (document_html_name thy) html end
       
    88 
       
    89 end;
       
    90 
       
    91 
       
    92 (* hook for consolidated theory *)
    32 
    93 
    33 type presentation_context =
    94 type presentation_context =
    34   {options: Options.T, file_pos: Position.T, adjust_pos: Position.T -> Position.T,
    95   {options: Options.T, file_pos: Position.T, adjust_pos: Position.T -> Position.T,
    35     segments: Thy_Output.segment list};
    96     segments: Thy_Output.segment list};
    36 
    97 
    50 
   111 
    51 fun add_presentation f = Presentation.map (cons (f, stamp ()));
   112 fun add_presentation f = Presentation.map (cons (f, stamp ()));
    52 
   113 
    53 val _ =
   114 val _ =
    54   Theory.setup (add_presentation (fn {options, file_pos, segments, ...} => fn thy =>
   115   Theory.setup (add_presentation (fn {options, file_pos, segments, ...} => fn thy =>
    55    (Present.export_html thy (map #span segments);
   116    (export_html thy (map #span segments);
    56     if exists (Toplevel.is_skipped_proof o #state) segments then ()
   117     if exists (Toplevel.is_skipped_proof o #state) segments then ()
    57     else
   118     else
    58       let
   119       let
    59         val body = Thy_Output.present_thy options thy segments;
   120         val body = Thy_Output.present_thy options thy segments;
    60       in
   121       in
    61         if Options.string options "document" = "false" then ()
   122         if Options.string options "document" = "false" then ()
    62         else
   123         else
    63           let
   124           let
    64             val thy_name = Context.theory_name thy;
   125             val thy_name = Context.theory_name thy;
    65             val document_tex_name = Present.document_tex_name thy;
   126             val document_tex_name = document_tex_name thy;
    66 
   127 
    67             val latex = Latex.isabelle_body thy_name body;
   128             val latex = Latex.isabelle_body thy_name body;
    68             val output = [Latex.output_text latex, Latex.output_positions file_pos latex];
   129             val output = [Latex.output_text latex, Latex.output_positions file_pos latex];
    69           in Export.export thy document_tex_name (XML.blob output) end
   130           in Export.export thy document_tex_name (XML.blob output) end
    70       end)));
   131       end)));