get rid of useless config var and use dynamic var for *pronouns-table*, clean up project.clj

This commit is contained in:
Morgan Astra 2016-08-09 21:05:50 -07:00
parent cd832bcd70
commit e3c5f5d366
5 changed files with 38 additions and 34 deletions

View File

@ -1,5 +1,5 @@
(defproject pronouns "1.0.0-SNAPSHOT" (defproject witch-house/pronouns "1.9.0-SNAPSHOT"
:description "FIXME: write description" :description "Pronoun.is is a web app for showing usage examples of personal pronouns in English."
:url "http://pronouns.herokuapp.com" :url "http://pronouns.herokuapp.com"
:license {:name "FIXME: choose" :license {:name "FIXME: choose"
:url "http://example.com/FIXME"} :url "http://example.com/FIXME"}
@ -8,12 +8,12 @@
[ring/ring-jetty-adapter "1.2.2"] [ring/ring-jetty-adapter "1.2.2"]
[ring.middleware.logger "0.5.0"] [ring.middleware.logger "0.5.0"]
[ring/ring-devel "1.2.2"] [ring/ring-devel "1.2.2"]
[ring-basic-authentication "1.0.5"]
[environ "0.5.0"] [environ "0.5.0"]
[hiccup "1.0.5"] [hiccup "1.0.5"]]
[com.cemerick/drawbridge "0.0.6"]]
:min-lein-version "2.0.0" :min-lein-version "2.0.0"
:plugins [[environ/environ.lein "0.2.1"]] :plugins [[environ/environ.lein "0.2.1"]
[lein-ring "0.9.7"]]
:hooks [environ.leiningen.hooks] :hooks [environ.leiningen.hooks]
:uberjar-name "pronouns-standalone.jar" :uberjar-name "pronouns-standalone.jar"
:profiles {:production {:env {:production true}}}) :profiles {:production {:env {:production true}}}
:ring {:handler pronouns.web/app})

5
src/pronouns/config.clj Normal file
View File

@ -0,0 +1,5 @@
(ns pronouns.config
(:require [pronouns.util :as u]))
(def ^:dynamic *pronouns-table*
(u/slurp-tabfile "resources/pronouns.tab"))

View File

@ -1,5 +1,6 @@
(ns pronouns.pages (ns pronouns.pages
(:require [clojure.string :as s] (:require [clojure.string :as s]
[pronouns.config :refer [*pronouns-table*]]
[pronouns.util :as u] [pronouns.util :as u]
[hiccup.core :refer :all] [hiccup.core :refer :all]
[hiccup.util :refer [escape-html]])) [hiccup.util :refer [escape-html]]))
@ -89,20 +90,22 @@
(about-block) (about-block)
(contact-block)]]))) (contact-block)]])))
(defn lookup-pronouns [pronouns-string pronouns-table] (defn lookup-pronouns [pronouns-string]
(let [inputs (s/split pronouns-string #"/") (let [inputs (s/split pronouns-string #"/")
n (count inputs)] n (count inputs)]
(println *pronouns-table*)
(if (>= n 5) (if (>= n 5)
(take 5 inputs) (take 5 inputs)
(u/table-lookup inputs pronouns-table)))) (u/table-lookup inputs *pronouns-table*))))
(defn make-link [path] (defn make-link [path]
(let [link (str "/" path) (let [link (str "/" path)
label path] label path]
[:li [:a {:href link} label]])) [:li [:a {:href link} label]]))
(defn front [pronouns-table] (defn front []
(let [abbreviations (u/abbreviate pronouns-table) (let [blah (println *pronouns-table*)
abbreviations (u/abbreviate *pronouns-table*)
links (map make-link abbreviations) links (map make-link abbreviations)
title "Pronoun Island"] title "Pronoun Island"]
(html (html
@ -135,15 +138,13 @@
(about-block) (about-block)
(contact-block)]]))) (contact-block)]])))
(defn pronouns [params pronouns-table] (defn pronouns [params]
(let [path (params :*) (let [path (params :*)
alts (or (params "or") []) alts (or (params "or") [])
pronouns (concat [path] (u/vec-coerce alts)) pronouns (concat [path] (u/vec-coerce alts))
pronoun-declensions (filter some? (map #(lookup-pronouns (escape-html %) pronoun-declensions (filter some? (map #(lookup-pronouns
pronouns-table) (escape-html %))
pronouns))] pronouns))]
(println path)
(println pronoun-declensions)
(if (seq pronoun-declensions) (if (seq pronoun-declensions)
(format-pronoun-examples pronoun-declensions) (format-pronoun-examples pronoun-declensions)
(not-found)))) (not-found))))

