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