Added navmap feature.

Navmap will allow nested navigation for pages. To 
* add a page to navmap, you've to add `{:navmap? true}` to pages meta.
* define navigation hierarchy you can use directory-structure (first
level will be in /pages/nav1.md, second level will be in
/pages/nav1/nav11.md)
* use navmap, you can 
  * access navmap-pages (sequence of pages) in your template, same as
you've used navbar-pages and
  * access navigation childs as :navmap-children in specific pages (eg.
nav1 will contain a sequence of one page representing nav11).
This commit is contained in:
Michael Jerger 2017-01-04 09:33:34 +01:00
parent 78e4a6d067
commit 3c415f48e0
2 changed files with 73 additions and 4 deletions

View file

@ -14,7 +14,8 @@
[get-resource find-assets create-folder create-file-recursive create-file wipe-public-folder
copy-resources copy-resources-from-theme path]]
[cryogen-core.sitemap :as sitemap]
[cryogen-core.rss :as rss])
[cryogen-core.rss :as rss]
[clojure.inspector :as inspector])
(:import java.util.Locale))
(cache-off!)
@ -216,6 +217,36 @@
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)
]
(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]
@ -470,6 +501,7 @@
home-pages (filter #(boolean (:home? %)) pages)
pages-without-home (filter #(boolean (not (:home? %))) pages)
[navbar-pages sidebar-pages] (group-pages pages-without-home)
navmap-pages (build-nav-map pages-without-home)
posts-by-tag (group-by-tags posts)
posts (tag-posts posts config)
latest-posts (->> posts (take recent-posts) vec)
@ -480,6 +512,7 @@
: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
:home-page (if (not-empty home-pages)
(first home-pages)
@ -491,9 +524,12 @@
:rss-uri (path "/" blog-prefix rss-name)
:site-url (if (.endsWith site-url "/") (.substring site-url 0 (dec (count site-url))) site-url)
:theme-path (str "file:resources/templates/themes/" (:theme config))})]
(println (blue "debug info"))
(println "\t-->" (cyan navbar-pages))
(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! (:theme-path params))
(wipe-public-folder keep-files)
(println (blue "copying theme resources"))

View file

@ -116,3 +116,36 @@ and more content.
(reset-resources)
(copy-and-check-markup-folders dirs mu false))))
(reset-resources))
(defn- page [uri]
{:navmap? true, :uri uri :content uri})
(defn- enhanced-page [uri children]
{:navmap? true, :uri uri :content uri
:navmap-children children})
(deftest test-navmap-pages
(testing
"No pages or posts nothing to copy"
(let [pages [{:navmap? false, :content "1"}
(page "/pages/nav1/")
(page "/pages/nav1/nav11/")
(page "/pages/nav1/nav12/")
(page "/pages/nav1/nav13/")
(page "/pages/nav1/nav11/nav111/")
(page "/pages/nav1/nav11/nav112/")]
expected [(enhanced-page
"/pages/nav1/"
[(enhanced-page
"/pages/nav1/nav11/"
[(page "/pages/nav1/nav11/nav111/")
(page "/pages/nav1/nav11/nav112/")])
(page "/pages/nav1/nav12/")
(page "/pages/nav1/nav13/")]
)]
]
(is (= expected
(build-nav-map pages)))
)
))