-
Notifications
You must be signed in to change notification settings - Fork 49
/
elnode-wiki.el
232 lines (197 loc) · 7.36 KB
/
elnode-wiki.el
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
;;; elnode-wiki.el --- a wiki with Elnode -*- lexical-binding: t -*-
;; Copyright (C) 2010, 2011, 2012 Nic Ferrier
;; Author: Nic Ferrier <[email protected]>
;; Maintainer: Nic Ferrier <[email protected]>
;; Created: 5th October 2010
;; Keywords: lisp, http, hypermedia
;; This file is NOT part of GNU Emacs.
;; This program is free software; you can redistribute it and/or modify
;; it under the terms of the GNU General Public License as published by
;; the Free Software Foundation, either version 3 of the License, or
;; (at your option) any later version.
;; This program is distributed in the hope that it will be useful,
;; but WITHOUT ANY WARRANTY; without even the implied warranty of
;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
;; GNU General Public License for more details.
;; You should have received a copy of the GNU General Public License
;; along with this program. If not, see <http://www.gnu.org/licenses/>.
;;; Commentary:
;;
;; This is a Wiki Engine completely written in EmacsLisp, using Elnode
;; as a server.
;;
;;; Source code
;;
;; elnode's code can be found here:
;; http://github.com/nicferrier/elnode
;;; Style note
;;
;; This codes uses the Emacs style of:
;;
;; elnode-wiki--private-function
;;
;; for private functions.
;;; Code:
(require 'elnode)
(require 'db)
(eval-when-compile 'fakir)
(require 'creole nil 't)
;;(require 'vc)
(defgroup elnode-wikiserver nil
"A Wiki server written with Elnode."
:group 'elnode)
;;;###autoload
(defconst elnode-wikiserver-wikiroot-default
(expand-file-name (concat elnode-config-directory "wiki/"))
"The default location of the wiki root.
This is used to detect whether elnode needs to create this
directory or not.")
;;;###autoload
(defcustom elnode-wikiserver-wikiroot
elnode-wikiserver-wikiroot-default
"The root for the Elnode wiki files.
This is where elnode-wikiserver serves wiki files from."
:type '(directory)
:group 'elnode-wikiserver)
(defcustom elnode-wikiserver-body-header
"<div id='top'></div>"
"HTML BODY preamable of a rendered Wiki page."
:type '(string)
:group 'elnode-wikiserver)
(defcustom elnode-wikiserver-body-footer
"<div id='footer'>
<form action='{{page}}' method='POST'>
<fieldset>
<legend>Edit this page</legend>
<textarea cols='80' rows='20' name='wikitext'>
{{text}}
</textarea><br/>
<input type='text' name='comment' value=''/>
<input type='submit' name='save' value='save'/>
<input type='submit' name='preview' value='preview'/>
</fieldset>
</form>
</div>"
"HTML BODY footter for a rendered Wiki page."
:type '(string)
:group 'elnode-wikiserver)
(defcustom elnode-wikiserver-body-footer-not-loggedin
"<div id='footer'>
<a href='/wiki/login/?redirect={{page}}'>login to edit</a>
</div>"
"HTML BODY footter for a rendered Wiki page."
:type '(string)
:group 'elnode-wikiserver)
(defun elnode-wiki--setup ()
"Setup the wiki."
(elnode--dir-setup elnode-wikiserver-wikiroot
elnode-wikiserver-wikiroot-default
"default-wiki-index.creole"
"index.creole"
"default-wiki-logo.gif"))
;; Internal wiki stuff
(defvar elnode-wiki-db
(db-make
`(db-hash
:filename
,(expand-file-name
(concat elnode-config-directory "elnode-wiki-auth")))))
;; Define the authentication scheme for the wiki
(elnode-defauth 'elnode-wiki-auth
:auth-db elnode-wiki-db
:redirect "/wiki/login/")
(defun elnode-wiki-page (httpcon wikipage &optional pageinfo)
"Creole render a WIKIPAGE back to the HTTPCON."
;; Otherwise just do it
(elnode-http-start httpcon 200 `("Content-type" . "text/html"))
(with-stdout-to-elnode httpcon
(let ((page-info (or pageinfo (elnode-http-pathinfo httpcon)))
(header elnode-wikiserver-body-header)
(footer (if-elnode-auth httpcon 'elnode-wiki-auth
elnode-wikiserver-body-footer
elnode-wikiserver-body-footer-not-loggedin)))
(creole-wiki
wikipage
:destination t
:variables (list (cons 'page page-info))
:body-header header
:body-footer footer))))
(defun elnode-wiki--text-param (httpcon)
"Get the text param from HTTPCON and convert it."
(replace-regexp-in-string
"\r" "" ; browsers send text in DOS line ending format
(elnode-http-param httpcon "wikitext")))
(defun elnode-wiki--save-request (httpcon wikiroot path text)
"Process an update request."
(let* ((page (if path
(save-match-data
(string-match "/wiki/\\(.*\\)$" path)
(match-string 1 path))))
(comment (elnode-http-param httpcon "comment"))
(file-name (if (equal page "")
(concat wikiroot "index.creole")
(concat (file-name-as-directory wikiroot) page)))
(buffer (find-file-noselect file-name)))
(with-current-buffer buffer
(erase-buffer)
(insert text)
(save-buffer)
(let ((git-buf
(get-buffer-create
(generate-new-buffer-name
"* elnode wiki commit buf *"))))
(shell-command
(format "git commit -m '%s' %s" comment file-name)
git-buf)
(kill-buffer git-buf))
(elnode-wiki-page httpcon file-name))))
(defun elnode-wiki-handler (httpcon wikiroot)
"A low level handler for Wiki operations.
Send the Wiki page requested, which must be a file existing under
the WIKIROOT, back to the HTTPCON.
Update operations are protected by authentication."
(elnode-method httpcon
(GET
(elnode-docroot-for wikiroot
with target-path
on httpcon
do
;; Do we need to serve an index?
(if (equal target-path (expand-file-name (concat wikiroot "/")))
(elnode-wiki-page httpcon (concat wikiroot "/index.creole"))
;; Else it's a wiki page or some collateral
(if (string-match "\\.creole$" target-path)
;; Serve a creole page
(elnode-wiki-page httpcon target-path)
;; Else serve just content
(elnode-send-file httpcon target-path)))))
(POST
(with-elnode-auth httpcon 'elnode-wiki-auth
(let* ((path (elnode-http-pathinfo httpcon))
(text (elnode-wiki--text-param httpcon)))
(if (not (elnode-http-param httpcon "preview"))
;; A save request in which case save the new text and then
;; send the wiki text.
(elnode-wiki--save-request httpcon wikiroot path text)
;; Might be a preview request in which case send back the WIKI
;; text that's been sent.
(with-temp-file "/tmp/preview"
(insert text))
(elnode-wiki-page httpcon "/tmp/preview" path)))))))
;;;###autoload
(defun elnode-wikiserver-test ()
"Test whether we should serve Wiki or not."
(featurep 'creole))
;;;###autoload
(defun elnode-wikiserver (httpcon)
"Serve Wiki pages from `elnode-wikiserver-wikiroot'.
HTTPCON is the request.
The Wiki server is only available if the `creole' package is
provided. Otherwise it will just error."
(if (not (elnode-wikiserver-test))
(elnode-send-500 httpcon "The Emacs feature 'creole is required.")
(elnode-auth-dispatcher httpcon 'elnode-wiki-auth
(elnode-wiki--setup)
(elnode-wiki-handler httpcon elnode-wikiserver-wikiroot))))
(provide 'elnode-wiki)
;;; elnode-wiki.el ends here