diff options
| author | Asko Nomm <asko@bien.ee> | 2022-04-09 13:51:16 +0200 |
|---|---|---|
| committer | Asko Nomm <asko@bien.ee> | 2022-04-09 13:51:16 +0200 |
| commit | 9f8f4fc36adef06458320d7bfdef01dbe41f52a1 (patch) | |
| tree | 48b51c46809dedb79f5891e113443a4dec028921 | |
| parent | 7055c7bd45c5156df61ab3a77b8e22a037f04408 (diff) | |
Support settext headings (closes #6)
| -rw-r--r-- | resources/test/core.md | 8 | ||||
| -rw-r--r-- | resources/test/core_result.html | 20 | ||||
| -rw-r--r-- | src/clarktown/parser.clj | 2 | ||||
| -rw-r--r-- | src/clarktown/parsers/heading_block.clj | 59 | ||||
| -rw-r--r-- | test/clarktown/parsers/heading_block_test.clj | 44 |
5 files changed, 122 insertions, 11 deletions
diff --git a/resources/test/core.md b/resources/test/core.md index fe284ad..d699887 100644 --- a/resources/test/core.md +++ b/resources/test/core.md @@ -68,4 +68,10 @@ This is ___bold italic text___ and ***this is also***. *What about italic text t * Another sub list item 1. Sub sub list item 2. Continuing sub list item -* Continuing list item
\ No newline at end of file +* Continuing list item + +This is a H1 heading with settext +================================= + +And this is a H2 heading with settext +-------------------------------------
\ No newline at end of file diff --git a/resources/test/core_result.html b/resources/test/core_result.html index fb64b35..5e9513b 100644 --- a/resources/test/core_result.html +++ b/resources/test/core_result.html @@ -1,4 +1,8 @@ -<p>Lorem ipsum dolor <strong>sit</strong> amet. Lorem ipsum <em>dolor</em> <em>sit</em> <strong>amet</strong>.</p><ol><li>List item</li><li>Another list item<ol><li>Sub list item</li><li>Another sub list item<ol><li>Sub sub list item</li></ol></li><li>Continuing sub list item</li></ol></li><li>Continuing list item</li></ol><pre><code class="language-javascript">// Detect horizontal line block +<p>Lorem ipsum dolor <strong>sit</strong> amet. Lorem ipsum <em>dolor</em> <em>sit</em> <strong>amet</strong>.</p> + +<ol><li>List item</li><li>Another list item<ol><li>Sub list item</li><li>Another sub list item<ol><li>Sub sub list item</li></ol></li><li>Continuing sub list item</li></ol></li><li>Continuing list item</li></ol> + +<pre><code class="language-javascript">// Detect horizontal line block function isHorizontalLineBlock(block) { return block === "***"; } @@ -36,4 +40,16 @@ function markdownToHTML(markdown) { // And at last, join the blocks together for one big block. return parsedBlocks.join(""); -}</code></pre><p>This is <em><strong>bold italic text</strong></em> and <em><strong>this is also</strong></em>. <em>What about italic text that <strong>has bold text</strong></em>?</p><h2>Hi there, world!</h2><ul><li>List item</li><li>Another list item<ul><li>Sub list item</li><li>Another sub list item<ul><li>Sub sub list item</li><li>Continuing sub list item</li></ul></li></ul></li><li>Continuing list item</li></ul><ul><li>List item</li><li>Another list item<ul><li>Sub list item</li><li>Another sub list item<ol><li>Sub sub list item</li><li>Continuing sub list item</li></ol></li></ul></li><li>Continuing list item</li></ul>
\ No newline at end of file +}</code></pre> + +<p>This is <em><strong>bold italic text</strong></em> and <em><strong>this is also</strong></em>. <em>What about italic text that <strong>has bold text</strong></em>?</p> + +<h2>Hi there, world!</h2> + +<ul><li>List item</li><li>Another list item<ul><li>Sub list item</li><li>Another sub list item<ul><li>Sub sub list item</li><li>Continuing sub list item</li></ul></li></ul></li><li>Continuing list item</li></ul> + +<ul><li>List item</li><li>Another list item<ul><li>Sub list item</li><li>Another sub list item<ol><li>Sub sub list item</li><li>Continuing sub list item</li></ol></li></ul></li><li>Continuing list item</li></ul> + +<h1>This is a H1 heading with settext</h1> + +<h2>And this is a H2 heading with settext</h2>
\ No newline at end of file diff --git a/src/clarktown/parser.clj b/src/clarktown/parser.clj index 95776d7..0b6911d 100644 --- a/src/clarktown/parser.clj +++ b/src/clarktown/parser.clj @@ -94,4 +94,4 @@ (let [blocks (-> (string/split markdown #"\n\n") stitch-code-blocks) parsed-blocks (parse-blocks blocks parsers)] - (string/join "" parsed-blocks))) + (string/join "\n\n" parsed-blocks))) diff --git a/src/clarktown/parsers/heading_block.clj b/src/clarktown/parsers/heading_block.clj index 84a5fdb..def2394 100644 --- a/src/clarktown/parsers/heading_block.clj +++ b/src/clarktown/parsers/heading_block.clj @@ -3,17 +3,35 @@ [clojure.string :as string])) +(defn is-hashbang-heading? + "Determines whether the given block is a hashbang heading." + [block] + (-> (string/replace block #"\n" "") + string/trim + (string/starts-with? "#"))) + + +(defn is-settext-heading? + "Determines whether the given block is a settext heading." + [block] + (let [lines (-> (string/split-lines block)) + chars (-> (last lines) + string/trim + (string/split #""))] + (and (> (count lines) 1) + (every? #{"-" "="} chars)))) + + (defn is? - "Determines whether the given block is a heading block or not." + "Determines whether the given block is a heading block." [block] - (= true (-> (string/replace block #"\n" "") - string/trim - (string/starts-with? "#")))) + (or (is-hashbang-heading? block) + (is-settext-heading? block))) -(defn render - "Renders the heading block." - [block _] +(defn render-hashbang-heading + "Renders the hashbang heading block." + [block] (let [single-line-block (-> (string/replace block #"\n" "") string/trim) size (-> (string/split single-line-block #" ") @@ -25,3 +43,30 @@ (string/join " ") string/trim)] (str "<h" size ">" value "</h" size ">"))) + + +(defn render-settext-heading + "Renders the settext heading block." + [block] + (let [lines (string/split-lines block) + value (->> (split-at (- (count lines) 1) lines) + first + (string/join "\n")) + h1? (= "=" (-> (last lines) + string/trim + (string/split #"") + first))] + (if h1? + (str "<h1>" value "</h1>") + (str "<h2>" value "</h2>")))) + + +(render-settext-heading "Hello world\nAnd you too\n===") + + +(defn render + "Renders the heading block." + [block _] + (if (is-hashbang-heading? block) + (render-hashbang-heading block) + (render-settext-heading block))) diff --git a/test/clarktown/parsers/heading_block_test.clj b/test/clarktown/parsers/heading_block_test.clj new file mode 100644 index 0000000..9bfff4f --- /dev/null +++ b/test/clarktown/parsers/heading_block_test.clj @@ -0,0 +1,44 @@ +(ns clarktown.parsers.heading-block-test + (:require + [clojure.test :refer [deftest testing is]] + [clarktown.parsers.heading-block :as heading-block])) + + +(deftest hashbang-heading-test + (testing "Hashbang heading block that's a H1" + (is (= "<h1>This is a heading block.</h1>" + (heading-block/render "# This is a heading block." nil)))) + + (testing "Hashbang heading block that's a H2" + (is (= "<h2>This is a heading block.</h2>" + (heading-block/render "## This is a heading block." nil)))) + + (testing "Hashbang heading block that's a H3" + (is (= "<h3>This is a heading block.</h3>" + (heading-block/render "### This is a heading block." nil)))) + + (testing "Hashbang heading block that's a H4" + (is (= "<h4>This is a heading block.</h4>" + (heading-block/render "#### This is a heading block." nil)))) + + (testing "Hashbang heading block that's a H5" + (is (= "<h5>This is a heading block.</h5>" + (heading-block/render "##### This is a heading block." nil))))) + + +(deftest settext-heading-text + (testing "Settext heading block that's a H1" + (is (= "<h1>This is a heading block.</h1>" + (heading-block/render "This is a heading block.\n=========" nil)))) + + (testing "Settext heading block that's a H1 spanning multiple lines" + (is (= "<h1>This is a \nheading block spanning multiple lines.</h1>" + (heading-block/render "This is a \nheading block spanning multiple lines.\n========" nil)))) + + (testing "Settext heading block that's a H2" + (is (= "<h2>This is a heading block.</h2>" + (heading-block/render "This is a heading block.\n---------" nil)))) + + (testing "Settext heading block that's a H2 spanning multiple lines" + (is (= "<h2>This is a \nheading block spanning multiple lines.</h2>" + (heading-block/render "This is a \nheading block spanning multiple lines.\n--------" nil)))))
\ No newline at end of file |
