Skip to content

Instantly share code, notes, and snippets.

@nathanmarz
Created June 14, 2016 16:54
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save nathanmarz/1a5728e8a0e12e08c02d98aa1df675aa to your computer and use it in GitHub Desktop.
Save nathanmarz/1a5728e8a0e12e08c02d98aa1df675aa to your computer and use it in GitHub Desktop.
;; given this data structure
(def data
[{:type :root,
:content
[{:line-type :comment, :text "#+title: Sample Org File"}
{:line-type :comment, :text "#+author: Seylerius"}
{:line-type :blank, :text ""}
{:line-type :paragraph,
:text "This is an attempt to test Org-Mode processing."}
{:line-type :blank, :text ""}]}
{:type :section,
:content
[{:line-type :blank, :text ""}
{:line-type :paragraph,
:text "This subsection contains a nested list."}
{:line-type :blank, :text ""}
{:line-type :ordered-list, :text "1. The first OL"}
{:line-type :unordered-list, :text " + UL 1.1"}
{:line-type :unordered-list, :text " + UL 1.2"}
{:line-type :ordered-list, :text "2. The second OL"}
{:line-type :unordered-list, :text " + UL 2.1"}
{:line-type :unordered-list, :text " + UL 2.2"}
{:line-type :ordered-list, :text "3. The third OL"}
{:line-type :unordered-list, :text " + UL 3.1"}
{:line-type :unordered-list, :text " + UL 3.2"}
{:line-type :blank, :text ""}],
:level 2,
:name "The First SubSection",
:tags nil,
:kw nil}
{:type :section,
:content
[{:line-type :blank, :text ""}
{:line-type :ordered-list, :text "1. OL 1 of List 1"}
{:line-type :ordered-list, :text "2. OL 2 of List 1"}
{:line-type :blank, :text ""}
{:line-type :paragraph,
:text "There are two lists in this subsection"}
{:line-type :blank, :text ""}
{:line-type :ordered-list, :text "1. OL 1 of List 2"}
{:line-type :ordered-list, :text "2. OL 2 of List 2"}
{:line-type :blank, :text ""}],
:level 2,
:name "The Second SubSection",
:tags nil,
:kw nil}])
;; this code wraps any continuous sequence of :ordered-list or :unordered-list into a :ul or :ol, with the inner
;; types being changed to :li
(use 'com.rpl.specter)
(use 'com.rpl.specter.macros)
(transform
[ALL
:content
(multi-path
(continuous-subseqs #(= (:line-type %) :ordered-list))
(continuous-subseqs #(= (:line-type %) :unordered-list)))
]
(fn [aseq]
(let [type (-> aseq first :line-type)]
[{:line-type (if (= type :ordered-list) :ol :ul)
:content (setval [ALL :line-type] :li aseq)
}]
))
data
)
;; Output:
[{:type :root,
:content
[{:line-type :comment, :text "#+title: Sample Org File"}
{:line-type :comment, :text "#+author: Seylerius"}
{:line-type :blank, :text ""}
{:line-type :paragraph,
:text "This is an attempt to test Org-Mode processing."}
{:line-type :blank, :text ""}]}
{:type :section,
:content
[{:line-type :blank, :text ""}
{:line-type :paragraph,
:text "This subsection contains a nested list."}
{:line-type :blank, :text ""}
{:line-type :ol,
:content [{:line-type :li, :text "1. The first OL"}]}
{:line-type :ul,
:content
[{:line-type :li, :text " + UL 1.1"}
{:line-type :li, :text " + UL 1.2"}]}
{:line-type :ol,
:content [{:line-type :li, :text "2. The second OL"}]}
{:line-type :ul,
:content
[{:line-type :li, :text " + UL 2.1"}
{:line-type :li, :text " + UL 2.2"}]}
{:line-type :ol,
:content [{:line-type :li, :text "3. The third OL"}]}
{:line-type :ul,
:content
[{:line-type :li, :text " + UL 3.1"}
{:line-type :li, :text " + UL 3.2"}]}
{:line-type :blank, :text ""}],
:level 2,
:name "The First SubSection",
:tags nil,
:kw nil}
{:type :section,
:content
[{:line-type :blank, :text ""}
{:line-type :ol,
:content
[{:line-type :li, :text "1. OL 1 of List 1"}
{:line-type :li, :text "2. OL 2 of List 1"}]}
{:line-type :blank, :text ""}
{:line-type :paragraph,
:text "There are two lists in this subsection"}
{:line-type :blank, :text ""}
{:line-type :ol,
:content
[{:line-type :li, :text "1. OL 1 of List 2"}
{:line-type :li, :text "2. OL 2 of List 2"}]}
{:line-type :blank, :text ""}],
:level 2,
:name "The Second SubSection",
:tags nil,
:kw nil}]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment