~subsetpark/bagatto

ref: 6ac2d6e6da139126e3abd7108d15395d030e29ed bagatto/main.janet -rw-r--r-- 2.1 KiB
6ac2d6e6 — Zach Smith Add some basic error handling 1 year, 6 days 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
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
62
63
64
65
66
67
68
(import temple)
(import argparse :prefix "")

(import src/core)

(def bagatto
  ```
  An environment populated by the "stdlib" we want to expose to
  template and index module authors.
  ```
  (require "bagatto-require"))

# Monkey-patch the temple environment with our additional functions.
(merge-into temple/base-env bagatto)

(defn load-file [index]
  ```
  Given the filename of our index module, evaluate it in the execution
  environment with our "stdlib" in it. This will give index module
  authors access to its additional namespaces without having to import
  them (and, as the environment was created at compile time, without
  those libraries having to be present when the index module is
  written).
  ```
  (temple/add-loader)
  (dofile index :env bagatto))



(def argparse-params ["A transparent, extensible static site generator."
                      "repl" {:kind :flag :help "Compile your index module and enter the REPL."}
                      :default {:kind :option :help "The index module to evaluate." :required true} ])

(defn main [& args]
  
  (match (os/getenv "JANET_PATH")
    nil :ok
    janet-path (put root-env :syspath janet-path))

  (let [args (argparse ;argparse-params)
        index (args :default)
        env (load-file index)]
    (merge-into temple/base-env env)

    (if (args "repl")
      # REPL mode: Enter a REPL to experiment with the contents of the
      # index module.
      (repl nil nil env)
      # Normal mode: evaluate index module and write site.
      (do
        (defn value
          [sym]
          (try ((env sym) :value)
               ([err fib]
                (error
                 (string "Expected symbol "
                         sym
                         "; couldn't be found after evaluating "
                         index)))))
    
        (setdyn :bagatto-defaults (env :bagatto-defaults))

        (let [data-spec (value 'data)
              data (core/load-data data-spec)
              site (value 'site)
              writer-specs (core/produce-writer-specs site data)
              writers (core/produce-writers writer-specs)]
          (core/resume-writers writers))))))