 
 (def ^:dynamic *meta-sep*
   "The separator that splits each content file into meta and content sections."
-  
   "===")
 
 (def ^:dynamic *block-sep*
   "Separator used in content files to denote line-oriented preprocessing
   blocks."
-  
   "$$$")
 
-(defn mk-file-record
+(defn mk-page-record
   [file lines]
   (merge (->> lines (take-while #(not= % *meta-sep*)) (join " ") (read-string))
          {:path (.getPath file) :mod-time (fs/mod-time file)}))

  (if-let [e (fs/extension s)] (clojure.string/replace s e "") s))

(defn strip-file-info
  [file-record]
  (dissoc file-record :path :mod-time))

(defn get-site-map
  []
  (deref *site-map*))
  
(defn humane-site-map
  []
  (clojure.walk/postwalk (fn [form]
                           (cond (file-record? form)
                                 (strip-file-info form)
                                 
                                 (keyword? form)
                                 (-> form name strip-extension keyword)

                                 :default form))
                         (get-site-map)))

(defmulti render-file
  "Takes as its first argument a map of the type returned by `build-file!`,
  and as its second argument a lazy sequence of the lines of the corresponding
  file. Should return a string which will be written to the output file."

  (comp fs/extension :path))

(defmethod render-file ".md"
  [file-record content-lines]
  (hiccup/html
   [:html
    [:head [:title (:title file-record)]]
    [:body (md-to-html-string (join "\n" content-lines))]]))

(defn mk-output

   (str (join "/" (cons root (drop 2 (fs/split (strip-extension path)))))
        ".html")))

(defn navigate-site-map
  [site-map page-record]
  (assoc (humane-site-map site-map)
         :current-page
         (strip-file-info page-record)))

(defn compile-file!
  [site-map build-path [page-record lines]]
  (let [output (mk-output build-path (:path page-record))]
    (with-parent-dir output
      (fn []
        (with-open [writer (clojure.java.io/writer output)]
          (binding [site (navigate-site-map site-map page-record)]
            (.write writer (render-file page-record lines))))))))

(defn compile-content!
  [site-map content-root build-path]
  (do-directory! (comp (partial compile-file! site-map build-path)
                       build-file!)
                 (complement fs/directory?)
                 content-root))
