src/Pure/Thy/markdown.ML
author wenzelm
Wed Oct 14 19:44:43 2015 +0200 (2015-10-14)
changeset 61443 78bbfadd1034
parent 61442 467ebb937294
child 61444 1fcdfc1a7e50
permissions -rw-r--r--
more document structure;
wenzelm@61441
     1
(*  Title:      Pure/Thy/markdown.ML
wenzelm@61441
     2
    Author:     Makarius
wenzelm@61441
     3
wenzelm@61441
     4
Minimal support for Markdown documents (see also http://commonmark.org).
wenzelm@61441
     5
*)
wenzelm@61441
     6
wenzelm@61441
     7
signature MARKDOWN =
wenzelm@61441
     8
sig
wenzelm@61443
     9
  datatype kind = Itemize | Enumerate | Description
wenzelm@61441
    10
  type line
wenzelm@61443
    11
  val line_content: line -> Antiquote.text_antiquote list
wenzelm@61443
    12
  datatype block = Paragraph of line list | List of kind * block list list
wenzelm@61443
    13
  val read_document: Input.source -> block list
wenzelm@61441
    14
end;
wenzelm@61441
    15
wenzelm@61441
    16
structure Markdown: MARKDOWN =
wenzelm@61441
    17
struct
wenzelm@61441
    18
wenzelm@61443
    19
(* document structure *)
wenzelm@61441
    20
wenzelm@61441
    21
datatype kind = Itemize | Enumerate | Description;
wenzelm@61441
    22
wenzelm@61441
    23
datatype line =
wenzelm@61441
    24
  Line of
wenzelm@61441
    25
   {content: Antiquote.text_antiquote list,
wenzelm@61441
    26
    is_empty: bool,
wenzelm@61441
    27
    indent: int,
wenzelm@61441
    28
    marker: (kind * Position.T) option};
wenzelm@61441
    29
wenzelm@61441
    30
fun line_content (Line {content, ...}) = content;
wenzelm@61441
    31
fun line_is_empty (Line {is_empty, ...}) = is_empty;
wenzelm@61441
    32
fun line_indent (Line {indent, ...}) = indent;
wenzelm@61441
    33
fun line_marker (Line {marker, ...}) = marker;
wenzelm@61441
    34
wenzelm@61443
    35
datatype block = Paragraph of line list | List of kind * block list list;
wenzelm@61443
    36
wenzelm@61443
    37
wenzelm@61443
    38
(* make line *)
wenzelm@61443
    39
wenzelm@61441
    40
local
wenzelm@61441
    41
wenzelm@61441
    42
fun bad_blank ((s, _): Symbol_Pos.T) = Symbol.is_ascii_blank s andalso s <> Symbol.space;
wenzelm@61441
    43
val bad_blanks = maps (fn Antiquote.Text ss => filter bad_blank ss | _ => []);
wenzelm@61441
    44
wenzelm@61441
    45
fun check_blanks content =
wenzelm@61441
    46
  (case bad_blanks content of
wenzelm@61441
    47
    [] => ()
wenzelm@61441
    48
  | (c, pos) :: _ =>
wenzelm@61441
    49
      error ("Bad blank character " ^ quote (ML_Syntax.print_char c) ^ Position.here pos));
wenzelm@61441
    50
wenzelm@61441
    51
fun is_space ((s, _): Symbol_Pos.T) = s = Symbol.space;
wenzelm@61441
    52
val is_empty = forall (fn Antiquote.Text ss => forall is_space ss | _ => false);
wenzelm@61441
    53
wenzelm@61442
    54
val scan_prefix =
wenzelm@61442
    55
  (Scan.many is_space >> length) --
wenzelm@61442
    56
  Scan.option
wenzelm@61442
    57
    ((Symbol_Pos.$$ "\<^item>" >> K Itemize ||
wenzelm@61442
    58
      Symbol_Pos.$$ "\<^enum>" >> K Enumerate ||
wenzelm@61442
    59
      Symbol_Pos.$$ "\<^descr>" >> K Description) -- Symbol_Pos.scan_pos);
wenzelm@61442
    60
wenzelm@61442
    61
fun scan_line (Antiquote.Text ss :: _) =
wenzelm@61442
    62
      the_default (0, NONE) (Scan.read Symbol_Pos.stopper scan_prefix ss)
wenzelm@61442
    63
  | scan_line _ = (0, NONE);
wenzelm@61441
    64
wenzelm@61441
    65
in
wenzelm@61441
    66
wenzelm@61441
    67
fun make_line content =
wenzelm@61441
    68
  let
wenzelm@61441
    69
    val _ = check_blanks content;
wenzelm@61442
    70
    val (indent, marker) = scan_line content;
wenzelm@61441
    71
  in Line {content = content, is_empty = is_empty content, indent = indent, marker = marker} end;
wenzelm@61441
    72
wenzelm@61441
    73
end;
wenzelm@61441
    74
wenzelm@61441
    75
wenzelm@61443
    76
(* make blocks *)
wenzelm@61443
    77
wenzelm@61443
    78
fun make_blocks spans = map Paragraph spans;
wenzelm@61443
    79
wenzelm@61443
    80
wenzelm@61443
    81
(* read document *)
wenzelm@61441
    82
wenzelm@61441
    83
local
wenzelm@61441
    84
wenzelm@61442
    85
val eof =
wenzelm@61442
    86
  Line {content = [Antiquote.Text [(Symbol.eof, Position.none)]],
wenzelm@61442
    87
    is_empty = false, indent = 0, marker = NONE};
wenzelm@61441
    88
val stopper = Scan.stopper (K eof) (fn line => line = eof);
wenzelm@61441
    89
wenzelm@61442
    90
fun plain_line line =
wenzelm@61442
    91
  not (line_is_empty line) andalso is_none (line_marker line) andalso line <> eof;
wenzelm@61441
    92
wenzelm@61441
    93
val parse_span =
wenzelm@61443
    94
  Scan.many1 plain_line || Scan.one (is_some o line_marker) -- Scan.many plain_line >> op ::;
wenzelm@61443
    95
wenzelm@61443
    96
val parse_document =
wenzelm@61443
    97
  parse_span ::: Scan.repeat (Scan.one line_is_empty |-- parse_span) >> make_blocks;
wenzelm@61443
    98
wenzelm@61443
    99
val parse_documents =
wenzelm@61443
   100
  (Scan.many line_is_empty |-- parse_document) :::
wenzelm@61443
   101
    (Scan.repeat (Scan.many1 line_is_empty |-- parse_document) --| Scan.many line_is_empty)
wenzelm@61443
   102
  >> flat;
wenzelm@61441
   103
wenzelm@61441
   104
in
wenzelm@61441
   105
wenzelm@61443
   106
val read_document =
wenzelm@61443
   107
  Antiquote.read #> Antiquote.split_lines #> map make_line #>
wenzelm@61443
   108
  Scan.read stopper parse_documents #> the_default [];
wenzelm@61441
   109
wenzelm@61441
   110
end;
wenzelm@61441
   111
wenzelm@61441
   112
end;