View File

@ -16,19 +16,20 @@
(table-lookup query-key (slurp-tabfile tabfile))) (table-lookup query-key (slurp-tabfile tabfile)))
(defn minimum-unambiguous-path (defn minimum-unambiguous-path
([pronouns-table sections] (minimum-unambiguous-path pronouns-table sections 1)) ([table sections] (minimum-unambiguous-path table sections 1))
([pronouns-table sections number-of-sections] ([table sections number-of-sections]
(let [sections-subset (take number-of-sections sections) (let [sections-subset (take number-of-sections sections)
results (filter #(= (take number-of-sections %) sections-subset) pronouns-table)] results (filter #(= (take number-of-sections %) sections-subset)
table)]
(case (count results) (case (count results)
0 nil 0 nil
1 (clojure.string/join "/" sections-subset) 1 (clojure.string/join "/" sections-subset)
(recur pronouns-table sections (+ number-of-sections 1)))))) (recur table sections (+ number-of-sections 1))))))
(defn abbreviate (defn abbreviate
"given a list of pronoun rows, return a list of minimum unabiguous paths" "given a list of pronoun rows, return a list of minimum unabiguous paths"
[pronouns-table] [table]
(map (partial minimum-unambiguous-path pronouns-table) pronouns-table)) (map (partial minimum-unambiguous-path table) table))
(defn vec-coerce [x] (defn vec-coerce [x]
(if (vector? x) x [x])) (if (vector? x) x [x]))

View File

@ -12,15 +12,11 @@
[pronouns.util :as u] [pronouns.util :as u]
[pronouns.pages :as pages])) [pronouns.pages :as pages]))
(def config {:default-server-port 5000
:pronoun-table-path "resources/pronouns.tab"})
(def pronouns-table (u/slurp-tabfile (:pronoun-table-path config)))
(defroutes app-routes (defroutes app-routes
(GET "/" [] (GET "/" []
{:status 200 {:status 200
:headers {"Content-Type" "text/html"} :headers {"Content-Type" "text/html"}
:body (pages/front pronouns-table)}) :body (pages/front)})
(GET "/pronouns.css" [] (GET "/pronouns.css" []
{:status 200 {:status 200
@ -30,7 +26,7 @@
(GET "/*" {params :params} (GET "/*" {params :params}
{:status 200 {:status 200
:headers {"Content-Type" "text/html"} :headers {"Content-Type" "text/html"}
:body (pages/pronouns params pronouns-table)}) :body (pages/pronouns params)})
(ANY "*" [] (ANY "*" []
(route/not-found (slurp (io/resource "404.html"))))) (route/not-found (slurp (io/resource "404.html")))))
@ -39,9 +35,11 @@
(fn [req] (fn [req]
(try (handler req) (try (handler req)
(catch Exception e (catch Exception e
(binding [*out* *err*]
(println e)
{:status 500 {:status 500
:headers {"Content-Type" "text/html"} :headers {"Content-Type" "text/html"}
:body (slurp (io/resource "500.html"))})))) :body (slurp (io/resource "500.html"))})))))
(def app (def app
(-> app-routes (-> app-routes
@ -51,8 +49,7 @@
params/wrap-params)) params/wrap-params))
(defn -main [] (defn -main []
(let [port (Integer. (:port env (let [port (Integer. (:port env))]
(:default-server-port config)))]
(jetty/run-jetty app {:port port}))) (jetty/run-jetty app {:port port})))
;; For interactive development: ;; For interactive development: