From b97891cc45b9a087a372767e25aa36788bc979bf Mon Sep 17 00:00:00 2001 From: Asko Nõmm Date: Sun, 8 May 2022 12:23:35 +0300 Subject: Naming improvements, code block matcher test --- src/clarktown/correctors.clj | 14 +++++----- src/clarktown/correctors/atx_heading_block.clj | 4 +-- src/clarktown/correctors/code_block.clj | 37 -------------------------- src/clarktown/correctors/fenced_code_block.clj | 37 ++++++++++++++++++++++++++ src/clarktown/correctors/list_block.clj | 4 +-- src/clarktown/engine.clj | 4 +-- src/clarktown/matchers/code_block.clj | 10 ------- src/clarktown/matchers/fenced_code_block.clj | 10 +++++++ src/clarktown/parsers.clj | 14 +++++----- src/clarktown/renderers/code_block.clj | 23 ---------------- src/clarktown/renderers/fenced_code_block.clj | 23 ++++++++++++++++ 11 files changed, 90 insertions(+), 90 deletions(-) delete mode 100644 src/clarktown/correctors/code_block.clj create mode 100644 src/clarktown/correctors/fenced_code_block.clj delete mode 100644 src/clarktown/matchers/code_block.clj create mode 100644 src/clarktown/matchers/fenced_code_block.clj delete mode 100644 src/clarktown/renderers/code_block.clj create mode 100644 src/clarktown/renderers/fenced_code_block.clj (limited to 'src/clarktown') diff --git a/src/clarktown/correctors.clj b/src/clarktown/correctors.clj index 1a61f2d..6b7cc1c 100644 --- a/src/clarktown/correctors.clj +++ b/src/clarktown/correctors.clj @@ -1,6 +1,6 @@ (ns clarktown.correctors (:require - [clarktown.correctors.code-block :as code-block] + [clarktown.correctors.fenced-code-block :as fenced-code-block] [clarktown.correctors.atx-heading-block :as atx-heading-block] [clarktown.correctors.list-block :as list-block])) @@ -9,13 +9,13 @@ ^{:doc "The default block separation correctors."} default-block-separation-correctors {:newline-above - [code-block/empty-line-above? - atx-heading-block/empty-line-above? - list-block/empty-line-above?] + [fenced-code-block/newline-above? + atx-heading-block/newline-above? + list-block/newline-above?] :newline-below - [code-block/empty-line-below? - atx-heading-block/empty-line-below? - list-block/empty-line-below?]}) + [fenced-code-block/newline-below? + atx-heading-block/newline-below? + list-block/newline-below?]}) (def diff --git a/src/clarktown/correctors/atx_heading_block.clj b/src/clarktown/correctors/atx_heading_block.clj index a2a948b..6668700 100644 --- a/src/clarktown/correctors/atx_heading_block.clj +++ b/src/clarktown/correctors/atx_heading_block.clj @@ -13,7 +13,7 @@ odd?)) -(defn empty-line-above? +(defn newline-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 @@ -27,7 +27,7 @@ (not (in-code-block? lines index)))) -(defn empty-line-below? +(defn newline-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 diff --git a/src/clarktown/correctors/code_block.clj b/src/clarktown/correctors/code_block.clj deleted file mode 100644 index 73989fe..0000000 --- a/src/clarktown/correctors/code_block.clj +++ /dev/null @@ -1,37 +0,0 @@ -(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/correctors/fenced_code_block.clj b/src/clarktown/correctors/fenced_code_block.clj new file mode 100644 index 0000000..77bc023 --- /dev/null +++ b/src/clarktown/correctors/fenced_code_block.clj @@ -0,0 +1,37 @@ +(ns clarktown.correctors.fenced-code-block + (:require + [clojure.string :as string])) + + +(defn newline-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 newline-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/correctors/list_block.clj b/src/clarktown/correctors/list_block.clj index cf84bb1..4525ff7 100644 --- a/src/clarktown/correctors/list_block.clj +++ b/src/clarktown/correctors/list_block.clj @@ -4,7 +4,7 @@ [clarktown.matchers.list-block :as matcher])) -(defn empty-line-above? +(defn newline-above? "Determines whether there's a need for an empty new line above the `line` at the current `index`. In the list block case that's when the above line is not a newline and is not @@ -20,7 +20,7 @@ "")))) -(defn empty-line-below? +(defn newline-below? "Determines whether there's a need for an empty new line above the `line` at the current `index`. In the list block case that's when the below line is not a newline and is not diff --git a/src/clarktown/engine.clj b/src/clarktown/engine.clj index eaa0fb0..5eb22d5 100644 --- a/src/clarktown/engine.clj +++ b/src/clarktown/engine.clj @@ -3,7 +3,7 @@ [clojure.string :as string])) -(defn- stitch-code-blocks +(defn- stitch-fenced-code-blocks "Since code blocks can span multiple blocks (a block is separated by two line breaks from another block) , we need to stitch them together into one block in order for a block parser to be able to do anything @@ -144,6 +144,6 @@ [markdown given-parsers given-correctors] (let [blocks (-> (correct-markdown markdown given-correctors) (string/split #"\n\n") - stitch-code-blocks) + stitch-fenced-code-blocks) 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 deleted file mode 100644 index 655c951..0000000 --- a/src/clarktown/matchers/code_block.clj +++ /dev/null @@ -1,10 +0,0 @@ -(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/matchers/fenced_code_block.clj b/src/clarktown/matchers/fenced_code_block.clj new file mode 100644 index 0000000..9bbcdca --- /dev/null +++ b/src/clarktown/matchers/fenced_code_block.clj @@ -0,0 +1,10 @@ +(ns clarktown.matchers.fenced-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 "```"))) diff --git a/src/clarktown/parsers.clj b/src/clarktown/parsers.clj index 77c9794..0d3b019 100644 --- a/src/clarktown/parsers.clj +++ b/src/clarktown/parsers.clj @@ -8,8 +8,8 @@ [clarktown.renderers.heading-block] [clarktown.matchers.quote-block] [clarktown.renderers.quote-block] - [clarktown.matchers.code-block] - [clarktown.renderers.code-block] + [clarktown.matchers.fenced-code-block] + [clarktown.renderers.fenced-code-block] [clarktown.matchers.list-block] [clarktown.renderers.list-block] [clarktown.renderers.paragraph-block] @@ -54,10 +54,10 @@ (def - ^{:doc "Detects, parses and renders a code block."} - code-block-parser - {:matcher clarktown.matchers.code-block/match? - :renderers [clarktown.renderers.code-block/render]}) + ^{:doc "Detects, parses and renders a fenced code block."} + fenced-code-block-parser + {:matcher clarktown.matchers.fenced-code-block/match? + :renderers [clarktown.renderers.fenced-code-block/render]}) (def @@ -90,6 +90,6 @@ horizontal-line-block-parser heading-block-parser quote-block-parser - code-block-parser + fenced-code-block-parser list-block-parser paragraph-block-parser]) diff --git a/src/clarktown/renderers/code_block.clj b/src/clarktown/renderers/code_block.clj deleted file mode 100644 index 184e90e..0000000 --- a/src/clarktown/renderers/code_block.clj +++ /dev/null @@ -1,23 +0,0 @@ -(ns clarktown.renderers.code-block - (:require - [clojure.string :as string])) - - -(defn render - "Renders the code block." - [block _ _] - (let [language (->> block - (re-find #"\`\`\`(\w+)") - second) - lines (string/split-lines block) - block* (->> (next lines) - (take (- (count lines) 2)) - (string/join \newline)) - code (-> block* - (string/replace #"&" "&") - (string/replace #"<" "<") - (string/replace #">" ">") - string/trim)] - (if language - (str "
" code "
") - (str "
" code "
")))) diff --git a/src/clarktown/renderers/fenced_code_block.clj b/src/clarktown/renderers/fenced_code_block.clj new file mode 100644 index 0000000..905c061 --- /dev/null +++ b/src/clarktown/renderers/fenced_code_block.clj @@ -0,0 +1,23 @@ +(ns clarktown.renderers.fenced-code-block + (:require + [clojure.string :as string])) + + +(defn render + "Renders the code block." + [block _ _] + (let [language (->> block + (re-find #"\`\`\`(\w+)") + second) + lines (string/split-lines block) + block* (->> (next lines) + (take (- (count lines) 2)) + (string/join \newline)) + code (-> block* + (string/replace #"&" "&") + (string/replace #"<" "<") + (string/replace #">" ">") + string/trim)] + (if language + (str "
" code "
") + (str "
" code "
")))) -- cgit v1.2.3