ProgTutorial/output_tutorial.ML
author Christian Urban <urbanc@in.tum.de>
Thu, 04 Jun 2009 09:28:29 +0200
changeset 259 a0af7fe3f558
parent 258 03145998190b
child 261 358f325f4db6
permissions -rw-r--r--
minor tuning
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
255
ef1da1abee46 added infrastructure for index; antiquotations have now the options [index] and [indexc]
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
     1
256
1fb8d62c88a0 added some first index-information
Christian Urban <urbanc@in.tum.de>
parents: 255
diff changeset
     2
structure OutputTutorial =
255
ef1da1abee46 added infrastructure for index; antiquotations have now the options [index] and [indexc]
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
     3
struct
ef1da1abee46 added infrastructure for index; antiquotations have now the options [index] and [indexc]
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
     4
ef1da1abee46 added infrastructure for index; antiquotations have now the options [index] and [indexc]
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
     5
(* rebuilding the output function from ThyOutput in order to *)
ef1da1abee46 added infrastructure for index; antiquotations have now the options [index] and [indexc]
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
     6
(* enable the options [gray, linenos, index]                   *)
ef1da1abee46 added infrastructure for index; antiquotations have now the options [index] and [indexc]
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
     7
ef1da1abee46 added infrastructure for index; antiquotations have now the options [index] and [indexc]
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
     8
val gray = ref false
ef1da1abee46 added infrastructure for index; antiquotations have now the options [index] and [indexc]
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
     9
val linenos = ref false
ef1da1abee46 added infrastructure for index; antiquotations have now the options [index] and [indexc]
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    10
ef1da1abee46 added infrastructure for index; antiquotations have now the options [index] and [indexc]
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    11
fun output txt =
ef1da1abee46 added infrastructure for index; antiquotations have now the options [index] and [indexc]
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    12
let
ef1da1abee46 added infrastructure for index; antiquotations have now the options [index] and [indexc]
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    13
  val prts = map Pretty.str txt
ef1da1abee46 added infrastructure for index; antiquotations have now the options [index] and [indexc]
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    14
in   
ef1da1abee46 added infrastructure for index; antiquotations have now the options [index] and [indexc]
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    15
  prts
ef1da1abee46 added infrastructure for index; antiquotations have now the options [index] and [indexc]
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    16
  |> (if ! ThyOutput.quotes then map Pretty.quote else I)
