Skip to content


Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
tree: ce63f18431
Fetching contributors…

Cannot retrieve contributors at this time

100 lines (92 sloc) 3.823 kb
(ns tryclojure.core
(:use ring.adapter.jetty
[net.licenser.sandbox tester matcher]
[hiccup core form-helpers page-helpers]
[ring.middleware reload stacktrace params file session]
[clojure.stacktrace :only [root-cause]])
(def sandbox-tester
(extend-tester secure-tester
(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 (
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)]
(:html5 doctype)
(include-css "/resources/public/css/tryclojure.css")
[:title "TryClojure"]]
[:body {:onload "SetFocus()"}
[:h1 "Welcome to TryClojure!"]
[:table {:border "0" :width "100%" :cellpadding "10"}
[:td.sides {:width "15%" :align "left"}
"Useful links:" [:br] [:br]
[(link-to "" "")
(link-to "" "Clojure Tutorial")
(link-to "" "The Joy of Clojure")
(link-to "" "Clojure Mailing List")
(link-to "" "Clojure Reddit")])
[:br] [:br]
"Personal links:"
[:br] [:br]
(link-to "" "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\");
(form-to [:post "/"]
[:input#code_input {:name "code" :size 99}]
(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 "" "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
;(wrap-reload '(tryclojure.core))
(wrap-file (System/getProperty "user.dir"))
[""] handler))
(defn tryclj [] (run-jetty #'clojureroutes {:port 8801}))
Jump to Line
Something went wrong with that request. Please try again.