Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Newer
Older
100644 98 lines (67 sloc) 3.016 kb
db7fed4 @willkg Fix formatting in README
willkg authored
1 Summary
2 =======
3
4 Formats text following the `MediaWiki <http://meta.wikimedia.org/wiki/Help:Editing>`_
5 syntax.
6
8fbc0fb Added license and readme
David Cramer authored
7
20bb806 Moved quote example into README. Converted from tabs to spaces. Added te...
David Cramer authored
8 Usage
db7fed4 @willkg Fix formatting in README
willkg authored
9 =====
20bb806 Moved quote example into README. Converted from tabs to spaces. Added te...
David Cramer authored
10
8fbc0fb Added license and readme
David Cramer authored
11 To return HTML from Wiki::
12
db7fed4 @willkg Fix formatting in README
willkg authored
13 from wikimarkup import parse
8fbc0fb Added license and readme
David Cramer authored
14
db7fed4 @willkg Fix formatting in README
willkg authored
15 html = parse(text[, show_toc=True])
8fbc0fb Added license and readme
David Cramer authored
16
17 To return HTML without certain "annoying" (TODO: define annoying) elements, such as headings::
18
db7fed4 @willkg Fix formatting in README
willkg authored
19 from wikimarkup import parselite
20
21 parselite(text)
8fbc0fb Added license and readme
David Cramer authored
22
20bb806 Moved quote example into README. Converted from tabs to spaces. Added te...
David Cramer authored
23
24 Adding New Tags
db7fed4 @willkg Fix formatting in README
willkg authored
25 ===============
20bb806 Moved quote example into README. Converted from tabs to spaces. Added te...
David Cramer authored
26
27 You can add new tags with the `registerTagHook` method.::
28
db7fed4 @willkg Fix formatting in README
willkg authored
29 from wikimarkup import registerTagHook, parse
30 import cgi
31
32 def blockquoteTagHook(parser_env, body, attributes={}):
33 """<quote[ cite="Person"]>A paragraph of text.</quote>"""
34 text = ['<blockquote>']
35 if 'cite' in attributes:
36 text.append('<cite>%s</cite>' % (cgi.escape(attributes['cite']),))
37 text.append(parse(body.strip()))
38 text.append('</blockquote>')
39 return u'\n'.join(text)
40
41 registerTagHook('quote', blockquoteTagHook)
42
f15b0b9 @bjj Add [[internal link]] hook support
bjj authored
43
44 Adding Internal Links
db7fed4 @willkg Fix formatting in README
willkg authored
45 =====================
f15b0b9 @bjj Add [[internal link]] hook support
bjj authored
46
db7fed4 @willkg Fix formatting in README
willkg authored
47 You can support ``[[internal links]]`` with the `registerInternalLinkHook`
f15b0b9 @bjj Add [[internal link]] hook support
bjj authored
48 method. There is no default handling for internal links. If no hook
49 handles the link, it will appear unchanged in the output. An internal
50 link may have a `namespace:` prefix. Hooks are registered per namespace,
db7fed4 @willkg Fix formatting in README
willkg authored
51 with 'None' for unprefixed links::
f15b0b9 @bjj Add [[internal link]] hook support
bjj authored
52
53 def internalLinkHook(parser_env, namespace, body):
db7fed4 @willkg Fix formatting in README
willkg authored
54 ...
55 return replacement
f15b0b9 @bjj Add [[internal link]] hook support
bjj authored
56
57 registerInternalLinkHook(None, internalLinkHook) # called for [[link]]
db7fed4 @willkg Fix formatting in README
willkg authored
58 registerInternalLinkHook('Wikipedia', hook) # called for [[Wikipedia: Link]]
59 registerInternalLinkHook(':en', hook) # called for [[:en:link]
60 registerInternalLinkHook(':', hook) # called for [[:any:link]]
61 registerInternalLinkHook('*', hook) # called for [[anything]]
62
f15b0b9 @bjj Add [[internal link]] hook support
bjj authored
63
db7fed4 @willkg Fix formatting in README
willkg authored
64 Examples::
f15b0b9 @bjj Add [[internal link]] hook support
bjj authored
65
66 from wikimarkup import parse, registerInternalLinkHook
67
68 def wikipediaLinkHook(parser_env, namespace, body):
db7fed4 @willkg Fix formatting in README
willkg authored
69 # namespace is going to be 'Wikipedia'
70 (article, pipe, text) = body.partition('|')
71 href = article.strip().capitalize().replace(' ', '_')
72 text = (text or article).strip()
73 return '<a href="http://en.wikipedia.org/wiki/%s">%s</a>' % (href, text)
f15b0b9 @bjj Add [[internal link]] hook support
bjj authored
74
75 registerInternalLinkHook('Wikipedia', wikipediaLinkHook)
76
77 print parse("[[Wikipedia:public transport|public transportation]]")
78 print parse("[[Wikipedia: bus]]")
79
80 import settings
81 from pytils.translit import slugify
82 from blog.models import Post
83
84 def byteflowLinkHook(parser_env, namespace, body):
db7fed4 @willkg Fix formatting in README
willkg authored
85 (article, pipe, text) = body.partition('|')
86 slug = slugify(article.strip())
87 text = (text or article).strip()
88 try:
89 post = Post.objects.get(slug=slug)
90 href = post.get_absolute_url()
91 except Post.DoesNotExist:
92 href = '#'
93 return '<a href="%s">%s</a>' % (href, text)
f15b0b9 @bjj Add [[internal link]] hook support
bjj authored
94
95 registerInternalLinkHook(None, byteflowLinkHook)
96
97 parse("[[Blog post title]]")
Something went wrong with that request. Please try again.