Module:Find sources/autodoc: Difference between revisions
From Zoophilia Wiki
Jump to navigationJump to search
meta>Mr. Stradivarius (add a function to get subpages from a table of page names) |
meta>Mr. Stradivarius (add a makeWikitable helper function, and start functions for link and template tables) |
||
Line 1: | Line 1: | ||
-- Config | |||
local EXAMPLE_SEARCH_TERM = 'Example' | |||
-- 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 p = {} | local p = {} | ||
function | 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) | local specialText = string.format('{{Special:PrefixIndex/%s}}', prefix) | ||
specialText = mw.getCurrentFrame():preprocess(specialText) | specialText = mw.getCurrentFrame():preprocess(specialText) | ||
Line 12: | Line 25: | ||
end | end | ||
function | local function getSubpages(pagenames, prefix) | ||
local stripped = {} | local stripped = {} | ||
for i, page in ipairs(pagenames) do | for i, page in ipairs(pagenames) do | ||
Line 19: | Line 32: | ||
end | end | ||
return stripped | return stripped | ||
end | |||
local function getPrefixSubpages(prefix) | |||
return getSubpages(getPrefixPagenames(prefix), prefix) | |||
end | |||
local function makeWikitable(headers, ...) | |||
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{...} 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 | |||
function p.linkTable() | |||
local codes = getPrefixSubpages(LINK_ROOT) | |||
end | |||
function p.templateTable() | |||
local templates = getPrefixSubpages(TEMPLATE_ROOT) | |||
end | end | ||
return p | return p |
Revision as of 14:22, 26 September 2014
Documentation for this module may be created at Module:Find sources/autodoc/doc
-- Config local EXAMPLE_SEARCH_TERM = 'Example' -- 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 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, ...) 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{...} 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 function p.linkTable() local codes = getPrefixSubpages(LINK_ROOT) end function p.templateTable() local templates = getPrefixSubpages(TEMPLATE_ROOT) end return p