From ee7ab7fd1163a1a8444e16017793eb32a0eb08ee Mon Sep 17 00:00:00 2001 From: Asko Nõmm Date: Sat, 30 Apr 2022 12:09:32 +0300 Subject: Close #22, Close #23 --- .gitignore | 2 ++ src/clarktown/correctors/atx_heading_block.clj | 9 +++++---- src/clarktown/engine.clj | 15 ++++++++++---- src/clarktown/matchers/heading_block.clj | 6 ++---- src/clarktown/renderers/heading_block.clj | 13 ++++++++----- test/clarktown/renderers/heading_block_test.clj | 26 ++++++++++++++++++------- 6 files changed, 47 insertions(+), 24 deletions(-) diff --git a/.gitignore b/.gitignore index 6338a04..1638cd3 100644 --- a/.gitignore +++ b/.gitignore @@ -16,3 +16,5 @@ pom.xml.asc .idea/ .calva/ test.md + +clarktown.iml diff --git a/src/clarktown/correctors/atx_heading_block.clj b/src/clarktown/correctors/atx_heading_block.clj index 8246ec3..a2a948b 100644 --- a/src/clarktown/correctors/atx_heading_block.clj +++ b/src/clarktown/correctors/atx_heading_block.clj @@ -1,6 +1,7 @@ (ns clarktown.correctors.atx-heading-block (:require - [clojure.string :as string])) + [clojure.string :as string] + [clarktown.matchers.heading-block :refer [is-atx-heading?]])) (defn- in-code-block? @@ -19,12 +20,12 @@ there's no empty newline above, we need to create one, and so this function must then return `true`." [lines line index] - (and (is-atx-heading? line) + (and (is-atx-heading? (string/trim line)) (> index 0) (not (= (-> (nth lines (- index 1)) string/trim) "")) - (not (in-code-block? lines index)))) - + (not (in-code-block? lines index)))) + (defn empty-line-below? "Determines whether there's a need for an empty new line diff --git a/src/clarktown/engine.clj b/src/clarktown/engine.clj index deca176..eaa0fb0 100644 --- a/src/clarktown/engine.clj +++ b/src/clarktown/engine.clj @@ -69,13 +69,20 @@ lines))) +(defn- remove-excess-newlines + "Replaces all occurences of 3 or more concecutive newlines into + two newlines." + [markdown] + (string/replace markdown #"\n{3,}" "\n\n")) + + (defn- correct-markdown "Corrects invalid Markdown for the parser." [markdown given-correctors] - (let [lines (string/split-lines markdown)] - (->> lines - (correct-block-separations (:block-separations given-correctors)) - (string/join \newline)))) + (->> (string/split-lines markdown) + (correct-block-separations (:block-separations given-correctors)) + (string/join \newline) + (remove-excess-newlines))) (defn- find-parser-by-block diff --git a/src/clarktown/matchers/heading_block.clj b/src/clarktown/matchers/heading_block.clj index 2295f26..1a9a451 100644 --- a/src/clarktown/matchers/heading_block.clj +++ b/src/clarktown/matchers/heading_block.clj @@ -6,9 +6,7 @@ (defn is-atx-heading? "Determines whether the given block is a atx heading." [block] - (-> (string/replace block #"\n" "") - string/trim - (string/starts-with? "#"))) + (re-matches #"^\#{1,6}\s.*" block)) (defn is-settext-heading? @@ -26,4 +24,4 @@ "Determines whether the given block is a heading block." [block] (or (is-atx-heading? block) - (is-settext-heading? block))) \ No newline at end of file + (is-settext-heading? block))) diff --git a/src/clarktown/renderers/heading_block.clj b/src/clarktown/renderers/heading_block.clj index 4da9bda..7f4c7de 100644 --- a/src/clarktown/renderers/heading_block.clj +++ b/src/clarktown/renderers/heading_block.clj @@ -7,8 +7,7 @@ (defn render-atx-heading "Renders the hashbang heading block." [block] - (let [single-line-block (-> (string/replace block #"\n" "") - string/trim) + (let [single-line-block (string/trim block) size (-> (string/split single-line-block #" ") first string/trim @@ -39,6 +38,10 @@ (defn render "Renders the heading block." [block _ _] - (if (matcher/is-atx-heading? block) - (render-atx-heading block) - (render-settext-heading block))) + (cond (matcher/is-atx-heading? block) + (render-atx-heading block) + + (matcher/is-settext-heading? block) + (render-settext-heading block) + + :else block)) diff --git a/test/clarktown/renderers/heading_block_test.clj b/test/clarktown/renderers/heading_block_test.clj index 2196422..7647b53 100644 --- a/test/clarktown/renderers/heading_block_test.clj +++ b/test/clarktown/renderers/heading_block_test.clj @@ -5,25 +5,37 @@ (deftest atx-heading-renderer-test - (testing "Hashbang heading block that's a H1" + (testing "ATX heading block that's a H1" (is (= "

This is a heading block.

" (heading-block/render "# This is a heading block." nil nil)))) - (testing "Hashbang heading block that's a H2" + (testing "ATX heading block that's a H2" (is (= "

This is a heading block.

" (heading-block/render "## This is a heading block." nil nil)))) - (testing "Hashbang heading block that's a H3" + (testing "ATX heading block that's a H3" (is (= "

This is a heading block.

" (heading-block/render "### This is a heading block." nil nil)))) - (testing "Hashbang heading block that's a H4" + (testing "ATX heading block that's a H4" (is (= "

This is a heading block.

" (heading-block/render "#### This is a heading block." nil nil)))) - (testing "Hashbang heading block that's a H5" + (testing "ATX heading block that's a H5" (is (= "
This is a heading block.
" - (heading-block/render "##### This is a heading block." nil nil))))) + (heading-block/render "##### This is a heading block." nil nil)))) + + (testing "ATX heading block that's a H6" + (is (= "
This is a heading block.
" + (heading-block/render "###### This is a heading block." nil nil)))) + + (testing "No H tag when 7 or more # characters" + (is (= "####### This is not a heading block." + (heading-block/render "####### This is not a heading block." nil nil)))) + + (testing "No H tag when there is no space between # characters and value" + (is (= "#This is not a heading block." + (heading-block/render "#This is not a heading block." nil nil))))) (deftest settext-heading-renderer-text @@ -41,4 +53,4 @@ (testing "Settext heading block that's a H2 spanning multiple lines" (is (= "

This is a \nheading block spanning multiple lines.

" - (heading-block/render "This is a \nheading block spanning multiple lines.\n--------" nil nil))))) \ No newline at end of file + (heading-block/render "This is a \nheading block spanning multiple lines.\n--------" nil nil))))) -- cgit v1.2.3