src/Pure/Thy/markdown.ML
author wenzelm
Sat Oct 17 19:47:34 2015 +0200 (2015-10-17)
changeset 61461 77c9643a6353
parent 61460 732028edfbc7
child 61595 3591274c607e
permissions -rw-r--r--
more explicit output of list items;
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@61461
    27
  val line_is_item: line -> bool
wenzelm@61443
    28
  val line_content: line -> Antiquote.text_antiquote list
wenzelm@61445
    29
  val make_line: Antiquote.text_antiquote list -> line
wenzelm@61445
    30
  val empty_line: line
wenzelm@61459
    31
  datatype block = Paragraph of line list | List of {indent: int, kind: kind, body: block list}
wenzelm@61445
    32
  val read_lines: line list -> block list
wenzelm@61457
    33
  val read_antiquotes: Antiquote.text_antiquote list -> block list
wenzelm@61457
    34
  val read_source: Input.source -> block list
wenzelm@61457
    35
  val text_reports: Antiquote.text_antiquote list -> Position.report list
wenzelm@61449
    36
  val reports: block list -> Position.report list
wenzelm@61441
    37
end;
wenzelm@61441
    38
wenzelm@61441
    39
structure Markdown: MARKDOWN =
wenzelm@61441
    40
struct
wenzelm@61441
    41
wenzelm@61445
    42
(* document lines *)
wenzelm@61441
    43
wenzelm@61457
    44
val is_control = member (op =) ["\\<^item>", "\\<^enum>", "\\<^descr>"];
wenzelm@61457
    45
wenzelm@61441
    46
datatype kind = Itemize | Enumerate | Description;
wenzelm@61449
    47
wenzelm@61449
    48
fun print_kind Itemize = "itemize"
wenzelm@61449
    49
  | print_kind Enumerate = "enumerate"
wenzelm@61449
    50
  | print_kind Description = "description";
wenzelm@61449
    51
wenzelm@61441
    52
datatype line =
wenzelm@61441
    53
  Line of
wenzelm@61451
    54
   {source: Antiquote.text_antiquote list,
wenzelm@61441
    55
    is_empty: bool,
wenzelm@61459
    56
    indent: int,
wenzelm@61459
    57
    item: kind option,
wenzelm@61459
    58
    item_pos: Position.T,
wenzelm@61459
    59
    content: Antiquote.text_antiquote list};
wenzelm@61445
    60
wenzelm@61445
    61
val eof_line =
wenzelm@61451
    62
  Line {source = [Antiquote.Text [(Symbol.eof, Position.none)]],
wenzelm@61459
    63
    is_empty = false, indent = 0, item = NONE, item_pos = Position.none, content = []};
wenzelm@61441
    64
wenzelm@61451
    65
fun line_source (Line {source, ...}) = source;
wenzelm@61459
    66
fun line_is_empty (Line {is_empty, ...}) = is_empty;
wenzelm@61461
    67
fun line_is_item (Line {item, ...}) = is_some item;
wenzelm@61441
    68
fun line_content (Line {content, ...}) = content;
wenzelm@61441
    69
wenzelm@61443
    70
wenzelm@61443
    71
(* make line *)
wenzelm@61443
    72
wenzelm@61441
    73
local
wenzelm@61441
    74
wenzelm@61441
    75
fun bad_blank ((s, _): Symbol_Pos.T) = Symbol.is_ascii_blank s andalso s <> Symbol.space;
wenzelm@61441
    76
val bad_blanks = maps (fn Antiquote.Text ss => filter bad_blank ss | _ => []);
wenzelm@61441
    77
wenzelm@61451
    78
fun check_blanks source =
wenzelm@61451
    79
  (case bad_blanks source of
wenzelm@61441
    80
    [] => ()
wenzelm@61441
    81
  | (c, pos) :: _ =>
wenzelm@61441
    82
      error ("Bad blank character " ^ quote (ML_Syntax.print_char c) ^ Position.here pos));
wenzelm@61441
    83
wenzelm@61441
    84
fun is_space ((s, _): Symbol_Pos.T) = s = Symbol.space;
wenzelm@61441
    85
val is_empty = forall (fn Antiquote.Text ss => forall is_space ss | _ => false);
wenzelm@61441
    86
wenzelm@61444
    87
val scan_marker =
wenzelm@61449
    88
  Scan.many is_space -- Symbol_Pos.scan_pos --
wenzelm@61459
    89
  Scan.option
wenzelm@61459
    90
   (Symbol_Pos.$$ "\\<^item>" >> K Itemize ||
wenzelm@61459
    91
    Symbol_Pos.$$ "\\<^enum>" >> K Enumerate ||
wenzelm@61459
    92
    Symbol_Pos.$$ "\\<^descr>" >> K Description) --| Scan.many is_space
wenzelm@61459
    93
  >> (fn ((sp, pos), item) => (length sp, item, if is_some item then pos else Position.none));
wenzelm@61442
    94
wenzelm@61451
    95
fun read_marker (Antiquote.Text ss :: rest) =
wenzelm@61459
    96
      (case Scan.finite Symbol_Pos.stopper scan_marker ss of
wenzelm@61451
    97
        (marker, []) => (marker, rest)
wenzelm@61451
    98
      | (marker, ss') => (marker, Antiquote.Text ss' :: rest))
wenzelm@61459
    99
  | read_marker source = ((0, NONE, Position.none), source);
wenzelm@61441
   100
wenzelm@61441
   101
in
wenzelm@61441
   102
wenzelm@61451
   103
fun make_line source =
wenzelm@61441
   104
  let
