From e9d1c1e1942e0b1e19e7c3861e80af7521051cd8 Mon Sep 17 00:00:00 2001 From: Michael Jerger Date: Wed, 8 Feb 2017 19:22:15 +0100 Subject: [PATCH] refactored navbar-model out of compiler & introduced new :navbar-model parameter to config --- src/cryogen_core/compiler.clj | 54 +++++++------------------ src/cryogen_core/navbar_model.clj | 33 +++++++++++++++ test/cryogen_core/compiler_test.clj | 35 ---------------- test/cryogen_core/navbar_model_test.clj | 40 ++++++++++++++++++ 4 files changed, 87 insertions(+), 75 deletions(-) create mode 100644 src/cryogen_core/navbar_model.clj create mode 100644 test/cryogen_core/navbar_model_test.clj diff --git a/src/cryogen_core/compiler.clj b/src/cryogen_core/compiler.clj index 8f16190..f3c8fe6 100644 --- a/src/cryogen_core/compiler.clj +++ b/src/cryogen_core/compiler.clj @@ -14,7 +14,8 @@ [cryogen-core.sass :as sass] [cryogen-core.sitemap :as sitemap] [clojure.inspector :as inspector] - [cryogen-core.toc :as toc]) + [cryogen-core.toc :as toc] + [cryogen-core.navbar-model :as navbar-model]) (:import java.util.Locale)) (cache-off!) @@ -221,37 +222,6 @@ sidebar-pages false} (group-by #(boolean (:navbar? %)) pages)] (map (partial sort-by :page-index) [navbar-pages sidebar-pages]))) -(defn- uri-level [uri] - (count (clojure.string/split uri #"/"))) - -(defn- filter-pages-for-uri [uri pages] - (filter #(clojure.string/starts-with? (:uri %) uri) pages)) - -(defn build-nav-map-level - "builds one level of nav-map and recurs to next level." - [parent-uri pages] - (let [current-level (+ 1 (uri-level parent-uri)) - pages-of-parent (filter-pages-for-uri parent-uri pages) - pages-on-level (filter #(= current-level (uri-level (:uri %))) pages-of-parent) - pages-on-child-level (filter #(< current-level (uri-level (:uri %))) pages-of-parent) - ] - (sort-by :page-index - (map #(let [page-on-level % - child-pages (filter-pages-for-uri (:uri page-on-level) pages-on-child-level)] - (if (empty? child-pages) - page-on-level - (merge page-on-level - {:navmap-children (build-nav-map-level (:uri page-on-level) child-pages)}))) pages-on-level)) - )) - -(defn build-nav-map - "builds a nav-map from pages" - [pages] - (let [filtered-pages (filter #(boolean (:navmap? %)) pages) - sorted-pages (sort-by :uri filtered-pages)] - (build-nav-map-level "/pages/" sorted-pages) - )) - (defn write-html "When `clean-urls?` is set, appends `/index.html` before spit; otherwise just spits." [file-uri {:keys [clean-urls?]} data] @@ -495,7 +465,9 @@ (update-in [:compass-path] (fnil str "compass")) (update-in [:post-date-format] (fnil str "yyyy-MM-dd")) (update-in [:keep-files] (fnil seq [])) - (update-in [:ignored-files] (fnil seq [#"^\.#.*" #".*\.swp$"])))] + (update-in [:ignored-files] (fnil seq [#"^\.#.*" #".*\.swp$"])) + (update-in [:navbar-model] (fnil :flat)) + )] (merge config {:page-root-uri (root-uri :page-root-uri config) @@ -517,7 +489,7 @@ [] (println (green "compiling assets...")) (let [{:keys [^String site-url blog-prefix rss-name recent-posts sass-dest keep-files ignored-files previews? - author-root-uri theme debug?] + author-root-uri theme debug? navbar-model] :as config} (read-config) posts (map klipsify (add-prev-next (read-posts config))) posts-by-tag (group-by-tags posts) @@ -532,16 +504,20 @@ (add-prev-next)) [navbar-pages sidebar-pages] (group-pages other-pages) - navmap-pages (build-nav-map other-pages) params (merge config {:today (java.util.Date.) :title (:site-title config) :active-page "home" :tags (map (partial tag-info config) (keys posts-by-tag)) :latest-posts latest-posts - :navbar-pages navbar-pages - :navmap-pages navmap-pages - :sidebar-pages sidebar-pages + :navbar-pages (cond + (= navbar-model :flat) navbar-pages + (= navbar-model :hierarchic) (navbar-model/build-nav-map navbar-pages) + ) + :sidebar-pages (cond + (= navbar-model :flat) sidebar-pages + ;(= navbar-model :hierarchic) (build-nav-map navbar-pages) + ) :home-page (if home-page home-page (assoc (first latest-posts) :layout "home.html")) @@ -553,8 +529,6 @@ (when debug? (println (blue "debug: navbar-pages:")) (println "\t-->" (cyan navbar-pages)) - (println (blue "debug: navmap-pages:")) - (println "\t-->" (cyan navmap-pages)) ) (set-custom-resource-path! (str "file:resources/templates/themes/" theme)) (cryogen-io/wipe-public-folder keep-files) diff --git a/src/cryogen_core/navbar_model.clj b/src/cryogen_core/navbar_model.clj new file mode 100644 index 0000000..7042577 --- /dev/null +++ b/src/cryogen_core/navbar_model.clj @@ -0,0 +1,33 @@ +(ns cryogen-core.navbar-model + (:require + [clojure.string :as s])) + +(defn- uri-level [uri] + (count (s/split uri #"/"))) + +(defn- filter-pages-for-uri [uri pages] + (filter #(s/starts-with? (:uri %) uri) pages)) + +(defn build-nav-map-level + "builds one level of nav-map and recurs to next level." + [parent-uri pages] + (let [current-level (+ 1 (uri-level parent-uri)) + pages-of-parent (filter-pages-for-uri parent-uri pages) + pages-on-level (filter #(= current-level (uri-level (:uri %))) pages-of-parent) + pages-on-child-level (filter #(< current-level (uri-level (:uri %))) pages-of-parent) + ] + (sort-by :page-index + (map #(let [page-on-level % + child-pages (filter-pages-for-uri (:uri page-on-level) pages-on-child-level)] + (if (empty? child-pages) + page-on-level + (merge page-on-level + {:children (build-nav-map-level (:uri page-on-level) child-pages)}))) pages-on-level)) + )) + +(defn build-nav-map + "builds a nav-map from pages" + [pages] + (let [sorted-pages (sort-by :uri pages)] + (build-nav-map-level "/pages/" sorted-pages) + )) diff --git a/test/cryogen_core/compiler_test.clj b/test/cryogen_core/compiler_test.clj index 58af165..dcd9525 100644 --- a/test/cryogen_core/compiler_test.clj +++ b/test/cryogen_core/compiler_test.clj @@ -116,38 +116,3 @@ and more content. (reset-resources) (copy-and-check-markup-folders dirs mu false)))) (reset-resources)) - - -(defn- page [uri page-index] - {:navmap? true, :uri uri :content uri :page-index page-index}) - -(defn- enhanced-page [uri page-index children] - {:navmap? true, :uri uri :content uri - :page-index page-index - :navmap-children children}) - -(deftest test-navmap-pages - (testing - "No pages or posts nothing to copy" - (let [pages [{:navmap? false, :content "1"} - (page "/pages/nav1/" 0) - (page "/pages/nav1/nav11/" 1) - (page "/pages/nav1/nav13/" 3) - (page "/pages/nav1/nav11/nav112/" 2) - (page "/pages/nav1/nav12/" 2) - (page "/pages/nav1/nav11/xnav111/" 1) - ] - expected [(enhanced-page - "/pages/nav1/" 0 - [(enhanced-page - "/pages/nav1/nav11/" 1 - [(page "/pages/nav1/nav11/xnav111/" 1) - (page "/pages/nav1/nav11/nav112/" 2)]) - (page "/pages/nav1/nav12/" 2) - (page "/pages/nav1/nav13/" 3)] - )] - ] - (is (= expected - (build-nav-map pages))) - ) - )) diff --git a/test/cryogen_core/navbar_model_test.clj b/test/cryogen_core/navbar_model_test.clj new file mode 100644 index 0000000..2251016 --- /dev/null +++ b/test/cryogen_core/navbar_model_test.clj @@ -0,0 +1,40 @@ +(ns cryogen-core.navbar-model-test + (:require + [clojure.test :refer :all] + [cryogen-core.navbar-model :as sut])) + +(defn- page [uri page-index] + {:uri uri + :content uri + :page-index page-index}) + +(defn- enhanced-page [uri page-index children] + {:uri uri + :content uri + :page-index page-index + :children children}) + +(deftest test-navmap-pages + (testing + "No pages or posts nothing to copy" + (let [pages [(page "/pages/nav1/" 0) + (page "/pages/nav1/nav11/" 1) + (page "/pages/nav1/nav13/" 3) + (page "/pages/nav1/nav11/nav112/" 2) + (page "/pages/nav1/nav12/" 2) + (page "/pages/nav1/nav11/xnav111/" 1) + ] + expected [(enhanced-page + "/pages/nav1/" 0 + [(enhanced-page + "/pages/nav1/nav11/" 1 + [(page "/pages/nav1/nav11/xnav111/" 1) + (page "/pages/nav1/nav11/nav112/" 2)]) + (page "/pages/nav1/nav12/" 2) + (page "/pages/nav1/nav13/" 3)] + )] + ] + (is (= expected + (sut/build-nav-map pages))) + ) + ))