Module:Find sources/autodoc

From Zoophilia Wiki
Revision as of 14:22, 26 September 2014 by meta>Mr. Stradivarius (add a makeWikitable helper function, and start functions for link and template tables)
Jump to navigationJump to search

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