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