-
Notifications
You must be signed in to change notification settings - Fork 2
/
html_to_md.clj
195 lines (172 loc) · 4.87 KB
/
html_to_md.clj
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
186
187
188
189
190
191
192
193
194
(ns html-to-md.html-to-md
(:require
[clojure.string :as s]
[net.cgrand.enlive-html :as html]
[html-to-md.transformer :refer [process]]))
(defn markdown-a
"Process the anchor element `e` into markdown, using dispatcher `d`."
[e d]
(str
"["
(s/trim (apply str (process (:content e) d)))
"]("
(-> e :attrs :href)
")"))
(defn markdown-br
"Process the line-break element `e`, so beloved of tag-soupers, into
markdown"
[e d]
"\n")
(defn markdown-code
"Process the code or samp `e` into markdown, using dispatcher `d`."
[e d]
(str
"`"
(s/trim (apply str (map #(process % d) (:content e))))
"`"))
(defn markdown-default
"Process an element `e` for which we have no other function into markdown,
using dispatcher `d`."
[e d]
(apply str (map #(process % d) (:content e))))
(defn markdown-div
"Process the division element `e` into markdown, using dispatcher `d`."
[e d]
(apply
str
(flatten
(list "\n" (map #(process % d) (:content e)) "\n"))))
(defn markdown-em
"Process the emphasis element `e` into markdown, using dispatcher `d`."
[e d]
(str
"*"
(s/trim (apply str (map #(process % d) (:content e))))
"*"))
(defn markdown-header
"Process the header element `e` into markdown, with level `level`,
using dispatcher `d`."
[e d level]
(str
"\n"
(apply str (take level (repeat "#")))
" "
(s/trim (apply str (process (:content e) d)))
"\n"))
(defn markdown-h1
"Process the header element `e` into markdown, with level 1, using
dispatcher `d`."
[e d]
(markdown-header e d 1))
(defn markdown-h2
"Process the header element `e` into markdown, with level 2, using
dispatcher `d`."
[e d]
(markdown-header e d 2))
(defn markdown-h3
"Process the header element `e` into markdown, with level 3, using
dispatcher `d`."
[e d]
(markdown-header e d 3))
(defn markdown-h4
"Process the header element `e` into markdown, with level 4, using
dispatcher `d`."
[e d]
(markdown-header e d 4))
(defn markdown-h5
"Process the header element `e` into markdown, with level 5, using
dispatcher `d`."
[e d]
(markdown-header e d 5))
(defn markdown-h6
"Process the header element `e` into markdown, with level 6, using
dispatcher `d`."
[e d]
(markdown-header e d 6))
(defn markdown-html
"Process this HTML element `e` into markdown, using dispatcher `d`."
[e d]
(apply str (process (first (html/select e [:body])) d) ))
(defn markdown-img
"Process this image element `e` into markdown, using dispatcher `d`."
[e d]
(str "![image: " (-> e :attrs :alt) "](" (-> e :attrs :src) ")"))
(defn markdown-ol
"Process this ordered list element `e` into markdown, using dispatcher
`d`."
[e d]
(str
"\n"
(apply str
(doall
(map
#(apply
str
(flatten
(list "\n" (inc %2) ". " (process %1 d))))
(html/select e [:li])
(range))))
"\n\n"))
(defn markdown-omit
"Don't process the element `e` into markdown, but return `nil`."
[e d]
nil)
(defn markdown-pre
"Process the preformatted emphasis element `e` into markdown, using
dispatcher `d`."
[e d]
(str
"\n```\n"
(s/trim (apply str (map #(process % d) (:content e))))
"\n```\n"))
(defn markdown-strong
"Process the strong emphasis element `e` into markdown, using dispatcher
`d`."
[e d]
(str
"**"
(s/trim (apply str (map #(process % d) (:content e))))
"**"))
(defn markdown-ul
"Process this unordered list element `e` into markdown, using dispatcher
`d`."
[e d]
(str
"\n"
(apply str
(doall
(map
#(apply
str
(flatten
(list "\n* " (process % d))))
(html/select e [:li]))))
"\n\n"))
(def markdown-dispatcher
"A despatcher for transforming (X)HTML into Markdown."
{:a markdown-a
:b markdown-strong
:br markdown-br
:code markdown-code
:body markdown-default
:div markdown-div
:em markdown-em
:h1 markdown-h1
:h2 markdown-h2
:h3 markdown-h3
:h4 markdown-h4
:h5 markdown-h5
:h6 markdown-h6
:html markdown-html
:i markdown-em
:img markdown-img
:ol markdown-ol
:p markdown-div
:pre markdown-pre
:samp markdown-code
:script markdown-omit
:span markdown-default
:strong markdown-strong
:style markdown-omit
:ul markdown-ul
})