Skip to content

Commit 79ab682

Browse files
committed
Put yaml parsing into its own namespace
Future content parsers will need to use the same yaml parsing that `markdown` does. This is a modified form of changes in hashobject#113.
1 parent 7ec5b3a commit 79ab682

File tree

2 files changed

+50
-49
lines changed

2 files changed

+50
-49
lines changed

src/io/perun/markdown.clj

Lines changed: 5 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,8 @@
11
(ns io.perun.markdown
22
(:require [io.perun.core :as perun]
3-
[clojure.java.io :as io]
4-
[clojure.string :as str]
5-
[clj-yaml.core :as yaml]
6-
[clojure.walk :as walk])
7-
(:import [org.pegdown PegDownProcessor Extensions]
8-
[flatland.ordered.map OrderedMap]
9-
[flatland.ordered.set OrderedSet]))
3+
[io.perun.yaml :as yaml]
4+
[clojure.java.io :as io])
5+
(:import [org.pegdown PegDownProcessor Extensions]))
106

117
;; Extension handling has been copied from endophile.core
128
;; See https://github.com/sirthias/pegdown/blob/master/src/main/java/org/pegdown/Extensions.java
@@ -35,8 +31,6 @@
3531
:all-optionals Extensions/ALL_OPTIONALS
3632
:all-with-optionals Extensions/ALL_WITH_OPTIONALS})
3733

38-
(def ^:dynamic *yaml-head* #"---\r?\n")
39-
4034
(defn extensions-map->int [opts]
4135
(->> opts
4236
(merge {:autolinks true
@@ -49,55 +43,17 @@
4943
(apply bit-or 0)
5044
int))
5145

52-
(defn substr-between
53-
"Find string that is nested in between two strings. Return first match.
54-
Copied from https://github.com/funcool/cuerdas"
55-
[s prefix suffix]
56-
(cond
57-
(nil? s) nil
58-
(nil? prefix) nil
59-
(nil? suffix) nil
60-
:else
61-
(some-> s
62-
(str/split prefix)
63-
second
64-
(str/split suffix)
65-
first)))
66-
67-
(defn normal-colls
68-
"Clj-yaml keeps order of map properties by using ordered maps. These are inconvenient
69-
for us as the ordered library is not necessarily available in other pods."
70-
[x]
71-
(walk/postwalk
72-
(fn [y]
73-
(cond
74-
(instance? OrderedMap y) (into {} y)
75-
(instance? OrderedSet y) (into #{} y)
76-
:else y))
77-
x))
78-
79-
(defn parse-file-metadata [file-content]
80-
(when-let [metadata-str (substr-between file-content *yaml-head* *yaml-head*)]
81-
(when-let [parsed-yaml (normal-colls (yaml/parse-string metadata-str))]
82-
parsed-yaml)))
83-
84-
(defn remove-metadata [content]
85-
(let [splitted (str/split content *yaml-head* 3)]
86-
(if (> (count splitted) 2)
87-
(first (drop 2 splitted))
88-
content)))
89-
9046
(defn markdown-to-html [file-content options]
9147
(let [processor (PegDownProcessor. (extensions-map->int (:extensions options)))]
9248
(->> file-content
93-
remove-metadata
49+
yaml/remove-metadata
9450
char-array
9551
(.markdownToHtml processor))))
9652

9753
(defn process-file [file options]
9854
(perun/report-debug "markdown" "processing markdown" (:filename file))
9955
(let [file-content (-> file :full-path io/file slurp)
100-
md-metadata (merge (:meta options) (parse-file-metadata file-content))
56+
md-metadata (merge (:meta options) (yaml/parse-file-metadata file-content))
10157
html (markdown-to-html file-content (:options options))]
10258
(merge md-metadata {:content html} file)))
10359

src/io/perun/yaml.clj

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
(ns io.perun.yaml
2+
(:require [clojure.string :as str]
3+
[clj-yaml.core :as yaml]
4+
[clojure.walk :as walk])
5+
(:import [flatland.ordered.map OrderedMap]
6+
[flatland.ordered.set OrderedSet]))
7+
8+
(def ^:dynamic *yaml-head* #"---\r?\n")
9+
10+
(defn substr-between
11+
"Find string that is nested in between two strings. Return first match.
12+
Copied from https://github.com/funcool/cuerdas"
13+
[s prefix suffix]
14+
(cond
15+
(nil? s) nil
16+
(nil? prefix) nil
17+
(nil? suffix) nil
18+
:else
19+
(some-> s
20+
(str/split prefix)
21+
second
22+
(str/split suffix)
23+
first)))
24+
25+
(defn normal-colls
26+
"Clj-yaml keeps order of map properties by using ordered maps. These are inconvenient
27+
for us as the ordered library is not necessarily available in other pods."
28+
[x]
29+
(walk/postwalk
30+
(fn [y]
31+
(cond
32+
(instance? OrderedMap y) (into {} y)
33+
(instance? OrderedSet y) (into #{} y)
34+
:else y))
35+
x))
36+
37+
(defn parse-file-metadata [file-content]
38+
(when-let [metadata-str (substr-between file-content *yaml-head* *yaml-head*)]
39+
(normal-colls (yaml/parse-string metadata-str))))
40+
41+
(defn remove-metadata [content]
42+
(let [splitted (str/split content *yaml-head* 3)]
43+
(if (> (count splitted) 2)
44+
(first (drop 2 splitted))
45+
content)))

0 commit comments

Comments
 (0)