Skip to content

Instantly share code, notes, and snippets.

@lucamauri
Last active April 19, 2020 09:26
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save lucamauri/6cf9c68299936e0f743815e53800deb8 to your computer and use it in GitHub Desktop.
Save lucamauri/6cf9c68299936e0f743815e53800deb8 to your computer and use it in GitHub Desktop.
LUA module to automatically create example of a template in MediaWiki
local p = {} --p stands for package
function p.TemplateExample(frame)
local SubPageName
local SubPageTitle
local Title
local Content
local pre = mw.html.create('pre')
local DoubleLF = string.char(10) .. string.char(10)
if not frame.args[1] then
SubPageName='Example'
else
SubPageName=frame.args[1]
end
Title = mw.title.getCurrentTitle()
SubPageTitle = mw.title.makeTitle(Title.namespace, Title.text .. '/' .. SubPageName)
local Intro = 'This example is automatically generated via LUA script from the code in the page <code>[[' .. SubPageTitle.prefixedText .. ']]</code>'
local CodeString = 'The code'
local ReturnString = 'renders as'
Content = SubPageTitle:getContent()
if not Content then
return "Page <code>[[" .. SubPageTitle.prefixedText .. "]]</code> does not exist: it is not possible to extract any text to generate example with"
else
pre
:css( 'width', '65%' )
:wikitext(mw.text.nowiki(SubPageTitle:getContent()))
return Intro .. DoubleLF .. CodeString .. DoubleLF .. tostring(pre) .. DoubleLF .. ReturnString .. DoubleLF .. frame:expandTemplate{ title = SubPageTitle }
end
end
return p
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment