{"id":15134644,"url":"https://github.com/valyauw/scribunto-parse-wikitemplates","last_synced_at":"2026-01-17T03:47:20.937Z","repository":{"id":185754436,"uuid":"665361057","full_name":"ValYauw/Scribunto-Parse-Wikitemplates","owner":"ValYauw","description":"A Scribunto (Lua) module for use in MediaWiki-powered sites (e.g. Wikipedia, FANDOM wikis, Miraheze wikis). Used to parse wikitext templates into a Lua table.","archived":false,"fork":false,"pushed_at":"2023-10-07T15:25:59.000Z","size":10,"stargazers_count":1,"open_issues_count":0,"forks_count":0,"subscribers_count":1,"default_branch":"main","last_synced_at":"2025-04-05T23:12:47.645Z","etag":null,"topics":["lua","mediawiki","parser","scribunto"],"latest_commit_sha":null,"homepage":"","language":"Lua","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":null,"status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/ValYauw.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":null,"code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2023-07-12T03:28:35.000Z","updated_at":"2024-05-12T14:53:47.000Z","dependencies_parsed_at":null,"dependency_job_id":"59afc492-d7e0-4ed9-b1cd-b595c06e04a4","html_url":"https://github.com/ValYauw/Scribunto-Parse-Wikitemplates","commit_stats":null,"previous_names":["valyauw/scribunto-parse-wikitemplates"],"tags_count":0,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/ValYauw%2FScribunto-Parse-Wikitemplates","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/ValYauw%2FScribunto-Parse-Wikitemplates/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/ValYauw%2FScribunto-Parse-Wikitemplates/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/ValYauw%2FScribunto-Parse-Wikitemplates/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/ValYauw","download_url":"https://codeload.github.com/ValYauw/Scribunto-Parse-Wikitemplates/tar.gz/refs/heads/main","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":247411243,"owners_count":20934654,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["lua","mediawiki","parser","scribunto"],"created_at":"2024-09-26T05:23:23.992Z","updated_at":"2026-01-17T03:47:20.908Z","avatar_url":"https://github.com/ValYauw.png","language":"Lua","funding_links":[],"categories":[],"sub_categories":[],"readme":"# About \n**ParseTemplate** is a Lua-based module for wikis (powered by MediaWiki) that provides a handy way of parsing [wiki templates](https://www.mediawiki.org/wiki/Help:Templates), [variables](https://www.mediawiki.org/wiki/Help:Magic_words#Variables) and [parser functions](https://www.mediawiki.org/wiki/Manual:Parser_functions) from a given portion of wikitext. \n\nThe extension [Scribunto](https://www.mediawiki.org/wiki/Extension:Scribunto) is required to use this module.\n\nWritten for the [Vocaloid Lyrics Wiki](https://vocaloidlyrics.fandom.com/wiki/Module:ParseTemplate) to extract information from thousands of pages.\n\n# Quick Start Guide\n\nTo start using this module, type the following lines of code in a module page:\n```lua\n-- Import module\nlocal module = require('Module:ParseTemplate')\n\n-- Extract the actual wikitext string\nlocal page_contents = mw.title.new(\"EXAMPLE PAGE\"):getContent()\n\n-- Parse the templates, variables and parser functions in the wikitext and organize them into a Lua table\nlocal table_of_templates = module.extractTemplates(page_contents)\n```\n\nYou can then index the templates in \u003ccode\u003etable_of_templates\u003c/code\u003e:\n```lua\n-- Index the first invocation of the template 'Template:Foo' parsed in the wikitext\nlocal obj_template = table_of_templates[\"Foo\"][1]\n\n-- Get the contents of this template invocation\nprint(obj_template[\"template_contents\"])  -- Example output: {{foo|param1|name=param2}}\n\n-- Get the first unnamed parameter of this template invocation\nprint(obj_template[\"template_params\"][1])  -- Example output: param1\n\n-- Get the named parameter \"name\" of this template invocation\nprint(obj_template[\"template_params\"][\"name\"])  -- Example output: param2\n```\n\nTo iterate through the templates organized in \u003ccode\u003etable_of_templates\u003c/code\u003e:\n```lua\n-- Iterate through each group of template invocations\nfor template_group_name, arr_templates in ipairs(table_of_templates) do\n\n  -- Iterate through each template in the sub-group\n  for i, obj_template in ipairs(arr_templates) do\n\n    -- Iterate through all parameters in each template\n    for param_name, param_value in ipairs(obj_template[\"template_params\"]) do\n      ...\n    end\n\n  end\n\nend\n```\n\nYou can also use \u003ccode\u003emw.text.jsonEncode\u003c/code\u003e to encode \u003ccode\u003etable_of_templates\u003c/code\u003e into a human-readable JSON string:\n```lua\nprint( mw.text.jsonEncode(table_of_templates) )\n```\n\n\n## Example Usage\n\nTake the example wikitext portion of a page:\n\u003cpre\u003e{{Stub}}{{Infobox character\n | title         = Daisy\n | image         = Example.jpg\n | imagecaption  = Daisy, blowing in the wind\n | position      = Supreme flower\n | age           = 2 months\n | status        = Active\n | height        = 5 inches\n | weight        = 20 grams \n}}\n\nlorem ipsum dolor sit amet\n\n==References==\n{{Reflist}}\n\u003c/pre\u003e\n\n\u003ccode\u003eextractTemplates\u003c/code\u003e will extract the three templates (Stub, Infobox character, and Reflist) in the form of a Lua table as follows:\n```lua\ntable_of_templates = {\n  [\"Stub\"] = {\n    [1] = {\n\t  [\"start_pos\"] = 1,\n\t  [\"end_pos\"] = 8,\n\t  [\"template_contents\"] = \"{{Stub}}\",\n\t  [\"template_name\"] = \"Stub\",\n\t  [\"template_params\"] = { }\n    }\n  },\n  [\"Infobox character\"] = {\n    [1] = {\n\t  [\"start_pos\"] = 9,\n\t  [\"end_pos\"] = 277,\n\t  [\"template_contents\"] = [=[{{Infobox character\n | title         = Daisy\n | image         = Example.jpg\n | imagecaption  = Daisy, blowing in the wind\n | position      = Supreme flower\n | age           = 2 months\n | status        = Active\n | height        = 5 inches\n | weight        = 20 grams \n}}]=],\n\t  [\"template_name\"] = \"Infobox character\",\n\t  [\"template_params\"] = { \n        [\"title\"] = \"Daisy\",\n        [\"image\"] = \"Example.jpg\",\n        [\"imagecaption\"] = \"Daisy, blowing in the wind\",\n        [\"position\"] = \"Supreme flower\",\n        [\"age\"] = \"2 months\",\n        [\"status\"] = \"Active\",\n        [\"height\"] = \"5 inches\",\n        [\"weight\"] = \"20 grams\"\n      }\n\t}\n  },\n  [\"Reflist\"] = {\n    [1] = {\n\t  [\"start_pos\"] = 323,\n\t  [\"end_pos\"] = 333,\n\t  [\"template_contents\"] = \"{{Reflist}}\",\n\t  [\"template_name\"] = \"Reflist\",\n\t  [\"template_params\"] = { }\n    }\n  }\n}\n```\n\nWhich is equivalent to the following JSON data tree:\n```js\n{\n   \"Stub\":[\n      {\n         \"start_pos\":1,\n         \"end_pos\":8,\n         \"template_contents\":\"{{Stub}}\",\n         \"template_name\":\"Stub\",\n         \"template_params\":{},\n      }\n   ],\n   \"Infobox character\":[\n      {\n         \"start_pos\":9,\n         \"end_pos\":277,\n         \"template_contents\":`{{Infobox character\n | title         = Daisy\n | image         = Example.jpg\n | imagecaption  = Daisy, blowing in the wind\n | position      = Supreme flower\n | age           = 2 months\n | status        = Active\n | height        = 5 inches\n | weight        = 20 grams \n}}`,\n         \"template_name\":\"Infobox character\",\n         \"template_params\":{\n            \"title\":\"Daisy\",\n            \"image\":\"Example.jpg\",\n            \"imagecaption\":\"Daisy, blowing in the wind\",\n            \"position\":\"Supreme flower\",\n            \"age\":\"2 months\",\n            \"status\":\"Active\",\n            \"height\":\"5 inches\",\n            \"weight\":\"20 grams\"\n         }\n      }\n   ],\n   \"Reflist\":[\n      {\n         \"start_pos\":323,\n         \"end_pos\":333,\n         \"template_contents\":\"{{Reflist}}\",\n         \"template_name\":\"Reflist\",\n         \"template_params\":{}\n      }\n   ]\n}\n```\n\n## Notes\n\n* Templates are grouped based on the template base page name. I.e. separate invocations using the call \u003ccode\u003e{{some template}}\u003c/code\u003e, \u003ccode\u003e{{Some template}}\u003c/code\u003e, and \u003ccode\u003e{{some_template}}\u003c/code\u003e will be grouped into the same group by the name of \"Some template\".\n* Variables and parser functions (such as \u003ccode\u003e{{DEFAULTSORT}}\u003c/code\u003e) will be grouped based on the base name of the variables/parser functions.\n* Because Lua tables are **unordered** by default, order of keys and values in the output may be different than expected.\n* This module is able to deal with templates nested within other templates.\n* This module is able to deal with characters escaped using the \u003ccode\u003e{{=}}\u003c/code\u003e \u0026 \u003ccode\u003e{{!}}\u003c/code\u003e magic words as well as characters enclosed within \u0026lt;nowiki\u0026gt; tags.","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fvalyauw%2Fscribunto-parse-wikitemplates","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fvalyauw%2Fscribunto-parse-wikitemplates","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fvalyauw%2Fscribunto-parse-wikitemplates/lists"}