Pretty much everything except tables done.

This commit is contained in:
Simon Brooke 2019-04-30 16:01:55 +01:00
parent 7f50863d83
commit 80f4857d73
4 changed files with 119 additions and 7 deletions

2
.gitignore vendored
View file

@ -1,3 +1,4 @@
/html-to-md
/target /target
/classes /classes
/checkouts /checkouts
@ -9,3 +10,4 @@ pom.xml.asc
/.nrepl-port /.nrepl-port
.hgignore .hgignore
.hg/ .hg/
*~

@ -1 +0,0 @@
Subproject commit e07835e724c856bc095f2090858ca88c6e7d2754

View file

@ -17,14 +17,19 @@
(-> e :attrs :href) (-> e :attrs :href)
")")))) ")"))))
(defn markdown-strong (defn markdown-code
"Process the strong emphasis element `e` into markdown, using dispatcher "Process the code or samp `e` into markdown, using dispatcher `d`."
`d`."
[e d] [e d]
(str (str
"**" "`"
(s/trim (apply str (map #(process % d) (:content e)))) (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 (defn markdown-div
"Process the division element `e` into markdown, using dispatcher `d`." "Process the division element `e` into markdown, using dispatcher `d`."
@ -95,7 +100,7 @@
(defn markdown-html (defn markdown-html
"Process this HTML element `e` into markdown, using dispatcher `d`." "Process this HTML element `e` into markdown, using dispatcher `d`."
[e d] [e d]
(apply str (process (html/select e [:body]) d) )) (apply str (process (first (html/select e [:body])) d) ))
(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`."
@ -119,6 +124,24 @@
(range)))) (range))))
"\n\n")) "\n\n"))
(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 (defn markdown-ul
"Process this unordered list element `e` into markdown, using dispatcher "Process this unordered list element `e` into markdown, using dispatcher
`d`." `d`."
@ -139,6 +162,8 @@
(def markdown-dispatcher (def markdown-dispatcher
{:a markdown-a {:a markdown-a
:b markdown-strong :b markdown-strong
:code markdown-code
:body markdown-default
:div markdown-div :div markdown-div
:em markdown-em :em markdown-em
:h1 markdown-h1 :h1 markdown-h1
@ -151,6 +176,10 @@
:i markdown-em :i markdown-em
:img markdown-img :img markdown-img
:ol markdown-ol :ol markdown-ol
:p markdown-div
:pre markdown-pre
:samp markdown-code
:span markdown-default
:strong markdown-strong :strong markdown-strong
:ul markdown-ul :ul markdown-ul
}) })

View file

@ -103,3 +103,85 @@
markdown-dispatcher)] markdown-dispatcher)]
(is (= expected actual))))) (is (= expected actual)))))
(deftest body-test
(testing "A complete document body"
(let [expected "\n# This is the top level header\n\n## Table of contents\n\n\n1. [Paragraph One](paragraph-1)\n2. [Paragraph Two](paragraph-2)\n\n\n## Paragraph-1\n\nThis is the first paragraph. It is *very* dull.\n\n## Paragraph-2\n\nThis is the second paragraph. It is no more interesting.\n"
actual (process
{:tag :body
:content
[{:tag :h1
:content ["This is the top level header"]}
{:tag :h2
:content ["Table of contents"]}
{:tag :ol
:content
[{:tag :li
:content
[{:tag :a
:attrs {:href "paragraph-1"}
:content ["Paragraph One"]}]}
{:tag :li
:content
[{:tag :a
:attrs {:href "paragraph-2"}
:content ["Paragraph Two"]}]}]}
{:tag :h2
:content ["Paragraph-1"]}
{:tag :p
:content ["This is the first paragraph. It is "
{:tag :em
:content ["very"]}
" dull."]}
{:tag :h2
:content ["Paragraph-2"]}
{:tag :p
:content ["This is the second paragraph. It is no more interesting."]}]}
markdown-dispatcher)]
(println actual)
(is (= expected actual)))))
(deftest html-test
(testing "a complete sample document"
(let [expected "\n# This is the top level header\n\n## Table of contents\n\n\n1. [Paragraph One](paragraph-1)\n2. [Paragraph Two](paragraph-2)\n\n\n## Paragraph-1\n\nThis is the first paragraph. It is *very* dull.\n\n## Paragraph-2\n\nThis is the second paragraph. It is no more interesting.\n"
actual (process
{:tag :html
:content
[{:tag :head
:content
[{:tag :title
:content ["This is the title"]}]}
{:tag :body
:content
[{:tag :h1
:content ["This is the top level header"]}
{:tag :h2
:content ["Table of contents"]}
{:tag :ol
:content
[{:tag :li
:content
[{:tag :a
:attrs {:href "paragraph-1"}
:content ["Paragraph One"]}]}
{:tag :li
:content
[{:tag :a
:attrs {:href "paragraph-2"}
:content ["Paragraph Two"]}]}]}
{:tag :h2
:content ["Paragraph-1"]}
{:tag :p
:content ["This is the first paragraph. It is "
{:tag :em
:content ["very"]}
" dull."]}
{:tag :h2
:content ["Paragraph-2"]}
{:tag :p
:content ["This is the second paragraph. It is no more interesting."]}]}]}
markdown-dispatcher)]
(is (= expected actual)))))