 (ns incise.parsers.core
   (:require [incise.parsers.helpers :refer [extension]]
-            [incise.config :as conf]
+            (incise [config :as conf]
+                    [utils :refer [directory?]])
+            [clojure.java.io :refer [file]]
             [taoensso.timbre :refer [info]])
   (:import [java.io File]))
 
+;; A Parse is simply a record definition which may be optionally used by incise
+;; parsers. All HTML parsers use it as an intermediate format which gets stored
+;; in the parses atom so they can be accessed globally during the final parsing
+;; step. This is particularly useful for generating tags and the like.
 (defrecord Parse [^String title
                   ^String extension
                   content

                       (repeat parser))))

(defn register-mappings
  [mappings]
  (doseq [[function-key new-function-key] mappings]
    (register new-function-key (@parsers (name function-key)))))

        current-parsers @parsers]
    (when (contains? current-parsers ext)
      (info "Parsing" (.getPath handle))
      ((current-parsers ext) handle))))

(defn input-file-seq
  "Returns a sequence of files (exclusing directories) from the input
  directory."
  []
  (->> (conf/get :in-dir)
       (file)
       (file-seq)
       (remove directory?)))

(defn- invoke-delay-or-function
  "Invoke the given delay or function."
  [delay-or-fn]
  {:pre [((some-fn fn? delay?) delay-or-fn)]}
  (condp #(%1 %2) delay-or-fn
    fn? (delay-or-fn)
    delay? (force delay-or-fn)))

(defn- log-file
  "Take a file and return it after logging its path with the given prefix."
  [prefix ^File a-file]
  (info prefix (.getPath a-file))
  a-file)

(defn parse-all
  "Parse all of the given files completely by calling parse on each one and
  invoking the parse result."
  [files]
  (->> files
       (map parse)
       (keep identity)
       (doall) ; Ensure that all files have been parsed.
       (mapcat invoke-delay-or-function)
       (map (partial log-file "Generated"))))

(defn parse-all-input-files
  "Completely parse all of the files from the input directory."
  []
  (parse-all (input-file-seq)))
