Skip to content

Commit

Permalink
Merge 0f16d0c into 16ea817
Browse files Browse the repository at this point in the history
  • Loading branch information
legoktm committed Dec 28, 2014
2 parents 16ea817 + 0f16d0c commit fdab6a4
Show file tree
Hide file tree
Showing 4 changed files with 38 additions and 37 deletions.
33 changes: 17 additions & 16 deletions README.rst
Original file line number Diff line number Diff line change
Expand Up @@ -47,19 +47,19 @@ For example::

>>> text = "I has a template! {{foo|bar|baz|eggs=spam}} See it?"
>>> wikicode = mwparserfromhell.parse(text)
>>> print wikicode
>>> print(wikicode)
I has a template! {{foo|bar|baz|eggs=spam}} See it?
>>> templates = wikicode.filter_templates()
>>> print templates
>>> print(templates)
['{{foo|bar|baz|eggs=spam}}']
>>> template = templates[0]
>>> print template.name
>>> print(template.name)
foo
>>> print template.params
>>> print(template.params)
['bar', 'baz', 'eggs=spam']
>>> print template.get(1).value
>>> print(template.get(1).value)
bar
>>> print template.get("eggs").value
>>> print(template.get("eggs").value)
spam

Since nodes can contain other nodes, getting nested templates is trivial::
Expand All @@ -73,14 +73,14 @@ templates manually. This is possible because nodes can contain additional
``Wikicode`` objects::

>>> code = mwparserfromhell.parse("{{foo|this {{includes a|template}}}}")
>>> print code.filter_templates(recursive=False)
>>> print(code.filter_templates(recursive=False))
['{{foo|this {{includes a|template}}}}']
>>> foo = code.filter_templates(recursive=False)[0]
>>> print foo.get(1).value
>>> print(foo.get(1).value)
this {{includes a|template}}
>>> print foo.get(1).value.filter_templates()[0]
>>> print(foo.get(1).value.filter_templates()[0])
{{includes a|template}}
>>> print foo.get(1).value.filter_templates()[0].get(1).value
>>> print(foo.get(1).value.filter_templates()[0].get(1).value)
template

Templates can be easily modified to add, remove, or alter params. ``Wikicode``
Expand All @@ -95,19 +95,19 @@ whitespace::
... if template.name.matches("Cleanup") and not template.has("date"):
... template.add("date", "July 2012")
...
>>> print code
>>> print(code)
{{cleanup|date=July 2012}} '''Foo''' is a [[bar]]. {{uncategorized}}
>>> code.replace("{{uncategorized}}", "{{bar-stub}}")
>>> print code
>>> print(code)
{{cleanup|date=July 2012}} '''Foo''' is a [[bar]]. {{bar-stub}}
>>> print code.filter_templates()
>>> print(code.filter_templates())
['{{cleanup|date=July 2012}}', '{{bar-stub}}']

You can then convert ``code`` back into a regular ``unicode`` object (for
saving the page!) by calling ``unicode()`` on it::

>>> text = unicode(code)
>>> print text
>>> print(text)
{{cleanup|date=July 2012}} '''Foo''' is a [[bar]]. {{bar-stub}}
>>> text == code
True
Expand Down Expand Up @@ -136,14 +136,15 @@ If you're not using a library, you can parse any page using the following code
(via the API_)::

import json
import urllib
from urllib.parse import urlencode
from urllib.request import urlopen
import mwparserfromhell
API_URL = "http://en.wikipedia.org/w/api.php"

def parse(title):
data = {"action": "query", "prop": "revisions", "rvlimit": 1,
"rvprop": "content", "format": "json", "titles": title}
raw = urllib.urlopen(API_URL, urllib.urlencode(data)).read()
raw = urlopen(API_URL, urlencode(data).encode()).read()
res = json.loads(raw)
text = res["query"]["pages"].values()[0]["revisions"][0]["*"]
return mwparserfromhell.parse(text)
Expand Down
4 changes: 2 additions & 2 deletions docs/integration.rst
Original file line number Diff line number Diff line change
Expand Up @@ -22,12 +22,12 @@ If you're not using a library, you can parse any page using the following code
(via the API_)::

