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