Lots of improvements from running against real live tag soup

This commit is contained in:
Simon Brooke 2019-04-30 18:35:34 +01:00
parent 7bc60a0bbb
commit 81a7337eb3
4 changed files with 33 additions and 24 deletions

View file

@ -5,6 +5,7 @@
:url "http://www.eclipse.org/legal/epl-v10.html"} :url "http://www.eclipse.org/legal/epl-v10.html"}
:dependencies [[org.clojure/clojure "1.8.0"] :dependencies [[org.clojure/clojure "1.8.0"]
[enlive "1.1.6"]] [enlive "1.1.6"]]
:plugins [[lein-codox "0.10.3"]] :plugins [[lein-codox "0.10.3"]
[lein-release "1.0.5"]]
:lein-release {:deploy-via :clojars} :lein-release {:deploy-via :clojars}
:signing {:gpg-key "Simon Brooke (Stultus in monte) <simon@journeyman.cc>"}) :signing {:gpg-key "Simon Brooke (Stultus in monte) <simon@journeyman.cc>"})

View file

@ -7,15 +7,18 @@
(defn markdown-a (defn markdown-a
"Process the anchor element `e` into markdown, using dispatcher `d`." "Process the anchor element `e` into markdown, using dispatcher `d`."
[e d] [e d]
(apply (str
str "["
(flatten (s/trim (apply str (process (:content e) d)))
(list "]("
"[" (-> e :attrs :href)
(map #(process % d) (:content e)) ")"))
"]("
(-> e :attrs :href) (defn markdown-br
")")))) "Process the line-break element `e`, so beloved of tag-soupers, into
markdown"
[e d]
"\n\n")
(defn markdown-code (defn markdown-code
"Process the code or samp `e` into markdown, using dispatcher `d`." "Process the code or samp `e` into markdown, using dispatcher `d`."
@ -51,15 +54,12 @@
"Process the header element `e` into markdown, with level `level`, "Process the header element `e` into markdown, with level `level`,
using dispatcher `d`." using dispatcher `d`."
[e d level] [e d level]
(apply (str
str "\n"
(flatten (apply str (take level (repeat "#")))
(list " "
"\n" (s/trim (apply str (process (:content e) d)))
(take level (repeat "#")) "\n"))
" "
(map #(process % d) (:content e))
"\n"))))
(defn markdown-h1 (defn markdown-h1
"Process the header element `e` into markdown, with level 1, using "Process the header element `e` into markdown, with level 1, using
@ -105,7 +105,7 @@
(defn markdown-img (defn markdown-img
"Process this image element `e` into markdown, using dispatcher `d`." "Process this image element `e` into markdown, using dispatcher `d`."
[e d] [e d]
(str "![" (-> e :attrs :alt) "](" (-> e :attrs :src) ")")) (str "![image: " (-> e :attrs :alt) "](" (-> e :attrs :src) ")"))
(defn markdown-ol (defn markdown-ol
"Process this ordered list element `e` into markdown, using dispatcher "Process this ordered list element `e` into markdown, using dispatcher
@ -120,10 +120,15 @@
str str
(flatten (flatten
(list "\n" (inc %2) ". " (process %1 d)))) (list "\n" (inc %2) ". " (process %1 d))))
(:content e) (html/select e [:li])
(range)))) (range))))
"\n\n")) "\n\n"))
(defn markdown-omit
"Don't process the element `e` into markdown, but return `nil`."
[e d]
nil)
(defn markdown-pre (defn markdown-pre
"Process the preformatted emphasis element `e` into markdown, using "Process the preformatted emphasis element `e` into markdown, using
dispatcher `d`." dispatcher `d`."
@ -155,13 +160,14 @@
str str
(flatten (flatten
(list "\n* " (process % d)))) (list "\n* " (process % d))))
(:content e)))) (html/select e [:li]))))
"\n\n")) "\n\n"))
(def markdown-dispatcher (def markdown-dispatcher
{:a markdown-a {:a markdown-a
:b markdown-strong :b markdown-strong
:br markdown-br
:code markdown-code :code markdown-code
:body markdown-default :body markdown-default
:div markdown-div :div markdown-div
@ -179,8 +185,10 @@
:p markdown-div :p markdown-div
:pre markdown-pre :pre markdown-pre
:samp markdown-code :samp markdown-code
:script markdown-omit
:span markdown-default :span markdown-default
:strong markdown-strong :strong markdown-strong
:style markdown-omit
:ul markdown-ul :ul markdown-ul
}) })

View file

@ -29,7 +29,7 @@
(string? element) element (string? element) element
(or (seq? element) (vector? element)) (or (seq? element) (vector? element))
(map #(process % dispatcher) element))) (doall (map #(process % dispatcher) element))))
(defn- transformer-dispatch (defn- transformer-dispatch
[a _] [a _]

View file

@ -73,7 +73,7 @@
(deftest img-test (deftest img-test
(testing "Image tag." (testing "Image tag."
(let [expected "![Hello dere!](http://foo.bar/image.png)" (let [expected "![image: Hello dere!](http://foo.bar/image.png)"
actual (process actual (process
{:tag :img {:tag :img
:attrs {:src "http://foo.bar/image.png" :attrs {:src "http://foo.bar/image.png"