A basic MediaWiki markup parser.
Project description
Summary
Formats text following the MediaWiki syntax.
Usage
To return HTML from Wiki:
from wikimarkup.parser import Parser parser = Parser() html = parser.parse(text[, show_toc=True])
To return HTML without certain “annoying” (TODO: define annoying) elements, such as headings:
from wikimarkup.parser import parselite parselite(text)
Adding Internal Links
You can support [[internal links]] with the registerInternalLinkHook method. There is no default handling for internal links. If no hook handles the link, it will appear unchanged in the output. An internal link may have a namespace: prefix. Hooks are registered per namespace, with ‘None’ for unprefixed links:
def internalLinkHook(parser_env, namespace, body): ... return replacement parser.registerInternalLinkHook(None, internalLinkHook) # called for [[link]] parser.registerInternalLinkHook('Wikipedia', hook) # called for [[Wikipedia: Link]] parser.registerInternalLinkHook(':en', hook) # called for [[:en:link] parser.registerInternalLinkHook(':', hook) # called for [[:any:link]] parser.registerInternalLinkHook('*', hook) # called for [[anything]]
Examples:
from wikimarkup.parser import Parser def wikipediaLinkHook(parser_env, namespace, body): # namespace is going to be 'Wikipedia' (article, pipe, text) = body.partition('|') href = article.strip().capitalize().replace(' ', '_') text = (text or article).strip() return '<a href="http://en.wikipedia.org/wiki/%s">%s</a>' % (href, text) parser = Parser() parser.registerInternalLinkHook('Wikipedia', wikipediaLinkHook) print parser.parse("[[Wikipedia:public transport|public transportation]]") print parser.parse("[[Wikipedia: bus]]") import settings from pytils.translit import slugify from blog.models import Post def byteflowLinkHook(parser_env, namespace, body): (article, pipe, text) = body.partition('|') slug = slugify(article.strip()) text = (text or article).strip() try: post = Post.objects.get(slug=slug) href = post.get_absolute_url() except Post.DoesNotExist: href = '#' return '<a href="%s">%s</a>' % (href, text) parser.registerInternalLinkHook(None, byteflowLinkHook) parser.parse("[[Blog post title]]")
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
py-wikimarkup-2.1.2.tar.gz
(36.8 kB
view hashes)
Built Distribution
Close
Hashes for py_wikimarkup-2.1.2-py3-none-any.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 078dd3d0ae3afd012ff147b76bfc465780668d1000555b503377a396fd2010a7 |
|
MD5 | 9b3e408b9171b57cc210b7f50ccbd8e4 |
|
BLAKE2b-256 | 140b2fe8a60f2bf8bfad6f9fdd929409f3b2c83b7f9f0f4278ab6bfef461260f |