wenzelm@61451
   105
    val _ = check_blanks source;
wenzelm@61459
   106
    val ((indent, item, item_pos), content) = read_marker source;
wenzelm@61459
   107
  in
wenzelm@61459
   108
    Line {source = source, is_empty = is_empty source, indent = indent,
wenzelm@61459
   109
      item = item, item_pos = item_pos, content = content}
wenzelm@61459
   110
  end;
wenzelm@61441
   111
wenzelm@61445
   112
val empty_line = make_line [];
wenzelm@61445
   113
wenzelm@61441
   114
end;
wenzelm@61441
   115
wenzelm@61441
   116
wenzelm@61445
   117
(* document blocks *)
wenzelm@61445
   118
wenzelm@61459
   119
datatype block =
wenzelm@61459
   120
  Paragraph of line list | List of {indent: int, kind: kind, body: block list};
wenzelm@61445
   121
wenzelm@61450
   122
fun block_lines (Paragraph lines) = lines
wenzelm@61459
   123
  | block_lines (List {body, ...}) = maps block_lines body;
wenzelm@61450
   124
wenzelm@61452
   125
fun block_range (Paragraph lines) = Antiquote.range (maps line_content lines)
wenzelm@61459
   126
  | block_range (List {body, ...}) = Antiquote.range (maps line_source (maps block_lines body));
wenzelm@61459
   127
wenzelm@61459
   128
fun block_indent (List {indent, ...}) = indent
wenzelm@61460
   129
  | block_indent (Paragraph (Line {indent, ...} :: _)) = indent
wenzelm@61459
   130
  | block_indent _ = 0;
wenzelm@61459
   131
wenzelm@61459
   132
fun block_list indent0 kind0 (List {indent, kind, body}) =
wenzelm@61459
   133
      if indent0 = indent andalso kind0 = kind then SOME body else NONE
wenzelm@61459
   134
  | block_list _ _ _ = NONE;
wenzelm@61459
   135
wenzelm@61459
   136
val is_list = fn List _ => true | _ => false;
wenzelm@61452
   137
wenzelm@61452
   138
wenzelm@61452
   139
(* read document *)
wenzelm@61452
   140
wenzelm@61452
   141
local
wenzelm@61450
   142
wenzelm@61459
   143
fun build (indent, item, rev_body) document =
wenzelm@61459
   144
  (case (item, document) of
wenzelm@61459
   145
    (SOME kind, block :: blocks) =>
wenzelm@61459
   146
      (case block_list indent kind block of
wenzelm@61459
   147
        SOME list => List {indent = indent, kind = kind, body = fold cons rev_body list} :: blocks
wenzelm@61459
   148
      | NONE =>
wenzelm@61459
   149
          if (if is_list block then indent < block_indent block else indent <= block_indent block)
wenzelm@61459
   150
          then build (indent, item, block :: rev_body) blocks
wenzelm@61459
   151
          else List {indent = indent, kind = kind, body = rev rev_body} :: document)
wenzelm@61459
   152
  | (SOME kind, []) => [List {indent = indent, kind = kind, body = rev rev_body}]
wenzelm@61459
   153
  | (NONE, _) => fold cons rev_body document);
wenzelm@61445
   154
wenzelm@61460
   155
fun plain_line (line as Line {is_empty, item, ...}) =
wenzelm@61460
   156
  not is_empty andalso is_none item andalso line <> eof_line;
wenzelm@61444
   157
wenzelm@61459
   158
val parse_paragraph =
wenzelm@61459
   159
  Scan.one (fn line => line <> eof_line) -- Scan.many plain_line >> (fn (line, lines) =>
wenzelm@61460
   160
    let
wenzelm@61460
   161
      val Line {indent, item, ...} = line;
wenzelm@61460
   162
      val block = Paragraph (line :: lines);
wenzelm@61460
   163
    in (indent, item, [block]) end);
wenzelm@61443
   164
wenzelm@61443
   165
val parse_document =
wenzelm@61459
   166
  parse_paragraph ::: Scan.repeat (Scan.option (Scan.one line_is_empty) |-- parse_paragraph)
wenzelm@61459
   167
    >> (fn pars => fold_rev build pars []);
wenzelm@61441
   168
wenzelm@61441
   169
in
wenzelm@61441
   170
wenzelm@61445
   171
val read_lines =
wenzelm@61445
   172
  Scan.read (Scan.stopper (K eof_line) (fn line => line = eof_line))
wenzelm@61445
   173
    (Scan.repeat (Scan.many line_is_empty |-- parse_document) --| Scan.many line_is_empty) #>
wenzelm@61445
   174
  the_default [] #> flat;
wenzelm@61441
   175
wenzelm@61457
   176
val read_antiquotes = Antiquote.split_lines #> map make_line #> read_lines;
wenzelm@61457
   177
val read_source = Antiquote.read #> read_antiquotes;
wenzelm@61441
   178
wenzelm@61457
   179
end;
wenzelm@61445
   180
wenzelm@61449
   181
wenzelm@61449
   182
(* PIDE reports *)
wenzelm@61449
   183
wenzelm@61457
   184
val text_reports =
wenzelm@61457
   185
  maps (fn Antiquote.Text ss => [(#1 (Symbol_Pos.range ss), Markup.words)] | _ => []);
wenzelm@61457
   186
wenzelm@61449
   187
local
wenzelm@61449
   188
wenzelm@61460
   189
fun line_reports depth (Line {item_pos, content, ...}) =
wenzelm@61460
   190
  cons (item_pos, Markup.markdown_item depth) #> append (text_reports content);
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;