aboutsummaryrefslogtreecommitdiff
path: root/stdlib/source/lux/data/format/markdown.lux
blob: 5fcf85cbfa36dc36735e8e2443a998d5fdb994fc (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
(.module:
  [lux (#- and)
   [data
    ["." text
     format]
    [collection
     ["." list ("list/." functor)]]]
   [type
    abstract]
   [world
    [net (#+ URL)]]])

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

(def: sanitize
  (-> 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: #export Span {} Any)
(abstract: #export Block {} Any)

(abstract: #export (Markdown brand)
  {}

  Text

  (def: #export empty
    Markdown
    (:abstraction ""))

  (def: #export text
    (-> Text (Markdown Span))
    (|>> ..sanitize :abstraction))

  (def: blank-line (format text.new-line text.new-line))

  (do-template [<name> <prefix>]
    [(def: #export (<name> content)
       (-> Text Markdown)
       (:abstraction (format <prefix> " " (..sanitize 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: #export paragraph
    (-> (Markdown Span) (Markdown Block))
    (|>> :representation ..block))

  (def: #export break
    (Markdown Span)
    (:abstraction (format "  " text.new-line)))

  (do-template [<name> <wrapper>]
    [(def: #export <name>
       (-> (Markdown Span) (Markdown Span))
       (|>> :representation
            (text.enclose [<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: #export quote
    (-> (Markdown Block) (Markdown Block))
    (|>> :representation
         (..prefix "> ")
         :abstraction))

  (def: #export numbered-list
    (-> (List [(Markdown Span) (Maybe (Markdown Block))])
        (Markdown Block))
    (|>> list.enumerate
         (list/map (function (_ [idx [summary detail]])
                     (format (%n (inc idx)) ". " (:representation summary) text.new-line
                             (case detail
                               (#.Some detail)
                               (|> detail :representation ..indent (text.enclose [text.new-line text.new-line]))
                               
                               #.None
                               ""))))
         (text.join-with text.new-line)
         ..block))

  (def: #export 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.enclose [text.new-line text.new-line]))
                               
                               #.None
                               ""))))
         (text.join-with text.new-line)
         ..block))

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

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

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

  (def: #export horizontal-rule
    (Markdown Block)
    (..block "___"))

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

  (type: #export Email Text)

  (do-template [<name> <type>]
    [(def: #export <name>
       (-> <type> (Markdown Span))
       (|>> (text.enclose ["<" ">"]) :abstraction))]

    [url URL]
    [email Email]
    )

  (do-template [<name> <brand> <infix>]
    [(def: #export (<name> pre post)
       (-> (Markdown <brand>) (Markdown <brand>) (Markdown <brand>))
       (:abstraction (format (:representation pre) <infix> (:representation post))))]

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

  (def: #export markdown
    (-> (Markdown Any) Text)
    (|>> :representation))
  )