wparser
Methods and classes for parsing wikitext into a object-oriented format which is easier to work with. BEWARE: This is an experimental module and may not work for 100% of cases; you have been warned...
WParser
Entry point for the WParser module
parse(wiki, title=None, text=None)
staticmethod
Parses the title or text into WikiText/WTemplate objects. If title and text are both specified, then text will be parsed as if it was on title.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
wiki |
Wiki
|
The Wiki object to use. |
required |
title |
str
|
The title to use. If |
None
|
text |
str
|
The text to parse. If |
None
|
Raises:
| Type | Description |
|---|---|
ValueError
|
If |
Returns:
| Name | Type | Description |
|---|---|---|
WikiText |
WikiText
|
The result of the parsing operation. |
revision_metadata(wiki, r, categories=False, external_links=False, images=False, links=False, templates=False)
staticmethod
Fetches metadata from the Revision, r.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
wiki |
Wiki
|
The Wiki object to use |
required |
r |
Revision
|
The |
required |
categories |
bool
|
Set |
False
|
external_links |
bool
|
Set |
False
|
images |
bool
|
Set |
False
|
links |
bool
|
Set |
False
|
templates |
bool
|
Set |
False
|
Returns:
| Name | Type | Description |
|---|---|---|
dict |
dict
|
A |
WikiExt
Represents an extension tag. Extensions technically aren't supported, so this is a meta class which will be interpreted as WikiText during lexing.
__init__(name=None, attr=None, inner=None, close=None)
Creates a new WikiExt object
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
name |
str
|
The name of the extension tag. Defaults to None. |
None
|
attr |
str
|
The attribute str of the extension tag. Defaults to None. |
None
|
inner |
WikiText
|
The inner text of the tag. Defaults to None. |
None
|
close |
str
|
The closing tag (including carets). Defaults to None. |
None
|
WikiTemplate
Represents a MediaWiki template. These usually contain a title and parameters.
__bool__()
Get a bool representation of this WikiTemplate object.
Returns:
| Name | Type | Description |
|---|---|---|
bool |
bool
|
|
__contains__(item)
Check if the key item is the name of a parameter
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
item |
Any
|
The key to check. If this is not a |
required |
Returns:
| Name | Type | Description |
|---|---|---|
bool |
bool
|
|
__eq__(o)
Compares two WikiTemplates for equality. Checks for matching titles (does not automatically normalize, if you want this, then call normalize() before checking equality!) and matching parameters. CAVEAT: does not compare order of parameters (not that it matters anyways)
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
o |
Any
|
The other object to compare |
required |
Returns:
| Name | Type | Description |
|---|---|---|
bool |
bool
|
True if the objects are simillar. |
__getitem__(key)
Returns the parameter value associated with key in this WikiTemplate's params
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
key |
Any
|
The key associated with the value to look up. |
required |
Raises:
| Type | Description |
|---|---|
KeyError
|
If |
Returns:
| Name | Type | Description |
|---|---|---|
WikiText |
WikiText
|
The |
__init__(title=None, params=None, parent=None)
Initializer, creates a new WikiTemplate object
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
title |
str
|
The |
None
|
params |
dict[str, Union[str, WikiText]]
|
Default parameters to initialize this WikiTemplate with. Defaults to None. |
None
|
parent |
WikiText
|
The WikiText associated with this WikiTemplate. Defaults to None. |
None
|
__iter__()
Returns an iterator that iterates over the keys of this WikiTemplate
Returns:
| Name | Type | Description |
|---|---|---|
Iterator |
Iterator
|
An iterator with the keys of this WikiTemplate. |
__setitem__(key, value)
Associates key and value as entries in this WikiTemplate's parameter list.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
key |
Any
|
The key to use. This must be a |
required |
value |
Any
|
The value to use. This must be a |
required |
Raises:
| Type | Description |
|---|---|
TypeError
|
If |
__str__()
Generates a str representaiton of this WikiTemplate.
Returns:
| Name | Type | Description |
|---|---|---|
str |
str
|
The |
append_to_params(k, e)
Appends e to the value associated with k. If k does not exist in this WikiTemplate, then a new entry will be created.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
k |
str
|
The key to lookup |
required |
e |
Union[str, WikiTemplate, WikiText]
|
The element to append to the value associated with |
required |
as_text(indent=False)
Renders this WikiTemplate as wikitext, in str form.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
indent |
bool
|
Set |
False
|
Returns:
| Name | Type | Description |
|---|---|---|
str |
str
|
The |
drop()
If possible, remove this WikiTemplate from its parent WikiText.
get_param(k, default=None)
Returns the parameter value associated with k in this WikiTemplate's params
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
k |
str
|
The key associated with the value to look up. |
required |
default |
Union[str, WikiText]
|
The value to return if |
None
|
Returns:
| Name | Type | Description |
|---|---|---|
WikiText |
Union[str, WikiText]
|
The |
has_key(key, empty_ok=True)
Check if the key item is the name of a parameter in this WikiTemplate.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
key |
str
|
The key to check. |
required |
empty_ok |
bool
|
Set |
True
|
Returns:
| Name | Type | Description |
|---|---|---|
bool |
bool
|
|
keys()
Gets the parameter keys in this WikiTemplate
Returns:
| Name | Type | Description |
|---|---|---|
KeysView |
KeysView
|
The keys in this WikiTemplate |
normalize(wiki, *tl, bypass_redirects=False)
staticmethod
Normalizes titles of templates. This usually fixes capitalization and removes random underscores.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
wiki |
Wiki
|
The |
required |
tl |
WikiTemplate
|
The |
()
|
bypass_redirects |
bool
|
Set |
False
|
Returns:
| Type | Description |
|---|---|
list[WikiTemplate]
|
list[WikiTemplate]: The WikiTemplates passed in as |
pop(k=None)
Removes the key, k, and its associated value from this WikiTemplate's parameters, and then return the value.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
k |
str
|
The key to lookup. If |
None
|
Returns:
| Name | Type | Description |
|---|---|---|
WikiText |
WikiText
|
The value formerly associated with |
remap(old_key, new_key)
Remap a key in this WikiTemplate's parameters.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
old_key |
str
|
The key to remap. If this key does not exist in this WikiTemplate, then this method exits without making any changes. |
required |
new_key |
str
|
The key to remap the value associated with |
required |
set_param(k, v)
Associates key k with value v in this WikiTemplate's parameter list. Alias of self[k] = v.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
k |
str
|
The key to use |
required |
v |
Union[str, WikiText, WikiTemplate]
|
The value to associate with |
required |
touch(k)
If k does not exist in this WikiTemplate, create a mapping for k to an empty WikiText
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
k |
[type]
|
[description] |
required |
values()
Gets the parameter values in this WikiTemplate
Returns:
| Name | Type | Description |
|---|---|---|
ValuesView |
ValuesView
|
The values contained in this WikiTemplate |
WikiText
Mutable representation of parsed WikiText. This is basically a container which contains str and WikiTemplate objects
templates: list[WikiTemplate]
property
Convenience property, gets the templates contained in this WikiText. CAVEAT: this does not recursively search sub-templates, see all_templates() for more details.
Returns:
| Type | Description |
|---|---|
list[WikiTemplate]
|
list[WikiTemplate]: A list of |
__bool__()
Get a bool representation of this WikiText object.
Returns:
| Name | Type | Description |
|---|---|---|
bool |
bool
|
|
__eq__(o)
Compares two WikiText objects for equality.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
o |
Any
|
The object to compare with self. |
required |
Returns:
| Name | Type | Description |
|---|---|---|
bool |
bool
|
True if the objects are the same type and contain the same elements in the same order. |
__iadd__(other)
Appends the specified element to the end of this WikiText object. CAVEAT: if other is a WikiText object, then other's contents will be merged into this WikiText.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
other |
Any
|
A |
required |
Raises:
| Type | Description |
|---|---|
TypeError
|
If |
Returns:
| Name | Type | Description |
|---|---|---|
WikiText |
WikiText
|
A reference to original, now modified WikiText object |
__init__(*elements)
Initializer, creates a new WikiText object.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
elements |
Union[str, WikiTemplate]
|
Default values to initialize this WikiText with. These will be appended together in the order passed. |
()
|
__str__()
Gets a str representation of this WikiText object.
Returns:
| Name | Type | Description |
|---|---|---|
str |
str
|
A |
all_templates()
Recursively finds all templates contained in this WikiText and their subtemplates.
Returns:
| Type | Description |
|---|---|
list[WikiTemplate]
|
list[WikiTemplate]: all |
as_text(trim=False)
Generate a str representation of this WikiText.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
trim |
bool
|
Set |
False
|
Returns:
| Name | Type | Description |
|---|---|---|
str |
str
|
A |