    _ filterer FILTER   code "predicate to use for selecting entries (default: `:content`)"
    r renderer RENDERER sym  "page renderer (fully qualified symbol which resolves to a function)"
    m meta     META     edn  "metadata to set on each entry"]
-  (let [tmp     (boot/tmp-dir!)
-        options (merge +render-defaults+ *opts*)]
-    (boot/with-pre-wrap fileset
-      (pod/with-call-in @render-pod
-        (io.perun.render/update!))
-      (let [files (filter (:filterer options) (perun/get-meta fileset))
-            updated-files (doall
-                           (for [{:keys [path] :as file} files]
-                             (let [entry         (merge meta file)
+  (let [options (merge +render-defaults+ *opts*)]
+    (letfn [(render-paths [fileset options]
+              (let [entries (filter (:filterer options) (perun/get-meta fileset))
+                    paths (reduce
+                           (fn [result {:keys [path] :as entry*}]
+                             (let [entry (merge meta entry*)
                                    render-data   {:meta    (perun/get-global-meta fileset)
-                                                  :entries (vec files)
+                                                  :entries (vec entries)
                                                   :entry   entry}
-                                   html          (render-in-pod @render-pod renderer render-data)
                                    page-filepath (perun/create-filepath
                                                   (:out-dir options)
                                                   ; If permalink ends in slash, append index.html as filename
-                                                  (or (some-> (:permalink file)
+                                                  (or (some-> (:permalink entry)
                                                               (string/replace #"/$" "/index.html")
                                                               perun/url-to-path)
                                                       (string/replace path #"(?i).[a-z]+$" ".html")))]
                                (perun/report-debug "render" "rendered page for path" path)
-                               (perun/create-file tmp page-filepath html)
-                               entry)))]
-        (perun/report-info "render" "rendered %s pages" (count files))
-        (perun/merge-meta (commit fileset tmp) updated-files)))))
+                               (assoc result page-filepath {:render-data render-data
+                                                            :entry       entry})))
+                           {}
+                           entries)]
+                (perun/report-info "render" "rendered %s pages" (count paths))
+                paths))]
+      (render-pre-wrap render-paths options :io.perun/render))))
+
+(defn- grouped-paths
+  "Produces path maps of the shape required by `render-to-paths`, based
+  on the provided `fileset` and `options`."
+  [task-name fileset options]
+  (let [global-meta (perun/get-global-meta fileset)
+        grouper (:grouper options)]
+    (->> fileset
+         perun/get-meta
+         (filter (:filterer options))
+         grouper
+         (reduce
+          (fn [result [path {:keys [entries group-meta]}]]
+            (let [sorted        (sort-by (:sortby options) (:comparator options) entries)
+                  page-filepath (perun/create-filepath (:out-dir options) path)
+                  new-entry     (merge {:path          page-filepath
+                                        :canonical-url (str (:base-url global-meta) path)
+                                        :date-build    (:date-build global-meta)}
+                                       group-meta)
+                  render-data   {:meta    global-meta
+                                 :entry   new-entry
+                                 :entries (vec sorted)}]
+              (perun/report-info task-name (str "rendered " task-name " " path))
+              (assoc result page-filepath {:render-data render-data
+                                           :entry       new-entry})))
+          {}))))
 
 (def ^:private +collection-defaults+
   {:out-dir "public"
    :filterer :content
-   :groupby (fn [data] "index.html")
    :sortby (fn [file] (:date-published file))
    :comparator (fn [i1 i2] (compare i2 i1))})
 


(def render-pod (delay (create-pod' render-deps)))

(deftask render
  "Render individual pages for entries in perun data.


   c comparator COMPARATOR code "sort by comparator function"
   p page       PAGE       str  "collection result page path"
   m meta       META       edn  "metadata to set on each collection entry"]
  (let [options (merge +collection-defaults+
                       (dissoc *opts* :page)
                       (if-let [p (:page *opts*)]
                         {:grouper #(-> {p {:entries %}})}
                         (if-let [gb (:groupby *opts*)]
                           {:grouper #(->> %
                                           (group-by gb)
                                           (map (fn [[page entries]]
                                                  [page {:entries entries}]))
                                           (into {}))}
                           {:grouper #(-> {"index.html" {:entries %}})})))]
    (cond (not (fn? (:comparator options)))
          (u/fail "collection task :comparator option should implement IFn\n")
          (not (ifn? (:filterer options)))
          (u/fail "collection task :filterer option value should implement IFn\n")
          (and (:page options) (:groupby *opts*))
          (u/fail "using the :page option will render any :groupby option setting effectless\n")
          (and (:groupby options) (not (ifn? (:groupby options))))
          (u/fail "collection task :groupby option value should implement IFn\n")
          (not (ifn? (:sortby options)))
          (u/fail "collection task :sortby option value should implement IFn\n")
          :else
          (let [collection-paths (partial grouped-paths "collection")]
            (render-pre-wrap collection-paths options :io.perun/collection)))))

(deftask inject-scripts
  "Inject JavaScript scripts into html files.
