- Welcome to Touhou Wiki!
- Registering is temporarily disabled. Check in our Discord server to request an account and for assistance of any kind.
Module:Index
Jump to navigation
Jump to search
Documentation for this module may be created at Module:Index/doc
--[[
Module for automatic indexing of main section headers on given pages or in a subpage range.
Designed mostly for indexing archives of talk pages.
]]
local common = require("Module:Common")
mw.text2 = require("Module:MW.text")
local function parse_page(content)
local tst1 = mw.text2.split(content, '\n')
local headers = {}
local sections = {}
local cur_sect = {}
for k,v in pairs(tst1) do
s,e,t = string.find(v, '^==([^=].+[^=])==$')
if s ~= nil then
-- remove the MediaWiki parser tags from section header
t = string.gsub(t, "%\127(UNIQ.-QINU)%\127", "")
-- reduce links to labels only
t = string.gsub(t, '%[%[(.-)%]%]', function(s)
local pts = mw.text2.split(s, '|')
return pts[#pts]
end)
-- remove HTML tags
t = common.stripTags(t)
-- remove preceding and trailing whitespaces
t = mw.text.trim(t)
cur_sect = {}
sections[#sections+1] = cur_sect
headers[#sections] = t
else
cur_sect[#cur_sect+1] = v
end
end
return headers,sections
end
local function build_list(frame, subpages)
local ret = {}
local ol = common.isset(frame.args['numbers'])
local ucnt = 1
for k,v in pairs(subpages) do
--if (v['headers'] ~= nil) and (#v['headers'] > 0) then
if common.isset(v['name']) then
ret[#ret+1] = "\n<center>'''[[" .. v['link'] .. "|" .. v['name'] .. "]]'''</center>"
else
ret[#ret+1] = "\n<center>'''[[" .. v['link'] .. "]]'''</center>"
end
ret[#ret+1] = common.cv(ol, '<ol start="' .. ucnt .. '">', '<ul>')
for k1,v1 in pairs(v['headers']) do
ret[#ret+1] = '\n<li>[[' .. v['link'] .. '#' .. v1 .. '|' .. v1 .. ']]</li>'
ucnt = ucnt + 1
end
ret[#ret+1] = common.cv(ol, '</ol>', '</ul>')
--end
end
return table.concat(ret)
end
local function index_pages(frame)
local ret = {}
local pages = {}
-- prepare data
if common.isset(frame.args['root']) and common.isset(frame.args['prefix']) then
local root = frame.args['root']
local prefix = string.gsub(frame.args['prefix'], '_', ' ')
local pnum = 1
if string.sub(root, -1) ~= '/' then root = root .. '/' end
while true do
local pname = prefix .. pnum
local plink = root .. pname
local cont = common.getPage(plink)
if not common.isset(cont) then break end
pages[#pages+1] = {['link'] = plink, ['name'] = pname, ['headers'] = parse_page(cont)}
pnum = pnum + 1
end
else
for k,v in frame:argumentPairs() do
if type(k) == "number" then
local page = {['link'] = mw.text.trim(v), ['headers'] = {}}
if common.isset(frame.args['L' .. k]) then
page['name'] = frame.args['L' .. k]
end
local cont = common.getPage(v)
if common.isset(cont) then
page['headers'] = parse_page(cont)
end
pages[#pages+1] = page
end
end
end
-- build result
if common.isset(frame.args['frame']) then
ret[#ret+1] = "<div style=\"background-color: #F9F9F9; border: solid 1px #7384B5\"><center>'''"
ret[#ret+1] = common.cv(common.isset(frame.args['title']), frame.args['title'], "Contents")
ret[#ret+1] = "''' <toggledisplay status=\"show\" showtext=\"Show\" hidetext=\"Hide\">"
ret[#ret+1] = "<div style=\""
if common.isset(frame.args['cols']) then
local cols = tonumber(frame.args['cols'])
if cols > 0 then
ret[#ret+1] = "-moz-column-count:" .. frame.args['cols'] .. "; column-count:" .. frame.args['cols'] .. ";"
end
end
ret[#ret+1] = "text-align: left; padding-top: 0.5em;\">"
end
ret[#ret+1] = build_list(frame, pages)
if common.isset(frame.args['frame']) then
ret[#ret+1] = "</div></toggledisplay></center></div>"
end
return frame:preprocess(table.concat(ret))
end
local function index_subpages(frame)
return ''
end
return {
['index_pages'] = index_pages,
['index_pages_template'] = function(frame) return index_pages(frame:getParent()) end,
}
-- [[Category:Lua Scripts|{{PAGENAME}}]]