src/Pure/Thy/markdown.ML
author wenzelm
Sat Oct 17 19:26:34 2015 +0200 (2015-10-17)
changeset 61459 5f2ddeb15c06
parent 61457 3e21699bb83b
child 61460 732028edfbc7
permissions -rw-r--r--
clarified nesting of paragraphs: indentation is taken into account more uniformly;
tuned;
wenzelm@61441
     1
(*  Title:      Pure/Thy/markdown.ML
wenzelm@61441
     2
    Author:     Makarius
wenzelm@61441
     3
wenzelm@61448
     4
Minimal support for Markdown documents (see also http://commonmark.org)
wenzelm@61448
     5
that consist only of paragraphs and (nested) lists:
wenzelm@61448
     6
wenzelm@61448
     7
  * list items start with marker \<^item> (itemize), \<^enum> (enumerate), \<^descr> (description)
wenzelm@61448
     8
  * adjacent list items with same indentation and same marker are grouped
wenzelm@61448
     9
    into a single list
wenzelm@61448
    10
  * singleton blank lines separate paragraphs
wenzelm@61448
    11
  * multiple blank lines escape from the current list hierarchy
wenzelm@61448
    12
wenzelm@61448
    13
Notable differences to official Markdown:
wenzelm@61448
    14
wenzelm@61448
    15
  * indentation of list items needs to match exactly
wenzelm@61448
    16
  * indentation is unlimited (Markdown interprets 4 spaces as block quote)
wenzelm@61448
    17
  * list items always consist of paragraphs -- no notion of "tight" list
wenzelm@61441
    18
*)
wenzelm@61441
    19
wenzelm@61441
    20
signature MARKDOWN =
wenzelm@61441
    21
sig
wenzelm@61457
    22
  val is_control: Symbol.symbol -> bool
wenzelm@61443
    23
  datatype kind = Itemize | Enumerate | Description
wenzelm@61449
    24
  val print_kind: kind -> string
wenzelm@61441
    25
  type line
wenzelm@61451
    26
  val line_source: line -> Antiquote.text_antiquote list
wenzelm@61443
    27
  val line_content: line -> Antiquote.text_antiquote list
wenzelm@61445
    28
  val make_line: Antiquote.text_antiquote list -> line
wenzelm@61445
    29
  val empty_line: line
wenzelm@61459
    30
  datatype block = Paragraph of line list | List of {indent: int, kind: kind, body: block list}
wenzelm@61445
    31
  val read_lines: line list -> block list
wenzelm@61457
    32
  val read_antiquotes: Antiquote.text_antiquote list -> block list
wenzelm@61457
    33
  val read_source: Input.source -> block list
wenzelm@61457
    34
  val text_reports: Antiquote.text_antiquote list -> Position.report list
wenzelm@61449
    35
  val reports: block list -> Position.report list
wenzelm@61441
    36
end;
wenzelm@61441
    37
wenzelm@61441
    38
structure Markdown: MARKDOWN =
wenzelm@61441
    39
struct
wenzelm@61441
    40
wenzelm@61445
    41
(* document lines *)
wenzelm@61441
    42
wenzelm@61457
    43
val is_control = member (op =) ["\\<^item>", "\\<^enum>", "\\<^descr>"];
wenzelm@61457
    44
wenzelm@61441
    45
datatype kind = Itemize | Enumerate | Description;
wenzelm@61449
    46
wenzelm@61449
    47
fun print_kind Itemize = "itemize"
wenzelm@61449
    48
  | print_kind Enumerate = "enumerate"
wenzelm@61449
    49
  | print_kind Description = "description";
wenzelm@61449
    50
wenzelm@61441
    51
datatype line =
wenzelm@61441
    52
  Line of
wenzelm@61451
    53
   {source: Antiquote.text_antiquote list,
wenzelm@61441
    54
    is_empty: bool,
wenzelm@61459
    55
    indent: int,
wenzelm@61459
    56
    item: kind option,
wenzelm@61459
    57
    item_pos: Position.T,
wenzelm@61459
    58
    content: Antiquote.text_antiquote list};
wenzelm@61445
    59
wenzelm@61445
    60
val eof_line =
wenzelm@61451
    61
  Line {source = [Antiquote.Text [(Symbol.eof, Position.none)]],
wenzelm@61459
    62
    is_empty = false, indent = 0, item = NONE, item_pos = Position.none, content = []};
wenzelm@61441
    63
wenzelm@61451
    64
fun line_source (Line {source, ...}) = source;
wenzelm@61459
    65
fun line_is_empty (Line {is_empty, ...}) = is_empty;
wenzelm@61459
    66
fun line_indent (Line {indent, ...}) = indent;
wenzelm@61459
    67
fun line_item (Line {item, ...}) = item;
wenzelm@61459
    68
fun line_item_pos (Line {item_pos, ...}) = item_pos;
wenzelm@61441
    69
fun line_content (Line {content, ...}) = content;
wenzelm@61441
    70
wenzelm@61443
    71
wenzelm@61443
    72
(* make line *)
wenzelm@61443
    73
wenzelm@61441
    74
local
wenzelm@61441
    75
wenzelm@61441
    76
fun bad_blank ((s, _): Symbol_Pos.T) = Symbol.is_ascii_blank s andalso s <> Symbol.space;
wenzelm@61441
    77
val bad_blanks = maps (fn Antiquote.Text ss => filter bad_blank ss | _ => []);
wenzelm@61441
    78
wenzelm@61451
    79
fun check_blanks source =
wenzelm@61451
    80
  (case bad_blanks source of
wenzelm@61441
    81
    [] => ()
wenzelm@61441
    82
  | (c, pos) :: _ =>
wenzelm@61441
    83
      error ("Bad blank character " ^ quote (ML_Syntax.print_char c) ^ Position.here pos));
wenzelm@61441
    84
wenzelm@61441
    85
fun is_space ((s, _): Symbol_Pos.T) = s = Symbol.space;
wenzelm@61441
    86
val is_empty = forall (fn Antiquote.Text ss => forall is_space ss | _ => false);
wenzelm@61441
    87
wenzelm@61444
    88
val scan_marker =
wenzelm@61449
    89
  Scan.many is_space -- Symbol_Pos.scan_pos --
wenzelm@61459
    90
  Scan.option
wenzelm@61459
    91
   (Symbol_Pos.$$ "\\<^item>" >> K Itemize ||
wenzelm@61459
    92
    Symbol_Pos.$$ "\\<^enum>" >> K Enumerate ||
wenzelm@61459
    93
    Symbol_Pos.$$ "\\<^descr>" >> K Description) --| Scan.many is_space
wenzelm@61459
    94
  >> (fn ((sp, pos), item) => (length sp, item, if is_some item then pos else Position.none));
wenzelm@61442
    95
wenzelm@61451
    96
fun read_marker (Antiquote.Text ss :: rest) =
wenzelm@61459
    97
      (case Scan.finite Symbol_Pos.stopper scan_marker ss of
wenzelm@61451
    98
        (marker, []) => (marker, rest)
wenzelm@61451
    99
      | (marker, ss') => (marker, Antiquote.Text ss' :: rest))
wenzelm@61459
   100
  | read_marker source = ((0, NONE, Position.none), source);
wenzelm@61441
   101
wenzelm@61441
   102
in
wenzelm@61441
   103
wenzelm@61451
   104
fun make_line source =
wenzelm@61441
   105
  let
wenzelm@61451
   106
    val _ = check_blanks source;
wenzelm@61459
   107
    val ((indent, item, item_pos), content) = read_marker source;
wenzelm@61459
   108
  in
wenzelm@61459
   109
    Line {source = source, is_empty = is_empty source, indent = indent,
wenzelm@61459
   110
      item = item, item_pos = item_pos, content = content}
wenzelm@61459
   111
  end;
wenzelm@61441
   112
wenzelm@61445
   113
val empty_line = make_line [];
wenzelm@61445
   114
wenzelm@61441
   115
end;
wenzelm@61441
   116
wenzelm@61441
   117
wenzelm@61445
   118
(* document blocks *)
wenzelm@61445
   119
wenzelm@61459
   120
datatype block =
wenzelm@61459
   121
  Paragraph of line list | List of {indent: int, kind: kind, body: block list};
wenzelm@61445
   122
wenzelm@61450
   123
fun block_lines (Paragraph lines) = lines
wenzelm@61459
   124
  | block_lines (List {body, ...}) = maps block_lines body;
wenzelm@61450
   125
wenzelm@61452
   126
fun block_range (Paragraph lines) = Antiquote.range (maps line_content lines)
wenzelm@61459
   127
  | block_range (List {body, ...}) = Antiquote.range (maps line_source (maps block_lines body));
wenzelm@61459
   128
wenzelm@61459
   129
fun block_indent (List {indent, ...}) = indent
wenzelm@61459
   130
  | block_indent (Paragraph (line :: _)) = line_indent line
wenzelm@61459
   131
  | block_indent _ = 0;
wenzelm@61459
   132
wenzelm@61459
   133
fun block_list indent0 kind0 (List {indent, kind, body}) =
wenzelm@61459
   134
      if indent0 = indent andalso kind0 = kind then SOME body else NONE
wenzelm@61459
   135
  | block_list _ _ _ = NONE;
wenzelm@61459
   136
wenzelm@61459
   137
val is_list = fn List _ => true | _ => false;
wenzelm@61452
   138
wenzelm@61452
   139
wenzelm@61452
   140
(* read document *)
wenzelm@61452
   141
wenzelm@61452
   142
local
wenzelm@61450
   143
wenzelm@61459
   144
fun build (indent, item, rev_body) document =
wenzelm@61459
   145
  (case (item, document) of
wenzelm@61459
   146
    (SOME kind, block :: blocks) =>
wenzelm@61459
   147
      (case block_list indent kind block of
wenzelm@61459
   148
        SOME list => List {indent = indent, kind = kind, body = fold cons rev_body list} :: blocks
wenzelm@61459
   149
      | NONE =>
wenzelm@61459
   150
          if (if is_list block then indent < block_indent block else indent <= block_indent block)
wenzelm@61459
   151
          then build (indent, item, block :: rev_body) blocks
wenzelm@61459
   152
          else List {indent = indent, kind = kind, body = rev rev_body} :: document)
wenzelm@61459
   153
  | (SOME kind, []) => [List {indent = indent, kind = kind, body = rev rev_body}]
wenzelm@61459
   154
  | (NONE, _) => fold cons rev_body document);
wenzelm@61445
   155
wenzelm@61442
   156
fun plain_line line =
wenzelm@61459
   157
  not (line_is_empty line) andalso is_none (line_item line) andalso line <> eof_line;
wenzelm@61444
   158
wenzelm@61459
   159
val parse_paragraph =
wenzelm@61459
   160
  Scan.one (fn line => line <> eof_line) -- Scan.many plain_line >> (fn (line, lines) =>
wenzelm@61459
   161
    let val block = Paragraph (line :: lines)
wenzelm@61459
   162
    in (line_indent line, line_item line, [block]) end);
wenzelm@61443
   163
wenzelm@61443
   164
val parse_document =
wenzelm@61459
   165
  parse_paragraph ::: Scan.repeat (Scan.option (Scan.one line_is_empty) |-- parse_paragraph)
wenzelm@61459
   166
    >> (fn pars => fold_rev build pars []);
wenzelm@61441
   167
wenzelm@61441
   168
in
wenzelm@61441
   169
wenzelm@61445
   170
val read_lines =
wenzelm@61445
   171
  Scan.read (Scan.stopper (K eof_line) (fn line => line = eof_line))
wenzelm@61445
   172
    (Scan.repeat (Scan.many line_is_empty |-- parse_document) --| Scan.many line_is_empty) #>
wenzelm@61445
   173
  the_default [] #> flat;
wenzelm@61441
   174
wenzelm@61457
   175
val read_antiquotes = Antiquote.split_lines #> map make_line #> read_lines;
wenzelm@61457
   176
val read_source = Antiquote.read #> read_antiquotes;
wenzelm@61441
   177
wenzelm@61457
   178
end;
wenzelm@61445
   179
wenzelm@61449
   180
wenzelm@61449
   181
(* PIDE reports *)
wenzelm@61449
   182
wenzelm@61457
   183
val text_reports =
wenzelm@61457
   184
  maps (fn Antiquote.Text ss => [(#1 (Symbol_Pos.range ss), Markup.words)] | _ => []);
wenzelm@61457
   185
wenzelm@61449
   186
local
wenzelm@61449
   187
wenzelm@61459
   188
fun line_reports depth line =
wenzelm@61459
   189
  cons (line_item_pos line, Markup.markdown_item depth) #>
wenzelm@61459
   190
  append (text_reports (line_content line));
wenzelm@61449
   191
wenzelm@61452
   192
fun block_reports depth block =
wenzelm@61452
   193
  (case block of
wenzelm@61452
   194
    Paragraph lines =>
wenzelm@61452
   195
      cons (#1 (block_range block), Markup.markdown_paragraph) #>
wenzelm@61450
   196
      fold (line_reports depth) lines
wenzelm@61459
   197
  | List {kind, body, ...} =>
wenzelm@61452
   198
      cons (#1 (block_range block), Markup.markdown_list (print_kind kind)) #>
wenzelm@61452
   199
      fold (block_reports (depth + 1)) body);
wenzelm@61449
   200
wenzelm@61449
   201
in
wenzelm@61449
   202
wenzelm@61450
   203
fun reports blocks =
wenzelm@61450
   204
  filter (Position.is_reported o #1) (fold (block_reports 0) blocks []);
wenzelm@61449
   205
wenzelm@61441
   206
end;
wenzelm@61449
   207
wenzelm@61449
   208
end;