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