diff options
| author | Asko Nomm <asko@bien.ee> | 2022-04-06 13:57:11 +0200 |
|---|---|---|
| committer | Asko Nomm <asko@bien.ee> | 2022-04-06 13:57:11 +0200 |
| commit | f89770a3c312cc55fcd96f3f9130595cb6fa75ea (patch) | |
| tree | 47f5c3f2565109b78fb12fcbcce0703e5add7116 /src/clarktown | |
| parent | 2072d2730e0a7991bdf6474275f9fe9cd8fac182 (diff) | |
Add comments.
Diffstat (limited to 'src/clarktown')
| -rw-r--r-- | src/clarktown/parsers/list_block.clj | 17 |
1 files changed, 16 insertions, 1 deletions
diff --git a/src/clarktown/parsers/list_block.clj b/src/clarktown/parsers/list_block.clj index 9485f67..3f32067 100644 --- a/src/clarktown/parsers/list_block.clj +++ b/src/clarktown/parsers/list_block.clj @@ -11,11 +11,16 @@ (defn string->indent-n + "Returns the indentation count from left of `str`, which must be + in spaces and not tabs." [str] (count (take-while #{\space} str))) (defn compose-items-with-indent-guides + "Composes a vector of maps from given `block` that adds a unique + ID to each line as well as its `indent-n` which is used later + on to determine hierarchies. " [block] (->> (string/split-lines block) (mapv @@ -27,6 +32,8 @@ (defn find-parent-id + "Assuming a 1-level `items`, will attempt to find the parent `id` + of the item at given `index`. Will return `nil` otherwise." [items index] (let [indent-n-at-index (:indent-n (nth items index))] (-> (->> (split-at index items) @@ -39,6 +46,9 @@ (defn compose-items-with-parents + "Composes a 1-level list of items from `block` and adds parent + information to each if they belong to another item. The result + of this is used to build the final data tree." [block] (let [items (compose-items-with-indent-guides block)] (->> items @@ -48,6 +58,8 @@ (defn add-to-parent + "Recursively scans `items`, which can be multiple levels deep, + and tries to find a home for `item` according to its parent ID." [items item] (->> items (mapv @@ -62,6 +74,8 @@ (defn compose-item-tree + "Given a `block`, composes a data representation of it based on + the indentation of each line." [block] (loop [result [] items (compose-items-with-parents block)] @@ -78,6 +92,7 @@ (defn render-items + "Renders an ordered/un-ordered list hierarchy from given `items`." [items] (loop [result "" inner-items items] @@ -98,7 +113,7 @@ (defn render - "Renders the ordered list block" + "Renders the list block" [block _] (-> (compose-item-tree block) (render-items)))
\ No newline at end of file |
