-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.py
executable file
·483 lines (373 loc) · 16.8 KB
/
main.py
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
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
#!/usr/bin/env python
#-*- coding:utf-8 -*-
import cgi
import os
import shutil
import web
import conf
from commons import zmarkdown_utils
from commons import zsh_util
from commons import zunicode
osp = os.path
urls = (
'/', 'WikiIndex',
'/~([a-zA-Z0-9_\-/.]+)', 'SpecialWikiPage',
ur'/([a-zA-Z0-9_\-/.%s]+)' % zunicode.CJK_RANGE, 'WikiPage',
)
app = web.application(urls, globals())
#
# template & session
#
if not web.config.get('_session'):
session = web.session.Session(app, web.session.DiskStore(conf.sessions_path), initializer={"username": None})
web.config._session = session
else:
session = web.config._session
t_globals = {
'utils' : web.utils,
"session" : session,
"ctx" : web.ctx
}
t_render = web.template.render(conf.templates_path, globals=t_globals)
def session_hook():
web.ctx.session = session
web.template.Template.globals['session'] = session
app.add_processor(web.loadhook(session_hook))
def get_recent_change_list(limit):
get_rc_list_cmd = " cd %s; find . -name '*.md' | xargs ls -t | head -n %d " % \
(conf.pages_path, limit)
buf = os.popen(get_rc_list_cmd).read().strip()
if buf:
lines = buf.split("\n")
strips_seq_item = ".md"
return zmarkdown_utils.sequence_to_unorder_list(lines, strips_seq_item)
def get_page_file_or_dir_fullpath_by_req_path(req_path):
if not req_path.endswith("/"):
return "%s.md" % osp.join(conf.pages_path, req_path)
elif req_path == "/":
return conf.pages_path
else:
return osp.join(conf.pages_path, req_path)
def get_dot_idx_content_by_fullpath(fullpath):
dot_idx_fullpath = osp.join(fullpath, ".index.md")
return zsh_util.cat(dot_idx_fullpath)
def get_page_file_list_content_by_fullpath(fullpath):
req_path = fullpath.replace(conf.pages_path, "")
req_path = web.utils.strips(req_path, "/")
tree_cmd = " cd %s; find %s -name '*.md' " % (conf.pages_path, req_path)
buf = os.popen(tree_cmd).read().strip()
if buf:
lines = buf.split("\n")
strips_seq_item = ".md"
return zmarkdown_utils.sequence_to_unorder_list(lines=lines, strips_seq_item=strips_seq_item)
def delete_page_file_by_fullpath(fullpath):
if osp.isfile(fullpath):
os.remove(fullpath)
return True
elif osp.isdir(fullpath):
idx_dot_md = osp.join(fullpath, ".index.md")
os.remove(idx_dot_md)
return True
return False
def get_page_file_index(limit=1000):
get_page_file_index_cmd = " cd %s; find . -name '*.md' | xargs ls -t | head -n %d " % (conf.pages_path, limit)
lines = os.popen(get_page_file_index_cmd).read().strip()
if lines:
lines = lines.split("\n")
content = zmarkdown_utils.sequence_to_unorder_list(lines, strips_seq_item=".md")
return content
def search_by_filename_and_file_content(keywords, limit):
"""
Following doesn't works if cmd contains pipe character:
p_obj = subprocess.Popen(shlex.split(cmd), stdout=subprocess.PIPE)
p_obj.wait()
resp = p_obj.stdout.read().strip()
So we have to do use deprecated syntax ```os.popen```, for more detail, see
http://stackoverflow.com/questions/89228/how-to-call-external-command-in-python .
"""
find_by_filename_matched = " -o -name ".join([" '*%s*' " % i for i in keywords.split()])
find_by_content_matched = " \| ".join(keywords.split())
is_multiple_keywords = find_by_content_matched.find("\|") != -1
if is_multiple_keywords:
find_by_filename_cmd = " cd %s; "\
" find . \( -name %s \) -type f | " \
" grep '.md' | head -n %d " % \
(conf.pages_path, find_by_filename_matched, limit)
find_by_content_cmd = " cd %s; " \
" grep ./ --recursive --ignore-case --include='*.md' --regexp ' \(%s\) ' | " \
" awk -F ':' '{print $1}' | uniq | head -n %d " % \
(conf.pages_path, find_by_content_matched, limit)
else:
find_by_filename_cmd = " cd %s; " \
" find . -name %s -type f | head -n %d " % \
(conf.pages_path, find_by_filename_matched, limit)
find_by_content_cmd = " cd %s; " \
" grep ./ --recursive --ignore-case --include='*.md' --regexp '%s' | " \
" awk -F ':' '{print $1}' | uniq | head -n %d " % \
(conf.pages_path, find_by_content_matched, limit)
# print "find_by_filename_cmd:"
# print find_by_filename_cmd
# print "find_by_content_cmd:"
# print find_by_content_cmd
matched_content_lines = os.popen(find_by_content_cmd).read().strip()
matched_content_lines = web.utils.safeunicode(matched_content_lines)
if matched_content_lines:
matched_content_lines = matched_content_lines.split("\n")
matched_filename_lines = os.popen(find_by_filename_cmd).read().strip()
matched_filename_lines = web.utils.safeunicode(matched_filename_lines)
if matched_filename_lines:
matched_filename_lines = matched_filename_lines.split("\n")
if matched_content_lines and matched_filename_lines:
# NOTICE: build-in function set doen't keep order, we shouldn't use it.
# mixed = set(matched_filename_lines)
# mixed.update(set(matched_content_lines))
mixed = web.utils.uniq(matched_filename_lines + matched_content_lines)
elif matched_content_lines and not matched_filename_lines:
mixed = matched_content_lines
elif not matched_content_lines and matched_filename_lines:
mixed = matched_filename_lines
else:
return None
lines = mixed
content = zmarkdown_utils.sequence_to_unorder_list(lines, strips_seq_item=".md")
return content
special_path_mapping = {
'index' : get_page_file_index,
's' : search_by_filename_and_file_content,
}
def _append_static_file(buf, filepath, file_type, add_newline=False):
assert file_type in ("css", "js")
if file_type == "css":
ref = '<link href="%s" rel="stylesheet" type="text/css">' % filepath
else:
ref = '<script type="text/javascript" src="%s"></script>' % filepath
if not add_newline:
static_files = '%s\n %s' % (buf, ref)
else:
static_files = '%s\n\n %s' % (buf, ref)
return static_files
def _get_trac_wiki_theme():
static_files = ""
css_files = ["trac.css", "wiki.css"]
for i in css_files:
filepath = osp.join("/static", "css", i)
static_files = _append_static_file(static_files, filepath, file_type="css")
return static_files
def get_global_default_static_files():
static_files = _get_trac_wiki_theme()
css_files = ["main.css"]
for i in css_files:
filepath = osp.join("/static", "css", i)
static_files = _append_static_file(static_files, filepath, file_type="css")
filepath = osp.join("/static", "js", "prettify", "prettify.css")
static_files = _append_static_file(static_files, filepath, file_type="css")
static_files = "%s\n" % static_files
js_files = ["jquery.js", "jquery-ui.js",
osp.join("prettify", "prettify.js"),
"main.js",
"Markdown.Converter.js", "Markdown.Sanitizer.js", "Markdown.Editor.js"]
for i in js_files:
filepath = osp.join("/static", "js", i)
static_files = _append_static_file(static_files, filepath, file_type="js")
return static_files
DEFAULT_GLOBAL_STATIC_FILES = get_global_default_static_files()
def get_the_same_folders_cssjs_files(req_path):
# NOTICE: this features doesn't works on file system mounted by sshfs.
fullpath = get_page_file_or_dir_fullpath_by_req_path(req_path)
if osp.isfile(fullpath):
work_path = osp.dirname(fullpath)
static_file_prefix = osp.join("/static/pages", osp.dirname(req_path))
elif osp.isdir(fullpath):
work_path = fullpath
static_file_prefix = osp.join("/static/pages", req_path)
else:
work_path = conf.pages_path
iters = os.listdir(work_path)
cssjs_files = [i for i in iters
if (not i.startswith(".")) and (i.endswith(".js") or i.endswith(".css"))]
if not cssjs_files:
return ""
css_buf = ""
js_buf = ""
for i in cssjs_files:
if i.endswith(".css"):
filepath = osp.join(static_file_prefix, i)
css_buf = _append_static_file(css_buf, filepath, file_type="css")
elif i.endswith(".js"):
filepath = osp.join(static_file_prefix, i)
js_buf = _append_static_file(js_buf, filepath, file_type="js")
return "%s\n %s" % (css_buf, js_buf)
class WikiIndex:
def GET(self):
inputs = web.input()
limit = inputs.get("limit")
title = "Recnet Changes"
static_file_prefix = "/static/pages"
req_path = "/"
if limit:
limit = int(limit) or conf.index_page_limit
content = get_recent_change_list(limit)
else:
content = get_recent_change_list(conf.index_page_limit)
fullpath = get_page_file_or_dir_fullpath_by_req_path(req_path)
content = zmarkdown_utils.markdown(text=content,
work_fullpath=fullpath,
static_file_prefix=static_file_prefix)
static_files = DEFAULT_GLOBAL_STATIC_FILES
# static_files = "%s\n %s" % (static_files, get_the_same_folders_cssjs_files(req_path))
return t_render.canvas(req_path=req_path, title=title, content=content, toolbox=False,
static_files = static_files)
class WikiPage:
def GET(self, req_path):
req_path = cgi.escape(req_path)
inputs = web.input()
action = inputs.get("action", "read")
if action and action not in ("edit", "read", "rename", "delete"):
raise web.BadRequest()
fullpath = get_page_file_or_dir_fullpath_by_req_path(req_path)
if conf.use_button_mode_path:
buf = zmarkdown_utils.convert_text_path_to_button_path("/%s" % req_path)
title = zmarkdown_utils.markdown(buf)
else:
title = req_path
if osp.isfile(fullpath):
work_fullpath = osp.dirname(fullpath)
static_file_prefix = osp.join("/static/pages", osp.dirname(req_path))
elif osp.isdir(fullpath):
work_fullpath = fullpath
static_file_prefix = osp.join("/static/pages", req_path)
if action == "read":
if osp.isfile(fullpath):
content = zsh_util.cat(fullpath)
elif osp.isdir(fullpath):
dot_idx_content = get_dot_idx_content_by_fullpath(fullpath)
page_file_list_content = get_page_file_list_content_by_fullpath(fullpath)
content = ""
if dot_idx_content:
content = dot_idx_content
if page_file_list_content:
content = "%s\n\n----\n%s" % (content, page_file_list_content)
else:
web.seeother("/%s?action=edit" % req_path)
return
content = zmarkdown_utils.markdown(text=content,
work_fullpath=work_fullpath,
static_file_prefix=static_file_prefix)
static_files = DEFAULT_GLOBAL_STATIC_FILES
static_files = "%s\n %s" % (static_files, get_the_same_folders_cssjs_files(req_path))
return t_render.canvas(req_path=req_path, title=title, content=content, static_files=static_files)
elif action == "edit":
if osp.isfile(fullpath):
content = zsh_util.cat(fullpath)
elif osp.isdir(fullpath):
content = get_dot_idx_content_by_fullpath(fullpath)
elif not osp.exists(fullpath):
content = ""
else:
raise Exception("unknow path")
static_files = DEFAULT_GLOBAL_STATIC_FILES
filepath = osp.join("/static", "css", "pagedown.css")
static_files = _append_static_file(static_files, filepath, file_type="css", add_newline=True)
filepath = osp.join("/static", "js", "editor.js")
static_files = _append_static_file(static_files, filepath, file_type="js", add_newline=True)
return t_render.editor(title, content, static_files=static_files)
elif action == "rename":
if not osp.exists(fullpath):
raise web.NotFound()
return t_render.rename(req_path, static_files=DEFAULT_GLOBAL_STATIC_FILES)
elif action == "delete":
delete_page_file_by_fullpath(fullpath)
web.seeother("/")
return
raise web.BadRequest()
def POST(self, req_path):
req_path = cgi.escape(req_path)
inputs = web.input()
action = inputs.get("action")
if action and action not in ("edit", "rename"):
raise web.BadRequest()
content = inputs.get("content")
content = web.utils.safestr(content)
# NOTICE: if req_path == `users/`, fullpath will be `/path/to/users/`,
# parent will be `/path/to/users`.
fullpath = get_page_file_or_dir_fullpath_by_req_path(req_path)
parent = osp.dirname(fullpath)
if not osp.exists(parent):
os.makedirs(parent)
if action == "edit":
if not osp.isdir(fullpath):
web.utils.safewrite(fullpath, content)
else:
idx_dot_md_fullpath = osp.join(fullpath, ".index.md")
web.utils.safewrite(idx_dot_md_fullpath, content)
web.seeother("/%s" % req_path)
elif action == "rename":
new_path = inputs.get("new_path")
if not new_path:
raise web.BadRequest()
old_fullpath = get_page_file_or_dir_fullpath_by_req_path(req_path)
if osp.isfile(old_fullpath):
new_fullpath = get_page_file_or_dir_fullpath_by_req_path(new_path)
elif osp.isdir(old_fullpath):
new_fullpath = osp.join(conf.pages_path, new_path)
else:
raise Exception('unknow path')
if osp.exists(new_fullpath):
err_info = "Warning: The page foobar already exists."
return t_render.rename(req_path, err_info, static_files=DEFAULT_GLOBAL_STATIC_FILES)
parent = osp.dirname(new_fullpath)
if not osp.exists(parent):
os.makedirs(parent)
shutil.move(old_fullpath, new_fullpath)
if osp.isfile(new_fullpath):
web.seeother("/%s" % new_path)
elif osp.isdir(new_fullpath):
web.seeother("/%s/" % new_path)
return
url = osp.join("/", req_path)
web.redirect(url)
class SpecialWikiPage:
def GET(self, req_path):
f = special_path_mapping.get(req_path)
if f:
if req_path == "index":
index = f
content = index()
content = zmarkdown_utils.markdown(content)
static_files = DEFAULT_GLOBAL_STATIC_FILES
static_files = "%s\n %s" % (static_files, get_the_same_folders_cssjs_files(req_path))
req_path = "~index"
title = "index"
return t_render.canvas(req_path=req_path, title=title, content=content, toolbox=False,
static_files=static_files)
raise web.NotFound()
def POST(self, req_path):
f = special_path_mapping.get(req_path)
inputs = web.input()
if f:
keywords = inputs.get("k")
limit = inputs.get("limit")
keywords = web.utils.safestr(keywords)
search = f
if limit:
limit = int(limit) or conf.search_page_limit
content = search(keywords, limit)
else:
content = search(keywords, conf.search_page_limit)
if content:
content = zmarkdown_utils.markdown(content)
else:
content = "not found matched"
return t_render.search(keywords=keywords, content=content,
static_files=DEFAULT_GLOBAL_STATIC_FILES)
else:
raise web.NotFound()
if __name__ == "__main__":
# Notice:
# you should remove datas/user.sqlite and sessions/* if you want a clean environment
if not osp.exists(conf.sessions_path):
os.mkdir(conf.sessions_path)
if not osp.exists(conf.pages_path):
os.mkdir(conf.pages_path)
# web.wsgi.runwsgi = lambda func, addr=None: web.wsgi.runfcgi(func, addr)
app.run()