refactored navbar-model out of compiler & introduced new :navbar-model

parameter to config
This commit is contained in:
Michael Jerger 2017-02-08 19:22:15 +01:00
parent 28cb312dc2
commit e9d1c1e194
4 changed files with 87 additions and 75 deletions

View file

@ -14,7 +14,8 @@
[cryogen-core.sass :as sass] [cryogen-core.sass :as sass]
[cryogen-core.sitemap :as sitemap] [cryogen-core.sitemap :as sitemap]
[clojure.inspector :as inspector] [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)) (:import java.util.Locale))
(cache-off!) (cache-off!)
@ -221,37 +222,6 @@
sidebar-pages false} (group-by #(boolean (:navbar? %)) pages)] sidebar-pages false} (group-by #(boolean (:navbar? %)) pages)]
(map (partial sort-by :page-index) [navbar-pages sidebar-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 (defn write-html
"When `clean-urls?` is set, appends `/index.html` before spit; otherwise just spits." "When `clean-urls?` is set, appends `/index.html` before spit; otherwise just spits."
[file-uri {:keys [clean-urls?]} data] [file-uri {:keys [clean-urls?]} data]
@ -495,7 +465,9 @@
(update-in [:compass-path] (fnil str "compass")) (update-in [:compass-path] (fnil str "compass"))
(update-in [:post-date-format] (fnil str "yyyy-MM-dd")) (update-in [:post-date-format] (fnil str "yyyy-MM-dd"))
(update-in [:keep-files] (fnil seq [])) (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 (merge
config config
{:page-root-uri (root-uri :page-root-uri config) {:page-root-uri (root-uri :page-root-uri config)
@ -517,7 +489,7 @@
[] []
(println (green "compiling assets...")) (println (green "compiling assets..."))
(let [{:keys [^String site-url blog-prefix rss-name recent-posts sass-dest keep-files ignored-files previews? (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) :as config} (read-config)
posts (map klipsify (add-prev-next (read-posts config))) posts (map klipsify (add-prev-next (read-posts config)))
posts-by-tag (group-by-tags posts) posts-by-tag (group-by-tags posts)
@ -532,16 +504,20 @@
(add-prev-next)) (add-prev-next))
[navbar-pages [navbar-pages
sidebar-pages] (group-pages other-pages) sidebar-pages] (group-pages other-pages)
navmap-pages (build-nav-map other-pages)
params (merge config params (merge config
{:today (java.util.Date.) {:today (java.util.Date.)
:title (:site-title config) :title (:site-title config)
:active-page "home" :active-page "home"
:tags (map (partial tag-info config) (keys posts-by-tag)) :tags (map (partial tag-info config) (keys posts-by-tag))
:latest-posts latest-posts :latest-posts latest-posts
:navbar-pages navbar-pages :navbar-pages (cond
:navmap-pages navmap-pages (= navbar-model :flat) navbar-pages
:sidebar-pages sidebar-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 (if home-page
home-page home-page
(assoc (first latest-posts) :layout "home.html")) (assoc (first latest-posts) :layout "home.html"))
@ -553,8 +529,6 @@
(when debug? (when debug?
(println (blue "debug: navbar-pages:")) (println (blue "debug: navbar-pages:"))
(println "\t-->" (cyan 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)) (set-custom-resource-path! (str "file:resources/templates/themes/" theme))
(cryogen-io/wipe-public-folder keep-files) (cryogen-io/wipe-public-folder keep-files)

View file

@ -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)
))

View file

@ -116,38 +116,3 @@ and more content.
(reset-resources) (reset-resources)
(copy-and-check-markup-folders dirs mu false)))) (copy-and-check-markup-folders dirs mu false))))
(reset-resources)) (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)))
)
))

View file

@ -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)))
)
))