Merge pull request #94 from witch-house/slash-or

Support `:or` in path, in addition to parameter
This commit is contained in:
Morgan Astra 2018-11-15 22:33:16 -08:00 committed by GitHub
commit 26148e3c23
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 46 additions and 21 deletions

View File

@ -136,13 +136,20 @@
examples
(footer-block)]])))
(defn lookup-pronouns [pronouns-string]
(defn table-lookup* [pronouns-string]
(let [inputs (s/split pronouns-string #"/")
n (count inputs)]
(if (>= n 5)
(take 5 inputs)
(u/table-lookup inputs @pronouns-table))))
(defn lookup-pronouns
"Given a seq of pronoun sets, look up each set in the pronouns table"
[pronoun-sets]
(->> pronoun-sets
(map (comp table-lookup* escape-html))
(filter some?)))
(defn make-link [path]
(let [link (str "/" path)
label path]
@ -184,8 +191,9 @@
[:ul links]]]
(footer-block)])))
(defn not-found []
(let [title "Pronoun Island: English Language Examples"]
(defn not-found [path]
(let [title "Pronoun Island: English Language Examples"
or-re #"/[oO][rR]/"]
(html
[:html
[:head
@ -194,18 +202,24 @@
[:link {:rel "stylesheet" :href "/pronouns.css"}]]
[:body
(header-block title)
[:div {:class "section examples"}
[:p [:h2 (str "We couldn't find those pronouns in our database. "
"If you think we should have them, please reach out!")]]]
[:div {:class "section examples"}
[:p [:h2 "We couldn't find those pronouns in our database :("]
"If you think we should have them, please reach out!"]
(when (re-find or-re path)
(let [alts (s/split path or-re)
new-path (str "/" (s/join "/:OR/" alts))]
[:div
"Did you mean: "
(href new-path
(str "pronoun.is"
new-path))]))]
(footer-block)]])))
(defn pronouns [params]
(let [path (params :*)
alts (or (params "or") [])
pronouns (concat [path] (u/vec-coerce alts))
pronoun-declensions (filter some? (map #(lookup-pronouns
(escape-html %))
pronouns))]
(if (seq pronoun-declensions)
(format-pronoun-examples pronoun-declensions)
(not-found))))
param-alts (u/vec-coerce (or (params "or") []))
path-alts (s/split path #"/:[oO][rR]/")
pronouns (lookup-pronouns (concat path-alts param-alts))]
(if (seq pronouns)
(format-pronoun-examples pronouns)
(not-found path))))

View File

@ -3,10 +3,21 @@
[clojure.test :refer [deftest testing is are]]))
(deftest prose-comma-list
(testing "prose-comma-list turns a list of strings into a prose list"
(are [call result] (= call result)
(pages/prose-comma-list ["foo"]) "foo"
(pages/prose-comma-list ["foo" "bar"]) "foo and bar"
(pages/prose-comma-list ["foo" "bar" "baz"]) "foo, bar, and baz"
(pages/prose-comma-list ["foo" "bar" "baz" "bobble"]) "foo, bar, baz, and bobble"
(pages/prose-comma-list []) "")))
(testing "`prose-comma-list` turns a list of strings into a prose list"
(are [v s] (= (pages/prose-comma-list v) s)
["foo" "bar" "baz" "bobble"] "foo, bar, baz, and bobble"
["foo" "bar" "baz"] "foo, bar, and baz"
["foo" "bar"] "foo and bar"
["foo"] "foo"
[] "")))
(deftest lookup-pronouns
(are [pronoun-strs pronouns]
(= (pages/lookup-pronouns pronoun-strs)
pronouns)
["she/her"] '(["she" "her" "her" "hers" "herself"])
["she" "they"] '(["she" "her" "her" "hers" "herself"]
["they" "them" "their" "theirs" "themselves"])
["she/her" "foo/bar"] '(["she" "her" "her" "hers" "herself"])
["foo/bar"] '()
["a/b/c/d/e"] '(("a" "b" "c" "d" "e"))))