~subsetpark/bagatto

ref: 3ab1647981984556aaf4e2678d549eb83c302dcb bagatto/demo/vendoring/janet_modules/mago.janet -rw-r--r-- 868 bytes
3ab16479 — Zach Smith Add vendoring demo 5 months ago
                                                                                
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
(def default-separator "%%%")

(defn peg
  [separator]
  (peg/compile ~(* (<- (to ,separator))
                   ,separator
                   (<- (any 1)))))

(def default-peg (peg default-separator))

(def- boot-parse parse)

(defn parse
  ```
  Parse a Mago document into Janet frontmatter and arbitrary body text.

  The text before the separator is treated as a single Janet term and
  parsed. The text after the separator is left untouched.

  Accepts an optional second argument to specify the separator string
  to use. The default is `%%%`.
  ```
  [src &opt separator]
  (default separator default-separator)
  (let [peg (case separator
              default-separator default-peg
              (peg separator))
        matched (peg/match peg src)]
    (match matched
      [front body] {:front (boot-parse front) :body body}
      {:front {} :body src})))