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