diff --git a/src/cryogen_core/navbar_model.clj b/src/cryogen_core/navbar_model.clj index 7042577..de05418 100644 --- a/src/cryogen_core/navbar_model.clj +++ b/src/cryogen_core/navbar_model.clj @@ -2,11 +2,21 @@ (:require [clojure.string :as s])) -(defn- uri-level [uri] - (count (s/split uri #"/"))) +(defn uri-level [uri] + (- (count + (s/split uri #"/")) + 1) + ) -(defn- filter-pages-for-uri [uri pages] - (filter #(s/starts-with? (:uri %) uri) pages)) +(defn filter-pages-for-uri [uri pages] + (let [html? (s/ends-with? uri ".html") + clean? (s/ends-with? uri "/") + clean-uri (cond + html? (subs uri 0 (- (count uri) 5)) + clean? (subs uri 0 (- (count uri) 1)) + :default uri)] + (filter #(s/starts-with? (:uri %) clean-uri) pages)) + ) (defn build-nav-map-level "builds one level of nav-map and recurs to next level." diff --git a/test/cryogen_core/navbar_model_test.clj b/test/cryogen_core/navbar_model_test.clj index 2251016..c4887aa 100644 --- a/test/cryogen_core/navbar_model_test.clj +++ b/test/cryogen_core/navbar_model_test.clj @@ -13,28 +13,60 @@ :content uri :page-index page-index :children children}) + +(def pages-clean [(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) + ]) + +(def pages-dirty [(page "/pages/nav1.html" 0) + (page "/pages/nav1/nav11.html" 1) + (page "/pages/nav1/nav13.html" 3) + (page "/pages/nav1/nav11/nav112.html" 2) + (page "/pages/nav1/nav12.html" 2) + (page "/pages/nav1/nav11/xnav111.html" 1) + ]) +(deftest test-uri-level + (testing + (is (= 2 (sut/uri-level "/pages/nav1/"))) + (is (= 2 (sut/uri-level "/pages/nav1.html"))) + )) + +(deftest test-filter-pages-for-uri + (testing + (is (= 6 (count (sut/filter-pages-for-uri "/pages/nav1/" pages-clean)))) + (is (= 6 (count (sut/filter-pages-for-uri "/pages/nav1.html" pages-dirty)))) + )) + (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)] - )] + (let [expected-clean [(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)] + )] + expected-dirty [(enhanced-page + "/pages/nav1.html" 0 + [(enhanced-page + "/pages/nav1/nav11.html" 1 + [(page "/pages/nav1/nav11/xnav111.html" 1) + (page "/pages/nav1/nav11/nav112.html" 2)]) + (page "/pages/nav1/nav12.html" 2) + (page "/pages/nav1/nav13.html" 3)] + )] ] - (is (= expected - (sut/build-nav-map pages))) + (is (= expected-clean + (sut/build-nav-map pages-clean))) + (is (= expected-dirty + (sut/build-nav-map pages-dirty))) ) ))