2 is dead, long live 3. Mainly turning print info a function and urllib import fixestags/v0.4
@@ -47,19 +47,19 @@ For example:: | |||||
>>> text = "I has a template! {{foo|bar|baz|eggs=spam}} See it?" | >>> text = "I has a template! {{foo|bar|baz|eggs=spam}} See it?" | ||||
>>> wikicode = mwparserfromhell.parse(text) | >>> wikicode = mwparserfromhell.parse(text) | ||||
>>> print wikicode | |||||
>>> print(wikicode) | |||||
I has a template! {{foo|bar|baz|eggs=spam}} See it? | I has a template! {{foo|bar|baz|eggs=spam}} See it? | ||||
>>> templates = wikicode.filter_templates() | >>> templates = wikicode.filter_templates() | ||||
>>> print templates | |||||
>>> print(templates) | |||||
['{{foo|bar|baz|eggs=spam}}'] | ['{{foo|bar|baz|eggs=spam}}'] | ||||
>>> template = templates[0] | >>> template = templates[0] | ||||
>>> print template.name | |||||
>>> print(template.name) | |||||
foo | foo | ||||
>>> print template.params | |||||
>>> print(template.params) | |||||
['bar', 'baz', 'eggs=spam'] | ['bar', 'baz', 'eggs=spam'] | ||||
>>> print template.get(1).value | |||||
>>> print(template.get(1).value) | |||||
bar | bar | ||||
>>> print template.get("eggs").value | |||||
>>> print(template.get("eggs").value) | |||||
spam | spam | ||||
Since nodes can contain other nodes, getting nested templates is trivial:: | Since nodes can contain other nodes, getting nested templates is trivial:: | ||||
@@ -73,14 +73,14 @@ templates manually. This is possible because nodes can contain additional | |||||
``Wikicode`` objects:: | ``Wikicode`` objects:: | ||||
>>> code = mwparserfromhell.parse("{{foo|this {{includes a|template}}}}") | >>> 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|this {{includes a|template}}}}'] | ||||
>>> foo = code.filter_templates(recursive=False)[0] | >>> foo = code.filter_templates(recursive=False)[0] | ||||
>>> print foo.get(1).value | |||||
>>> print(foo.get(1).value) | |||||
this {{includes a|template}} | this {{includes a|template}} | ||||
>>> print foo.get(1).value.filter_templates()[0] | |||||
>>> print(foo.get(1).value.filter_templates()[0]) | |||||
{{includes a|template}} | {{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 | template | ||||
Templates can be easily modified to add, remove, or alter params. ``Wikicode`` | Templates can be easily modified to add, remove, or alter params. ``Wikicode`` | ||||
@@ -95,19 +95,19 @@ whitespace:: | |||||
... if template.name.matches("Cleanup") and not template.has("date"): | ... if template.name.matches("Cleanup") and not template.has("date"): | ||||
... template.add("date", "July 2012") | ... template.add("date", "July 2012") | ||||
... | ... | ||||
>>> print code | |||||
>>> print(code) | |||||
{{cleanup|date=July 2012}} '''Foo''' is a [[bar]]. {{uncategorized}} | {{cleanup|date=July 2012}} '''Foo''' is a [[bar]]. {{uncategorized}} | ||||
>>> code.replace("{{uncategorized}}", "{{bar-stub}}") | >>> code.replace("{{uncategorized}}", "{{bar-stub}}") | ||||
>>> print code | |||||
>>> print(code) | |||||
{{cleanup|date=July 2012}} '''Foo''' is a [[bar]]. {{bar-stub}} | {{cleanup|date=July 2012}} '''Foo''' is a [[bar]]. {{bar-stub}} | ||||
>>> print code.filter_templates() | |||||
>>> print(code.filter_templates()) | |||||
['{{cleanup|date=July 2012}}', '{{bar-stub}}'] | ['{{cleanup|date=July 2012}}', '{{bar-stub}}'] | ||||
You can then convert ``code`` back into a regular ``unicode`` object (for | You can then convert ``code`` back into a regular ``unicode`` object (for | ||||
saving the page!) by calling ``unicode()`` on it:: | saving the page!) by calling ``unicode()`` on it:: | ||||
>>> text = unicode(code) | >>> text = unicode(code) | ||||
>>> print text | |||||
>>> print(text) | |||||
{{cleanup|date=July 2012}} '''Foo''' is a [[bar]]. {{bar-stub}} | {{cleanup|date=July 2012}} '''Foo''' is a [[bar]]. {{bar-stub}} | ||||
>>> text == code | >>> text == code | ||||
True | True | ||||
@@ -136,14 +136,15 @@ If you're not using a library, you can parse any page using the following code | |||||
(via the API_):: | (via the API_):: | ||||
import json | import json | ||||
import urllib | |||||
from urllib.parse import urlencode | |||||
from urllib.request import urlopen | |||||
import mwparserfromhell | import mwparserfromhell | ||||
API_URL = "http://en.wikipedia.org/w/api.php" | API_URL = "http://en.wikipedia.org/w/api.php" | ||||
def parse(title): | def parse(title): | ||||
data = {"action": "query", "prop": "revisions", "rvlimit": 1, | data = {"action": "query", "prop": "revisions", "rvlimit": 1, | ||||
"rvprop": "content", "format": "json", "titles": title} | "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) | res = json.loads(raw) | ||||
text = res["query"]["pages"].values()[0]["revisions"][0]["*"] | text = res["query"]["pages"].values()[0]["revisions"][0]["*"] | ||||
return mwparserfromhell.parse(text) | return mwparserfromhell.parse(text) | ||||
@@ -22,12 +22,12 @@ If you're not using a library, you can parse any page using the following code | |||||
(via the API_):: | (via the API_):: | ||||
import json | import json | ||||
import urllib | |||||
import urllib.request | |||||
import mwparserfromhell | import mwparserfromhell | ||||
API_URL = "http://en.wikipedia.org/w/api.php" | API_URL = "http://en.wikipedia.org/w/api.php" | ||||
def parse(title): | def parse(title): | ||||
raw = urllib.urlopen(API_URL, data).read() | |||||
raw = urllib.request.urlopen(API_URL, data).read() | |||||
res = json.loads(raw) | res = json.loads(raw) | ||||
text = res["query"]["pages"].values()[0]["revisions"][0]["*"] | text = res["query"]["pages"].values()[0]["revisions"][0]["*"] | ||||
return mwparserfromhell.parse(text) | return mwparserfromhell.parse(text) | ||||
@@ -12,19 +12,19 @@ extra methods. For example:: | |||||
>>> text = "I has a template! {{foo|bar|baz|eggs=spam}} See it?" | >>> text = "I has a template! {{foo|bar|baz|eggs=spam}} See it?" | ||||
>>> wikicode = mwparserfromhell.parse(text) | >>> wikicode = mwparserfromhell.parse(text) | ||||
>>> print wikicode | |||||
>>> print(wikicode) | |||||
I has a template! {{foo|bar|baz|eggs=spam}} See it? | I has a template! {{foo|bar|baz|eggs=spam}} See it? | ||||
>>> templates = wikicode.filter_templates() | >>> templates = wikicode.filter_templates() | ||||
>>> print templates | |||||
>>> print(templates) | |||||
['{{foo|bar|baz|eggs=spam}}'] | ['{{foo|bar|baz|eggs=spam}}'] | ||||
>>> template = templates[0] | >>> template = templates[0] | ||||
>>> print template.name | |||||
>>> print(template.name) | |||||
foo | foo | ||||
>>> print template.params | |||||
>>> print(template.params) | |||||
['bar', 'baz', 'eggs=spam'] | ['bar', 'baz', 'eggs=spam'] | ||||
>>> print template.get(1).value | |||||
>>> print(template.get(1).value) | |||||
bar | bar | ||||
>>> print template.get("eggs").value | |||||
>>> print(template.get("eggs").value) | |||||
spam | spam | ||||
Since nodes can contain other nodes, getting nested templates is trivial:: | Since nodes can contain other nodes, getting nested templates is trivial:: | ||||
@@ -38,14 +38,14 @@ templates manually. This is possible because nodes can contain additional | |||||
:class:`.Wikicode` objects:: | :class:`.Wikicode` objects:: | ||||
>>> code = mwparserfromhell.parse("{{foo|this {{includes a|template}}}}") | >>> 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|this {{includes a|template}}}}'] | ||||
>>> foo = code.filter_templates(recursive=False)[0] | >>> foo = code.filter_templates(recursive=False)[0] | ||||
>>> print foo.get(1).value | |||||
>>> print(foo.get(1).value) | |||||
this {{includes a|template}} | this {{includes a|template}} | ||||
>>> print foo.get(1).value.filter_templates()[0] | |||||
>>> print(foo.get(1).value.filter_templates()[0]) | |||||
{{includes a|template}} | {{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 | template | ||||
Templates can be easily modified to add, remove, or alter params. | Templates can be easily modified to add, remove, or alter params. | ||||
@@ -61,24 +61,24 @@ takes care of capitalization and whitespace:: | |||||
... if template.name.matches("Cleanup") and not template.has("date"): | ... if template.name.matches("Cleanup") and not template.has("date"): | ||||
... template.add("date", "July 2012") | ... template.add("date", "July 2012") | ||||
... | ... | ||||
>>> print code | |||||
>>> print(code) | |||||
{{cleanup|date=July 2012}} '''Foo''' is a [[bar]]. {{uncategorized}} | {{cleanup|date=July 2012}} '''Foo''' is a [[bar]]. {{uncategorized}} | ||||
>>> code.replace("{{uncategorized}}", "{{bar-stub}}") | >>> code.replace("{{uncategorized}}", "{{bar-stub}}") | ||||
>>> print code | |||||
>>> print(code) | |||||
{{cleanup|date=July 2012}} '''Foo''' is a [[bar]]. {{bar-stub}} | {{cleanup|date=July 2012}} '''Foo''' is a [[bar]]. {{bar-stub}} | ||||
>>> print code.filter_templates() | |||||
>>> print(code.filter_templates()) | |||||
['{{cleanup|date=July 2012}}', '{{bar-stub}}'] | ['{{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}} | {{cleanup|date=July 2012}} '''Foo''' is a [[bar]]. {{bar-stub}} | ||||
>>> text == code | >>> text == code | ||||
True | 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 | For more tips, check out :class:`Wikicode's full method list <.Wikicode>` and | ||||
the :mod:`list of Nodes <.nodes>`. | the :mod:`list of Nodes <.nodes>`. |
@@ -567,7 +567,7 @@ class Wikicode(StringMixIn): | |||||
following:: | following:: | ||||
>>> text = "Lorem ipsum {{foo|bar|{{baz}}|spam=eggs}}" | >>> text = "Lorem ipsum {{foo|bar|{{baz}}|spam=eggs}}" | ||||
>>> print mwparserfromhell.parse(text).get_tree() | |||||
>>> print(mwparserfromhell.parse(text).get_tree()) | |||||
Lorem ipsum | Lorem ipsum | ||||
{{ | {{ | ||||
foo | foo | ||||