Skip to content

Instantly share code, notes, and snippets.

@bluemont
Last active December 16, 2015 22:09
Show Gist options
  • Save bluemont/5505213 to your computer and use it in GitHub Desktop.
Save bluemont/5505213 to your computer and use it in GitHub Desktop.

Tree Parse

I want to parse this (newlines are important):

L1
==1==
L2
L3
===1.1===
L4
L5
===1.2===
L6
====1.2.1====
L7
====1.2.2====
L8
==2==
L9

Into this:

["L1"
 [{:h "1"}
  ["L2"
   "L3"
   [{:h "1.1"}
    "L4"
    "L5"
    [{:h "1.2"}
     "L6"
     [{:h "1.2.1"}
      "L7"]
     [{:h "1.2.2"}
      "L8"]]]]]
 [{:h "2"}]]

Helper Functions

  1. (heading? "===Whatever===") -> true
  2. (heading-title "===Whatever===") -> "Whatever"
  3. (heading-level "===Whatever===") -> 2 (the minimum heading level is 1, with "==")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment