src/Pure/Thy/markdown.ML
author wenzelm
Thu Oct 15 13:48:47 2015 +0200 (2015-10-15)
changeset 61448 25e40e78f6d4
parent 61446 9b09acfb7e06
child 61449 4f31f79cf2d1
permissions -rw-r--r--
more comments;
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@61445
    23
  type marker = {indent: int, kind: kind}
wenzelm@61441
    24
  type line
wenzelm@61443
    25
  val line_content: line -> Antiquote.text_antiquote list
wenzelm@61445
    26
  val make_line: Antiquote.text_antiquote list -> line
wenzelm@61445
    27
  val empty_line: line
wenzelm@61445
    28
  datatype block = Paragraph of line list | List of marker * block list
wenzelm@61445
    29
  val read_lines: line list -> block list
wenzelm@61445
    30
  val read: Input.source -> block list
wenzelm@61441
    31
end;
wenzelm@61441
    32
wenzelm@61441
    33
structure Markdown: MARKDOWN =
wenzelm@61441
    34
struct
wenzelm@61441
    35
wenzelm@61445
    36
(* document lines *)
wenzelm@61441
    37
wenzelm@61441
    38
datatype kind = Itemize | Enumerate | Description;
wenzelm@61445
    39
type marker = {indent: int, kind: kind};
wenzelm@61441
    40
wenzelm@61441
    41
datatype line =
wenzelm@61441
    42
  Line of
wenzelm@61441
    43
   {content: Antiquote.text_antiquote list,
wenzelm@61441
    44
    is_empty: bool,
wenzelm@61445
    45
    marker: (marker * Position.T) option};
wenzelm@61445
    46
wenzelm@61445
    47
val eof_line =
wenzelm@61445
    48
  Line {content = [Antiquote.Text [(Symbol.eof, Position.none)]],
wenzelm@61445
    49
    is_empty = false, marker = NONE};
wenzelm@61441
    50
wenzelm@61441
    51
fun line_content (Line {content, ...}) = content;
wenzelm@61441
    52
fun line_is_empty (Line {is_empty, ...}) = is_empty;
wenzelm@61441
    53
fun line_marker (Line {marker, ...}) = marker;
wenzelm@61441
    54
wenzelm@61443
    55
wenzelm@61443
    56
(* make line *)
wenzelm@61443
    57
wenzelm@61441
    58
local
wenzelm@61441
    59
wenzelm@61441
    60
fun bad_blank ((s, _): Symbol_Pos.T) = Symbol.is_ascii_blank s andalso s <> Symbol.space;
wenzelm@61441
    61
val bad_blanks = maps (fn Antiquote.Text ss => filter bad_blank ss | _ => []);
wenzelm@61441
    62
wenzelm@61441
    63
fun check_blanks content =
wenzelm@61441
    64
  (case bad_blanks content of
wenzelm@61441
    65
    [] => ()
wenzelm@61441
    66
  | (c, pos) :: _ =>
wenzelm@61441
    67
      error ("Bad blank character " ^ quote (ML_Syntax.print_char c) ^ Position.here pos));
wenzelm@61441
    68
wenzelm@61441
    69
fun is_space ((s, _): Symbol_Pos.T) = s = Symbol.space;
wenzelm@61441
    70
val is_empty = forall (fn Antiquote.Text ss => forall is_space ss | _ => false);
wenzelm@61441
    71
wenzelm@61444
    72
val scan_marker =
wenzelm@61444
    73
  Scan.many is_space --
wenzelm@61445
    74
  (Symbol_Pos.$$ "\<^item>" >> K Itemize ||
wenzelm@61445
    75
   Symbol_Pos.$$ "\<^enum>" >> K Enumerate ||
wenzelm@61445
    76
   Symbol_Pos.$$ "\<^descr>" >> K Description) >> (fn (a, b) => {indent = length a, kind = b});
wenzelm@61442
    77
wenzelm@61444
    78
fun read_marker (Antiquote.Text ss :: _) =
wenzelm@61445
    79
      #1 (Scan.finite Symbol_Pos.stopper (Scan.option (scan_marker -- Symbol_Pos.scan_pos)) ss)
wenzelm@61444
    80
  | read_marker _ = NONE;
wenzelm@61441
    81
wenzelm@61441
    82
in
wenzelm@61441
    83
wenzelm@61441
    84
fun make_line content =
wenzelm@61441
    85
  let
wenzelm@61441
    86
    val _ = check_blanks content;
wenzelm@61444
    87
    val marker = read_marker content;
wenzelm@61444
    88
  in Line {content = content, is_empty = is_empty content, marker = marker} end;
wenzelm@61441
    89
wenzelm@61445
    90
val empty_line = make_line [];
wenzelm@61445
    91
wenzelm@61441
    92
end;
wenzelm@61441
    93
wenzelm@61441
    94
wenzelm@61445
    95
(* document blocks *)
wenzelm@61445
    96
wenzelm@61445
    97
datatype block = Paragraph of line list | List of marker * block list;
wenzelm@61445
    98
wenzelm@61445
    99
fun add_span (opt_marker, body) document =
wenzelm@61445
   100
  (case (opt_marker, document) of
wenzelm@61446
   101
    (SOME marker, (list as List (list_marker, list_body)) :: rest) =>
wenzelm@61446
   102
      if marker = list_marker then
wenzelm@61446
   103
        List (list_marker, body @ list_body) :: rest
wenzelm@61446
   104
      else if #indent marker < #indent list_marker then
wenzelm@61446
   105
        List (marker, body @ [list]) :: rest
wenzelm@61446
   106
      else
wenzelm@61446
   107
        List (marker, body) :: document
wenzelm@61445
   108
  | (SOME marker, _) => List (marker, body) :: document
wenzelm@61445
   109
  | (NONE, _) => body @ document);
wenzelm@61445
   110
wenzelm@61445
   111
wenzelm@61443
   112
(* read document *)
wenzelm@61441
   113
wenzelm@61441
   114
local
wenzelm@61441
   115
wenzelm@61442
   116
fun plain_line line =
wenzelm@61445
   117
  not (line_is_empty line) andalso is_none (line_marker line) andalso line <> eof_line;
wenzelm@61441
   118
wenzelm@61444
   119
val parse_paragraph = Scan.many1 plain_line >> Paragraph;
wenzelm@61444
   120
wenzelm@61441
   121
val parse_span =
wenzelm@61444
   122
  parse_paragraph >> (fn par => (NONE, [par])) ||
wenzelm@61444
   123
  Scan.one (is_some o line_marker) -- Scan.many plain_line --
wenzelm@61444
   124
    Scan.repeat (Scan.one line_is_empty |-- parse_paragraph) >>
wenzelm@61445
   125
      (fn ((line, lines), pars) =>
wenzelm@61445
   126
        (Option.map #1 (line_marker line), Paragraph (line :: lines) :: pars));
wenzelm@61443
   127
wenzelm@61443
   128
val parse_document =
wenzelm@61445
   129
  parse_span ::: Scan.repeat (Scan.option (Scan.one line_is_empty) |-- parse_span)
wenzelm@61445
   130
    >> (fn spans => fold_rev add_span spans []);
wenzelm@61441
   131
wenzelm@61441
   132
in
wenzelm@61441
   133
wenzelm@61445
   134
val read_lines =
wenzelm@61445
   135
  Scan.read (Scan.stopper (K eof_line) (fn line => line = eof_line))
wenzelm@61445
   136
    (Scan.repeat (Scan.many line_is_empty |-- parse_document) --| Scan.many line_is_empty) #>
wenzelm@61445
   137
  the_default [] #> flat;
wenzelm@61441
   138
wenzelm@61441
   139
end;
wenzelm@61441
   140
wenzelm@61445
   141
val read = Antiquote.read #> Antiquote.split_lines #> map make_line #> read_lines;
wenzelm@61445
   142
wenzelm@61441
   143
end;