Jump to content
Wikipedia The Free Encyclopedia

Module:Find sources/autodoc

From Wikipedia, the free encyclopedia
Module documentation[view] [edit] [history] [purge]

This module provides automatic documentation for templates based on Module:Find sources. See Module:Find sources#Automatic documentation for an overview.

The above documentation is transcluded from Module:Find sources/autodoc/doc. (edit | history)
Editors can experiment in this module's sandbox (edit | diff) and testcases (create) pages.
Subpages of this module.

 -- Define constants
 localROOT_PAGE='Module:Find sources'
 localTEMPLATE_LIST=ROOT_PAGE..'/templates'-- template config module list
 localTEMPLATE_ROOT=TEMPLATE_LIST..'/'-- template config module prefix
 localLINK_CONFIG=ROOT_PAGE..'/links'-- link config module list
 localAUTODOC_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)
 returnsuccessanddata
 end

 localfunctionsubstituteParams(msg,...)
 returnmw.message.newRawMessage(msg,...):plain()
 end

 localfunctionsortKeys(t)
 localret={}
 forkinpairs(t)do
 ret[#ret+1]=k
 end
 table.sort(ret)
 returnret
 end

 localfunctionisValidLinkCfg(linkCfg)
 iftype(linkCfg)~='table'then
 returnfalse
 end
 for_,sinipairs{'url','display'}do
 iftype(linkCfg[s])~='string'then
 returnfalse
 end
 end
 for_,sinipairs{'separator'}do
 iflinkCfg[s]~=nilandtype(linkCfg[s])~='string'then
 returnfalse
 end
 end
 returntrue
 end

 localfunctionisValidLink(code)
 iftype(code)~='string'orcode==''then
 returnfalse
 end
 locallinks=maybeLoadData(LINK_CONFIG)
 locallinkCfg=links[code]
 returnisValidLinkCfg(linkCfg)
 end

 localfunctionisValidTemplateCfg(templateCfg)
 iftype(templateCfg)~='table'then
 returnfalse
 end
 for_,sinipairs{'blurb'}do
 iftype(templateCfg[s])~='string'then
 returnfalse
 end
 end
 for_,sinipairs{'separator','class','style'}do
 iftemplateCfg[s]~=nilandtype(templateCfg[s])~='string'then
 returnfalse
 end
 end
 iftemplateCfg.isUsedInMainspaceandtemplateCfg.isUsedInMainspace~=truethen
 returnfalse
 end
 iftype(templateCfg.links)~='table'then
 returnfalse
 end

 localfunctionisValidLinkTable(t)
 iftype(t)~='table'then
 returnfalse
 end
 iftype(t.code)~='string'then
 returnfalse
 end
 ift.displayandtype(t.display)~='string'then
 returnfalse
 end
 returntrue
 end

 iftemplateCfg.introLinkandnotisValidLinkTable(templateCfg.introLink)then
 returnfalse
 end
 for_,tinipairs(templateCfg.links)do
 ifnotisValidLinkTable(t)then
 returnfalse
 end
 end

 returntrue
 end

 localfunctionisValidTemplate(template)
 iftype(template)~='string'ortemplate==''then
 returnfalse
 end
 localtemplateCfg=maybeLoadData(TEMPLATE_ROOT..template)
 returnisValidTemplateCfg(templateCfg)
 end

 localfunctionisValidTemplateAutdocCfg(t)
 iftype(t)~='table'then
 returnfalse
 end
 for_,sinipairs{'description','docIntro'}do
 ift[s]andtype(t[s])~='string'then
 returnfalse
 end
 end
 ift.shortcutsandtype(t.shortcuts)~='table'then
 returnfalse
 elseift.shortcutsthen
 for_,sinipairs(t.shortcuts)do
 iftype(s)~='string'then
 returnfalse
 end
 end
 end
 returntrue
 end

 localfunctionmakeWikitable(headers,rows)
 localret={}

 -- Table start
 ret[#ret+1]='{| class="wikitable"'

 -- Headers
 ret[#ret+1]='|-'
 fori,headerinipairs(headers)do
 ret[#ret+1]='! scope="col | '..header
 end

 -- Rows
 fori,rowinipairs(rows)do
 ret[#ret+1]='|-'
 forj,cellinipairs(row)do
 ifj==1then
 ret[#ret+1]='! scope="row" | '..cell
 else
 ret[#ret+1]='| '..cell
 end
 end
 end

 -- Table end
 ret[#ret+1]='|}'

 returntable.concat(ret,'\n')
 end

 localfunctiongrey(s)
 returnstring.format('<span style="color: gray;">%s</span>',s)
 end

 localfunctioncolspan(s,n)
 returnstring.format('colspan="%d" | %s',n,s)
 end

 localfunctionmakeWikitextError(msg)
 returnstring.format('<strong class="error">%s</strong>',msg)
 end

 localfunctionmakeWikilink(page,display)
 ifdisplaythen
 returnstring.format('[[%s|%s]]',page,display)
 else
 returnstring.format('[[%s]]',page)
 end
 end

 functionp.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)do
 ifisValidLink(code)then

 locallinkData=links[code]

 -- Make the example link.
 localsuccess,link=pcall(
 mFindSources._renderLink,
 code,
 {cfg['example-search-term']}
 )
 ifnotsuccessthen
 link=makeWikitextError(link)
 end

 -- Build the row.
 localrow={
 code,
 linkData.descriptionorgrey("''No description available''"),
 link,
 linkData.notesor'',
 }
 rows[i]=row
 else
 localmsg=substituteParams(cfg['invalid-link-config-error'],code)
 msg=makeWikitextError(msg)
 msg=colspan(msg,5)
 rows[i]={msg}
 end
 end
 returnmakeWikitable(headers,rows)
 end

 functionp.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)do
 ifisValidTemplate(template)then
 localconfigPage=TEMPLATE_ROOT..template
 localautodocConfigPage=configPage..AUTODOC_SUFFIX
 localtemplateData=maybeLoadData(autodocConfigPage)
 ifnotisValidTemplateAutdocCfg(templateData)then
 templateData={}
 end

 -- Make the example text
 localsuccess,example=pcall(
 mFindSources._main,
 template,
 {cfg['example-search-term']}
 )
 ifnotsuccessthen
 example=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]=row
 else
 localmsg=substituteParams(
 cfg['invalid-template-config-error'],
 TEMPLATE_ROOT..template
 )
 msg=makeWikitextError(msg)
 msg=colspan(msg,4)
 rows[i]={msg}
 end
 end
 returnmakeWikitable(headers,rows)
 end

 localfunctiondocumentation(template)
 -- This function makes documentation for the template specified in
 -- the template parameter. The template should be without the "Template:"
 -- prefix.

 -- Load necessary modules
 localmDocumentation=require('Module:Documentation')
 localmList=require('Module:List')
 localframe=mw.getCurrentFrame()

 -- Load the config files
 localtemplateCfg=maybeLoadData(TEMPLATE_ROOT..template)
 ifnotisValidTemplateCfg(templateCfg)then
 error(substituteParams(
 cfg['invalid-template-name-error'],
 template,
 TEMPLATE_ROOT..template
 ))
 end
 localautodocCfg=maybeLoadData(TEMPLATE_ROOT..template..AUTODOC_SUFFIX)
 ifnotisValidTemplateAutdocCfg(autodocCfg)then
 autodocCfg={}
 end

 -- Get the documentation content
 localcontent
 do
 -- Shortcuts
 localshortcuts
 ifautodocCfg.shortcutsthen
 shortcuts=frame:expandTemplate{title='Template shortcut',args=autodocCfg.shortcuts}
 end

 -- Link descriptions
 localcodes={}
 iftemplateCfg.introLinkthen
 codes[#codes+1]=templateCfg.introLink.code
 end
 for_,tinipairs(templateCfg.links)do
 codes[#codes+1]=t.code
 end

 locallinks=maybeLoadData(LINK_CONFIG)
 fori,codeinipairs(codes)do
 iflinks[code]then
 codes[i]=links[code].descriptionorcode
 else
 codes[i]=code
 end
 end
 locallinkDescriptions=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
 }}
 end

 returnmDocumentation.main{content=content,['link box']=cfg['end-box-blurb']}
 end

 setmetatable(p,{__index=function(t,template)
 returnfunction()
 returndocumentation(template)
 end
 end})

 returnp

AltStyle によって変換されたページ (->オリジナル) /