# Text::MediaWiki::Parser A Perl port of [mwparserfromhell](https://github.com/earwig/mwparserfromhell) (the MediaWiki Parser from Hell). It turns wikicode into a tree of nodes and serialises that tree back to the original text byte for byte. ```perl use Text::MediaWiki::Parser; use Text::MediaWiki::Parser::Node::Attribute; my $code = Text::MediaWiki::Parser::parse( "'''Foo''' is a [[bar]] with {{a|template}}." ); for my $tpl ($code->filter_templates) { print $tpl->name->to_str, "\n"; # "a" } print $code->to_str; # identical to the input ``` ## Filtering All `filter_*` methods (`filter_arguments`, `filter_comments`, `filter_external_links`, `filter_headings`, `filter_html_entities`, `filter_tags`, `filter_templates`, `filter_text`, `filter_wikilinks`) accept `matches` and `recursive`. The default is a recursive walk; `recursive => 0` looks only at immediate nodes, and `recursive => 'RECURSE_OTHERS'` returns matching nodes without descending into them. `matches` may be a coderef, a `qr//`, or a regex string searched in the node's string form (case-insensitive and dot-all by default, like `mwparserfromhell`). ```perl my $code = parse(<<'WIKI'); {{Infobox software | name = Example | latest = 1.0 | released = {{date|2026}} }} == History == First release was {{version|1.0}}. See [[Example]].Source WIKI # Every template, including nested ones (pre-order). my @templates = $code->filter_templates; # Only templates directly in the page, not inside tags or other templates. my @top_level = $code->filter_templates(recursive => 0); # Only Infobox templates. my @infoboxes = $code->filter_templates(matches => qr/^\{\{Infobox/); # Only tags. my @refs = $code->filter_tags(matches => '^filter_text(matches => qr/release/); ``` ## Editing Assuming `$code` from the example above: ```perl my ($infobox) = $code->filter_templates(matches => qr/^\{\{Infobox/); $infobox->add('license', 'MIT'); # add or overwrite a parameter $infobox->remove('latest'); # remove a parameter by name $infobox->name('New name'); # rename the template $code->remove($infobox); # remove the whole template from the page my ($link) = $code->filter_wikilinks; $link->title('New title'); # [[old]] -> [[New title]] $link->text('display'); # [[New title|display]] my ($ref) = $code->filter_tags(matches => '^attrs([ # replace all tag attributes Text::MediaWiki::Parser::Node::Attribute->new( name => 'name', value => 'source', pad_first => ' ' ), ]); ``` `Wikicode->replace($old, $new)` and `Wikicode->remove($node)` search the whole tree, so they work with nodes returned by any filter. `Template->name` and `Tag->attrs` accept new values; `attrs` is an array reference and can also be modified in place. ## Sections Using the same `$code`: ```perl my ($lead, $history) = $code->get_sections(levels => [2], include_lead => 1); print $history->to_str; # Sections share their nodes with the page: editing one edits the page. $history->replace($history->nodes->[0], "== Past =="); print $code->to_str; # the heading changed everywhere ``` Options include `levels`, `matches` (matched against the heading title), `flat`, `include_lead` and `include_headings`. ## Rendering text `strip_code` returns the text a reader would see. Templates render to nothing unless `keep_template_params` is set: ```perl my $plain = parse("'''Foo''' {{tag|bar}} and [[link|text]]."); print $plain->strip_code; # "Foo and text." print $plain->strip_code(keep_template_params => 1); # "Foo bar and text." ``` ## Status Alpha. The tokenizer and builder are a direct transliteration of the Python implementation. The port was verified against `mwparserfromhell` 0.7.2 on 3676 real Gentoo Wiki pages: for every page the token stream is byte-identical, `to_str` reproduces the source byte for byte, and `strip_code` returns the same text as the original. The bundled tests cover the same edge cases in miniature, because the corpus itself is far too large to ship. ## Author Valerii Dundukov aka chicagoist ## License MIT, the same terms as the original. See `LICENSE`: the copyright notice of Ben Kurtovic applies to the ported code.