summaryrefslogtreecommitdiff
path: root/src/clarktown
diff options
context:
space:
mode:
authorAsko Nõmm <asko@bien.ee>2022-04-21 19:13:02 +0300
committerAsko Nõmm <asko@bien.ee>2022-04-21 19:13:02 +0300
commit04821d8be5d773153718948454c864495704f67b (patch)
tree2b4bf3433ca40f30d0dbd282895e70021a769eda /src/clarktown
parenta178a0b1867cb5194a1a2cd4f6e01ac261bfa4b7 (diff)
Send correctors to each render as well
Diffstat (limited to 'src/clarktown')
-rw-r--r--src/clarktown/core.clj143
-rw-r--r--src/clarktown/correctors.clj8
-rw-r--r--src/clarktown/engine.clj140
-rw-r--r--src/clarktown/renderers/bold.clj2
-rw-r--r--src/clarktown/renderers/code_block.clj2
-rw-r--r--src/clarktown/renderers/empty_block.clj2
-rw-r--r--src/clarktown/renderers/heading_block.clj2
-rw-r--r--src/clarktown/renderers/horizontal_line_block.clj2
-rw-r--r--src/clarktown/renderers/inline_code.clj2
-rw-r--r--src/clarktown/renderers/italic.clj2
-rw-r--r--src/clarktown/renderers/link_and_image.clj2
-rw-r--r--src/clarktown/renderers/list_block.clj4
-rw-r--r--src/clarktown/renderers/paragraph_block.clj2
-rw-r--r--src/clarktown/renderers/quote_block.clj6
-rw-r--r--src/clarktown/renderers/strikethrough.clj2
15 files changed, 164 insertions, 157 deletions
diff --git a/src/clarktown/core.clj b/src/clarktown/core.clj
index cabd6e6..a624275 100644
--- a/src/clarktown/core.clj
+++ b/src/clarktown/core.clj
@@ -1,147 +1,10 @@
(ns clarktown.core
(:require
- [clojure.string :as string]
+ [clarktown.engine :as engine]
[clarktown.parsers :as parsers]
[clarktown.correctors :as correctors]))
-(defn- stitch-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
- with it."
- [blocks]
- (loop [stitched-blocks []
- code-block-started? false
- blocks blocks]
- (if (empty? blocks)
- stitched-blocks
- (let [block (first blocks)]
- (if (and (string/starts-with? (string/trim block) "```")
- (not (string/ends-with? (string/trim block) "```")))
- (recur (conj stitched-blocks block)
- true
- (drop 1 blocks))
- (if code-block-started?
- (let [last-block (last stitched-blocks)
- last-block-index (- (count stitched-blocks) 1)]
- (if (string/ends-with? (string/trim block) "```")
- (recur (assoc stitched-blocks last-block-index (str last-block "\n\n" block))
- false
- (drop 1 blocks))
- (recur (assoc stitched-blocks last-block-index (str last-block "\n\n" block))
- true
- (drop 1 blocks))))
- (recur (conj stitched-blocks block)
- false
- (drop 1 blocks))))))))
-
-
-(defn- correct-block-separations
- "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))))))
-
-
-(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))))
-
-
-(defn- find-parser-by-block
- "Find a parser from `parsers` that matches the given `block`."
- [parsers block]
- (->> parsers
- (filter
- (fn [{:keys [matcher]}]
- (when matcher
- (matcher block))))
- first))
-
-
-(defn- parse-block-with-known-parser
- "Parses a given `block` with a known `parser`."
- [parser parsers block]
- (loop [block block
- renderers (:renderers parser)]
- (if (empty? renderers)
- block
- (let [renderer (first renderers)]
- (recur (renderer block parsers)
- (drop 1 renderers))))))
-
-
-(defn- parse-block-with-unknown-parsers
- "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]
- (loop [block block
- parsers (filter #(= nil (:matcher %)) parsers)]
- (if (empty? parsers)
- block
- (recur (loop [block block
- renderers (:renderers (first parsers))]
- (if (empty? renderers)
- block
- (let [renderer (first renderers)]
- (recur (renderer block parsers)
- (drop 1 renderers)))))
- (drop 1 parsers)))))
-
-
-(defn- parse-blocks
- "Parses each individual Markdown block, given as `blocks`, with
- the list of `parsers`."
- [blocks parsers]
- (for [block blocks]
- (if-let [parser (find-parser-by-block parsers block)]
- (->> (string/trim block)
- (parse-block-with-known-parser parser parsers))
- (->> (string/trim block)
- (parse-block-with-unknown-parsers parsers)))))
-
-
-(defn parse
- "Parses given `markdown` with `parsers`."
- [markdown given-parsers given-correctors]
- (let [blocks (-> (correct-markdown markdown given-correctors)
- (string/split #"\n\n")
- stitch-code-blocks)
- parsed-blocks (parse-blocks blocks given-parsers)]
- (string/join "\n\n" parsed-blocks)))
-
-
(defn render
"Renders the given `markdown` into a consumable HTML form. Optionally,
a second argument can be passed that is made out of a vector of parsers.
@@ -155,7 +18,7 @@
argument, which is a given Markdown block.
An example parser:
- ```
+ ```clojure
{:matcher (fn [block] ...)
:renderers [(fn [block] ...) (fn [block] ...)]}
```"
@@ -164,4 +27,4 @@
([markdown given-parsers]
(render markdown given-parsers correctors/default-correctors))
([markdown given-parsers given-correctors]
- (parse markdown given-parsers given-correctors))) \ No newline at end of file
+ (engine/render markdown given-parsers given-correctors))) \ No newline at end of file
diff --git a/src/clarktown/correctors.clj b/src/clarktown/correctors.clj
index d97defa..14d82ca 100644
--- a/src/clarktown/correctors.clj
+++ b/src/clarktown/correctors.clj
@@ -4,7 +4,9 @@
[clarktown.correctors.atx-heading-block :as atx-heading-block]))
-(def default-block-separation-correctors
+(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?]
@@ -13,5 +15,7 @@
atx-heading-block/empty-line-below?]})
-(def default-correctors
+(def
+ ^{:doc "The default correctors."}
+ default-correctors
{:block-separations default-block-separation-correctors}) \ No newline at end of file
diff --git a/src/clarktown/engine.clj b/src/clarktown/engine.clj
new file mode 100644
index 0000000..17e5867
--- /dev/null
+++ b/src/clarktown/engine.clj
@@ -0,0 +1,140 @@
+(ns clarktown.engine
+ (:require
+ [clojure.string :as string]))
+
+
+(defn- stitch-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
+ with it."
+ [blocks]
+ (loop [stitched-blocks []
+ code-block-started? false
+ blocks blocks]
+ (if (empty? blocks)
+ stitched-blocks
+ (let [block (first blocks)]
+ (if (and (string/starts-with? (string/trim block) "```")
+ (not (string/ends-with? (string/trim block) "```")))
+ (recur (conj stitched-blocks block)
+ true
+ (drop 1 blocks))
+ (if code-block-started?
+ (let [last-block (last stitched-blocks)
+ last-block-index (- (count stitched-blocks) 1)]
+ (if (string/ends-with? (string/trim block) "```")
+ (recur (assoc stitched-blocks last-block-index (str last-block "\n\n" block))
+ false
+ (drop 1 blocks))
+ (recur (assoc stitched-blocks last-block-index (str last-block "\n\n" block))
+ true
+ (drop 1 blocks))))
+ (recur (conj stitched-blocks block)
+ false
+ (drop 1 blocks))))))))
+
+
+(defn- correct-block-separations
+ "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))))))
+
+
+(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))))
+
+
+(defn- find-parser-by-block
+ "Find a parser from `parsers` that matches the given `block`."
+ [parsers block]
+ (->> parsers
+ (filter
+ (fn [{:keys [matcher]}]
+ (when matcher
+ (matcher block))))
+ first))
+
+
+(defn- parse-block-with-known-parser
+ "Parses a given `block` with a known `parser`."
+ [parser given-parsers given-correctors block]
+ (loop [block block
+ renderers (:renderers parser)]
+ (if (empty? renderers)
+ block
+ (let [renderer (first renderers)]
+ (recur (renderer block given-parsers given-correctors)
+ (drop 1 renderers))))))
+
+
+(defn- parse-block-with-unknown-parsers
+ "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."
+ [given-parsers given-correctors block]
+ (loop [block block
+ parsers (filter #(= nil (:matcher %)) given-parsers)]
+ (if (empty? parsers)
+ block
+ (recur (loop [block block
+ renderers (:renderers (first parsers))]
+ (if (empty? renderers)
+ block
+ (let [renderer (first renderers)]
+ (recur (renderer block parsers given-correctors)
+ (drop 1 renderers)))))
+ (drop 1 parsers)))))
+
+
+(defn- parse-blocks
+ "Parses each individual Markdown block, given as `blocks`, with
+ the list of `parsers`."
+ [blocks given-parsers given-correctors]
+ (for [block blocks]
+ (if-let [parser (find-parser-by-block given-parsers block)]
+ (->> (string/trim block)
+ (parse-block-with-known-parser parser given-parsers given-correctors))
+ (->> (string/trim block)
+ (parse-block-with-unknown-parsers given-parsers given-correctors)))))
+
+
+(defn render
+ "Parses given `markdown` with `parsers`."
+ [markdown given-parsers given-correctors]
+ (let [blocks (-> (correct-markdown markdown given-correctors)
+ (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
diff --git a/src/clarktown/renderers/bold.clj b/src/clarktown/renderers/bold.clj
index 1ce7f84..64d6137 100644
--- a/src/clarktown/renderers/bold.clj
+++ b/src/clarktown/renderers/bold.clj
@@ -5,7 +5,7 @@
(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/renderers/code_block.clj b/src/clarktown/renderers/code_block.clj
index 2bed4e6..184e90e 100644
--- a/src/clarktown/renderers/code_block.clj
+++ b/src/clarktown/renderers/code_block.clj
@@ -5,7 +5,7 @@
(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
index 66e819e..84df1fb 100644
--- a/src/clarktown/renderers/empty_block.clj
+++ b/src/clarktown/renderers/empty_block.clj
@@ -3,5 +3,5 @@
(defn render
"Renders an empty block."
- [_ _]
+ [_ _ _]
"")
diff --git a/src/clarktown/renderers/heading_block.clj b/src/clarktown/renderers/heading_block.clj
index f953d0a..4da9bda 100644
--- a/src/clarktown/renderers/heading_block.clj
+++ b/src/clarktown/renderers/heading_block.clj
@@ -38,7 +38,7 @@
(defn render
"Renders the heading block."
- [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
index f141e5f..14e5d8a 100644
--- a/src/clarktown/renderers/horizontal_line_block.clj
+++ b/src/clarktown/renderers/horizontal_line_block.clj
@@ -3,5 +3,5 @@
(defn render
"Renders the horizontal line block."
- [_ _]
+ [_ _ _]
"<hr>")
diff --git a/src/clarktown/renderers/inline_code.clj b/src/clarktown/renderers/inline_code.clj
index 29593a8..e8c298f 100644
--- a/src/clarktown/renderers/inline_code.clj
+++ b/src/clarktown/renderers/inline_code.clj
@@ -5,7 +5,7 @@
(defn render
"Renders all occurring inline code."
- [block _]
+ [block _ _]
(loop [block block
matches (-> (re-seq #"\`.*?\`" block)
distinct)]
diff --git a/src/clarktown/renderers/italic.clj b/src/clarktown/renderers/italic.clj
index a1568f6..970364e 100644
--- a/src/clarktown/renderers/italic.clj
+++ b/src/clarktown/renderers/italic.clj
@@ -5,7 +5,7 @@
(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/renderers/link_and_image.clj b/src/clarktown/renderers/link_and_image.clj
index ea4a006..e61503e 100644
--- a/src/clarktown/renderers/link_and_image.clj
+++ b/src/clarktown/renderers/link_and_image.clj
@@ -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/renderers/list_block.clj b/src/clarktown/renderers/list_block.clj
index 2a40b06..27ca72a 100644
--- a/src/clarktown/renderers/list_block.clj
+++ b/src/clarktown/renderers/list_block.clj
@@ -116,6 +116,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/renderers/paragraph_block.clj b/src/clarktown/renderers/paragraph_block.clj
index c7bec22..0ab3788 100644
--- a/src/clarktown/renderers/paragraph_block.clj
+++ b/src/clarktown/renderers/paragraph_block.clj
@@ -5,5 +5,5 @@
(defn render
"Renders the paragraph block."
- [block _]
+ [block _ _]
(str "<p>" (string/trim block) "</p>"))
diff --git a/src/clarktown/renderers/quote_block.clj b/src/clarktown/renderers/quote_block.clj
index ee30635..1a302f9 100644
--- a/src/clarktown/renderers/quote_block.clj
+++ b/src/clarktown/renderers/quote_block.clj
@@ -1,16 +1,16 @@
(ns clarktown.renderers.quote-block
(:require
[clojure.string :as string]
- [clarktown.parser :as parser]))
+ [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/renderers/strikethrough.clj b/src/clarktown/renderers/strikethrough.clj
index 8e124a0..133be47 100644
--- a/src/clarktown/renderers/strikethrough.clj
+++ b/src/clarktown/renderers/strikethrough.clj
@@ -5,7 +5,7 @@
(defn render
"Renders all occurring strikethrough text."
- [block _]
+ [block _ _]
(loop [block block
matches (-> (re-seq #"~~.*?~~" block)
distinct)]