diff options
| author | Asko Nõmm <ano@ano.ee> | 2022-04-30 12:09:32 +0300 |
|---|---|---|
| committer | Asko Nõmm <ano@ano.ee> | 2022-04-30 12:09:32 +0300 |
| commit | ee7ab7fd1163a1a8444e16017793eb32a0eb08ee (patch) | |
| tree | 287468b35b2815a7164225883669e070bde39b63 /src/clarktown | |
| parent | 258fda339e9a5bf3b2cfc2f8dea28c4c02b28073 (diff) | |
Close #22, Close #23
Diffstat (limited to 'src/clarktown')
| -rw-r--r-- | src/clarktown/correctors/atx_heading_block.clj | 9 | ||||
| -rw-r--r-- | src/clarktown/engine.clj | 15 | ||||
| -rw-r--r-- | src/clarktown/matchers/heading_block.clj | 6 | ||||
| -rw-r--r-- | src/clarktown/renderers/heading_block.clj | 13 |
4 files changed, 26 insertions, 17 deletions
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)) |
