From 77b065ba5cd57d4a02b0cae68d74412d1234554c Mon Sep 17 00:00:00 2001 From: Asko Nõmm Date: Fri, 22 Apr 2022 14:34:18 +0300 Subject: Make code more narrow --- src/clarktown/core.clj | 2 +- src/clarktown/engine.clj | 56 +++++++++++++++++++++++++----------------------- 2 files changed, 30 insertions(+), 28 deletions(-) (limited to 'src') diff --git a/src/clarktown/core.clj b/src/clarktown/core.clj index a624275..2a3e3a1 100644 --- a/src/clarktown/core.clj +++ b/src/clarktown/core.clj @@ -27,4 +27,4 @@ ([markdown given-parsers] (render markdown given-parsers correctors/default-correctors)) ([markdown given-parsers given-correctors] - (engine/render markdown given-parsers given-correctors))) \ No newline at end of file + (engine/render markdown given-parsers given-correctors))) diff --git a/src/clarktown/engine.clj b/src/clarktown/engine.clj index 17e5867..2dc9089 100644 --- a/src/clarktown/engine.clj +++ b/src/clarktown/engine.clj @@ -39,32 +39,34 @@ "Corrects block separations and adds newlines above or below a block where needed." [correctors lines] - (->> lines - (map-indexed - (fn [index line] - (let [add-line-above? (some #(true? (% lines line index)) (:empty-line-above? correctors)) - add-line-below? (some #(true? (% lines line index)) (:empty-line-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)))))) + (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 @@ -137,4 +139,4 @@ (string/split #"\n\n") stitch-code-blocks) parsed-blocks (parse-blocks blocks given-parsers given-correctors)] - (string/join "\n\n" parsed-blocks))) \ No newline at end of file + (string/join "\n\n" parsed-blocks))) -- cgit v1.2.3