Source Mediawiki markup
Right now I'm using a variety of regexes to "parse" the data in the mediawiki mark-up into lists/dictionaries, so that elements within the article can be used.
This is hardly the best method, as the number of cases that have to be made are large.
How would one parse an article's mediawiki markup into a variety of python objects so that the data within can be used?
Example being:
A variety of regexes can achieve the above, but I'm finding the number I have to make rather large.
Here's the mediawiki unofficial specification (I don't find their official specification as useful).
pediapress/mwlib:
mwlib provides a library for parsing MediaWiki articles and converting them to different output formats. mwlib is used by wikipedia's "Print/export" feature in order to generate PDF documents from wikipedia articles.
Here's the documentation page. The older doc page used have a one-liner example:
from mwlib.uparser import simpleparse
simpleparse("=h1=\n*item 1\n*item2\n==h2==\nsome [[Link|caption]] there\n")
If you want to see how it's used in action, see the test cases that come with the code. (mwlib/tests/test_parser.py from git repository):
from mwlib import parser, expander, uparser
from mwlib.expander import DictDB
from mwlib.xfail import xfail
from mwlib.dummydb import DummyDB
from mwlib.refine import util, core
parse = uparser.simpleparse
def test_headings():
r=parse(u"""
= 1 =
== 2 ==
= 3 =
""")
sections = [x.children[0].asText().strip() for x in r.children if isinstance(x, parser.Section)]
assert sections == [u"1", u"3"]
Also see Markup spec and Alternative parsers for more information.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With