Skip to content

Instantly share code, notes, and snippets.

@tomjack
Created February 12, 2011 05:43
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save tomjack/1de0bec2aafc936d39b3 to your computer and use it in GitHub Desktop.
Save tomjack/1de0bec2aafc936d39b3 to your computer and use it in GitHub Desktop.
(fn [request response]
(let [
noop (.setCharacterEncoding response "UTF-8")
appId (.get com.google.appengine.api.utils.SystemProperty/applicationId)
urCode "(fn [request response] \"HelloWorld\")"
id (.getParameter request "id")
newCode (.getParameter request "code")
resource (.getParameter request "resource")
userService (com.google.appengine.api.users.UserServiceFactory/getUserService)
currUser (.getCurrentUser userService)
hasCode (.equals "add" (.getParameter request "add"))
service (com.google.appengine.api.datastore.DatastoreServiceFactory/getDatastoreService)
sortDir (com.google.appengine.api.datastore.Query$SortDirection/valueOf "DESCENDING")
limit (com.google.appengine.api.datastore.FetchOptions$Builder/withLimit 32)
bootstrapKey (com.google.appengine.api.datastore.KeyFactory/createKey "BootStrap" "RootHandler")
rootKeyS (.getProperty (.get service bootstrapKey) "rootHandler")
query
(doto
(com.google.appengine.api.datastore.Query. "BootStrap")
(.addSort "date" sortDir))
codeKey
(if id
(com.google.appengine.api.datastore.KeyFactory/createKey "BootStrap" (Long. id)))
eCode
(if id (.get service codeKey))
code
(if id
(.getValue (.getProperty eCode "content"))
urCode)
resourceVal
(if id
(.getProperty eCode "resource")
(if resource resource "/test"))
escapedCode
(.replace
(.replace code "<" "\u0026lt;")
"<" "<")
list (.asIterable (.prepare service query) limit)
nextId (if hasCode
(let
[text (com.google.appengine.api.datastore.Text. newCode)
e (doto
(com.google.appengine.api.datastore.Entity. "BootStrap")
(.setProperty "resource" resource)
(.setProperty "content" text)
(.setProperty "user" currUser)
(.setProperty "date" (java.util.Date.)))]
(.getId (.put service e))))]
(if (.equals "ajax" (.getParameter request "format"))
(let
[noop (.getParameter request "resource")]
(str ""))
(hiccup.core/html "<!DOCTYPE html>"
[:html
[:head
[:title appId " WebEditor"]
[:meta {:http-equiv "Content-Type" :content "text/html;charset=utf-8"}]
[:script {:type "text/javascript" :src "https://ajax.googleapis.com/ajax/libs/jquery/1.4.4/jquery.min.js"}]]
[:body {:style "background-color:#D1B15E;" }
[:div {:style "background-color:white; padding:10px; margin:10px;" }
(if (.isUserLoggedIn userService)
[:div "Hello " (.getNickname currUser) " ("
[:a {:href (.createLogoutURL userService "/")} "logout"] ")"]
[:a {:href (.createLoginURL userService "/")} "login"])
[:h1 [:a {:href "/"} appId] " WebEditor"]
[:table
[:tr
[:td {:style "vertical-align: top"}
"current " [:a {:href "/"} "RootHandler"] ": "
[:a {:href (str "/edit/" rootKeyS)} rootKeyS]
" (evaluated by "
[:a {:href "http://code.google.com/p/bigtablenomic/source/browse/trunk/src/main/webapp/index.jsp"}
"index.jsp"] ")"
[:h2 "RecentEdits"]
[:form {:action "/diff" :method "post"}
[:table.dataTable {:style "font-size: 10pt"}
[:thead
[:tr {:style "background-color:#D1B15E"}
[:th
[:input {:type "submit" :name "diff" :value "diff"
:style "padding: 0px; margin: 0px"}]]
[:th "resource"]
[:th "date"]
[:th "user"]
[:th "size"]
[:th {:colspan 3} "do"]]]
[:tbody
(map
(fn [e]
(let [
id (.getId (.getKey e))
nowSec (int (/ (.getTime (java.util.Date.)) 1000))
date (.getProperty e "date")
u (.getProperty e "user")
user (str "" (.getProperty e "user"))
userShort
(if (< (.length user) 10)
user
(str (.substring user 0 9) "..."))
secToNow (- nowSec (int (/ (.getTime date) 1000)))
content (.getProperty e "content")
contentVal (if content (.getValue content))
contentLen (if content (.length contentVal))]
[:tr
[:td [:input {:type "checkbox" :name "revision"
:value id}]]
[:td [:a {:href (.getProperty e "resource")}
(.getProperty e "resource")]]
[:td
(if (< secToNow 60)
(str secToNow " sec ago")
(if (< secToNow 3600)
(str (int (/ secToNow 60)) " min ago")
(if (< secToNow (* 3600 24))
(str (int (/ secToNow 3600)) " h ago")
(.format
(java.text.SimpleDateFormat. "yyyy-MM-dd")
date))))]
[:td userShort]
[:td contentLen]
[:td [:a {:href (str "/edit/" id)} "edit"]]
[:td [:a {:href (str "/eval/" id)} "eval"]]
[:td
[:a {:href
(str "http://validator.w3.org/check?uri="
(.substring (.getRequestURL request)
0 (- (.length (.getRequestURL request))
(.length (.getRequestURI request))))
"/eval/" id)
:target "validate"}
"validate"]]]))
list)]]
[:script {:type "text/javascript"} "
$('.dataTable tbody tr:nth-child(even)').css('background-color','#F8E8C0');"]]]
[:td {:style "vertical-align: top"}
[:form {:method "post"}
[:table
[:tr
[:td "target resource: "
[:input {:name "resource" :value resourceVal}]]
[:td {:style "vertical-align: top; text-align:right"}
[:input {:type "submit" :name "add" :value "add"}]]]
[:tr
[:td {:colspan 2}
[:textarea {:name "code" :cols 80 :rows 40} code]]]]]]]]
[:p "View Source - Edit Source"]]]]))))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment