-
-
Notifications
You must be signed in to change notification settings - Fork 15
/
transform.cljc
61 lines (45 loc) · 1.59 KB
/
transform.cljc
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
48
49
50
51
52
53
54
55
56
57
58
59
60
61
(ns org-parser.transform
(:require [clojure.string :as str]))
(defmulti reducer
"The reducer multi method takes a `result` and a `line` and dispatches
on the first element in `line`, which is the type (a keyword) of the
parsed line, e.g. `:headline`, `:content-line`, etc."
(fn [_ line] (first line)))
(defmethod reducer :default [state line]
(conj state line))
(defn- property
"Takes a `prop` (a keyword) and a seq `props`. Finds the occurence of
`prop` in `props` and returns a seq of its values."
[prop props]
(->> props
(filter #(= prop (first %)))
first
(drop 1)))
(defmethod reducer :headline [state [_ & properties]]
(let [level (->> properties
(property :stars)
first
count)
title (->> properties
(property :title)
(str/join " "))]
(conj state [:headline {:level level
:title title}])))
(defn- append
"Takes `state` which is the current result while transforming and a
content-line's `text`. Adds the `text` to the text of the last entry
of `state`."
[state text]
(let [keep (pop state)
[key content] (last state)]
(conj keep [key (str content text "\n")])))
(defmethod reducer :content-line [state [_ text]]
(if (-> state last first (= :content))
(append state text)
(conj state [:content (str text "\n")])))
(defmethod reducer :empty-line [state [_ _]]
(reducer state [:content-line ""]))
(defn transform [x]
(->> x
(drop 1) ;; drops the initial `:S`
(reduce reducer [])))