Skip to content

Instantly share code, notes, and snippets.

@pepijndevos
Created September 16, 2011 11:17
Show Gist options
  • Star 3 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save pepijndevos/1221889 to your computer and use it in GitHub Desktop.
Save pepijndevos/1221889 to your computer and use it in GitHub Desktop.
Micro Wiki
(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})))
(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