aboutsummaryrefslogtreecommitdiff
path: root/stdlib/source/library/lux/data/format/markdown.lux
blob: 5fe9a9a9dbb7392cdba1fa55cd835450cd64049c (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
(.module:
  [library
   [lux (#- and)
    [data
     ["." text
      ["%" format (#+ format)]]
     [collection
      ["." list ("#\." functor)]]]
    [type
     abstract]
    [world
     [net (#+ URL)]]]])

## https://www.markdownguide.org/basic-syntax/

(def: safe
  (-> Text Text)
  (|>> (text.replace_all "\" "\\")
       (text.replace_all "`" "\`")
       (text.replace_all "*" "\*")
       (text.replace_all "_" "\_")
       (text.replace_all "{" "\{")
       (text.replace_all "}" "\}")
       (text.replace_all "[" "\[")
       (text.replace_all "]" "\]")
       (text.replace_all "(" "\(")
       (text.replace_all ")" "\)")
       (text.replace_all "#" "\#")
       (text.replace_all "+" "\+")
       (text.replace_all "-" "\-")
       (text.replace_all "." "\.")
       (text.replace_all "!" "\!")))

(abstract: .public Span {} Any)
(abstract: .public Block {} Any)

(abstract: .public (Markdown brand)
  {}
  
  Text

  (def: .public empty
    Markdown
    (:abstraction ""))

  (def: .public text
    (-> Text (Markdown Span))
    (|>> ..safe :abstraction))

  (def: blank_line
    (format text.new_line text.new_line))

  (template [<name> <prefix>]
    [(def: .public (<name> content)
       (-> Text Markdown)
       (:abstraction (format <prefix> " " (..safe content) ..blank_line)))]

    [heading/1 "#"]
    [heading/2 "##"]
    [heading/3 "###"]
    [heading/4 "####"]
    [heading/5 "#####"]
    [heading/6 "######"]
    )

  (def: (block content)
    (-> Text (Markdown Block))
    (:abstraction (format content ..blank_line)))

  (def: .public paragraph
    (-> (Markdown Span) (Markdown Block))
    (|>> :representation ..block))

  (def: .public break
    (Markdown Span)
    (:abstraction (format "  " text.new_line)))

  (template [<name> <wrapper>]
    [(def: .public <name>
       (-> (Markdown Span) (Markdown Span))
       (|>> :representation
            (text.enclosed [<wrapper> <wrapper>])
            :abstraction))]

    [bold "**"]
    [italic "_"]
    )

  (def: (prefix with)
    (-> Text (-> Text Text))
    (|>> (text.split_all_with text.new_line)
         (list\map (function (_ line)
                     (if (text.empty? line)
                       line
                       (format with line))))
         (text.join_with text.new_line)))

  (def: indent
    (-> Text Text)
    (..prefix text.tab))

  (def: .public quote
    (-> (Markdown Block) (Markdown Block))
    (|>> :representation
         (..prefix "> ")
         :abstraction))

  (def: .public numbered_list
    (-> (List [(Markdown Span) (Maybe (Markdown Block))])
        (Markdown Block))
    (|>> list.enumeration
         (list\map (function (_ [idx [summary detail]])
                     (format (%.nat (inc idx)) ". " (:representation summary) text.new_line
                             (case detail
                               (#.Some detail)
                               (|> detail :representation ..indent (text.enclosed [text.new_line text.new_line]))
                               
                               #.None
                               ""))))
         (text.join_with text.new_line)
         ..block))

  (def: .public bullet_list
    (-> (List [(Markdown Span) (Maybe (Markdown Block))])
        (Markdown Block))
    (|>> (list\map (function (_ [summary detail])
                     (format "*. " (:representation summary) text.new_line
                             (case detail
                               (#.Some detail)
                               (|> detail :representation ..indent (text.enclosed [text.new_line text.new_line]))
                               
                               #.None
                               ""))))
         (text.join_with text.new_line)
         ..block))

  (def: .public snippet
    {#.doc "A snippet of code."}
    (-> Text (Markdown Span))
    (|>> ..safe (text.enclosed ["`" "`"]) :abstraction))

  (def: .public code
    {#.doc "A block of code."}
    (-> Text (Markdown Block))
    (let [open (format "```" text.new_line)
          close (format text.new_line "```")]
      (|>> (text.enclosed [open close]) ..block)))

  (def: .public (image description url)
    (-> Text URL (Markdown Span))
    (:abstraction (format "![" (..safe description) "](" url ")")))

  (def: .public horizontal_rule
    (Markdown Block)
    (..block "___"))

  (def: .public (link description url)
    (-> (Markdown Span) URL (Markdown Span))
    (:abstraction (format "[" (:representation description) "](" url ")")))

  (type: .public Email
    Text)

  (template [<name> <type>]
    [(def: .public <name>
       (-> <type> (Markdown Span))
       (|>> (text.enclosed ["<" ">"]) :abstraction))]

    [url URL]
    [email Email]
    )

  (template [<name> <brand> <infix>]
    [(def: .public (<name> pre post)
       (-> (Markdown <brand>) (Markdown <brand>) (Markdown <brand>))
       (:abstraction (format (:representation pre) <infix> (:representation post))))]

    [and Span " "]
    [then Block ""]
    )

  (def: .public markdown
    (-> (Markdown Any) Text)
    (|>> :representation))
  )