summaryrefslogtreecommitdiff
path: root/src/clarktown/renderers/heading_block.clj
blob: 7f4c7decb63c3ebe33a043b59ac36ca86fdca734 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
(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/trim block)
        size (-> (string/split single-line-block #" ")
                 first
                 string/trim
                 count)
        value (->> (string/split single-line-block #" ")
                   next
                   (string/join " ")
                   string/trim)]
    (str "<h" size ">" value "</h" size ">")))


(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 "<h1>" value "</h1>")
      (str "<h2>" value "</h2>"))))


(defn render
  "Renders the heading block."
  [block _ _]
  (cond (matcher/is-atx-heading? block)
        (render-atx-heading block)
        
        (matcher/is-settext-heading? block)
        (render-settext-heading block)
        
        :else block))