nodes Package

nodes Package

This package contains Wikicode “nodes”, which represent a single unit of wikitext, such as a Template, an HTML tag, a Heading, or plain text. The node “tree” is far from flat, as most types can contain additional Wikicode types within them - and with that, more nodes. For example, the name of a Template is a Wikicode object that can contain text or more templates.

class mwparserfromhell.nodes.Node[source]

Represents the base Node type, demonstrating the methods to override.

__unicode__() must be overridden. It should return a unicode or (str in py3k) representation of the node. If the node contains Wikicode objects inside of it, __iternodes__() should be overridden to yield tuples of (wikicode, node_in_wikicode) for each node in each wikicode, as well as the node itself (None, self). If the node is printable, __strip__() should be overridden to return the printable version of the node - it does not have to be a string, but something that can be converted to a string with str(). Finally, __showtree__() can be overridden to build a nice tree representation of the node, if desired, for get_tree().

argument Module

class mwparserfromhell.nodes.argument.Argument(name, default=None)[source]

Bases: mwparserfromhell.nodes.Node

Represents a template argument substitution, like {{{foo}}}.

default[source]

The default value to substitute if none is passed.

This will be None if the argument wasn’t defined with one. The MediaWiki parser handles this by rendering the argument itself in the result, complete braces. To have the argument render as nothing, set default to "" ({{{arg}}} vs. {{{arg|}}}).

name[source]

The name of the argument to substitute.

comment Module

class mwparserfromhell.nodes.comment.Comment(contents)[source]

Bases: mwparserfromhell.nodes.Node

Represents a hidden HTML comment, like <!-- foobar -->.

contents[source]

The hidden text contained between <!-- and -->.

heading Module

class mwparserfromhell.nodes.heading.Heading(title, level)[source]

Bases: mwparserfromhell.nodes.Node

Represents a section heading in wikicode, like == Foo ==.

level[source]

The heading level, as an integer between 1 and 6, inclusive.

title[source]

The title of the heading, as a Wikicode object.

html_entity Module

class mwparserfromhell.nodes.html_entity.HTMLEntity(value, named=None, hexadecimal=False, hex_char=u'x')[source]

Bases: mwparserfromhell.nodes.Node

Represents an HTML entity, like &nbsp;, either named or unnamed.

hex_char[source]

If the value is hexadecimal, this is the letter denoting that.

For example, the hex_char of "&#x1234;" is "x", whereas the hex_char of "&#X1234;" is "X". Lowercase and uppercase x are the only values supported.

hexadecimal[source]

If unnamed, this is whether the value is hexadecimal or decimal.

named[source]

Whether the entity is a string name for a codepoint or an integer.

For example, &Sigma;, &#931;, and &#x3a3; refer to the same character, but only the first is “named”, while the others are integer representations of the codepoint.

normalize()[source]

Return the unicode character represented by the HTML entity.

value[source]

The string value of the HTML entity.

tag Module

class mwparserfromhell.nodes.tag.Tag(type_, tag, contents=None, attrs=None, showtag=True, self_closing=False, open_padding=0, close_padding=0)[source]

Bases: mwparserfromhell.nodes.Node

Represents an HTML-style tag in wikicode, like <ref>.

attrs[source]

The list of attributes affecting the tag.

Each attribute is an instance of Attribute.

close_padding[source]

How much spacing to insert before the last closing >.

contents[source]

The contents of the tag, as a Wikicode object.

open_padding[source]

How much spacing to insert before the first closing >.

self_closing[source]

Whether the tag is self-closing with no content.

showtag[source]

Whether to show the tag itself instead of a wikicode version.

tag[source]

The tag itself, as a Wikicode object.

type[source]

The tag type.

template Module

class mwparserfromhell.nodes.template.Template(name, params=None)[source]

Bases: mwparserfromhell.nodes.Node

Represents a template in wikicode, like {{foo}}.

add(name, value, showkey=None, force_nonconformity=False)[source]

Add a parameter to the template with a given name and value.

name and value can be anything parasable by utils.parse_anything(); pipes (and equal signs, if appropriate) are automatically escaped from value where applicable. If showkey is given, this will determine whether or not to show the parameter’s name (e.g., {{foo|bar}}‘s parameter has a name of "1" but it is hidden); otherwise, we’ll make a safe and intelligent guess. If name is already a parameter, we’ll replace its value while keeping the same spacing rules unless force_nonconformity is True. We will also try to guess the dominant spacing convention when adding a new parameter using _get_spacing_conventions() unless force_nonconformity is True.

get(name)[source]

Get the parameter whose name is name.

The returned object is a Parameter instance. Raises ValueError if no parameter has this name. Since multiple parameters can have the same name, we’ll return the last match, since the last parameter is the only one read by the MediaWiki parser.

has_param(name, ignore_empty=True)[source]

Return True if any parameter in the template is named name.

With ignore_empty, False will be returned even if the template contains a parameter with the name name, if the parameter’s value is empty. Note that a template may have multiple parameters with the same name.

name[source]

The name of the template, as a Wikicode object.

params[source]

The list of parameters contained within the template.

remove(name, keep_field=False, force_no_field=False)[source]

Remove a parameter from the template whose name is name.

If keep_field is True, we will keep the parameter’s name, but blank its value. Otherwise, we will remove the parameter completely unless other parameters are dependent on it (e.g. removing bar from {{foo|bar|baz}} is unsafe because {{foo|baz}} is not what we expected, so {{foo||baz}} will be produced instead), unless force_no_field is also True. If the parameter shows up multiple times in the template, we will remove all instances of it (and keep one if keep_field is True - that being the first instance if none of the instances have dependents, otherwise that instance will be kept).

text Module

class mwparserfromhell.nodes.text.Text(value)[source]

Bases: mwparserfromhell.nodes.Node

Represents ordinary, unformatted text with no special properties.

value[source]

The actual text itself.