From 5287437792c34fef0dc2ec3a8b08a02283acd63c Mon Sep 17 00:00:00 2001 From: Brendon Walsh Date: Tue, 21 May 2019 14:52:27 -0400 Subject: [PATCH] Clean URLs with and without trailing slash --- src/cryogen_core/compiler.clj | 27 ++++++++++++++++++--------- src/cryogen_core/schemas.clj | 4 +++- test/cryogen_core/compiler_test.clj | 2 +- 3 files changed, 22 insertions(+), 11 deletions(-) diff --git a/src/cryogen_core/compiler.clj b/src/cryogen_core/compiler.clj index 4b9b595..e83b473 100644 --- a/src/cryogen_core/compiler.clj +++ b/src/cryogen_core/compiler.clj @@ -70,9 +70,12 @@ "Creates a URI from file name. `uri-type` is any of the uri types specified in config, e.g., `:post-root-uri`." ([file-name params] (page-uri file-name nil params)) - ([file-name uri-type {:keys [blog-prefix clean-urls?] :as params}] + ([file-name uri-type {:keys [blog-prefix clean-urls] :as params}] (let [page-uri (get params uri-type) - uri-end (if clean-urls? (string/replace file-name #"(index)?\.html" "") file-name)] + uri-end (condp = clean-urls + :trailing-slash (string/replace file-name #"(index)?\.html" "/") + :no-trailing-slash (string/replace file-name #"(index)?\.html" "") + :dirty file-name)] (cryogen-io/path "/" blog-prefix page-uri uri-end)))) (defn read-page-meta @@ -228,13 +231,19 @@ (map (partial sort-by :page-index) [navbar-pages sidebar-pages]))) (defn write-html - "When `clean-urls?` is set, appends `.html` before spit; otherwise just spits." - [file-uri {:keys [clean-urls?]} data] - (if clean-urls? - (cryogen-io/create-file - (if (= "/" file-uri) "index.html" (str file-uri ".html")) - data) - (cryogen-io/create-file file-uri data))) + "When `clean-urls` is set to: + - `:trailing-slash` appends `/index.html`. + - `:no-trailing-slash` appends `.html`. + - `:dirty` just spits." + [file-uri {:keys [clean-urls]} data] + (condp = clean-urls + :trailing-slash (cryogen-io/create-file-recursive + (cryogen-io/path file-uri "index.html") data) + :no-trailing-slash (cryogen-io/create-file (if (= "/" file-uri) + "index.html" + (str file-uri ".html")) + data) + :dirty (cryogen-io/create-file file-uri data))) (defn- print-debug-info [data] (println "DEBUG:") diff --git a/src/cryogen_core/schemas.clj b/src/cryogen_core/schemas.clj index 397646e..1cda2c7 100644 --- a/src/cryogen_core/schemas.clj +++ b/src/cryogen_core/schemas.clj @@ -54,7 +54,9 @@ :previews? s/Bool (s/optional-key :posts-per-page) s/Int (s/optional-key :blocks-per-preview) s/Int - :clean-urls? s/Bool + :clean-urls (s/enum :trailing-slash + :no-trailing-slash + :dirty) (s/optional-key :collapse-subdirs?) s/Bool :hide-future-posts? s/Bool (s/optional-key :klipse) Klipse diff --git a/test/cryogen_core/compiler_test.clj b/test/cryogen_core/compiler_test.clj index 2bc8395..54cade6 100644 --- a/test/cryogen_core/compiler_test.clj +++ b/test/cryogen_core/compiler_test.clj @@ -160,7 +160,7 @@ and more content. :posts-per-page 5 :blocks-per-preview 2 :previews? false - :clean-urls? true + :clean-urls :trailing-slash :collapse-subdirs? false :hide-future-posts? true :klipse {}