ef1da1abee46 added infrastructure for index; antiquotations have now the options [index] and [indexc]
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    17
  |> (if ! ThyOutput.display then
ef1da1abee46 added infrastructure for index; antiquotations have now the options [index] and [indexc]
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    18
    map (Output.output o Pretty.string_of o Pretty.indent (! ThyOutput.indent))
ef1da1abee46 added infrastructure for index; antiquotations have now the options [index] and [indexc]
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    19
    #> space_implode "\\isasep\\isanewline%\n"
ef1da1abee46 added infrastructure for index; antiquotations have now the options [index] and [indexc]
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    20
    #> (if ! linenos then (enclose "\\begin{linenos}%\n" "%\n\\end{linenos}") else I) 
ef1da1abee46 added infrastructure for index; antiquotations have now the options [index] and [indexc]
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    21
    #> (if ! gray then (enclose "\\begin{graybox}%\n" "%\n\\end{graybox}") else I) 
ef1da1abee46 added infrastructure for index; antiquotations have now the options [index] and [indexc]
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    22
    #> enclose "\\begin{isabelle}%\n" "%\n\\end{isabelle}"
ef1da1abee46 added infrastructure for index; antiquotations have now the options [index] and [indexc]
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    23
  else
ef1da1abee46 added infrastructure for index; antiquotations have now the options [index] and [indexc]
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    24
    map (Output.output o (if ! ThyOutput.break then Pretty.string_of else Pretty.str_of))
ef1da1abee46 added infrastructure for index; antiquotations have now the options [index] and [indexc]
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    25
    #> space_implode "\\isasep\\isanewline%\n"
256
1fb8d62c88a0 added some first index-information
Christian Urban <urbanc@in.tum.de>
parents: 255
diff changeset
    26
    #> enclose "\\isa{" "}")
255
ef1da1abee46 added infrastructure for index; antiquotations have now the options [index] and [indexc]
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    27
end
ef1da1abee46 added infrastructure for index; antiquotations have now the options [index] and [indexc]
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    28
256
1fb8d62c88a0 added some first index-information
Christian Urban <urbanc@in.tum.de>
parents: 255
diff changeset
    29
datatype qstring =
258
03145998190b slightly modified index generation
Christian Urban <urbanc@in.tum.de>
parents: 256
diff changeset
    30
  NoString
03145998190b slightly modified index generation
Christian Urban <urbanc@in.tum.de>
parents: 256
diff changeset
    31
| Plain  of string
03145998190b slightly modified index generation
Christian Urban <urbanc@in.tum.de>
parents: 256
diff changeset
    32
| Code   of string
03145998190b slightly modified index generation
Christian Urban <urbanc@in.tum.de>
parents: 256
diff changeset
    33
| IStruc of string
256
1fb8d62c88a0 added some first index-information
Christian Urban <urbanc@in.tum.de>
parents: 255
diff changeset
    34
1fb8d62c88a0 added some first index-information
Christian Urban <urbanc@in.tum.de>
parents: 255
diff changeset
    35
datatype entry = 
1fb8d62c88a0 added some first index-information
Christian Urban <urbanc@in.tum.de>
parents: 255
diff changeset
    36
   No_Entry
1fb8d62c88a0 added some first index-information
Christian Urban <urbanc@in.tum.de>
parents: 255
diff changeset
    37
 | Default 
1fb8d62c88a0 added some first index-information
Christian Urban <urbanc@in.tum.de>
parents: 255
diff changeset
    38
 | Explicit of string
1fb8d62c88a0 added some first index-information
Christian Urban <urbanc@in.tum.de>
parents: 255
diff changeset
    39
1fb8d62c88a0 added some first index-information
Christian Urban <urbanc@in.tum.de>
parents: 255
diff changeset
    40
val index = ref No_Entry
1fb8d62c88a0 added some first index-information
Christian Urban <urbanc@in.tum.de>
parents: 255
diff changeset
    41
1fb8d62c88a0 added some first index-information
Christian Urban <urbanc@in.tum.de>
parents: 255
diff changeset
    42
fun translate f = Symbol.explode #> map f #> implode;
1fb8d62c88a0 added some first index-information
Christian Urban <urbanc@in.tum.de>
parents: 255
diff changeset
    43
1fb8d62c88a0 added some first index-information
Christian Urban <urbanc@in.tum.de>
parents: 255
diff changeset
    44
val clean_string = translate
1fb8d62c88a0 added some first index-information
Christian Urban <urbanc@in.tum.de>
parents: 255
diff changeset
    45
  (fn "_" => "\\_"
1fb8d62c88a0 added some first index-information
Christian Urban <urbanc@in.tum.de>
parents: 255
diff changeset
    46
    | "#" => "\\#"
1fb8d62c88a0 added some first index-information
Christian Urban <urbanc@in.tum.de>
parents: 255
diff changeset
    47
    | "<" => "\\isacharless"
1fb8d62c88a0 added some first index-information
Christian Urban <urbanc@in.tum.de>
parents: 255
diff changeset
    48
    | ">" => "\\isachargreater"
1fb8d62c88a0 added some first index-information
Christian Urban <urbanc@in.tum.de>
parents: 255
diff changeset
    49
    | "{" => "\\{"
1fb8d62c88a0 added some first index-information
Christian Urban <urbanc@in.tum.de>
parents: 255
diff changeset
    50
    | "|" => "\\isacharbar"
1fb8d62c88a0 added some first index-information
Christian Urban <urbanc@in.tum.de>
parents: 255
diff changeset
    51
    | "}" => "\\}"
1fb8d62c88a0 added some first index-information
Christian Urban <urbanc@in.tum.de>
parents: 255
diff changeset
    52
    | "$" => "\\isachardollar"
1fb8d62c88a0 added some first index-information
Christian Urban <urbanc@in.tum.de>
parents: 255
diff changeset
    53
    | "!" => "\\isacharbang"
1fb8d62c88a0 added some first index-information
Christian Urban <urbanc@in.tum.de>
parents: 255
diff changeset
    54
    | "\\<dash>" => "-"
1fb8d62c88a0 added some first index-information
Christian Urban <urbanc@in.tum.de>
parents: 255
diff changeset
    55
    | c => c);
1fb8d62c88a0 added some first index-information
Christian Urban <urbanc@in.tum.de>
parents: 255
diff changeset
    56
1fb8d62c88a0 added some first index-information
Christian Urban <urbanc@in.tum.de>
parents: 255
diff changeset
    57
fun get_word str =
1fb8d62c88a0 added some first index-information
Christian Urban <urbanc@in.tum.de>
parents: 255
diff changeset
    58
let 
1fb8d62c88a0 added some first index-information
Christian Urban <urbanc@in.tum.de>
parents: 255
diff changeset
    59
  fun only_letters [] = true
1fb8d62c88a0 added some first index-information
Christian Urban <urbanc@in.tum.de>
parents: 255
diff changeset
    60
    | only_letters (x::xs) = 
1fb8d62c88a0 added some first index-information
Christian Urban <urbanc@in.tum.de>
parents: 255
diff changeset
    61
        if (Symbol.is_ascii_blank x) then false else only_letters xs
1fb8d62c88a0 added some first index-information
Christian Urban <urbanc@in.tum.de>
parents: 255
diff changeset
    62
in
1fb8d62c88a0 added some first index-information
Christian Urban <urbanc@in.tum.de>
parents: 255
diff changeset
    63
  if (only_letters (Symbol.explode str)) then (clean_string str)
1fb8d62c88a0 added some first index-information
Christian Urban <urbanc@in.tum.de>
parents: 255
diff changeset
    64
  else error ("Underspecified index entry only for single words allowed! Error with " ^ quote str)
1fb8d62c88a0 added some first index-information
Christian Urban <urbanc@in.tum.de>
parents: 255
diff changeset
    65
end  
1fb8d62c88a0 added some first index-information
Christian Urban <urbanc@in.tum.de>
parents: 255
diff changeset
    66
258
03145998190b slightly modified index generation
Christian Urban <urbanc@in.tum.de>
parents: 256
diff changeset
    67
fun get_qstring NoString = ""
03145998190b slightly modified index generation
Christian Urban <urbanc@in.tum.de>
parents: 256
diff changeset
    68
  | get_qstring (Plain s) = get_word s
256
1fb8d62c88a0 added some first index-information
Christian Urban <urbanc@in.tum.de>
parents: 255
diff changeset
    69
  | get_qstring (Code s) = let val w = get_word s in  (w ^ "@{\\tt\\slshape{}" ^ w ^ "}") end
258
03145998190b slightly modified index generation
Christian Urban <urbanc@in.tum.de>
parents: 256
diff changeset
    70
  | get_qstring (IStruc s) =  "in {\\tt\\slshape{}" ^ (get_word s) ^ "}"
256
1fb8d62c88a0 added some first index-information
Christian Urban <urbanc@in.tum.de>
parents: 255
diff changeset
    71
1fb8d62c88a0 added some first index-information
Christian Urban <urbanc@in.tum.de>
parents: 255
diff changeset
    72
fun is_empty_qstr (Plain "") = true
1fb8d62c88a0 added some first index-information
Christian Urban <urbanc@in.tum.de>
parents: 255
diff changeset
    73
  | is_empty_qstr (Code "") = true
1fb8d62c88a0 added some first index-information
Christian Urban <urbanc@in.tum.de>
parents: 255
diff changeset
    74
  | is_empty_qstr _ = false
1fb8d62c88a0 added some first index-information
Christian Urban <urbanc@in.tum.de>
parents: 255
diff changeset
    75
1fb8d62c88a0 added some first index-information
Christian Urban <urbanc@in.tum.de>
parents: 255
diff changeset
    76
fun get_index_info {main = m, minor = n} = 
258
03145998190b slightly modified index generation
Christian Urban <urbanc@in.tum.de>
parents: 256
diff changeset
    77
 (if n = NoString
256
1fb8d62c88a0 added some first index-information
Christian Urban <urbanc@in.tum.de>
parents: 255
diff changeset
    78
  then "\\index{" ^ (get_qstring m) ^ "}"  
258
03145998190b slightly modified index generation
Christian Urban <urbanc@in.tum.de>
parents: 256
diff changeset
    79
  else "\\indexdef{" ^ (get_qstring m) ^ "}{" ^ (get_qstring n) ^ "}")  
256
1fb8d62c88a0 added some first index-information
Christian Urban <urbanc@in.tum.de>
parents: 255
diff changeset
    80
    
1fb8d62c88a0 added some first index-information
Christian Urban <urbanc@in.tum.de>
parents: 255
diff changeset
    81
fun index_entry entry index_info  =
1fb8d62c88a0 added some first index-information
Christian Urban <urbanc@in.tum.de>
parents: 255
diff changeset
    82
 case entry of
1fb8d62c88a0 added some first index-information
Christian Urban <urbanc@in.tum.de>
parents: 255
diff changeset
    83
   No_Entry => I
1fb8d62c88a0 added some first index-information
Christian Urban <urbanc@in.tum.de>
parents: 255
diff changeset
    84
 | Explicit s => prefix s
1fb8d62c88a0 added some first index-information
Christian Urban <urbanc@in.tum.de>
parents: 255
diff changeset
    85
 | Default => prefix (get_index_info index_info)
1fb8d62c88a0 added some first index-information
Christian Urban <urbanc@in.tum.de>
parents: 255
diff changeset
    86
1fb8d62c88a0 added some first index-information
Christian Urban <urbanc@in.tum.de>
parents: 255
diff changeset
    87
fun output_indexed txt index_info =
1fb8d62c88a0 added some first index-information
Christian Urban <urbanc@in.tum.de>
parents: 255
diff changeset
    88
 txt |> output
1fb8d62c88a0 added some first index-information
Christian Urban <urbanc@in.tum.de>
parents: 255
diff changeset
    89
     |> index_entry (!index) index_info
255
ef1da1abee46 added infrastructure for index; antiquotations have now the options [index] and [indexc]
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    90
ef1da1abee46 added infrastructure for index; antiquotations have now the options [index] and [indexc]
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    91
fun boolean "" = true
ef1da1abee46 added infrastructure for index; antiquotations have now the options [index] and [indexc]
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    92
  | boolean "true" = true
ef1da1abee46 added infrastructure for index; antiquotations have now the options [index] and [indexc]
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    93
  | boolean "false" = false
ef1da1abee46 added infrastructure for index; antiquotations have now the options [index] and [indexc]
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    94
  | boolean s = error ("Bad boolean value: " ^ quote s);
ef1da1abee46 added infrastructure for index; antiquotations have now the options [index] and [indexc]
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    95
256
1fb8d62c88a0 added some first index-information
Christian Urban <urbanc@in.tum.de>
parents: 255
diff changeset
    96
fun explicit "" = Default
1fb8d62c88a0 added some first index-information
Christian Urban <urbanc@in.tum.de>
parents: 255
diff changeset
    97
  | explicit s  = Explicit s
1fb8d62c88a0 added some first index-information
Christian Urban <urbanc@in.tum.de>
parents: 255
diff changeset
    98
255
ef1da1abee46 added infrastructure for index; antiquotations have now the options [index] and [indexc]
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    99
val _ = ThyOutput.add_options
ef1da1abee46 added infrastructure for index; antiquotations have now the options [index] and [indexc]
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   100
 [("gray", Library.setmp gray o boolean),
ef1da1abee46 added infrastructure for index; antiquotations have now the options [index] and [indexc]
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   101
  ("linenos", Library.setmp linenos o boolean),
256
1fb8d62c88a0 added some first index-information
Christian Urban <urbanc@in.tum.de>
parents: 255
diff changeset
   102
  ("index", Library.setmp index o explicit)]
255
ef1da1abee46 added infrastructure for index; antiquotations have now the options [index] and [indexc]
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   103
ef1da1abee46 added infrastructure for index; antiquotations have now the options [index] and [indexc]
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   104
ef1da1abee46 added infrastructure for index; antiquotations have now the options [index] and [indexc]
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   105
end