From 52203a49aa544b2c11c96445d8732893160c436b Mon Sep 17 00:00:00 2001 From: Asko Nõmm Date: Tue, 19 Apr 2022 17:50:19 +0300 Subject: WIP #16 Pretty much done. Needs more testing. And new documentation. --- src/clarktown/renderers/bold.clj | 18 ++++ src/clarktown/renderers/code_block.clj | 23 ++++ src/clarktown/renderers/empty_block.clj | 7 ++ src/clarktown/renderers/heading_block.clj | 44 ++++++++ src/clarktown/renderers/horizontal_line_block.clj | 7 ++ src/clarktown/renderers/inline_code.clj | 21 ++++ src/clarktown/renderers/italic.clj | 18 ++++ src/clarktown/renderers/link_and_image.clj | 27 +++++ src/clarktown/renderers/list_block.clj | 121 ++++++++++++++++++++++ src/clarktown/renderers/paragraph_block.clj | 9 ++ src/clarktown/renderers/quote_block.clj | 16 +++ src/clarktown/renderers/strikethrough.clj | 18 ++++ 12 files changed, 329 insertions(+) create mode 100644 src/clarktown/renderers/bold.clj create mode 100644 src/clarktown/renderers/code_block.clj create mode 100644 src/clarktown/renderers/empty_block.clj create mode 100644 src/clarktown/renderers/heading_block.clj create mode 100644 src/clarktown/renderers/horizontal_line_block.clj create mode 100644 src/clarktown/renderers/inline_code.clj create mode 100644 src/clarktown/renderers/italic.clj create mode 100644 src/clarktown/renderers/link_and_image.clj create mode 100644 src/clarktown/renderers/list_block.clj create mode 100644 src/clarktown/renderers/paragraph_block.clj create mode 100644 src/clarktown/renderers/quote_block.clj create mode 100644 src/clarktown/renderers/strikethrough.clj (limited to 'src/clarktown/renderers') diff --git a/src/clarktown/renderers/bold.clj b/src/clarktown/renderers/bold.clj new file mode 100644 index 0000000..1ce7f84 --- /dev/null +++ b/src/clarktown/renderers/bold.clj @@ -0,0 +1,18 @@ +(ns clarktown.renderers.bold + (:require + [clojure.string :as string])) + + +(defn render + "Renders all occurring bold text as bold." + [block _] + (loop [block block + matches (-> (re-seq #"(\*{2}|\_{2})[^\*|\_](.*?)[^\*|\_](\*{2}|\_{2})" block) + distinct)] + (if (empty? matches) + block + (let [match (ffirst matches) + value (subs match 2 (- (count match) 2)) + replacement (str "" value "")] + (recur (string/replace block match replacement) + (drop 1 matches)))))) diff --git a/src/clarktown/renderers/code_block.clj b/src/clarktown/renderers/code_block.clj new file mode 100644 index 0000000..2bed4e6 --- /dev/null +++ b/src/clarktown/renderers/code_block.clj @@ -0,0 +1,23 @@ +(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/empty_block.clj b/src/clarktown/renderers/empty_block.clj new file mode 100644 index 0000000..66e819e --- /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/renderers/heading_block.clj b/src/clarktown/renderers/heading_block.clj new file mode 100644 index 0000000..f953d0a --- /dev/null +++ b/src/clarktown/renderers/heading_block.clj @@ -0,0 +1,44 @@ +(ns clarktown.renderers.heading-block + (:require + [clojure.string :as string] + [clarktown.matchers.heading-block :as matcher])) + + +(defn render-atx-heading + "Renders the hashbang heading block." + [block] + (let [single-line-block (-> (string/replace block #"\n" "") + string/trim) + size (-> (string/split single-line-block #" ") + first + string/trim + count) + value (->> (string/split single-line-block #" ") + next + (string/join " ") + string/trim)] + (str "" value ""))) + + +(defn render-settext-heading + "Renders the settext heading block." + [block] + (let [lines (string/split-lines block) + value (->> (split-at (- (count lines) 1) lines) + first + (string/join "\n")) + h1? (= "=" (-> (last lines) + string/trim + (string/split #"") + first))] + (if h1? + (str "

