Jump to content

Module:WikitextParser/doc

From Wikipedia, the free encyclopedia
This is an old revision of this page, as edited by Sophivorus (talk | contribs) at 23:34, 18 March 2025. The present address (URL) is a permanent link to this revision, which may differ significantly from the current revision.


This module is a general-purpose wikitext parser. It's designed to be used by other Lua modules and cannot be called directly by templates.

Usage

First, require WikitextParser and get some wikitext to parse. For example:

local parser = require( 'Module:WikitextParser' )
local title = mw.title.getCurrentTitle()
local wikitext = title:getContent()

Then, use and combine the available methods. For example:

local sections = parser.getSections( wikitext )
for sectionTitle, sectionContent in pairs( sections ) do
	local sectionFiles = parser.getFiles( sectionContent )
	-- Do stuff
end

Methods

getLead

getLead( wikitext )

Returns the lead section from the given wikitext. The lead section is defined as everything before the first section title. If there's no lead section, an empty string will be returned.

getSections

getSections( wikitext )

Returns a table with the section titles as keys and the section contents as values. This method doesn't get the lead section (use getLead for that).

getSection

getSection( wikitext, title )

Returns the content of the section with the given title, including subsections. If you don't want subsections, use getSections instead. If the given section title appears more than once, only the first will be returned. If the section is not found, nil will be returned.

getSectionTag

getSectionTag( wikitext, name )

Returns the contents of the <section> tag with the given name (see Help:Labeled section transclusion). If the tag is not found, nil will be returned.

getLists

getLists( wikitext )

Returns a table with each value a list.

See also