    ;"html" "shBrushXml.js"
    })
 
-(def ^:dynamic *theme*
-  "Path to a SyntaxHighlighter theme file."
-  "shThemeEclipse.css")
-
-(def ^:dynamic *language*
-  "A language string for use with SyntaxHighlighter."
-  "clojure")
-
-(def ^:dynamic *static-brushes*
-  "A list of SyntaxHighlighter brushes that will always be included in the
-  output."
-  nil)
-
-(def ^:dynamic *stylesheet*
-  "Path to a stylesheet file."
-  nil)
-
-; ### Pegdown variables
-
+
+; ### Pegdown instances
+;
 ; The same [pegdown processor][pp] and [link renderer][lr] are used for each
 ; program execution.
 ;


; Internal links (to element ids) are nice to have. These imports will be used
; to create a modified LinkRenderer.

     LinkRenderer LinkRenderer$Rendering]
    [org.pegdown.ast WikiLinkNode])

; To run from the commandline this namespace must be compiled with a `-main`
; method. When run from the REPL or from Leiningen `gen-class` does nothing.
  
  (:gen-class))

(def ^:dynamic *verbose?*
  "Whether to log messages to std-err or not."
  false)

; ## Utilities
;
; A few utility functions to make it easier to print messages to the console,
; iterate side-effects through a collection, access resources, encode a string
; for use as an id or fragment identifier, and bind an output stream to `*out*`
; over a lexical scope.

(defn message [& s]
  (when *verbose?*
    (binding [*out* (io/writer System/err)]
      (println (apply str s)))))

(defn each
  "Apply f to each item of coll in order for side effects only.
  Returns nil."
  [f coll]
  (doseq [c coll]
    (f c)))

(defn slurp-resource
  "Get the complete contents of a Java resource."
  [resource-name & continue-on-failure?]
  (try
    (-> (.getContextClassLoader (Thread/currentThread))
      (.getResourceAsStream resource-name)
      (java.io.InputStreamReader.)
      (slurp))
    (catch Exception e
      (message "Failed to read resource " resource-name)
      (if continue-on-failure?
        (message "    continuing anyway...")
        (do (message "    aborting...")
          (throw e))))))

(defn slurp-file|resource
  "Get the contents of the file at path, or failing that, the contents of a
  Java resource."
  [path & continue-on-failure?]
  (try (slurp path)
    (catch java.io.FileNotFoundException _
      (message "Using internal " path)
      (slurp-resource path continue-on-failure?))))

(defn encode-anchor [s]
  (java.net.URLEncoder/encode (.replace s " " "-") "UTF-8"))

(defmacro with-out-stream [out & body]
  `(with-open [w# (io/writer ~out)]
     (binding [*out* w#]
       ~@body)))

;@top-level-vars
; ## Top level variables
;
; A few objects need to be accessed by a number of functions: regular
; expressions that match the beginning of commented lines, SyntaxHighlighter
; options, a PegDownProcessor, and a modified LinkRenderer. The dynamic
; variables are bound once for each file that is processed and are not mutated
; at any other time, except `*brushes*` which is bound once per invocation of
; `render-files`.
;
; ### Parsing variables

(def ^:dynamic *single-comment*
  "A string containing a single comment token."
  ";")

(defn comment
  "A regular expression matching the beginning of a commented line."
  []
  (re-pattern (str "^" (Pattern/quote *single-comment*) " ?")))

(defn anchor
  "A regular expression matching the beginning of an anchor line."
  []
  (re-pattern (str (comment) "@ *")))

(defn include
  "A regular expression matching the beginning of an include line."
  []
  (re-pattern (str (comment) "%include +")))

; ### Style variables

;@language map
;
; Including SyntaxHighlighter brush files automatically based on file suffix or
; the language argument to an include directive is preferrable to explicitly

  (with-out-stream out
    (render-files in-paths)))


;@commandline
;
; ## Commandline
;
; Use this program from the commandline like so.
(def usage "Usage: java -jar story.jar [options] <input-files>")

(defn -main [& args]
