(use awful irregex posix utils lowdown srfi-13) (define wiki-dir "wiki") (define script-dir "wiki-libs") (create-directory wiki-dir 'recursively) (define start-page "WelcomeVisitor") (define all-pages "AllPages") (enable-sxml #t) ;; Grab the javascript files from hellojs.org's demo / the git repo (define-page (irregex "/[^/]*") (lambda (path) (with-request-variables ((edit (nonempty as-boolean)) (content (nonempty as-string)) (search (nonempty as-string))) (when (equal? path "/") (redirect-to start-page)) (let ((edit-path (string-append path "?edit=1")) (wiki-file (make-pathname wiki-dir path)) (page-name (string-trim path #\/))) `((script (@ (src ,(make-pathname script-dir "jquery.min.js")))) (script (@ (src ,(make-pathname script-dir "jquery-ui.min.js")))) (script (@ (src ,(make-pathname script-dir "rangy-core.js")))) (script (@ (src ,(make-pathname script-dir "hallo.js")))) (script (@ (src ,(make-pathname script-dir "showdown.js")))) (script (@ (src ,(make-pathname script-dir "to-markdown.js")))) (script (@ (src ,(make-pathname script-dir "editor.js")))) (link (@ (rel stylesheet) (href ,(make-pathname script-dir "css/font-awesome.css")))) ;; XXX ugh... (h1 (a (@ (href ,(string-append path "?search=" page-name))) ,page-name)) (hr) ,(cond ((equal? page-name all-pages) `((ul ,@(map (lambda (f) `(li (a (@ (href ,(pathname-file f))) ,(pathname-file f)))) (find-files wiki-dir))) (hr))) (edit (let ((content (if (file-exists? wiki-file) (read-all wiki-file) "This is a new page, please edit"))) `((div (@ (class editable)) ,(markdown->sxml content)) (div (@ (id "wiki-dialog"))) (form (@ (action ,path) (method post)) (textarea (@ (id "source") (name "content") (cols 80) (rows 25) (hidden)) ,content) (hr) (input (@ (type "submit") (value "Save"))))))) (content (with-output-to-file wiki-file (cut print content)) (redirect-to path)) (search `((h2 "Search results for " ,search) (ul ,(with-input-from-pipe (sprintf "grep -ri ~a ~s" (qs search) wiki-dir) (lambda () (map (lambda (l) (let* ((r (string-split l ":")) (p (pathname-file (car r))) (c (cdr r))) `(li (a (@ (href ,p)) ,p) " : " ,c))) (read-lines))))))) (else `(,(if (file-exists? wiki-file) `(,(with-input-from-file wiki-file markdown->sxml) (hr) (a (@ (href ,edit-path)) "Edit")) (redirect-to edit-path)) " | ") )) (a (@ (href ,start-page)) ,start-page) " | " (a (@ (href ,all-pages)) ,all-pages) " | " (form (@ (action ,path) (method post)) (input (@ (type "text") (name "search"))) (input (@ (type "submit") (value "Search")))))))) method: '(get post))