Skip to content
This repository
tree: ce63f18431
Fetching contributors…

Cannot retrieve contributors at this time

file 100 lines (92 sloc) 3.823 kb
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100
(ns tryclojure.core
  (:use ring.adapter.jetty
net.licenser.sandbox
[net.licenser.sandbox tester matcher]
[hiccup core form-helpers page-helpers]
[ring.middleware reload stacktrace params file session]
net.cgrand.moustache
[clojure.stacktrace :only [root-cause]])
  (:import java.io.StringWriter
org.apache.commons.lang.StringEscapeUtils
java.util.concurrent.TimeoutException))

(def sandbox-tester
     (extend-tester secure-tester
(whitelist
(function-matcher 'println 'print 'pr 'prn 'var 'print-doc 'doc 'throw
'def 'defn 'defmacro))))

(def sc (stringify-sandbox (new-sandbox-compiler :tester sandbox-tester
:timeout 1000)))

(defn execute-text [txt]
  (let [writer (java.io.StringWriter.)
result (try
(pr-str ((sc txt) {'*out* writer}))
(catch TimeoutException _ "Execution Timed Out!")
(catch SecurityException _ "Disabled for security purposes.")
(catch Exception e (str (root-cause e))))]
    (str writer result) ))

(defn format-links [& links] (interpose [:br] links))

(defn fire-html [text]
  (let [result (.replaceAll (if (seq text) text "") "\n" "<br />")
ftext (if (seq result) (html [:p result]) result)]
    (html
     (:html5 doctype)
     (include-css "/resources/public/css/tryclojure.css")
     [:head
      [:title "TryClojure"]]
     [:body {:onload "SetFocus()"}
      [:tr]
      [:h1 "Welcome to TryClojure!"]
      [:table {:border "0" :width "100%" :cellpadding "10"}
       [:tr]
       [:td.sides {:width "15%" :align "left"}
"Useful links:" [:br] [:br]
(unordered-list
[(link-to "http://clojure.org" "clojure.org")
(link-to "http://java.ociweb.com/mark/clojure/article.html" "Clojure Tutorial")
(link-to "http://joyofclojure.com/" "The Joy of Clojure")
(link-to "http://groups.google.com/group/clojure" "Clojure Mailing List")
(link-to "http://www.reddit.com/r/clojure" "Clojure Reddit")])
[:br] [:br]
"Personal links:"
[:br] [:br]
(format-links
(link-to "http://github.com/Raynes/tryclojure" "This site's source code"))]
       [:td {:width "60%"}
[:div#code.scroll ftext [:a#bottom_div]]
[:script {:type "text/javascript"} "var objDiv = document.getElementById(\"bottom_div\");
objDiv.scrollIntoView(false);
"]
(form-to [:post "/"]
[:input#code_input {:name "code" :size 99}]
[:p]
(submit-button "Make Magic Happen"))
(form-to [:post "/?clear=true"]
(submit-button "Clear REPL"))]
       [:script {:type "text/javascript"} "document.getElementById(\"code_input\").focus();"]
       [:td {:width "15%" :align "left"}
[:p (str "This is a largely HTML based web application for executing Clojure code and seeing the result. "
"Enter your code and press enter (or Make Magic Happen) and your code will be executed. "
"It works just like a normal REPL.")]
[:p "Written by Anthony Simpson (Raynes)."]
[:p "Powered by " (link-to "http://github.com/Licenser/clj-sandbox" "clj-sandbox.")]]]])))
  
(defn handler [{fparams :form-params qparams :query-params session :session}]
  (let [code (StringEscapeUtils/escapeHtml (if (seq (fparams "code")) (fparams "code") ""))
result (StringEscapeUtils/escapeHtml (if (seq code) (execute-text (fparams "code")) ""))
sess-history (:history session)
history (when-not (= "true" (qparams "clear"))
(if (seq result)
(str sess-history "=> " code "<br />" result "<br />")
(when (and (seq sess-history) ()) (str sess-history "<br />"))))]
    {:status 200
     :headers {"Content-Type" "text/html"}
     :body (fire-html history)
     :session {:history history}}))

(def clojureroutes
     (app
      ;(wrap-reload '(tryclojure.core))
      (wrap-session)
      (wrap-file (System/getProperty "user.dir"))
      (wrap-params)
      (wrap-stacktrace)
      [""] handler))

(defn tryclj [] (run-jetty #'clojureroutes {:port 8801}))
Something went wrong with that request. Please try again.