summaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorAsko Nomm <asko@bien.ee>2022-04-09 13:51:16 +0200
committerAsko Nomm <asko@bien.ee>2022-04-09 13:51:16 +0200
commit9f8f4fc36adef06458320d7bfdef01dbe41f52a1 (patch)
tree48b51c46809dedb79f5891e113443a4dec028921 /src
parent7055c7bd45c5156df61ab3a77b8e22a037f04408 (diff)
Support settext headings (closes #6)
Diffstat (limited to 'src')
-rw-r--r--src/clarktown/parser.clj2
-rw-r--r--src/clarktown/parsers/heading_block.clj59
2 files changed, 53 insertions, 8 deletions
diff --git a/src/clarktown/parser.clj b/src/clarktown/parser.clj
index 95776d7..0b6911d 100644
--- a/src/clarktown/parser.clj
+++ b/src/clarktown/parser.clj
@@ -94,4 +94,4 @@
(let [blocks (-> (string/split markdown #"\n\n")
stitch-code-blocks)
parsed-blocks (parse-blocks blocks parsers)]
- (string/join "" parsed-blocks)))
+ (string/join "\n\n" parsed-blocks)))
diff --git a/src/clarktown/parsers/heading_block.clj b/src/clarktown/parsers/heading_block.clj
index 84a5fdb..def2394 100644
--- a/src/clarktown/parsers/heading_block.clj
+++ b/src/clarktown/parsers/heading_block.clj
@@ -3,17 +3,35 @@
[clojure.string :as string]))
+(defn is-hashbang-heading?
+ "Determines whether the given block is a hashbang heading."
+ [block]
+ (-> (string/replace block #"\n" "")
+ string/trim
+ (string/starts-with? "#")))
+
+
+(defn is-settext-heading?
+ "Determines whether the given block is a settext heading."
+ [block]
+ (let [lines (-> (string/split-lines block))
+ chars (-> (last lines)
+ string/trim
+ (string/split #""))]
+ (and (> (count lines) 1)
+ (every? #{"-" "="} chars))))
+
+
(defn is?
- "Determines whether the given block is a heading block or not."
+ "Determines whether the given block is a heading block."
[block]
- (= true (-> (string/replace block #"\n" "")
- string/trim
- (string/starts-with? "#"))))
+ (or (is-hashbang-heading? block)
+ (is-settext-heading? block)))
-(defn render
- "Renders the heading block."
- [block _]
+(defn render-hashbang-heading
+ "Renders the hashbang heading block."
+ [block]
(let [single-line-block (-> (string/replace block #"\n" "")
string/trim)
size (-> (string/split single-line-block #" ")
@@ -25,3 +43,30 @@
(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>"))))
+
+
+(render-settext-heading "Hello world\nAnd you too\n===")
+
+
+(defn render
+ "Renders the heading block."
+ [block _]
+ (if (is-hashbang-heading? block)
+ (render-hashbang-heading block)
+ (render-settext-heading block)))