" value "

") + (str "

" value "

")))) + + +(defn render + "Renders the 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..f141e5f --- /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." + [_ _] + "
") diff --git a/src/clarktown/renderers/inline_code.clj b/src/clarktown/renderers/inline_code.clj new file mode 100644 index 0000000..29593a8 --- /dev/null +++ b/src/clarktown/renderers/inline_code.clj @@ -0,0 +1,21 @@ +(ns clarktown.renderers.inline-code + (:require + [clojure.string :as string])) + + +(defn render + "Renders all occurring inline code." + [block _] + (loop [block block + matches (-> (re-seq #"\`.*?\`" block) + distinct)] + (if (empty? matches) + block + (let [match (first matches) + value (-> (subs match 1 (- (count match) 1)) + (string/replace #"&" "&") + (string/replace #"<" "<") + (string/replace #">" ">")) + replacement (str "" value "")] + (recur (string/replace block match replacement) + (drop 1 matches)))))) diff --git a/src/clarktown/renderers/italic.clj b/src/clarktown/renderers/italic.clj new file mode 100644 index 0000000..a1568f6 --- /dev/null +++ b/src/clarktown/renderers/italic.clj @@ -0,0 +1,18 @@ +(ns clarktown.renderers.italic + (:require + [clojure.string :as string])) + + +(defn render + "Renders all occurring italic text as italic." + [block _] + (loop [block block + matches (-> (re-seq #"(\*{1,}?|\_{1,}?)(.*?)(\*{1,}?|\_{1,}?)" block) + distinct)] + (if (empty? matches) + block + (let [match (ffirst matches) + value (subs match 1 (- (count match) 1)) + replacement (str "" value "")] + (recur (string/replace block match replacement) + (drop 1 matches)))))) diff --git a/src/clarktown/renderers/link_and_image.clj b/src/clarktown/renderers/link_and_image.clj new file mode 100644 index 0000000..ea4a006 --- /dev/null +++ b/src/clarktown/renderers/link_and_image.clj @@ -0,0 +1,27 @@ +(ns clarktown.renderers.link-and-image + (:require + [clojure.string :as string])) + + +(defn encode-href + [href] + (-> href + (string/replace "_" "_"))) + + +(defn render + "Renders all occurring links and images." + [block _] + (loop [block block + matches (-> (re-seq #"\!?\[([a-zA-Z0-9\-\.\,]+( [a-zA-Z0-9\-\.\,]+)*)\]\((.*?)\)" block) + distinct)] + (if (empty? matches) + block + (let [[whole-match label _ href] (first matches) + image? (string/starts-with? whole-match "!") + image (str "\""") + link (str "" label "")] + (recur (if image? + (string/replace block whole-match image) + (string/replace block whole-match link)) + (drop 1 matches)))))) \ No newline at end of file diff --git a/src/clarktown/renderers/list_block.clj b/src/clarktown/renderers/list_block.clj new file mode 100644 index 0000000..2a40b06 --- /dev/null +++ b/src/clarktown/renderers/list_block.clj @@ -0,0 +1,121 @@ +(ns clarktown.renderers.list-block + (:require + [clojure.string :as string])) + + +(defn string->indent-n + "Returns the indentation count from left of `str`, which must be + in spaces and not tabs." + [str] + (count (take-while #{\space} str))) + + +(defn compose-items-with-indent-guides + "Composes a vector of maps from given `block` that adds a unique + ID to each line as well as its `indent-n` which is used later + on to determine hierarchies. " + [block] + (->> (string/split-lines block) + (mapv + (fn [line] + {:id (random-uuid) + :indent-n (string->indent-n line) + :value (-> line + string/trim)})))) + + +(defn find-parent-id + "Assuming a 1-level `items`, will attempt to find the parent `id` + of the item at given `index`. Will return `nil` otherwise." + [items index] + (let [indent-n-at-index (:indent-n (nth items index))] + (-> (->> (split-at index items) + first + reverse + (remove #(or (> (:indent-n %) indent-n-at-index) + (= (:indent-n %) indent-n-at-index))) + first + :id)))) + + +(defn compose-items-with-parents + "Composes a 1-level list of items from `block` and adds parent + information to each if they belong to another item. The result + of this is used to build the final data tree." + [block] + (let [items (compose-items-with-indent-guides block)] + (->> items + (map-indexed + (fn [index line] + (merge line {:parent (find-parent-id items index)})))))) + + +(defn add-to-parent + "Recursively scans `items`, which can be multiple levels deep, + and tries to find a home for `item` according to its parent ID." + [items item] + (->> 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)))))) + + +(defn compose-item-tree + "Given a `block`, composes a data representation of it based on + the indentation of each line." + [block] + (loop [result [] + items (compose-items-with-parents block)] + (if (empty? items) + result + (let [item (first items) + parent (:parent item) + new-item {:id (:id item) + :value (:value item)}] + (recur (if parent + (add-to-parent result item) + (concat result [new-item])) + (drop 1 items)))))) + + +(defn render-items + "Renders an ordered/un-ordered list hierarchy from given `items`." + [items] + (loop [result "" + inner-items items] + (if (empty? inner-items) + (if (or (string/starts-with? (:value (first items)) "*") + (string/starts-with? (:value (first items)) "-")) + (str "") + (str "
    " result "
")) + (let [inner-item (first inner-items) + value (cond + ; * unordered list + (string/starts-with? (:value inner-item) "*") + (-> (string/replace-first (:value inner-item) "*" "") + string/trim) + ; - unordered list + (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))] + (recur (if (:items inner-item) + (str result "
  • " value (render-items (:items inner-item)) "
  • ") + (str result "
  • " value "
  • ")) + (drop 1 inner-items)))))) + + +(defn render + "Renders the list block" + [block _] + (-> (compose-item-tree block) + (render-items))) \ No newline at end of file diff --git a/src/clarktown/renderers/paragraph_block.clj b/src/clarktown/renderers/paragraph_block.clj new file mode 100644 index 0000000..c7bec22 --- /dev/null +++ b/src/clarktown/renderers/paragraph_block.clj @@ -0,0 +1,9 @@ +(ns clarktown.renderers.paragraph-block + (:require + [clojure.string :as string])) + + +(defn render + "Renders the paragraph block." + [block _] + (str "

    " (string/trim block) "

    ")) diff --git a/src/clarktown/renderers/quote_block.clj b/src/clarktown/renderers/quote_block.clj new file mode 100644 index 0000000..ee30635 --- /dev/null +++ b/src/clarktown/renderers/quote_block.clj @@ -0,0 +1,16 @@ +(ns clarktown.renderers.quote-block + (:require + [clojure.string :as string] + [clarktown.parser :as parser])) + + +(defn render + "Renders a quote block." + [block parsers] + (let [matches (re-seq #">.*" block) + blocks (->> (for [match matches] + (-> (subs match 1) + string/trim)) + (string/join "\n")) + block (parser/parse blocks parsers)] + (str "
    " block "
    "))) diff --git a/src/clarktown/renderers/strikethrough.clj b/src/clarktown/renderers/strikethrough.clj new file mode 100644 index 0000000..8e124a0 --- /dev/null +++ b/src/clarktown/renderers/strikethrough.clj @@ -0,0 +1,18 @@ +(ns clarktown.renderers.strikethrough + (:require + [clojure.string :as string])) + + +(defn render + "Renders all occurring strikethrough text." + [block _] + (loop [block block + matches (-> (re-seq #"~~.*?~~" block) + distinct)] + (if (empty? matches) + block + (let [match (first matches) + value (subs match 2 (- (count match) 2)) + replacement (str "" value "")] + (recur (string/replace block match replacement) + (drop 1 matches)))))) -- cgit v1.2.3