This module provides automatic documentation for templates based onModule:Find sources. SeeModule:Find sources#Automatic documentation for an overview.
-- Define constantslocalROOT_PAGE='Module:Find sources'localTEMPLATE_LIST=ROOT_PAGE..'/templates'-- template config module listlocalTEMPLATE_ROOT=TEMPLATE_LIST..'/'-- template config module prefixlocalLINK_CONFIG=ROOT_PAGE..'/links'-- link config module listlocalAUTODOC_SUFFIX='/autodoc'-- Load necessary modules.localmFindSources=require('Module:Find sources')localcfg=mw.loadData('Module:Find sources/autodoc/config')localp={}localfunctionmaybeLoadData(page)localsuccess,data=pcall(mw.loadData,page)returnsuccessanddataendlocalfunctionsubstituteParams(msg,...)returnmw.message.newRawMessage(msg,...):plain()endlocalfunctionsortKeys(t)localret={}forkinpairs(t)doret[#ret+1]=kendtable.sort(ret)returnretendlocalfunctionisValidLinkCfg(linkCfg)iftype(linkCfg)~='table'thenreturnfalseendfor_,sinipairs{'url','display'}doiftype(linkCfg[s])~='string'thenreturnfalseendendfor_,sinipairs{'separator'}doiflinkCfg[s]~=nilandtype(linkCfg[s])~='string'thenreturnfalseendendreturntrueendlocalfunctionisValidLink(code)iftype(code)~='string'orcode==''thenreturnfalseendlocallinks=maybeLoadData(LINK_CONFIG)locallinkCfg=links[code]returnisValidLinkCfg(linkCfg)endlocalfunctionisValidTemplateCfg(templateCfg)iftype(templateCfg)~='table'thenreturnfalseendfor_,sinipairs{'blurb'}doiftype(templateCfg[s])~='string'thenreturnfalseendendfor_,sinipairs{'separator','class','style'}doiftemplateCfg[s]~=nilandtype(templateCfg[s])~='string'thenreturnfalseendendiftemplateCfg.isUsedInMainspaceandtemplateCfg.isUsedInMainspace~=truethenreturnfalseendiftype(templateCfg.links)~='table'thenreturnfalseendlocalfunctionisValidLinkTable(t)iftype(t)~='table'thenreturnfalseendiftype(t.code)~='string'thenreturnfalseendift.displayandtype(t.display)~='string'thenreturnfalseendreturntrueendiftemplateCfg.introLinkandnotisValidLinkTable(templateCfg.introLink)thenreturnfalseendfor_,tinipairs(templateCfg.links)doifnotisValidLinkTable(t)thenreturnfalseendendreturntrueendlocalfunctionisValidTemplate(template)iftype(template)~='string'ortemplate==''thenreturnfalseendlocaltemplateCfg=maybeLoadData(TEMPLATE_ROOT..template)returnisValidTemplateCfg(templateCfg)endlocalfunctionisValidTemplateAutdocCfg(t)iftype(t)~='table'thenreturnfalseendfor_,sinipairs{'description','docIntro'}doift[s]andtype(t[s])~='string'thenreturnfalseendendift.shortcutsandtype(t.shortcuts)~='table'thenreturnfalseelseift.shortcutsthenfor_,sinipairs(t.shortcuts)doiftype(s)~='string'thenreturnfalseendendendreturntrueendlocalfunctionmakeWikitable(headers,rows)localret={}-- Table startret[#ret+1]='{| class="wikitable"'-- Headersret[#ret+1]='|-'fori,headerinipairs(headers)doret[#ret+1]='! scope="col | '..headerend-- Rowsfori,rowinipairs(rows)doret[#ret+1]='|-'forj,cellinipairs(row)doifj==1thenret[#ret+1]='! scope="row" | '..cellelseret[#ret+1]='| '..cellendendend-- Table endret[#ret+1]='|}'returntable.concat(ret,'\n')endlocalfunctiongrey(s)returnstring.format('<span style="color: gray;">%s</span>',s)endlocalfunctioncolspan(s,n)returnstring.format('colspan="%d" | %s',n,s)endlocalfunctionmakeWikitextError(msg)returnstring.format('<strong class="error">%s</strong>',msg)endlocalfunctionmakeWikilink(page,display)ifdisplaythenreturnstring.format('[[%s|%s]]',page,display)elsereturnstring.format('[[%s]]',page)endendfunctionp.linkTable()localcodes=sortKeys(require(LINK_CONFIG))localheaders={cfg['link-table-code-header'],cfg['link-table-description-header'],cfg['link-table-example-header'],cfg['link-table-notes-header']}localrows={}locallinks=maybeLoadData(LINK_CONFIG)fori,codeinipairs(codes)doifisValidLink(code)thenlocallinkData=links[code]-- Make the example link.localsuccess,link=pcall(mFindSources._renderLink,code,{cfg['example-search-term']})ifnotsuccessthenlink=makeWikitextError(link)end-- Build the row.localrow={code,linkData.descriptionorgrey("''No description available''"),link,linkData.notesor'',}rows[i]=rowelselocalmsg=substituteParams(cfg['invalid-link-config-error'],code)msg=makeWikitextError(msg)msg=colspan(msg,5)rows[i]={msg}endendreturnmakeWikitable(headers,rows)endfunctionp.templateTable()localtemplates=sortKeys(require(TEMPLATE_LIST))localheaders={cfg['template-table-template-header'],cfg['template-table-description-header'],cfg['template-table-example-header'],cfg['template-table-config-header'],}localrows={}fori,templateinipairs(templates)doifisValidTemplate(template)thenlocalconfigPage=TEMPLATE_ROOT..templatelocalautodocConfigPage=configPage..AUTODOC_SUFFIXlocaltemplateData=maybeLoadData(autodocConfigPage)ifnotisValidTemplateAutdocCfg(templateData)thentemplateData={}end-- Make the example textlocalsuccess,example=pcall(mFindSources._main,template,{cfg['example-search-term']})ifnotsuccessthenexample=makeWikitextError(example)end-- Build the row.localrow={makeWikilink(mw.site.namespaces[10].name..':'..template,template),templateData.descriptionorgrey("''No description available''"),example,table.concat({makeWikilink(configPage,cfg['template-table-main-config-link-display']),makeWikilink(autodocConfigPage,cfg['template-table-autodoc-config-link-display'])},cfg['table-config-separator'])}rows[i]=rowelselocalmsg=substituteParams(cfg['invalid-template-config-error'],TEMPLATE_ROOT..template)msg=makeWikitextError(msg)msg=colspan(msg,4)rows[i]={msg}endendreturnmakeWikitable(headers,rows)endlocalfunctiondocumentation(template)-- This function makes documentation for the template specified in-- the template parameter. The template should be without the "Template:"-- prefix.-- Load necessary moduleslocalmDocumentation=require('Module:Documentation')localmList=require('Module:List')localframe=mw.getCurrentFrame()-- Load the config fileslocaltemplateCfg=maybeLoadData(TEMPLATE_ROOT..template)ifnotisValidTemplateCfg(templateCfg)thenerror(substituteParams(cfg['invalid-template-name-error'],template,TEMPLATE_ROOT..template))endlocalautodocCfg=maybeLoadData(TEMPLATE_ROOT..template..AUTODOC_SUFFIX)ifnotisValidTemplateAutdocCfg(autodocCfg)thenautodocCfg={}end-- Get the documentation contentlocalcontentdo-- ShortcutslocalshortcutsifautodocCfg.shortcutsthenshortcuts=frame:expandTemplate{title='Template shortcut',args=autodocCfg.shortcuts}end-- Link descriptionslocalcodes={}iftemplateCfg.introLinkthencodes[#codes+1]=templateCfg.introLink.codeendfor_,tinipairs(templateCfg.links)docodes[#codes+1]=t.codeendlocallinks=maybeLoadData(LINK_CONFIG)fori,codeinipairs(codes)doiflinks[code]thencodes[i]=links[code].descriptionorcodeelsecodes[i]=codeendendlocallinkDescriptions=mList.bulleted(codes)-- Build the content.content=frame:expandTemplate{title='Find sources documentation',args={template=template,shortcuts=shortcuts,docIntro=autodocCfg.docIntro,isUsedInMainspace=templateCfg.isUsedInMainspaceand'yes'ornil,linkDescriptions=linkDescriptions}}endreturnmDocumentation.main{content=content,['link box']=cfg['end-box-blurb']}endsetmetatable(p,{__index=function(t,template)returnfunction()returndocumentation(template)endend})returnp