Skip to content

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 text is not specified, then the text of title will be automatically fetched and parsed. Defaults to None.

None
text str

The text to parse. If title is specified, then the text will be parsed as if it is was published on title. Defaults to None.

None

Raises:

Type Description
ValueError

If title and text are both None.

Returns:

Name Type Description
WikiText WikiText

The result of the parsing operation. None if something went wrong.

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 Revision to get metadata of

required
categories bool

Set True to get the categories contained in this Revision. Defaults to False.

False
external_links bool

Set True to get the external links contained in this Revision. Defaults to False.

False
images bool

Set True to get the files contained in this Revision. Defaults to False.

False
links bool

Set True to get the links contained in this Revision. Defaults to False.

False
templates bool

Set True to get the templates contained in this Revision. Defaults to False.

False

Returns:

Name Type Description
dict dict

A dict where each key is the type of metadata (these match the bool parameters of this method), and each value is a list with the associated type of metadata.

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

True if this WikiTemplate is not empty.

__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 str, then False will be returned.

required

Returns:

Name Type Description
bool bool

True if the key item is the name of a parameter with a non-empty value in this WikiTemplate.

__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 key is not the name of a parameter in this WikiTemplate.

Returns:

Name Type Description
WikiText WikiText

The WikiText associated with the spceified key

__init__(title=None, params=None, parent=None)

Initializer, creates a new WikiTemplate object

Parameters:

Name Type Description Default
title str

The name of this WikiTemplate. Defaults to None.

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 str.

required
value Any

The value to use. This must be a str, WikiTemplate, or WikiText.

required

Raises:

Type Description
TypeError

If key or value are not acceptable types.

__str__()

Generates a str representaiton of this WikiTemplate.

Returns:

Name Type Description
str str

The str representation of this WikiTemplate. The result will not be indented, see self.as_text() for details.

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 k.

required

as_text(indent=False)

Renders this WikiTemplate as wikitext, in str form.

Parameters:

Name Type Description Default
indent bool

Set True to include newlines so as to 'pretty-print' this WikiTemplate. Defaults to False.

False

Returns:

Name Type Description
str str

The WikiTemplate rendered as wikitext.

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 k is not a parameter in this WikiTemplate. Defaults to None.

None

Returns:

Name Type Description
WikiText Union[str, WikiText]

The WikiText associated with the specified k, otherwise default

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 False to enable an additional check for whether the value assoaciated with key is non-empty. Defaults to True.

True

Returns:

Name Type Description
bool bool

True if key exists in this WikiTemplate. If empty_ok is False, then True will be returned if the value assoaciated with key is also non-empty.

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 Wiki object to use. The WikiTemplate titles will be normalized against this Wiki.

required
tl WikiTemplate

The WikiTemplate objects to normalize.

()
bypass_redirects bool

Set True to also bypass redirects. Defaults to False.

False

Returns:

Type Description
list[WikiTemplate]

list[WikiTemplate]: The WikiTemplates passed in as tl, for chaining convenience.

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, then an arbitrary key-value pair will be popped. Defaults to None.

None

Returns:

Name Type Description
WikiText WikiText

The value formerly associated with k. None if k is not in this WikiTemplate.

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 old_key to.

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 k

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 WikiTemplate objects contained in this WikiText (top level only)

__bool__()

Get a bool representation of this WikiText object.

Returns:

Name Type Description
bool bool

True if this WikiText is not empty.

__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 str or WikiTemplate object

required

Raises:

Type Description
TypeError

If other is not a str or WikiTemplate

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 str representation of this WikiText object. Leading and trailing whitespace will be stripped. If you don't want this, see as_text().

all_templates()

Recursively finds all templates contained in this WikiText and their subtemplates.

Returns:

Type Description
list[WikiTemplate]

list[WikiTemplate]: all WikiTemplate objects contained in this WikiText and their subtemplates.

as_text(trim=False)

Generate a str representation of this WikiText.

Parameters:

Name Type Description Default
trim bool

Set True to remove leading & trailing whitespace. Defaults to False.

False

Returns:

Name Type Description
str str

A str representation of this WikiText.