diff options
Diffstat (limited to 'src/pronouns')
-rw-r--r-- | src/pronouns/pages.clj | 55 | ||||
-rw-r--r-- | src/pronouns/util.clj | 12 | ||||
-rw-r--r-- | src/pronouns/web.clj | 16 |
3 files changed, 47 insertions, 36 deletions
diff --git a/src/pronouns/pages.clj b/src/pronouns/pages.clj index 3e63437..7d68422 100644 --- a/src/pronouns/pages.clj +++ b/src/pronouns/pages.clj @@ -1,5 +1,6 @@ (ns pronouns.pages (:require [clojure.string :as s] + [clojure.data.json :as json] [pronouns.util :as u] [hiccup.core :refer :all] [hiccup.util :refer [escape-html]])) @@ -60,30 +61,34 @@ (defn contact-block [] (let [twitter-name (fn [handle] [:a {:href (str "https://www.twitter.com/" handle)} (str "@" handle)])] - [:div {:class "contact"} - [:p - "Written by " - (twitter-name "morganastra") - ", whose " - [:a {:href "http://pronoun.is/ze/zir"} "pronoun.is/ze/zir"] - ". " - "Visit the project on " [:a {:href "https://github.com/witch-house/pronoun.is"} "github!"]]])) + [:div {:class "contact"} + [:p + "Written by " + (twitter-name "morganastra") + ", whose " + [:a {:href "http://pronoun.is/ze/zir"} "pronoun.is/ze/zir"] + ". " + "Visit the project on " [:a {:href "https://github.com/witch-house/pronoun.is"} "github!"]]])) (defn format-pronoun-examples [subject object possessive-determiner possessive-pronoun reflexive] (let [title "Pronoun Island: English Language Examples"] - (html - [:html - [:head - [:title title] - [:meta {:name "viewport" :content "width=device-width"}] - [:link {:rel "stylesheet" :href "/pronouns.css"}]] - [:body - (title-block title) - (examples-block subject object possessive-determiner possessive-pronoun reflexive) - (about-block) - (contact-block)]]))) + (html + [:html + [:head + [:title title] + [:meta {:name "viewport" :content "width=device-width"}] + [:link {:rel "stylesheet" :href "/pronouns.css"}]] + [:body + (title-block title) + (examples-block subject object possessive-determiner possessive-pronoun reflexive) + (about-block) + (contact-block)]]))) + + +(defn format-pronoun-json [pronouns] + (json/write-str pronouns)) (defn parse-pronouns-with-lookup [pronouns-string pronouns-table] @@ -111,9 +116,9 @@ [:body (title-block title) [:div {:class "table"} - [:p "pronoun.is is a www site for showing people how to use pronouns in English."] - [:p "here are some pronouns the site knows about:"] - [:ul links]]] + [:p "pronoun.is is a www site for showing people how to use pronouns in English."] + [:p "here are some pronouns the site knows about:"] + [:ul links]]] (contact-block)]))) (defn not-found [] @@ -121,8 +126,10 @@ "add them, or issue a pull request at " "https://github.com/witch-house/pronoun.is/blob/master/resources/pronouns.tab")) -(defn pronouns [path pronouns-table] +(defn pronouns [path pronouns-table accept] (let [pronouns (parse-pronouns-with-lookup (escape-html path) pronouns-table)] (if pronouns - (apply format-pronoun-examples pronouns) + (if (= accept :json) + (format-pronoun-json pronouns) + (apply format-pronoun-examples pronouns)) (not-found)))) diff --git a/src/pronouns/util.clj b/src/pronouns/util.clj index 4d4f928..51e8403 100644 --- a/src/pronouns/util.clj +++ b/src/pronouns/util.clj @@ -18,12 +18,12 @@ (defn minimum-unambiguous-path ([pronouns-table sections] (minimum-unambiguous-path pronouns-table sections 1)) ([pronouns-table sections number-of-sections] - (let [sections-subset (take number-of-sections sections) - results (filter #(= (take number-of-sections %) sections-subset) pronouns-table)] - (case (count results) - 0 nil - 1 (clojure.string/join "/" sections-subset) - (recur pronouns-table sections (+ number-of-sections 1)))))) + (let [sections-subset (take number-of-sections sections) + results (filter #(= (take number-of-sections %) sections-subset) pronouns-table)] + (case (count results) + 0 nil + 1 (clojure.string/join "/" sections-subset) + (recur pronouns-table sections (+ number-of-sections 1)))))) (defn abbreviate "given a list of pronoun rows, return a list of minimum unabiguous paths" diff --git a/src/pronouns/web.clj b/src/pronouns/web.clj index 23ac603..4ec1788 100644 --- a/src/pronouns/web.clj +++ b/src/pronouns/web.clj @@ -25,13 +25,17 @@ (GET "/pronouns.css" {params :params} {:status 200 - :headers {"Content-Type" "text/css"} - :body (slurp (io/resource "pronouns.css"))}) + :headers {"Content-Type" "text/css"} + :body (slurp (io/resource "pronouns.css"))}) - (GET "/*" {params :params} - {:status 200 - :headers {"Content-Type" "text/html"} - :body (pages/pronouns (:* params) pronouns-table)}) + (GET "/*" {params :params headers :headers} + (if (= "application/json" (get headers "accept")) + {:status 200 + :headers {"Content-Type" "application/json"} + :body (pages/pronouns (:* params) pronouns-table :json)} + {:status 200 + :headers {"Content-Type" "text/html"} + :body (pages/pronouns (:* params) pronouns-table :html)})) (ANY "*" [] (route/not-found (slurp (io/resource "404.html"))))) |