Fixed a nasty breaking bug when the side bar file wasn't named as expected!

This commit is contained in:
simon 2016-12-20 12:13:04 +00:00
parent 2fd03caade
commit a8929894d4
3 changed files with 28 additions and 8 deletions

View file

@ -124,6 +124,7 @@
(merge (util/standard-params request) (merge (util/standard-params request)
{:title page {:title page
:page page :page page
:side-bar (util/local-links (util/md->html "/content/_side-bar.md"))
:content (util/local-links (util/md->html file-name)) :content (util/local-links (util/md->html file-name))
:editable true}))) :editable true})))
true (response/redirect (str "/edit?page=" page))))) true (response/redirect (str "/edit?page=" page)))))

View file

@ -37,17 +37,23 @@
(md/md-to-html-string (io/slurp-resource filename))) (md/md-to-html-string (io/slurp-resource filename)))
;; Error to show if text to be rendered is nil.
(def no-text-error "No text: does the file exist?")
(defn local-links (defn local-links
"Rewrite text in `html-src` surrounded by double square brackets as a local link into this wiki." "Rewrite text in `html-src` surrounded by double square brackets as a local link into this wiki."
[^String html-src] [^String html-src]
(cs/replace html-src #"\[\[[^\[\]]*\]\]" (if html-src
#(let [text (clojure.string/replace %1 #"[\[\]]" "") (cs/replace html-src #"\[\[[^\[\]]*\]\]"
encoded (url-encode text) #(let [text (clojure.string/replace %1 #"[\[\]]" "")
;; I use '\_' to represent '_' in wiki markup, because encoded (url-encode text)
;; '_' is meaningful in Markdown. However, this needs to ;; I use '\_' to represent '_' in wiki markup, because
;; be stripped out when interpreting local links. ;; '_' is meaningful in Markdown. However, this needs to
munged (cs/replace encoded #"%26%2395%3B" "_")] ;; be stripped out when interpreting local links.
(format "<a href='wiki?page=%s'>%s</a>" munged text)))) munged (cs/replace encoded #"%26%2395%3B" "_")]
(format "<a href='wiki?page=%s'>%s</a>" munged text)))
no-text-error))
(defn standard-params (defn standard-params

View file

@ -0,0 +1,13 @@
(ns smeagol.test.util
(:use clojure.test
ring.mock.request
smeagol.util))
(deftest test-local-links
(testing "Rewriting of local links"
(is (= (local-links nil) no-text-error) "Should NOT fail with a no pointer exception!")
(is (= (local-links "") "") "Empty string should pass through unchanged.")
(is (= (local-links "[[froboz]]") "<a href='wiki?page=froboz'>froboz</a>") "Local link should be rewritten.")
(let [text (str "# This is a heading"
"[This is a foreign link](http://to.somewhere)")]
(is (= (local-links text) text) "Foreign links should be unchanged"))))