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