/
creole.py
84 lines (72 loc) · 2.71 KB
/
creole.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
import urllib, re
from genshi.builder import tag
from genshi.input import HTML
from flask import render_template
import creoleparser
from config import redis
from util import to_slug
def menu(macro, environ):
"""Places the contents in a class=".menu" div."""
return tag.div(macro.parsed_body(), class_="menu")
def related(macro, environ, predicate, obj):
"""Gets links to related items."""
import wiki
links = []
predicate = to_slug(predicate)
obj = to_slug(obj)
for slug in wiki.ask("*", predicate, obj):
node = wiki.get(slug)
if node:
links.append( HTML(render_template(["plate/%s.html" % node.get('type', 'default'), "plate/default.html"], node=node)) )
else:
links.append( tag.a(slug, href=slug) )
return tag.div(links, class_="related")
def hidden(macro, environ):
return tag.div(macro.parsed_body(), class_="hidden")
def recent(macro, environ):
"""Returns 30 links to the most recently changed nodes."""
import search
links = []
for node in search.recent():
if node:
links.append( HTML(render_template("plate/recent_plate.html", node=node)) )
else:
links.append( tag.a(slug, href=slug) )
return tag.div(links, class_="recent")
def float(macro, environ, direction="left"):
if direction == 'right':
return tag.div(macro.parsed_body(), class_="right")
else:
return tag.div(macro.parsed_body(), class_="left")
def center(macro, environ):
return tag.div(macro.parsed_body(), style='text-align: center')
def imagebox(macro, environ, direction="left"):
if direction == 'right':
return tag.div(macro.parsed_body(), class_="imagebox right")
else:
return tag.div(macro.parsed_body(), class_="imagebox left")
def wiki_links_path_func(src):
if (".com" in src or
".net" in src or
".org" in src):
return "http://%s" % src
return to_slug(src)
def wiki_links_class_func(src):
if (".com" in src or
".net" in src or
".org" in src):
return "external"
if not redis.exists("n:" + to_slug(src)):
return "missing"
dialect = creoleparser.create_dialect(
creoleparser.creole11_base,
wiki_links_class_func=wiki_links_class_func,
wiki_links_path_func=wiki_links_path_func,
wiki_links_space_char="-",
bodied_macros={'menu': menu, 'hidden': hidden, 'float': float, 'imagebox': imagebox, 'center': center},
non_bodied_macros={'related': related, 'recent': recent})
parser = creoleparser.Parser(dialect, encoding=None)
def text2html(src):
from wiki import re_related
src = re_related.sub(r"[[\2]]", src)
return parser( src )