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"
:description "FIXME: write description"
(defproject witch-house/pronouns "1.9.0-SNAPSHOT"
:description "Pronoun.is is a web app for showing usage examples of personal pronouns in English."
:url "http://pronouns.herokuapp.com"
:license {:name "FIXME: choose"
:url "http://example.com/FIXME"}
@ -8,12 +8,12 @@
[ring/ring-jetty-adapter "1.2.2"]
[ring.middleware.logger "0.5.0"]
[ring/ring-devel "1.2.2"]
[ring-basic-authentication "1.0.5"]
[environ "0.5.0"]
[hiccup "1.0.5"]
[com.cemerick/drawbridge "0.0.6"]]
[hiccup "1.0.5"]]
: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]
: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
(:require [clojure.string :as s]
[pronouns.config :refer [*pronouns-table*]]
[pronouns.util :as u]
[hiccup.core :refer :all]
[hiccup.util :refer [escape-html]]))
@ -89,20 +90,22 @@
(about-block)
(contact-block)]])))
(defn lookup-pronouns [pronouns-string pronouns-table]
(defn lookup-pronouns [pronouns-string]
(let [inputs (s/split pronouns-string #"/")
n (count inputs)]
(println *pronouns-table*)
(if (>= n 5)
(take 5 inputs)
(u/table-lookup inputs pronouns-table))))
(u/table-lookup inputs *pronouns-table*))))
(defn make-link [path]
(let [link (str "/" path)
label path]
[:li [:a {:href link} label]]))
(defn front [pronouns-table]
(let [abbreviations (u/abbreviate pronouns-table)
(defn front []
(let [blah (println *pronouns-table*)
abbreviations (u/abbreviate *pronouns-table*)
links (map make-link abbreviations)
title "Pronoun Island"]
(html
@ -135,15 +138,13 @@
(about-block)
(contact-block)]])))
(defn pronouns [params pronouns-table]
(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-table)
pronouns))]
(println path)
(println pronoun-declensions)
pronoun-declensions (filter some? (map #(lookup-pronouns
(escape-html %))
pronouns))]
(if (seq pronoun-declensions)
(format-pronoun-examples pronoun-declensions)
(not-found))))

View File

@ -16,19 +16,20 @@
(table-lookup query-key (slurp-tabfile tabfile)))
(defn minimum-unambiguous-path
([pronouns-table sections] (minimum-unambiguous-path pronouns-table sections 1))
([pronouns-table sections number-of-sections]
([table sections] (minimum-unambiguous-path table sections 1))
([table sections number-of-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)
0 nil
1 (clojure.string/join "/" sections-subset)
(recur pronouns-table sections (+ number-of-sections 1))))))
(recur table sections (+ number-of-sections 1))))))
(defn abbreviate
"given a list of pronoun rows, return a list of minimum unabiguous paths"
[pronouns-table]
(map (partial minimum-unambiguous-path pronouns-table) pronouns-table))
[table]
(map (partial minimum-unambiguous-path table) table))
(defn vec-coerce [x]
(if (vector? x) x [x]))

View File

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