diff --git a/.gitignore b/.gitignore index c53038e..1094326 100644 --- a/.gitignore +++ b/.gitignore @@ -1,3 +1,4 @@ +/html-to-md /target /classes /checkouts @@ -9,3 +10,4 @@ pom.xml.asc /.nrepl-port .hgignore .hg/ +*~ diff --git a/html-to-md b/html-to-md deleted file mode 160000 index e07835e..0000000 --- a/html-to-md +++ /dev/null @@ -1 +0,0 @@ -Subproject commit e07835e724c856bc095f2090858ca88c6e7d2754 diff --git a/src/html_to_md/html_to_md.clj b/src/html_to_md/html_to_md.clj index 3358bde..5bc9716 100644 --- a/src/html_to_md/html_to_md.clj +++ b/src/html_to_md/html_to_md.clj @@ -17,14 +17,19 @@ (-> e :attrs :href) ")")))) -(defn markdown-strong - "Process the strong emphasis element `e` into markdown, using dispatcher - `d`." +(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`." @@ -95,7 +100,7 @@ (defn markdown-html "Process this HTML element `e` into markdown, using dispatcher `d`." [e d] - (apply str (process (html/select e [:body]) d) )) + (apply str (process (first (html/select e [:body])) d) )) (defn markdown-img "Process this image element `e` into markdown, using dispatcher `d`." @@ -119,6 +124,24 @@ (range)))) "\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 "Process this unordered list element `e` into markdown, using dispatcher `d`." @@ -139,6 +162,8 @@ (def markdown-dispatcher {:a markdown-a :b markdown-strong + :code markdown-code + :body markdown-default :div markdown-div :em markdown-em :h1 markdown-h1 @@ -151,6 +176,10 @@ :i markdown-em :img markdown-img :ol markdown-ol + :p markdown-div + :pre markdown-pre + :samp markdown-code + :span markdown-default :strong markdown-strong :ul markdown-ul }) diff --git a/test/html_to_md/html_to_md_test.clj b/test/html_to_md/html_to_md_test.clj index c4f02b4..9e7945f 100644 --- a/test/html_to_md/html_to_md_test.clj +++ b/test/html_to_md/html_to_md_test.clj @@ -103,3 +103,85 @@ markdown-dispatcher)] (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))))) + +