Created
September 16, 2011 11:17
-
-
Save pepijndevos/1221889 to your computer and use it in GitHub Desktop.
Micro Wiki
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
(ns wiki.core | |
(:use net.cgrand.moustache | |
ring.adapter.jetty | |
ring.util.response | |
[ring.middleware params stacktrace reload] | |
hiccup.core | |
com.ashafa.clutch) | |
(:require [clojure.string :as s]) | |
(:import org.pegdown.PegDownProcessor)) | |
(def db (or (get-database "wiki") (create-database "wiki"))) | |
(defn markup [text] | |
(s/replace (.markdownToHtml (PegDownProcessor.) text) | |
#"(?:[A-Z][a-z]+){2,}" "<a href=\"/$0\">$0</a>")) | |
(defn page [title content rev] | |
(response | |
(html | |
[:html | |
[:head | |
[:title title]] | |
[:body | |
[:h1 title] | |
[:div ((fnil markup "") content)] | |
[:hr] | |
[:form {:method "POST" :action (str "/" title)} | |
[:textarea {:name "content"} content] | |
[:br] | |
[:input {:type "hidden", :value rev :name "rev"}] | |
[:input {:type "submit", :value "Submit!"}]]]]))) | |
(defn show [_ title] | |
(let [{:keys [content _rev]} (get-document title)] | |
(page title content _rev))) | |
(defn update [{{content "content" rev "rev"} :params} title] | |
(if (= rev "") | |
(create-document {:content content} title) | |
(update-document {:_rev rev :_id title :content content})) | |
(show {} title)) | |
(defn wdb [f] | |
(fn [req] | |
(with-db db | |
(f req)))) | |
(def wiki (app | |
wrap-params | |
wrap-stacktrace | |
(wrap-reload ['wiki.core]) | |
wdb | |
[[title #"(?:[A-Z][a-z]+){2,}"]] {:get (delegate show title) | |
:post (delegate update title)} | |
[&] (constantly (redirect "/MainPage")))) | |
(defn serve [] | |
(future (run-jetty #'wiki {:port 8080}))) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
(defproject wiki "0.0.1-SNAPSHOT" | |
:description "TODO: add summary of your project" | |
:dependencies [[clojure "1.2.1"] | |
[hiccup "0.3.6"] | |
[ring "0.3.11"] | |
[com.ashafa/clutch "0.2.4"] | |
[org.pegdown/pegdown "1.0.2"] | |
[net.cgrand/moustache "1.0.0"]] | |
:source-path "") |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment