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 ++++++++++++++ .../correctors/atx_heading_block_test.clj | 8 ++--- test/clarktown/correctors/code_block_test.clj | 37 ---------------------- .../correctors/fenced_code_block_test.clj | 37 ++++++++++++++++++++++ test/clarktown/correctors/list_block_test.clj | 20 ++++++------ test/clarktown/matchers/fenced_code_block_test.clj | 10 ++++++ test/clarktown/matchers/quote_block_test.clj | 2 +- test/clarktown/renderers/code_block_test.clj | 15 --------- .../clarktown/renderers/fenced_code_block_test.clj | 15 +++++++++ 19 files changed, 167 insertions(+), 157 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 delete mode 100644 test/clarktown/correctors/code_block_test.clj create mode 100644 test/clarktown/correctors/fenced_code_block_test.clj create mode 100644 test/clarktown/matchers/fenced_code_block_test.clj delete mode 100644 test/clarktown/renderers/code_block_test.clj create mode 100644 test/clarktown/renderers/fenced_code_block_test.clj 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 "
")))) diff --git a/test/clarktown/correctors/atx_heading_block_test.clj b/test/clarktown/correctors/atx_heading_block_test.clj index 3ece304..f363ad0 100644 --- a/test/clarktown/correctors/atx_heading_block_test.clj +++ b/test/clarktown/correctors/atx_heading_block_test.clj @@ -9,22 +9,22 @@ (let [line "# Hello" lines ["Some text goes here" line] index 1] - (is (true? (corrector/empty-line-above? lines line index))))) + (is (true? (corrector/newline-above? lines line index))))) (testing "No empty line above" (let [line "# Hello" lines ["Some text goes here" "\n" line] index 2] - (is (false? (corrector/empty-line-above? lines line index))))) + (is (false? (corrector/newline-above? lines line index))))) (testing "Empty line below" (let [line "# Hello" lines [line "Some text goes here"] index 0] - (is (true? (corrector/empty-line-below? lines line index))))) + (is (true? (corrector/newline-below? lines line index))))) (testing "No empty line below" (let [line "# Hello" lines [line "\n" "Some text goes here"] index 0] - (is (false? (corrector/empty-line-below? lines line index)))))) + (is (false? (corrector/newline-below? lines line index)))))) diff --git a/test/clarktown/correctors/code_block_test.clj b/test/clarktown/correctors/code_block_test.clj deleted file mode 100644 index 16711da..0000000 --- a/test/clarktown/correctors/code_block_test.clj +++ /dev/null @@ -1,37 +0,0 @@ -(ns clarktown.correctors.code-block-test - (:require - [clojure.test :refer [deftest testing is]] - [clarktown.correctors.code-block :as corrector])) - - -(deftest code-block-corrector - (testing "Empty line above" - (let [line "```clojure" - lines ["Some text goes here" line "some code here" "```"] - index 1] - (is (true? (corrector/empty-line-above? lines line index))))) - - (testing "No empty line above" - (let [line "```" - lines ["Some text goes here" "\n" line "some code" "```"] - index 2] - (is (false? (corrector/empty-line-above? lines line index))))) - - (testing "Empty line below" - (let [line "```" - lines ["Some text goes here" line "some code" line "some text"] - index 3] - (is (true? (corrector/empty-line-below? lines line index))))) - - (testing "No empty line below" - (let [line "```" - lines ["Some text goes here" line "some code" line "\n" "some text"] - index 3] - (is (false? (corrector/empty-line-below? lines line index))))) - - (testing "No empty line below when ending with code block" - (let [line "```" - lines ["Some text goes here" line "some code" line] - index 3] - (is (false? (corrector/empty-line-below? lines line index)))))) - diff --git a/test/clarktown/correctors/fenced_code_block_test.clj b/test/clarktown/correctors/fenced_code_block_test.clj new file mode 100644 index 0000000..a0f9a41 --- /dev/null +++ b/test/clarktown/correctors/fenced_code_block_test.clj @@ -0,0 +1,37 @@ +(ns clarktown.correctors.fenced-code-block-test + (:require + [clojure.test :refer [deftest testing is]] + [clarktown.correctors.fenced-code-block :as corrector])) + + +(deftest fenced-code-block-corrector-test + (testing "Empty line above" + (let [line "```clojure" + lines ["Some text goes here" line "some code here" "```"] + index 1] + (is (true? (corrector/newline-above? lines line index))))) + + (testing "No empty line above" + (let [line "```" + lines ["Some text goes here" "\n" line "some code" "```"] + index 2] + (is (false? (corrector/newline-above? lines line index))))) + + (testing "Empty line below" + (let [line "```" + lines ["Some text goes here" line "some code" line "some text"] + index 3] + (is (true? (corrector/newline-below? lines line index))))) + + (testing "No empty line below" + (let [line "```" + lines ["Some text goes here" line "some code" line "\n" "some text"] + index 3] + (is (false? (corrector/newline-below? lines line index))))) + + (testing "No empty line below when ending with code block" + (let [line "```" + lines ["Some text goes here" line "some code" line] + index 3] + (is (false? (corrector/newline-below? lines line index)))))) + diff --git a/test/clarktown/correctors/list_block_test.clj b/test/clarktown/correctors/list_block_test.clj index d435caf..54ff94e 100644 --- a/test/clarktown/correctors/list_block_test.clj +++ b/test/clarktown/correctors/list_block_test.clj @@ -9,58 +9,58 @@ (let [line "1. Hello" lines ["Some text goes here" line] index 1] - (is (true? (corrector/empty-line-above? lines line index))))) + (is (true? (corrector/newline-above? lines line index))))) (testing "Empty line above II" (let [line "* Hello" lines ["Some text goes here" line] index 1] - (is (true? (corrector/empty-line-above? lines line index))))) + (is (true? (corrector/newline-above? lines line index))))) (testing "No empty line above" (let [line "1. Hello" lines ["Some text goes here" "\n" line] index 2] - (is (false? (corrector/empty-line-above? lines line index))))) + (is (false? (corrector/newline-above? lines line index))))) (testing "No empty line above II" (let [line "* Hello" lines ["Some text goes here" "\n" line] index 2] - (is (false? (corrector/empty-line-above? lines line index))))) + (is (false? (corrector/newline-above? lines line index))))) (testing "No empty line above III" (let [line "* Hello" lines ["Some text here" "* Asd" line] index 2] - (is (false? (corrector/empty-line-above? lines line index))))) + (is (false? (corrector/newline-above? lines line index))))) (testing "Empty line below" (let [line "1. Hello" lines [line "Some text goes here"] index 0] - (is (true? (corrector/empty-line-below? lines line index))))) + (is (true? (corrector/newline-below? lines line index))))) (testing "Empty line below II" (let [line "* Hello" lines [line "Some text goes here"] index 0] - (is (true? (corrector/empty-line-below? lines line index))))) + (is (true? (corrector/newline-below? lines line index))))) (testing "No empty line below" (let [line "1. Hello" lines [line "\n" "Some text goes here"] index 0] - (is (false? (corrector/empty-line-below? lines line index))))) + (is (false? (corrector/newline-below? lines line index))))) (testing "No empty line below II" (let [line "* Hello" lines [line "\n" "Some text goes here"] index 0] - (is (false? (corrector/empty-line-below? lines line index))))) + (is (false? (corrector/newline-below? lines line index))))) (testing "No empty line below III" (let [line "* Hello" lines [line "* Asd" "Some text goes here"] index 0] - (is (false? (corrector/empty-line-below? lines line index)))))) + (is (false? (corrector/newline-below? lines line index)))))) diff --git a/test/clarktown/matchers/fenced_code_block_test.clj b/test/clarktown/matchers/fenced_code_block_test.clj new file mode 100644 index 0000000..5961c6d --- /dev/null +++ b/test/clarktown/matchers/fenced_code_block_test.clj @@ -0,0 +1,10 @@ +(ns clarktown.matchers.fenced-code-block-test + (:require + [clojure.test :refer [deftest testing is]] + [clarktown.matchers.fenced-code-block :as code-block])) + + +(deftest fenced-code-block-matcher-test + (testing "Checking a fenced code block" + (is (true? (code-block/match? "```\nblabla```"))) + (is (true? (code-block/match? "```language-spec\nblabla```"))))) diff --git a/test/clarktown/matchers/quote_block_test.clj b/test/clarktown/matchers/quote_block_test.clj index 05288d6..d4c9067 100644 --- a/test/clarktown/matchers/quote_block_test.clj +++ b/test/clarktown/matchers/quote_block_test.clj @@ -4,7 +4,7 @@ [clarktown.matchers.quote-block :as quote-block])) -(deftest quote-block-block-matcher-test +(deftest quote-block-matcher-test (testing "Checking a quote block" (is (true? (quote-block/match? "> Test"))) (is (true? (quote-block/match? " > Test"))) diff --git a/test/clarktown/renderers/code_block_test.clj b/test/clarktown/renderers/code_block_test.clj deleted file mode 100644 index c5779be..0000000 --- a/test/clarktown/renderers/code_block_test.clj +++ /dev/null @@ -1,15 +0,0 @@ -(ns clarktown.renderers.code-block-test - (:require - [clojure.test :refer [deftest testing is]] - [clojure.java.io :as io] - [clarktown.renderers.code-block :as code-block])) - - -(deftest code-block-renderer-test - (testing "Code block with language specification" - (is (= (slurp (io/file (io/resource "test/parsers/code_block_result.html"))) - (code-block/render (slurp (io/file (io/resource "test/parsers/code_block.md"))) nil nil)))) - - (testing "Code block with NO language specification" - (is (= (slurp (io/file (io/resource "test/parsers/code_block_no_language_result.html"))) - (code-block/render (slurp (io/file (io/resource "test/parsers/code_block_no_language.md"))) nil nil))))) \ No newline at end of file diff --git a/test/clarktown/renderers/fenced_code_block_test.clj b/test/clarktown/renderers/fenced_code_block_test.clj new file mode 100644 index 0000000..9a7f4cc --- /dev/null +++ b/test/clarktown/renderers/fenced_code_block_test.clj @@ -0,0 +1,15 @@ +(ns clarktown.renderers.fenced-code-block-test + (:require + [clojure.test :refer [deftest testing is]] + [clojure.java.io :as io] + [clarktown.renderers.fenced-code-block :as code-block])) + + +(deftest fenced-code-block-renderer-test + (testing "Fenced code block with language specification" + (is (= (slurp (io/file (io/resource "test/parsers/code_block_result.html"))) + (code-block/render (slurp (io/file (io/resource "test/parsers/code_block.md"))) nil nil)))) + + (testing "Fenced code block with NO language specification" + (is (= (slurp (io/file (io/resource "test/parsers/code_block_no_language_result.html"))) + (code-block/render (slurp (io/file (io/resource "test/parsers/code_block_no_language.md"))) nil nil))))) -- cgit v1.2.3