diff options
| author | Asko Nõmm <84135165+askonomm@users.noreply.github.com> | 2022-04-23 02:43:49 +0300 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2022-04-23 02:43:49 +0300 |
| commit | 70d88384ea788f7b2ad5ebb725762f7d27300504 (patch) | |
| tree | b371f12617949daf2b40538ba9a2e4457b657d0f /src | |
| parent | 059bfa7bd9bfdde0c75646bf1dfc20d23da8a02c (diff) | |
| parent | dc921cbe1c786995a8670efd9f5556ee9df064f9 (diff) | |
Merge pull request #17 from askonomm/16-improve-architecture
Improve architecture
Diffstat (limited to 'src')
25 files changed, 359 insertions, 263 deletions
diff --git a/src/clarktown/core.clj b/src/clarktown/core.clj index a6ed42c..2a3e3a1 100644 --- a/src/clarktown/core.clj +++ b/src/clarktown/core.clj @@ -1,7 +1,8 @@ (ns clarktown.core (:require - [clarktown.parser :as parser] - [clarktown.parsers :as parsers])) + [clarktown.engine :as engine] + [clarktown.parsers :as parsers] + [clarktown.correctors :as correctors])) (defn render @@ -17,14 +18,13 @@ argument, which is a given Markdown block. An example parser: - ``` + ```clojure {:matcher (fn [block] ...) :renderers [(fn [block] ...) (fn [block] ...)]} ```" ([markdown] - (render markdown parsers/parsers)) + (render markdown parsers/default-parsers)) ([markdown given-parsers] - (parser/parse markdown given-parsers))) - -(comment - (render (slurp "./test.md")))
\ No newline at end of file + (render markdown given-parsers correctors/default-correctors)) + ([markdown given-parsers given-correctors] + (engine/render markdown given-parsers given-correctors))) diff --git a/src/clarktown/correctors.clj b/src/clarktown/correctors.clj new file mode 100644 index 0000000..14d82ca --- /dev/null +++ b/src/clarktown/correctors.clj @@ -0,0 +1,21 @@ +(ns clarktown.correctors + (:require + [clarktown.correctors.code-block :as code-block] + [clarktown.correctors.atx-heading-block :as atx-heading-block])) + + +(def + ^{:doc "The default block separation correctors."} + default-block-separation-correctors + {:empty-line-above? + [code-block/empty-line-above? + atx-heading-block/empty-line-above?] + :empty-line-below? + [code-block/empty-line-below? + atx-heading-block/empty-line-below?]}) + + +(def + ^{:doc "The default correctors."} + default-correctors + {:block-separations default-block-separation-correctors})
\ No newline at end of file diff --git a/src/clarktown/correctors/atx_heading_block.clj b/src/clarktown/correctors/atx_heading_block.clj new file mode 100644 index 0000000..9ff789e --- /dev/null +++ b/src/clarktown/correctors/atx_heading_block.clj @@ -0,0 +1,29 @@ +(ns clarktown.correctors.atx-heading-block + (:require + [clojure.string :as string])) + + +(defn empty-line-above? + "Determines whether there's a need for an empty new line + above the `line` at the current `index`. In the case of a + ATX heading block that starts with the `#` character, if + there's no empty newline above, we need to create one, and + so this function must then return `true`." + [lines line index] + (and (string/starts-with? (string/trim line) "#") + (> index 0) + (not (= (-> (nth lines (- index 1)) + string/trim) "")))) + + +(defn empty-line-below? + "Determines whether there's a need for an empty new line + below the `line` at the current `index`. In the case of a + ATX heading block that starts with the `#` character, if + there's no empty newline below, we need to create one, and + so this function must then return `true`." + [lines line index] + (and (string/starts-with? (string/trim line) "#") + (< index (- (count lines) 1)) + (not (= (-> (nth lines (+ index 1)) + string/trim) "")))) diff --git a/src/clarktown/correctors/code_block.clj b/src/clarktown/correctors/code_block.clj new file mode 100644 index 0000000..73989fe --- /dev/null +++ b/src/clarktown/correctors/code_block.clj @@ -0,0 +1,37 @@ +(ns clarktown.correctors.code-block + (:require + [clojure.string :as string])) + + +(defn empty-line-above? + "Determines whether there's a need for an empty new line + above the `line` at the current `index`. In the case of a + code block, which starts with three backticks (```), if there's + no empty newline above, we need to create one, and so this + function must then return `true`." + [lines line index] + (and (string/starts-with? (string/trim line) "```") + (> index 0) + (->> (take index lines) + (filter #(string/starts-with? (string/trim %) "```")) + count + even?) + (not (= (-> (nth lines (- index 1)) + string/trim) "")))) + + +(defn empty-line-below? + "Determines whether there's a need for an empty new line + below the `line` at the current `index`. In the case of a + code block, which ends with three backticks (```), if there's + no empty newline above, we need to create one, and so this + function must then return `true`." + [lines line index] + (and (string/starts-with? (string/trim line) "```") + (< index (- (count lines) 1)) + (->> (take index lines) + (filter #(string/starts-with? (string/trim %) "```")) + count + odd?) + (not (= (-> (nth lines (+ index 1)) + string/trim) "")))) diff --git a/src/clarktown/parser.clj b/src/clarktown/engine.clj index ecee37d..2dc9089 100644 --- a/src/clarktown/parser.clj +++ b/src/clarktown/engine.clj @@ -1,4 +1,4 @@ -(ns clarktown.parser +(ns clarktown.engine (:require [clojure.string :as string])) @@ -35,99 +35,46 @@ (drop 1 blocks)))))))) -(defn- needs-empty-line-above? - "Determines whether the current line needs an empty line correction - above." - [lines line index] - (cond - ; code block - (and (= (string/trim line) "```") - (> index 0) - (->> (take index lines) - (filter #(= (string/trim %) "```")) - count - odd?) - (not (= (-> (nth lines (- index 1)) - string/trim) ""))) - true - - ; ATX heading block - (and (string/starts-with? (string/trim line) "#") - (> index 0) - (not (= (-> (nth lines (- index 1)) - string/trim) ""))) - true - - - ; everything else stays normal - :else false)) - - -(defn- needs-empty-line-below? - "Determines whether the current line needs an empty line correction - below." - [lines line index] - (cond - ; code block - (and (= (string/trim line) "```") - (< index (- (count lines) 1)) - (->> (take index lines) - (filter #(= (string/trim %) "```")) - count - even?) - (not (= (-> (nth lines (+ index 1)) - string/trim) ""))) - true - - ; ATX heading block - (and (string/starts-with? (string/trim line) "#") - (< index (- (count lines) 1)) - (not (= (-> (nth lines (+ index 1)) - string/trim) ""))) - true - - ; everything else stays normal - :else false)) - - (defn- correct-block-separations "Corrects block separations and adds newlines above or below a block where needed." - [lines] - (->> lines - (map-indexed - (fn [index line] - (let [add-line-above? (needs-empty-line-above? lines line index) - add-line-below? (needs-empty-line-below? lines line index)] - (cond - ; If code block starts but there is no empty newline - ; above, let's fix that - (and add-line-above? - (not add-line-below?)) - (str \newline line) - - ; If the code block ends, but there is no empty newline - ; below, let's fix that. - (and add-line-below? - (not add-line-above?)) - (str line \newline) - - ; If the code block needs a newline both above and below, - ; let's fix that. - (and add-line-above? - add-line-below?) - (str \newline line \newline) - - ; otherwise is what it is - :else line)))))) + [correctors lines] + (let [above-correctors (:empty-line-above? correctors) + below-correctors (:empty-line-below? correctors)] + (map-indexed + (fn [index line] + (let [add-line-above? (some #(true? (% lines line index)) above-correctors) + add-line-below? (some #(true? (% lines line index)) below-correctors)] + (cond + ; If code block starts but there is no empty newline + ; above, let's fix that + (and add-line-above? + (not add-line-below?)) + (str \newline line) + + ; If the code block ends, but there is no empty newline + ; below, let's fix that. + (and add-line-below? + (not add-line-above?)) + (str line \newline) + + ; If the code block needs a newline both above and below, + ; let's fix that. + (and add-line-above? + add-line-below?) + (str \newline line \newline) + + ; otherwise is what it is + :else line))) + lines))) (defn- correct-markdown "Corrects invalid Markdown for the parser." - [markdown] + [markdown given-correctors] (let [lines (string/split-lines markdown)] (->> lines - correct-block-separations + (correct-block-separations (:block-separations given-correctors)) (string/join \newline)))) @@ -144,13 +91,13 @@ (defn- parse-block-with-known-parser "Parses a given `block` with a known `parser`." - [parser parsers block] + [parser given-parsers given-correctors block] (loop [block block renderers (:renderers parser)] (if (empty? renderers) block (let [renderer (first renderers)] - (recur (renderer block parsers) + (recur (renderer block given-parsers given-correctors) (drop 1 renderers)))))) @@ -158,9 +105,9 @@ "Parses the given `block` with all the parsers that do not have a matcher function, useful for any fallback parsing one might want to do." - [parsers block] + [given-parsers given-correctors block] (loop [block block - parsers (filter #(= nil (:matcher %)) parsers)] + parsers (filter #(= nil (:matcher %)) given-parsers)] (if (empty? parsers) block (recur (loop [block block @@ -168,7 +115,7 @@ (if (empty? renderers) block (let [renderer (first renderers)] - (recur (renderer block parsers) + (recur (renderer block parsers given-correctors) (drop 1 renderers))))) (drop 1 parsers))))) @@ -176,20 +123,20 @@ (defn- parse-blocks "Parses each individual Markdown block, given as `blocks`, with the list of `parsers`." - [blocks parsers] + [blocks given-parsers given-correctors] (for [block blocks] - (if-let [parser (find-parser-by-block parsers block)] + (if-let [parser (find-parser-by-block given-parsers block)] (->> (string/trim block) - (parse-block-with-known-parser parser parsers)) + (parse-block-with-known-parser parser given-parsers given-correctors)) (->> (string/trim block) - (parse-block-with-unknown-parsers parsers))))) + (parse-block-with-unknown-parsers given-parsers given-correctors))))) -(defn parse +(defn render "Parses given `markdown` with `parsers`." - [markdown parsers] - (let [blocks (-> (correct-markdown markdown) + [markdown given-parsers given-correctors] + (let [blocks (-> (correct-markdown markdown given-correctors) (string/split #"\n\n") stitch-code-blocks) - parsed-blocks (parse-blocks blocks parsers)] + parsed-blocks (parse-blocks blocks given-parsers given-correctors)] (string/join "\n\n" parsed-blocks))) diff --git a/src/clarktown/matchers/code_block.clj b/src/clarktown/matchers/code_block.clj new file mode 100644 index 0000000..655c951 --- /dev/null +++ b/src/clarktown/matchers/code_block.clj @@ -0,0 +1,10 @@ +(ns clarktown.matchers.code-block + (:require + [clojure.string :as string])) + + +(defn match? + "Determines whether we're dealing with a code block." + [block] + (and (string/starts-with? block "```") + (string/ends-with? block "```")))
\ No newline at end of file diff --git a/src/clarktown/parsers/empty_block.clj b/src/clarktown/matchers/empty_block.clj index 0ed5a08..cc7b7f4 100644 --- a/src/clarktown/parsers/empty_block.clj +++ b/src/clarktown/matchers/empty_block.clj @@ -1,17 +1,11 @@ -(ns clarktown.parsers.empty-block +(ns clarktown.matchers.empty-block (:require [clojure.string :as string])) -(defn is? +(defn match? "Determines if the current block is an empty block or not." [block] (-> (string/replace block #"\n" "") string/trim - string/blank?)) - - -(defn render - "Renders an empty block." - [_ _] - "") + string/blank?))
\ No newline at end of file diff --git a/src/clarktown/matchers/heading_block.clj b/src/clarktown/matchers/heading_block.clj new file mode 100644 index 0000000..2295f26 --- /dev/null +++ b/src/clarktown/matchers/heading_block.clj @@ -0,0 +1,29 @@ +(ns clarktown.matchers.heading-block + (:require + [clojure.string :as string])) + + +(defn is-atx-heading? + "Determines whether the given block is a atx 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 match? + "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 diff --git a/src/clarktown/matchers/horizontal_line_block.clj b/src/clarktown/matchers/horizontal_line_block.clj new file mode 100644 index 0000000..a2dde6e --- /dev/null +++ b/src/clarktown/matchers/horizontal_line_block.clj @@ -0,0 +1,10 @@ +(ns clarktown.matchers.horizontal-line-block + (:require + [clojure.string :as string])) + + +(defn match? + "Determines whether the given block is a horizontal line block." + [block] + (or (= "***" (string/trim block)) + (= "---" (string/trim block))))
\ No newline at end of file diff --git a/src/clarktown/matchers/list_block.clj b/src/clarktown/matchers/list_block.clj new file mode 100644 index 0000000..a37b06e --- /dev/null +++ b/src/clarktown/matchers/list_block.clj @@ -0,0 +1,10 @@ +(ns clarktown.matchers.list-block + (:require + [clojure.string :as string])) + + +(defn match? + "Determines whether we're dealing with a list block or not." + [block] + (->> (string/trim block) + (re-matches #"(?s)^(\d\.\s|\*{1}\s|\-{1}\s).*$")))
\ No newline at end of file diff --git a/src/clarktown/matchers/quote_block.clj b/src/clarktown/matchers/quote_block.clj new file mode 100644 index 0000000..230b561 --- /dev/null +++ b/src/clarktown/matchers/quote_block.clj @@ -0,0 +1,11 @@ +(ns clarktown.matchers.quote-block + (:require + [clojure.string :as string])) + + +(defn match? + "Determines whether the given block is a quote block." + [block] + (-> (string/replace block #"\n" "") + string/trim + (string/starts-with? ">")))
\ No newline at end of file diff --git a/src/clarktown/parsers.clj b/src/clarktown/parsers.clj index cd909b3..77c9794 100644 --- a/src/clarktown/parsers.clj +++ b/src/clarktown/parsers.clj @@ -1,45 +1,95 @@ (ns clarktown.parsers (:require - [clarktown.parsers.bold :as bold] - [clarktown.parsers.italic :as italic] - [clarktown.parsers.inline-code :as inline-code] - [clarktown.parsers.strikethrough :as strikethrough] - [clarktown.parsers.link-and-image :as link-and-image] - [clarktown.parsers.empty-block :as empty-block] - [clarktown.parsers.horizontal-line-block :as horizontal-line-block] - [clarktown.parsers.quote-block :as quote-block] - [clarktown.parsers.heading-block :as heading-block] - [clarktown.parsers.code-block :as code-block] - [clarktown.parsers.list-block :as list-block] - [clarktown.parsers.paragraph-block :as paragraph-block])) - - -(def parsers - [{:matcher empty-block/is? - :renderers [empty-block/render]} - {:matcher horizontal-line-block/is? - :renderers [horizontal-line-block/render]} - {:matcher heading-block/is? - :renderers [link-and-image/render - bold/render - italic/render - inline-code/render - strikethrough/render - heading-block/render]} - {:matcher quote-block/is? - :renderers [quote-block/render]} - {:matcher code-block/is? - :renderers [code-block/render]} - {:matcher list-block/is? - :renderers [link-and-image/render - bold/render - italic/render - inline-code/render - strikethrough/render - list-block/render]} - {:renderers [link-and-image/render - bold/render - italic/render - inline-code/render - strikethrough/render - paragraph-block/render]}]) + [clarktown.matchers.empty-block] + [clarktown.renderers.empty-block] + [clarktown.matchers.horizontal-line-block] + [clarktown.renderers.horizontal-line-block] + [clarktown.matchers.heading-block] + [clarktown.renderers.heading-block] + [clarktown.matchers.quote-block] + [clarktown.renderers.quote-block] + [clarktown.matchers.code-block] + [clarktown.renderers.code-block] + [clarktown.matchers.list-block] + [clarktown.renderers.list-block] + [clarktown.renderers.paragraph-block] + [clarktown.renderers.link-and-image] + [clarktown.renderers.bold] + [clarktown.renderers.italic] + [clarktown.renderers.inline-code] + [clarktown.renderers.strikethrough])) + + +(def + ^{:doc "Detects, parses and renders a empty block."} + empty-block-parser + {:matcher clarktown.matchers.empty-block/match? + :renderers [clarktown.renderers.empty-block/render]}) + + +(def + ^{:doc "Detects, parses and renders a horizontal line block."} + horizontal-line-block-parser + {:matcher clarktown.matchers.horizontal-line-block/match? + :renderers [clarktown.renderers.horizontal-line-block/render]}) + + +(def + ^{:doc "Detects, parses and renders a heading block."} + heading-block-parser + {:matcher clarktown.matchers.heading-block/match? + :renderers [clarktown.renderers.link-and-image/render + clarktown.renderers.bold/render + clarktown.renderers.italic/render + clarktown.renderers.inline-code/render + clarktown.renderers.strikethrough/render + clarktown.renderers.heading-block/render]}) + + +(def + ^{:doc "Detects, parses and renders a quote block."} + quote-block-parser + {:matcher clarktown.matchers.quote-block/match? + :renderers [clarktown.renderers.quote-block/render]}) + + +(def + ^{:doc "Detects, parses and renders a code block."} + code-block-parser + {:matcher clarktown.matchers.code-block/match? + :renderers [clarktown.renderers.code-block/render]}) + + +(def + ^{:doc "Detects, parses and renders a list block."} + list-block-parser + {:matcher clarktown.matchers.list-block/match? + :renderers [clarktown.renderers.link-and-image/render + clarktown.renderers.bold/render + clarktown.renderers.italic/render + clarktown.renderers.inline-code/render + clarktown.renderers.strikethrough/render + clarktown.renderers.list-block/render]}) + + +(def + ^{:doc "Parses and renders a quote block."} + paragraph-block-parser + {:renderers [clarktown.renderers.link-and-image/render + clarktown.renderers.bold/render + clarktown.renderers.italic/render + clarktown.renderers.inline-code/render + clarktown.renderers.strikethrough/render + clarktown.renderers.paragraph-block/render]}) + + +(def + ^{:doc "A set of default parsers."} + default-parsers + [empty-block-parser + horizontal-line-block-parser + heading-block-parser + quote-block-parser + code-block-parser + list-block-parser + paragraph-block-parser]) diff --git a/src/clarktown/parsers/horizontal_line_block.clj b/src/clarktown/parsers/horizontal_line_block.clj deleted file mode 100644 index b1d1e05..0000000 --- a/src/clarktown/parsers/horizontal_line_block.clj +++ /dev/null @@ -1,16 +0,0 @@ -(ns clarktown.parsers.horizontal-line-block - (:require - [clojure.string :as string])) - - -(defn is? - "Determines whether the given block is a horizontal line block." - [block] - (or (= "***" (string/trim block)) - (= "---" (string/trim block)))) - - -(defn render - "Renders the horizontal line block." - [_ _] - "<hr>") diff --git a/src/clarktown/parsers/bold.clj b/src/clarktown/renderers/bold.clj index 79579bf..64d6137 100644 --- a/src/clarktown/parsers/bold.clj +++ b/src/clarktown/renderers/bold.clj @@ -1,11 +1,11 @@ -(ns clarktown.parsers.bold +(ns clarktown.renderers.bold (:require [clojure.string :as string])) (defn render "Renders all occurring bold text as bold." - [block _] + [block _ _] (loop [block block matches (-> (re-seq #"(\*{2}|\_{2})[^\*|\_](.*?)[^\*|\_](\*{2}|\_{2})" block) distinct)] diff --git a/src/clarktown/parsers/code_block.clj b/src/clarktown/renderers/code_block.clj index c6ecfea..184e90e 100644 --- a/src/clarktown/parsers/code_block.clj +++ b/src/clarktown/renderers/code_block.clj @@ -1,18 +1,11 @@ -(ns clarktown.parsers.code-block +(ns clarktown.renderers.code-block (:require [clojure.string :as string])) -(defn is? - "Determines whether we're dealing with a code block." - [block] - (and (string/starts-with? block "```") - (string/ends-with? block "```"))) - - (defn render "Renders the code block." - [block _] + [block _ _] (let [language (->> block (re-find #"\`\`\`(\w+)") second) diff --git a/src/clarktown/renderers/empty_block.clj b/src/clarktown/renderers/empty_block.clj new file mode 100644 index 0000000..84df1fb --- /dev/null +++ b/src/clarktown/renderers/empty_block.clj @@ -0,0 +1,7 @@ +(ns clarktown.renderers.empty-block) + + +(defn render + "Renders an empty block." + [_ _ _] + "") diff --git a/src/clarktown/parsers/heading_block.clj b/src/clarktown/renderers/heading_block.clj index def2394..4da9bda 100644 --- a/src/clarktown/parsers/heading_block.clj +++ b/src/clarktown/renderers/heading_block.clj @@ -1,35 +1,10 @@ -(ns clarktown.parsers.heading-block +(ns clarktown.renderers.heading-block (:require - [clojure.string :as string])) + [clojure.string :as string] + [clarktown.matchers.heading-block :as matcher])) -(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." - [block] - (or (is-hashbang-heading? block) - (is-settext-heading? block))) - - -(defn render-hashbang-heading +(defn render-atx-heading "Renders the hashbang heading block." [block] (let [single-line-block (-> (string/replace block #"\n" "") @@ -61,12 +36,9 @@ (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) + [block _ _] + (if (matcher/is-atx-heading? block) + (render-atx-heading block) (render-settext-heading block))) diff --git a/src/clarktown/renderers/horizontal_line_block.clj b/src/clarktown/renderers/horizontal_line_block.clj new file mode 100644 index 0000000..14e5d8a --- /dev/null +++ b/src/clarktown/renderers/horizontal_line_block.clj @@ -0,0 +1,7 @@ +(ns clarktown.renderers.horizontal-line-block) + + +(defn render + "Renders the horizontal line block." + [_ _ _] + "<hr>") diff --git a/src/clarktown/parsers/inline_code.clj b/src/clarktown/renderers/inline_code.clj index 1de73bf..e8c298f 100644 --- a/src/clarktown/parsers/inline_code.clj +++ b/src/clarktown/renderers/inline_code.clj @@ -1,11 +1,11 @@ -(ns clarktown.parsers.inline-code +(ns clarktown.renderers.inline-code (:require [clojure.string :as string])) (defn render "Renders all occurring inline code." - [block _] + [block _ _] (loop [block block matches (-> (re-seq #"\`.*?\`" block) distinct)] diff --git a/src/clarktown/parsers/italic.clj b/src/clarktown/renderers/italic.clj index 915a017..970364e 100644 --- a/src/clarktown/parsers/italic.clj +++ b/src/clarktown/renderers/italic.clj @@ -1,11 +1,11 @@ -(ns clarktown.parsers.italic +(ns clarktown.renderers.italic (:require [clojure.string :as string])) (defn render "Renders all occurring italic text as italic." - [block _] + [block _ _] (loop [block block matches (-> (re-seq #"(\*{1,}?|\_{1,}?)(.*?)(\*{1,}?|\_{1,}?)" block) distinct)] diff --git a/src/clarktown/parsers/link_and_image.clj b/src/clarktown/renderers/link_and_image.clj index 3be2efe..e61503e 100644 --- a/src/clarktown/parsers/link_and_image.clj +++ b/src/clarktown/renderers/link_and_image.clj @@ -1,4 +1,4 @@ -(ns clarktown.parsers.link-and-image +(ns clarktown.renderers.link-and-image (:require [clojure.string :as string])) @@ -11,7 +11,7 @@ (defn render "Renders all occurring links and images." - [block _] + [block _ _] (loop [block block matches (-> (re-seq #"\!?\[([a-zA-Z0-9\-\.\,]+( [a-zA-Z0-9\-\.\,]+)*)\]\((.*?)\)" block) distinct)] diff --git a/src/clarktown/parsers/list_block.clj b/src/clarktown/renderers/list_block.clj index 52f955f..0c07aaa 100644 --- a/src/clarktown/parsers/list_block.clj +++ b/src/clarktown/renderers/list_block.clj @@ -1,15 +1,8 @@ -(ns clarktown.parsers.list-block +(ns clarktown.renderers.list-block (:require [clojure.string :as string])) -(defn is? - "Determines whether we're dealing with a list block or not." - [block] - (->> (string/trim block) - (re-matches #"(?s)^(\d\.\s|\*{1}\s|\-{1}\s).*$"))) - - (defn string->indent-n "Returns the indentation count from left of `str`, which must be in spaces and not tabs." @@ -27,8 +20,7 @@ (fn [line] {:id (random-uuid) :indent-n (string->indent-n line) - :value (-> line - string/trim)})))) + :value (string/trim line)})))) (defn find-parent-id @@ -64,13 +56,14 @@ (->> items (mapv (fn [i] - (if (= (:id i) (:parent item)) - (if (:items i) - (assoc i :items (concat (:items i) [item])) - (assoc i :items [item])) - (if (:items i) - (assoc i :items (add-to-parent (:items i) item)) - i)))))) + (let [new-item (select-keys item [:id :value])] + (if (= (:id i) (:parent item)) + (if (:items i) + (assoc i :items (concat (:items i) [new-item])) + (assoc i :items [new-item])) + (if (:items i) + (assoc i :items (add-to-parent (:items i) item)) + i))))))) (defn compose-item-tree @@ -83,8 +76,7 @@ result (let [item (first items) parent (:parent item) - new-item {:id (:id item) - :value (:value item)}] + new-item (select-keys item [:id :value])] (recur (if parent (add-to-parent result item) (concat result [new-item])) @@ -111,6 +103,7 @@ (string/starts-with? (:value inner-item) "-") (-> (string/replace-first (:value inner-item) "-" "") string/trim) + ; ordered list :else (-> (string/replace-first (:value inner-item) #"\d\." "") string/trim))] @@ -122,6 +115,6 @@ (defn render "Renders the list block" - [block _] + [block _ _] (-> (compose-item-tree block) - (render-items)))
\ No newline at end of file + (render-items))) diff --git a/src/clarktown/parsers/paragraph_block.clj b/src/clarktown/renderers/paragraph_block.clj index 3a7c633..0ab3788 100644 --- a/src/clarktown/parsers/paragraph_block.clj +++ b/src/clarktown/renderers/paragraph_block.clj @@ -1,9 +1,9 @@ -(ns clarktown.parsers.paragraph-block +(ns clarktown.renderers.paragraph-block (:require [clojure.string :as string])) (defn render "Renders the paragraph block." - [block _] + [block _ _] (str "<p>" (string/trim block) "</p>")) diff --git a/src/clarktown/parsers/quote_block.clj b/src/clarktown/renderers/quote_block.clj index ff2dda8..1a302f9 100644 --- a/src/clarktown/parsers/quote_block.clj +++ b/src/clarktown/renderers/quote_block.clj @@ -1,24 +1,16 @@ -(ns clarktown.parsers.quote-block +(ns clarktown.renderers.quote-block (:require [clojure.string :as string] - [clarktown.parser :as parser])) - - -(defn is? - "Determines whether the given block is a quote block." - [block] - (-> (string/replace block #"\n" "") - string/trim - (string/starts-with? ">"))) + [clarktown.engine :as engine])) (defn render "Renders a quote block." - [block parsers] + [block parsers correctors] (let [matches (re-seq #">.*" block) blocks (->> (for [match matches] (-> (subs match 1) string/trim)) (string/join "\n")) - block (parser/parse blocks parsers)] + block (engine/render blocks parsers correctors)] (str "<blockquote>" block "</blockquote>"))) diff --git a/src/clarktown/parsers/strikethrough.clj b/src/clarktown/renderers/strikethrough.clj index 6f03152..133be47 100644 --- a/src/clarktown/parsers/strikethrough.clj +++ b/src/clarktown/renderers/strikethrough.clj @@ -1,11 +1,11 @@ -(ns clarktown.parsers.strikethrough +(ns clarktown.renderers.strikethrough (:require [clojure.string :as string])) (defn render "Renders all occurring strikethrough text." - [block _] + [block _ _] (loop [block block matches (-> (re-seq #"~~.*?~~" block) distinct)] |