import json
import urllib
import urllib.request
import mwparserfromhell
API_URL = "http://en.wikipedia.org/w/api.php"

def parse(title):
raw = urllib.urlopen(API_URL, data).read()
raw = urllib.request.urlopen(API_URL, data).read()
res = json.loads(raw)
text = res["query"]["pages"].values()[0]["revisions"][0]["*"]
return mwparserfromhell.parse(text)
Expand Down
36 changes: 18 additions & 18 deletions docs/usage.rst
Original file line number Diff line number Diff line change
Expand Up @@ -12,19 +12,19 @@ extra methods. For example::

>>> text = "I has a template! {{foo|bar|baz|eggs=spam}} See it?"
>>> wikicode = mwparserfromhell.parse(text)
>>> print wikicode
>>> print(wikicode)
I has a template! {{foo|bar|baz|eggs=spam}} See it?
>>> templates = wikicode.filter_templates()
>>> print templates
>>> print(templates)
['{{foo|bar|baz|eggs=spam}}']
>>> template = templates[0]
>>> print template.name
>>> print(template.name)
foo
>>> print template.params
>>> print(template.params)
['bar', 'baz', 'eggs=spam']
>>> print template.get(1).value
>>> print(template.get(1).value)
bar
>>> print template.get("eggs").value
>>> print(template.get("eggs").value)
spam

Since nodes can contain other nodes, getting nested templates is trivial::
Expand All @@ -38,14 +38,14 @@ templates manually. This is possible because nodes can contain additional
:class:`.Wikicode` objects::

>>> code = mwparserfromhell.parse("{{foo|this {{includes a|template}}}}")
>>> print code.filter_templates(recursive=False)
>>> print(code.filter_templates(recursive=False))
['{{foo|this {{includes a|template}}}}']
>>> foo = code.filter_templates(recursive=False)[0]
>>> print foo.get(1).value
>>> print(foo.get(1).value)
this {{includes a|template}}
>>> print foo.get(1).value.filter_templates()[0]
>>> print(foo.get(1).value.filter_templates()[0])
{{includes a|template}}
>>> print foo.get(1).value.filter_templates()[0].get(1).value
>>> print(foo.get(1).value.filter_templates()[0].get(1).value)
template

Templates can be easily modified to add, remove, or alter params.
Expand All @@ -61,24 +61,24 @@ takes care of capitalization and whitespace::
... if template.name.matches("Cleanup") and not template.has("date"):
... template.add("date", "July 2012")
...
>>> print code
>>> print(code)
{{cleanup|date=July 2012}} '''Foo''' is a [[bar]]. {{uncategorized}}
>>> code.replace("{{uncategorized}}", "{{bar-stub}}")
>>> print code
>>> print(code)
{{cleanup|date=July 2012}} '''Foo''' is a [[bar]]. {{bar-stub}}
>>> print code.filter_templates()
>>> print(code.filter_templates())
['{{cleanup|date=July 2012}}', '{{bar-stub}}']

You can then convert ``code`` back into a regular :class:`unicode` object (for
saving the page!) by calling :func:`unicode` on it::
You can then convert ``code`` back into a regular :class:`str` object (for
saving the page!) by calling :func:`str` on it::

>>> text = unicode(code)
>>> print text
>>> text = str(code)
>>> print(text)
{{cleanup|date=July 2012}} '''Foo''' is a [[bar]]. {{bar-stub}}
>>> text == code
True

(Likewise, use :func:`str(code) <str>` in Python 3.)
(Likewise, use :func:`unicode(code) <unicode>` in Python 2.)

For more tips, check out :class:`Wikicode's full method list <.Wikicode>` and
the :mod:`list of Nodes <.nodes>`.
2 changes: 1 addition & 1 deletion mwparserfromhell/wikicode.py
Original file line number Diff line number Diff line change
Expand Up @@ -567,7 +567,7 @@ def get_tree(self):
following::
>>> text = "Lorem ipsum {{foo|bar|{{baz}}|spam=eggs}}"
>>> print mwparserfromhell.parse(text).get_tree()
>>> print(mwparserfromhell.parse(text).get_tree())
Lorem ipsum
{{
foo
Expand Down

0 comments on commit fdab6a4

Please sign in to comment.