Module:WikitextParser/doc

This module is a general-purpose wikitext parser. It's designed to be used by other Lua modules and shouldn't 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, sectionTitle )

Returns the content of the section with the given section 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, tagName )

Returns the contents of the <section> tag with the given tag 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 being a list (ordered or unordered).

getParagraphs

getParagraphs( wikitext )

Returns a table with each value being a paragraph. Paragraphs are defined as block-level elements that are not lists, templates, files, categories, tables or section titles.

getTemplates

getTemplates( wikitext )

Returns a table with each value being a template.

getTemplate

getTemplate( wikitext, templateName )

Returns the template with the given template name.

getTemplateName

getTemplateName( templateWikitext )

Returns the name of the given template. If the given wikitext is not recognized as that of a template, nil will be returned.

getTemplateParameters

getTemplateParameters( templateWikitext )

Returns a table with the parameter names as keys and the parameter values as values. For unnamed parameters, the keys are numerical. If the given wikitext is not recognized as that of a template, nil will be returned.

getTags

getTags( wikitext )

Returns a table with each value being a tag and its contents (like <div>, <gallery>, <ref>, <noinclude>). Tags inside tags will be ignored. If you're interested in getting them, run this method again for each of the returned tags.

getTagName

getTagName( tagWikitext )

Returns the name of the tag in the given wikitext. For example 'div', 'span', 'gallery', 'ref', etc.

getTagAttribute

getTagAttribute( tagWikitext, attribute )

Returns the value of an attribute in the given tag. For example the id of a div or the name of a reference.

getGalleries

getGalleries( wikitext )

Returns a table with each value being a gallery.

getReferences

getReferences( wikitext )

Returns a table with each value being a reference. This includes self-closing references (like <ref name="foo" />) as well as full references.

getTables

getTables( wikitext )

Returns a table with each value being a wiki table.

getTableAttribute

getTableAttribute( tableWikitext, attribute )

Returns the value of an attribute in the given wiki table. For example the id or the class.

getTable

getTable( wikitext, id )

Returns the wiki table with the given id. If not found, nil will be returned.

getTableData

getTableData( tableWikitext )

Returns a Lua table representing the data of the given wiki table.

getLinks( wikitext )

Returns a Lua table with each value being a wiki link. For external links, use getExternalLinks instead.

getFileLinks( wikitext )

Returns a Lua table with each value being a file link.

getFileName

getFileName( fileWikitext )

Returns the name of the given file. If the given wikitext is not recognized as that of a file, nil will be returned.

getCategories

getCategories( wikitext )

Returns a Lua table with each value being a category link.

getExternalLinks( wikitext )

Returns a Lua table with each value being an external link. For internal links, use getLinks instead.

See also

Content Disclaimer

Informasi ini disarikan dari Wikipedia dan disajikan kembali untuk tujuan edukasi. Konten tersedia di bawah lisensi CC BY-SA 3.0. Kami tidak bertanggung jawab atas ketidakakuratan data yang bersumber dari kontribusi publik tersebut.

  1. The information displayed on this website is sourced in part or in whole from Wikipedia and has been adapted for the purpose of restating it. We strive to provide accurate and relevant information, however:
  2. There is no guarantee of absolute accuracy. Wikipedia is an open, collaborative project that can be edited by anyone, so information is subject to change.
  3. It is not intended to constitute professional advice. The content displayed is for informational and educational purposes only. For important decisions (e.g., medical, legal, or financial), please consult a professional.
  4. Content copyright. Wikipedia is licensed under the Creative Commons Attribution-ShareAlike License (CC BY-SA). This means that content may be reused with appropriate attribution and shared under a similar license.
  5. Responsible use. Any risk arising from the use of information from this website is entirely the responsibility of the user.