Skip to content

Commit

Permalink
4clojure repl
Browse files Browse the repository at this point in the history
  • Loading branch information
mknoszlig committed Oct 25, 2012
1 parent 7c64c0d commit 6708f84
Show file tree
Hide file tree
Showing 2 changed files with 73 additions and 0 deletions.
5 changes: 5 additions & 0 deletions src/fourclojure/client.clj
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,11 @@
:accept :json
:form-params {:id problem-id :code body}}))

(defn get-problem [id]
(json/parse-string
(:body
(http/get (str "http://www.4clojure.com/api/problem/" id)
{:accept :json}))))

(defn run-and-submit
[problem-id
Expand Down
68 changes: 68 additions & 0 deletions src/fourclojure/repl.clj
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
(ns fourclojure.repl
(:require [fourclojure.client :as client]
[cheshire.core :as json]
[clojure.string :as string]))

(def ^:dynamic *current-problem* nil)
(def ^:dynamic *cookie* nil)

(defn clean [s]
(-> s
(string/replace #"\r" "")
(string/replace #"<.?p>" "")
(string/replace #"<.?code>" "")
(string/replace #"<a.*?href=\"(.*?)\".*?>(.*?)<\/a>"
"$2 [$1]")
(string/replace #"<.?span.*?>" "")))

(defn keywordize
([source]
(keywordize source {}))
([source seed]
(into seed
(map (fn [[k v]] [(keyword k) v])
source))))

(defn parse-problem [problem-data id]
(keywordize problem-data {:id id}))

(defn parse-response [response]
(keywordize (json/parse-string response)))

(defn remote-check [code]
(parse-response
(:body (client/submit *cookie*
(:id *current-problem*)
code))))

;;
;; Commands
;;

(defn view []
(if *current-problem*
(let [{:keys [id title description tests]} *current-problem*]
(println
(str
id ". "title "\n"
(clean description) "\n\n"
"Tests:" "\n"
(apply str (interpose "\n"
(map clean tests))))))
(println "No problem selected - use select first.")))


(defn select [id]
(def ^:dynamic *current-problem*
(parse-problem (client/get-problem id) id))
(view))

(defmacro propose [body]
`(let [rsp# (remote-check '~body)]
(println
(str
(if (string/blank? (:message rsp#))
(str "/o\\ Error /o\\\n"
(:error rsp#))
(str "\\o/ Success \\o/\n"
(clean (:message rsp#))))))))

0 comments on commit 6708f84

Please sign in to comment.