Module:Find sources/autodoc: Difference between revisions
From All Skies Encyclopaedia
imported>Mr. Stradivarius (add a makeWikitable helper function, and start functions for link and template tables) |
imported>Mr. Stradivarius (finish making the link table) |
||
Line 1: | Line 1: | ||
-- Config |
-- Config |
||
local |
local cfg = {} |
||
cfg['example-search-term'] = 'Example' |
|||
cfg['link-table-code-header'] = 'Code' |
|||
cfg['link-table-description-header'] = 'Description' |
|||
cfg['link-table-example-header'] = 'Example' |
|||
cfg['link-table-config-header'] = 'Config' |
|||
cfg['link-table-doc-config-header'] = 'Auto-documentation config' |
|||
cfg['link-table-notes-header'] = 'Notes' |
|||
-- Define constants |
-- Define constants |
||
Line 6: | Line 13: | ||
local TEMPLATE_ROOT = ROOT_PAGE .. '/templates/' -- for template config modules |
local TEMPLATE_ROOT = ROOT_PAGE .. '/templates/' -- for template config modules |
||
local LINK_ROOT = ROOT_PAGE .. '/links/' -- for link config modules |
local LINK_ROOT = ROOT_PAGE .. '/links/' -- for link config modules |
||
local AUTODOC_SUFFIX = '/autodoc' |
|||
-- Load necessary modules. |
|||
local mFindSources = require('Module:Find sources') |
|||
local p = {} |
local p = {} |
||
Line 38: | Line 49: | ||
end |
end |
||
local function makeWikitable(headers, |
local function makeWikitable(headers, rows) |
||
local ret = {} |
local ret = {} |
||
Line 51: | Line 62: | ||
-- Rows |
-- Rows |
||
for i, row in ipairs |
for i, row in ipairs(rows) do |
||
ret[#ret + 1] = '|-' |
ret[#ret + 1] = '|-' |
||
for j, cell in ipairs(row) do |
for j, cell in ipairs(row) do |
||
Line 64: | Line 75: | ||
end |
end |
||
function |
local function grey(s) |
||
return string.format('<span style="color: gray;">%s</span>', s) |
|||
⚫ | |||
end |
end |
||
function |
local function makeWikilink(s) |
||
return string.format('[[%s]]', s) |
|||
local templates = getPrefixSubpages(TEMPLATE_ROOT) |
|||
end |
|||
function p.linkTable() |
|||
⚫ | |||
local headers = { |
|||
cfg['link-table-code-header'], |
|||
cfg['link-table-description-header'], |
|||
cfg['link-table-example-header'], |
|||
cfg['link-table-config-header'], |
|||
cfg['link-table-doc-config-header'], |
|||
cfg['link-table-notes-header'] |
|||
} |
|||
local rows = {} |
|||
for i, code in ipairs(codes) do |
|||
local configPage = LINK_ROOT .. code |
|||
local autodocConfigPage = configPage .. AUTODOC_SUFFIX |
|||
local linkData = maybeLoadData(autodocConfigPage) or {} |
|||
local row = { |
|||
"'''" .. code .. "'''", |
|||
linkData.description or grey("''No description available''"), |
|||
mFindSources._renderLink(code, {cfg['example-search-term']}), |
|||
makeWikilink(configPage), |
|||
makeWikilink(autodocConfigPage), |
|||
linkData.notes or '' |
|||
} |
|||
rows[i] = row |
|||
end |
|||
return makeWikitable(headers, rows) |
|||
end |
end |
||
Revision as of 08:11, 28 September 2014
This module provides automatic documentation for templates based on Module:Find sources. See Module:Find sources for an overview.
-- Config
local cfg = {}
cfg['example-search-term'] = 'Example'
cfg['link-table-code-header'] = 'Code'
cfg['link-table-description-header'] = 'Description'
cfg['link-table-example-header'] = 'Example'
cfg['link-table-config-header'] = 'Config'
cfg['link-table-doc-config-header'] = 'Auto-documentation config'
cfg['link-table-notes-header'] = 'Notes'
-- Define constants
local ROOT_PAGE = 'Module:Find sources'
local TEMPLATE_ROOT = ROOT_PAGE .. '/templates/' -- for template config modules
local LINK_ROOT = ROOT_PAGE .. '/links/' -- for link config modules
local AUTODOC_SUFFIX = '/autodoc'
-- Load necessary modules.
local mFindSources = require('Module:Find sources')
local p = {}
local function maybeLoadData(page)
local success, data = pcall(mw.loadData, page)
return success and data
end
local function getPrefixPagenames(prefix)
local specialText = string.format('{{Special:PrefixIndex/%s}}', prefix)
specialText = mw.getCurrentFrame():preprocess(specialText)
specialText = mw.text.unstrip(specialText)
local pagenames = {}
for s in string.gmatch(specialText, '<a href="[^"]*" title="([^"]*)"[^>]*>[^<]*</a>') do
pagenames[#pagenames + 1] = mw.text.decode(s)
end
return pagenames
end
local function getSubpages(pagenames, prefix)
local stripped = {}
for i, page in ipairs(pagenames) do
local pattern = '^' .. prefix:gsub('%p', '%%%0') -- Turn the prefix into a Lua pattern
stripped[i] = mw.ustring.gsub(page, pattern, '')
end
return stripped
end
local function getPrefixSubpages(prefix)
return getSubpages(getPrefixPagenames(prefix), prefix)
end
local function makeWikitable(headers, rows)
local ret = {}
-- Table start
ret[#ret + 1] = '{| class="wikitable"'
-- Headers
ret[#ret + 1] = '|-'
for i, header in ipairs(headers) do
ret[#ret + 1] = '! ' .. header
end
-- Rows
for i, row in ipairs(rows) do
ret[#ret + 1] = '|-'
for j, cell in ipairs(row) do
ret[#ret + 1] = '| ' .. cell
end
end
-- Table end
ret[#ret + 1] = '|}'
return table.concat(ret, '\n')
end
local function grey(s)
return string.format('<span style="color: gray;">%s</span>', s)
end
local function makeWikilink(s)
return string.format('[[%s]]', s)
end
function p.linkTable()
local codes = getPrefixSubpages(LINK_ROOT)
local headers = {
cfg['link-table-code-header'],
cfg['link-table-description-header'],
cfg['link-table-example-header'],
cfg['link-table-config-header'],
cfg['link-table-doc-config-header'],
cfg['link-table-notes-header']
}
local rows = {}
for i, code in ipairs(codes) do
local configPage = LINK_ROOT .. code
local autodocConfigPage = configPage .. AUTODOC_SUFFIX
local linkData = maybeLoadData(autodocConfigPage) or {}
local row = {
"'''" .. code .. "'''",
linkData.description or grey("''No description available''"),
mFindSources._renderLink(code, {cfg['example-search-term']}),
makeWikilink(configPage),
makeWikilink(autodocConfigPage),
linkData.notes or ''
}
rows[i] = row
end
return makeWikitable(headers, rows)
end
return p