Module:WikitextParser/doc: Difference between revisions

Content deleted Content added
mNo edit summary
 
(13 intermediate revisions by the same user not shown)
Line 1:
{{Module rating|alpha}}
 
This module is a general-purpose wikitext parser. It's designed to be used by other Lua modules and cannotshouldn't be called directly by templates.
 
== Usage ==
Line 13:
</syntaxhighlight>
 
Then, use and combine the available methods freely. For example:
 
<syntaxhighlight lang="lua">
Line 23:
</syntaxhighlight>
 
== Methods ==
Here's a list of available methods:
 
==== getLead ====
* <code>getLead( wikitext )</code> — Returns the lead section from the given wikitext. The lead section is defined as everything before the first section title. May be empty if there's no lead section.
* <code>getSections( wikitext )</code> — Returns the sections from the given wikitext, as a Lua table with the section titles as keys and the section content as values. This method doesn't get the lead section (use <code>getLead</code> for that).
* <code>getSection( wikitext, title )</code> — Returns the content of the section with the given title, including subsections. If you don't want subsections, use <code>getSections</code> instead. If the given section title appears more than once, only the first will be returned. If the section is not found, <code>nil</code> will be returned.
 
<code>getLead( wikitext )</code>
For the ultimate documentation, see the source code below.
 
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 ====
 
<code>getSections( wikitext )</code>
 
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|getLead]] for that).
 
==== getSection ====
 
<code>getSection( wikitext, sectionTitle )</code>
 
Returns the content of the section with the given section title, including subsections. If you don't want subsections, use [[#getSections|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 ====
 
<code>getSectionTag( wikitext, tagName )</code>
 
Returns the contents of the <nowiki><section></nowiki> tag with the given tag name (see [[Help:Labeled section transclusion]]). If the tag is not found, nil will be returned.
 
==== getLists ====
 
<code>getLists( wikitext )</code>
 
Returns a table with each value being a list (ordered or unordered).
 
==== getParagraphs ====
 
<code>getParagraphs( wikitext )</code>
 
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 ====
 
<code>getTemplates( wikitext )</code>
 
Returns a table with each value being a template.
 
==== getTemplate ====
 
<code>getTemplate( wikitext, templateName )</code>
 
Returns the template with the given template name.
 
==== getTemplateName ====
 
<code>getTemplateName( templateWikitext )</code>
 
Returns the name of the given template. If the given wikitext is not recognized as that of a template, nil will be returned.
 
==== getTemplateParameters ====
 
<code>getTemplateParameters( templateWikitext )</code>
 
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 ====
 
<code>getTags( wikitext )</code>
 
Returns a table with each value being a tag and its contents (like <nowiki><div></nowiki>, <nowiki><gallery></nowiki>, <nowiki><ref></nowiki>, <nowiki><noinclude></nowiki>). Tags inside tags will be ignored. If you're interested in getting them, run this method again for each of the returned tags.
 
==== getTagName ====
 
<code>getTagName( tagWikitext )</code>
 
Returns the name of the tag in the given wikitext. For example 'div', 'span', 'gallery', 'ref', etc.
 
==== getTagAttribute ====
 
<code>getTagAttribute( tagWikitext, attribute )</code>
 
Returns the value of an attribute in the given tag. For example the id of a div or the name of a reference.
 
==== getGalleries ====
 
<code>getGalleries( wikitext )</code>
 
Returns a table with each value being a gallery.
 
==== getReferences ====
 
<code>getReferences( wikitext )</code>
 
Returns a table with each value being a reference. This includes self-closing references (like <nowiki><ref name="foo" /></nowiki>) as well as full references.
 
==== getTables ====
 
<code>getTables( wikitext )</code>
 
Returns a table with each value being a wiki table.
 
==== getTableAttribute ====
 
<code>getTableAttribute( tableWikitext, attribute )</code>
 
Returns the value of an attribute in the given wiki table. For example the id or the class.
 
==== getTable ====
 
<code>getTable( wikitext, id )</code>
 
Returns the wiki table with the given id. If not found, nil will be returned.
 
==== getTableData ====
 
<code>getTableData( tableWikitext )</code>
 
Returns a Lua table representing the data of the given wiki table.
 
==== getLinks ====
 
<code>getLinks( wikitext )</code>
 
Returns a Lua table with each value being a wiki link. For external links, use [[#getExternalLinks|getExternalLinks]] instead.
 
==== getFileLinks ====
 
<code>getFileLinks( wikitext )</code>
 
Returns a Lua table with each value being a file link.
 
==== getFileName ====
 
<code>getFileName( fileWikitext )</code>
 
Returns the name of the given template. If the given wikitext is not recognized as that of a file, nil will be returned.
 
==== getCategories ====
 
<code>getCategories( wikitext )</code>
 
Returns a Lua table with each value being a category link.
 
==== getExternalLinks ====
 
<code>getExternalLinks( wikitext )</code>
 
Returns a Lua table with each value being an external link. For internal links, use [[#getLinks|getLinks]] instead.
 
== See also ==