|
1 | 1 | (ns io.perun.markdown |
2 | 2 | (: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])) |
10 | 6 |
|
11 | 7 | ;; Extension handling has been copied from endophile.core |
12 | 8 | ;; See https://github.com/sirthias/pegdown/blob/master/src/main/java/org/pegdown/Extensions.java |
|
35 | 31 | :all-optionals Extensions/ALL_OPTIONALS |
36 | 32 | :all-with-optionals Extensions/ALL_WITH_OPTIONALS}) |
37 | 33 |
|
38 | | -(def ^:dynamic *yaml-head* #"---\r?\n") |
39 | | - |
40 | 34 | (defn extensions-map->int [opts] |
41 | 35 | (->> opts |
42 | 36 | (merge {:autolinks true |
|
49 | 43 | (apply bit-or 0) |
50 | 44 | int)) |
51 | 45 |
|
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 | | - |
90 | 46 | (defn markdown-to-html [file-content options] |
91 | 47 | (let [processor (PegDownProcessor. (extensions-map->int (:extensions options)))] |
92 | 48 | (->> file-content |
93 | | - remove-metadata |
| 49 | + yaml/remove-metadata |
94 | 50 | char-array |
95 | 51 | (.markdownToHtml processor)))) |
96 | 52 |
|
97 | 53 | (defn process-file [file options] |
98 | 54 | (perun/report-debug "markdown" "processing markdown" (:filename file)) |
99 | 55 | (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)) |
101 | 57 | html (markdown-to-html file-content (:options options))] |
102 | 58 | (merge md-metadata {:content html} file))) |
103 | 59 |
|
|
0 commit comments