new templating stuff

master
tilde.town 2015-07-26 06:23:01 +00:00
parent d527b890be
commit 773efb7778
4 changed files with 54 additions and 22 deletions

View File

@ -1,2 +1,2 @@
#!/bin/bash #!/bin/bash
who | cut -d' ' -f1 | sort -u | wc -l who | cut -d' ' -f1 | sort -u | wc -l | xargs echo "active_user_count=" | sed 's/ //'

View File

@ -0,0 +1,18 @@
#!/bin/bash
export PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games:/usr/local/games
source /var/local/venvs/tildetown/bin/activate
stats=/usr/local/bin/generate_stats
template=/var/www/tilde.town/template.index.html
mustache=/var/local/tildetown/scripts/mustache.hy
output_path=/var/www/tilde.town/index.html
# hello from datagrok
#
# generate_stats was breaking and outputting no lines. i dunno how to fix that,
# but i can un-disaappear the homepage by saying "output empty json if broken"
#
# generate_stats | hy $mustache $template > $output_path
($stats || echo '{}') | hy $mustache $template > $output_path

View File

@ -0,0 +1,16 @@
(import [json [loads]])
(import [sys [argv stdin]])
(import [pystache [render]])
(defn slurp [filename]
(try
(.read (apply open [filename "r"] {"encoding" "utf-8"}))
(catch [e Exception]
"")))
(if (= __name__ "__main__")
(let [[template (-> (get argv 1)
slurp)]
[data (-> (.read stdin)
loads)]]
(print (render template data))))

View File

@ -1,14 +1,13 @@
(import [json [dumps]])
(import [os [listdir]]) (import [os [listdir]])
(import [os.path [getmtime]]) (import [os.path [getmtime]])
(import [datetime [datetime]]) (import [datetime [datetime]])
(import [sh [find]]) (import [sh [find facter]])
;; this script emits HTML on standard out that constitutes a user ;; this script emits json on standard out that has information about tilde.town
;; list. It denotes who has not updated their page from the ;; users. It denotes who has not updated their page from the default. It also
;; default. It also reports the time this script was run. ;; reports the time this script was run. The user list is sorted by public_html update time.
(def timestamp (.strftime (.now datetime) "%Y-%m-%d %H:%M:%S"))
(defn slurp [filename] (defn slurp [filename]
(try (try
@ -18,13 +17,8 @@
(def default-html (slurp "/etc/skel/public_html/index.html")) (def default-html (slurp "/etc/skel/public_html/index.html"))
(defn dir->html [username] (defn default? [username]
(let [[default (= default-html (slurp (.format "/home/{}/public_html/index.html" username)))]] (= default-html (slurp (.format "/home/{}/public_html/index.html" username))))
(.format "<li><a href=\"http://tilde.town/~{}\">{}</a> {}</li>"
username username
(if default
"(default :3)"
""))))
(defn bounded-find [path] (defn bounded-find [path]
;; find might return 1 but still have worked fine (because of dirs it can't ;; find might return 1 but still have worked fine (because of dirs it can't
@ -51,13 +45,17 @@
(defn user-generator [] (->> (listdir "/home") (defn user-generator [] (->> (listdir "/home")
(filter (fn [f] (and (not (= f "ubuntu")) (not (= f "poetry"))))))) (filter (fn [f] (and (not (= f "ubuntu")) (not (= f "poetry")))))))
(def user-list (->> (user-generator) (if (= __name__ "__main__")
(let [[users (->> (user-generator)
sort-user-list sort-user-list
reversed reversed
(map dir->html) (map (fn [un] {"username" un
(.join "\n"))) "default" (default? un)}))
list)]
(print (.format "our esteemed users ({})<br> <sub>generated at {}</sub><br><ul>{}</ul>" [data {"users" users
(len (list (user-generator))) "active_user_count" (-> (. (facter "active_user_count") stdout)
timestamp .strip
user-list)) int)
"generated_at" (.strftime (.now datetime) "%Y-%m-%d %H:%M:%S")
"num_users" (len users)}]]
(print (dumps data))))