src/Pure/Thy/thy_output.ML
changeset 61457 3e21699bb83b
parent 61456 b521b8b400f7
child 61458 987533262fc2
equal deleted inserted replaced
61456:b521b8b400f7 61457:3e21699bb83b
    22     ({source: Token.src, state: Toplevel.state, context: Proof.context} -> 'a -> string) ->
    22     ({source: Token.src, state: Toplevel.state, context: Proof.context} -> 'a -> string) ->
    23       theory -> theory
    23       theory -> theory
    24   val boolean: string -> bool
    24   val boolean: string -> bool
    25   val integer: string -> int
    25   val integer: string -> int
    26   val eval_antiquote: Toplevel.state -> Antiquote.text_antiquote -> string
    26   val eval_antiquote: Toplevel.state -> Antiquote.text_antiquote -> string
    27   val report_text: Input.source -> unit
    27   val output_text: Toplevel.state -> {markdown: bool} -> Input.source -> string
    28   val check_text: Input.source -> Toplevel.state -> unit
       
    29   val present_thy: theory -> (Toplevel.transition * Toplevel.state) list -> Token.T list -> Buffer.T
    28   val present_thy: theory -> (Toplevel.transition * Toplevel.state) list -> Token.T list -> Buffer.T
    30   val pretty_text: Proof.context -> string -> Pretty.T
    29   val pretty_text: Proof.context -> string -> Pretty.T
    31   val pretty_term: Proof.context -> term -> Pretty.T
    30   val pretty_term: Proof.context -> term -> Pretty.T
    32   val pretty_thm: Proof.context -> thm -> Pretty.T
    31   val pretty_thm: Proof.context -> thm -> Pretty.T
    33   val str_of_source: Token.src -> string
    32   val str_of_source: Token.src -> string
    35     Token.src -> 'a list -> Pretty.T list
    34     Token.src -> 'a list -> Pretty.T list
    36   val string_of_margin: Proof.context -> Pretty.T -> string
    35   val string_of_margin: Proof.context -> Pretty.T -> string
    37   val output: Proof.context -> Pretty.T list -> string
    36   val output: Proof.context -> Pretty.T list -> string
    38   val verbatim_text: Proof.context -> string -> string
    37   val verbatim_text: Proof.context -> string -> string
    39   val old_header_command: Input.source -> Toplevel.transition -> Toplevel.transition
    38   val old_header_command: Input.source -> Toplevel.transition -> Toplevel.transition
    40   val document_command: (xstring * Position.T) option * Input.source ->
    39   val document_command: {markdown: bool} -> (xstring * Position.T) option * Input.source ->
    41     Toplevel.transition -> Toplevel.transition
    40     Toplevel.transition -> Toplevel.transition
    42 end;
    41 end;
    43 
    42 
    44 structure Thy_Output: THY_OUTPUT =
    43 structure Thy_Output: THY_OUTPUT =
    45 struct
    44 struct
   158   >> (fn ((name, props), args) => (props, Token.src name args));
   157   >> (fn ((name, props), args) => (props, Token.src name args));
   159 
   158 
   160 end;
   159 end;
   161 
   160 
   162 
   161 
   163 (* eval antiquotes *)
   162 (* eval antiquote *)
   164 
       
   165 fun read_antiquotes state source =
       
   166   let
       
   167     val ants =
       
   168       Antiquote.read' (Input.pos_of source) (Symbol_Pos.trim_blanks (Input.source_explode source));
       
   169 
       
   170     fun words (Antiquote.Text ss) = [(#1 (Symbol_Pos.range ss), Markup.words)]
       
   171       | words (Antiquote.Antiq _) = [];
       
   172     val _ = Position.reports (maps words ants);
       
   173   in ants end;
       
   174 
   163 
   175 fun eval_antiquote _ (Antiquote.Text ss) = Symbol_Pos.content ss
   164 fun eval_antiquote _ (Antiquote.Text ss) = Symbol_Pos.content ss
   176   | eval_antiquote state (Antiquote.Antiq (ss, {range = (pos, _), ...})) =
   165   | eval_antiquote state (Antiquote.Antiq (ss, {range = (pos, _), ...})) =
   177       let
   166       let
   178         val keywords =
   167         val keywords =
   190         val _ = cmd preview_ctxt;
   179         val _ = cmd preview_ctxt;
   191         val print_modes = space_explode "," (Config.get print_ctxt modes) @ Latex.modes;
   180         val print_modes = space_explode "," (Config.get print_ctxt modes) @ Latex.modes;
   192       in Print_Mode.with_modes print_modes (fn () => cmd print_ctxt) () end;
   181       in Print_Mode.with_modes print_modes (fn () => cmd print_ctxt) () end;
   193 
   182 
   194 
   183 
   195 (* check_text *)
   184 (* output text *)
   196 
   185 
   197 fun report_text source =
   186 fun output_text state {markdown} source =
   198   Position.report (Input.pos_of source) (Markup.language_document (Input.is_delimited source));
   187   let
   199 
   188     val pos = Input.pos_of source;
   200 fun check_text source state =
   189     val _ = Position.report pos (Markup.language_document (Input.is_delimited source));
   201  (report_text source;
   190     val syms = Input.source_explode source;
   202   if Toplevel.is_skipped_proof state then ()
   191 
   203   else
   192     val output_antiquote = eval_antiquote state #> Symbol.explode #> Latex.output_ctrl_symbols;
   204     source
   193     val output_antiquotes = map output_antiquote #> implode;
   205     |> read_antiquotes state
   194 
   206     |> List.app (ignore o eval_antiquote state));
   195     fun output_blocks blocks = space_implode "\n\n" (map output_block blocks)
       
   196     and output_block (Markdown.Paragraph lines) =
       
   197           cat_lines (map (output_antiquotes o Markdown.line_source) lines)
       
   198       | output_block (Markdown.List (marker, body)) =
       
   199           let val env = Markdown.print_kind (#kind marker) in
       
   200             "%\n\\begin{" ^ env ^ "}%\n" ^
       
   201             output_blocks body ^
       
   202             "%\n\\end{" ^ env ^ "}%\n"
       
   203           end;
       
   204   in
       
   205     if Toplevel.is_skipped_proof state then ""
       
   206     else if markdown andalso exists (Markdown.is_control o Symbol_Pos.symbol) syms
       
   207       andalso false (* FIXME *)
       
   208     then
       
   209       let
       
   210         val ants = Antiquote.read' pos syms;
       
   211         val blocks = Markdown.read_antiquotes ants;
       
   212         val _ = Position.reports (Markdown.reports blocks);
       
   213       in output_blocks blocks end
       
   214     else
       
   215       let
       
   216         val ants = Antiquote.read' pos (Symbol_Pos.trim_blanks syms);
       
   217         val _ = Position.reports (Markdown.text_reports ants);
       
   218       in output_antiquotes ants end
       
   219   end;
   207 
   220 
   208 
   221 
   209 
   222 
   210 (** present theory source **)
   223 (** present theory source **)
   211 
   224 
   230 val newline_token = basic_token Token.is_newline;
   243 val newline_token = basic_token Token.is_newline;
   231 
   244 
   232 
   245 
   233 (* output token *)
   246 (* output token *)
   234 
   247 
   235 fun output_token state =
   248 fun output_token state tok =
   236   let
   249   (case tok of
   237     val make_text = read_antiquotes state #> map (eval_antiquote state) #> implode;
   250     No_Token => ""
   238     val output_text = make_text #> Symbol.explode #> Latex.output_ctrl_symbols;
   251   | Basic_Token tok => Latex.output_token tok
   239     fun output No_Token = ""
   252   | Markup_Token (cmd, source) =>
   240       | output (Basic_Token tok) = Latex.output_token tok
   253       "%\n\\isamarkup" ^ cmd ^ "{" ^ output_text state {markdown = false} source ^ "%\n}\n"
   241       | output (Markup_Token (cmd, source)) =
   254   | Markup_Env_Token (cmd, source) =>
   242           "%\n\\isamarkup" ^ cmd ^ "{" ^ output_text source ^ "%\n}\n"
   255       "%\n\\begin{isamarkup" ^ cmd ^ "}%\n" ^
   243       | output (Markup_Env_Token (cmd, source)) =
   256       output_text state {markdown = true} source ^
   244           "%\n\\begin{isamarkup" ^ cmd ^ "}%\n" ^
   257       "%\n\\end{isamarkup" ^ cmd ^ "}%\n"
   245           output_text source ^
   258   | Raw_Token source =>
   246           "%\n\\end{isamarkup" ^ cmd ^ "}%\n"
   259       "%\n" ^ output_text state {markdown = true} source ^ "\n");
   247       | output (Raw_Token source) =
       
   248           "%\n" ^ output_text source ^ "\n";
       
   249   in output end;
       
   250 
   260 
   251 
   261 
   252 (* command spans *)
   262 (* command spans *)
   253 
   263 
   254 type command = string * Position.T * string list;   (*name, position, tags*)
   264 type command = string * Position.T * string list;   (*name, position, tags*)
   727 
   737 
   728 fun old_header_command txt =
   738 fun old_header_command txt =
   729   Toplevel.keep (fn state =>
   739   Toplevel.keep (fn state =>
   730     if Toplevel.is_toplevel state then
   740     if Toplevel.is_toplevel state then
   731      (legacy_feature "Obsolete 'header' command -- use 'chapter', 'section' etc. instead";
   741      (legacy_feature "Obsolete 'header' command -- use 'chapter', 'section' etc. instead";
   732       check_text txt state)
   742       ignore (output_text state {markdown = false} txt))
   733     else raise Toplevel.UNDEF);
   743     else raise Toplevel.UNDEF);
   734 
   744 
   735 fun document_command (loc, txt) =
   745 fun document_command markdown (loc, txt) =
   736   Toplevel.keep (fn state =>
   746   Toplevel.keep (fn state =>
   737     (case loc of
   747     (case loc of
   738       NONE => check_text txt state
   748       NONE => ignore (output_text state markdown txt)
   739     | SOME (_, pos) =>
   749     | SOME (_, pos) =>
   740         error ("Illegal target specification -- not a theory context" ^ Position.here pos))) o
   750         error ("Illegal target specification -- not a theory context" ^ Position.here pos))) o
   741   Toplevel.present_local_theory loc (check_text txt);
   751   Toplevel.present_local_theory loc (fn state => ignore (output_text state markdown txt));
   742 
   752 
   743 end;
   753 end;