Module:Subpages 3: Difference between revisions

Everything About Fiction You Never Wanted to Know.
Content added Content deleted
mNo edit summary
(Undo revision 2020934 by Robkelk (talk))
Tag: Undo
 
(35 intermediate revisions by 2 users not shown)
Line 2: Line 2:


local pageorder = {
local pageorder = {
["trope"] = {
["trope"] = {
"Analysis", "Haiku", "Headscratchers", "Image Links", "Laconic", "Playing With",
"Analysis", "Haiku", "Headscratchers", "Image Links", "Laconic", "Playing With",
"Quotes", "Useful Notes"
"Quotes", "Useful Notes"
},
},
["work"] = {
["work"] = {
"Analysis", "Characters", "Laconic", "Recap", "Trivia", "Setting", "YMMV",
"Analysis", "Characters", "Laconic", "Recap", "Trivia", "Setting", "YMMV",
"Fridge", "Fanfic Recs", "Haiku", "Headscratchers", "Ho Yay", "Image Links",
"Fridge", "Fanfic Recs", "Haiku", "Headscratchers", "Ho Yay", "Image Links",
"Memes", "Radar", "Quotes", "Reviews", "Useful Notes", "WMG"
"Memes", "Radar", "Quotes", "Reviews", "Source", "Useful Notes", "WMG"
},
},
["creator"] = {"YMMV", "Quotes", "Fanfic Recs", "Analysis", "Trivia", "WMG", "Image Links", "Haiku", "Laconic"}
["creator"] = {"YMMV", "Quotes", "Fanfic Recs", "Analysis", "Trivia", "WMG", "Image Links", "Haiku", "Laconic"}
}
}
local crowners = {
local crowners = {
["trope"] = { "Animation", "Animated Films", "Anime", "Anime and Manga", "Board Games", "Comics", "Comic Books",
["trope"] = { "Animation", "Animated Films", "Anime", "Anime and Manga", "Board Games", "Comics", "Comic Books",
"Fan Works", "Fanfiction", "Film", "Literature", "Live Action Television", "Live Action TV", "Machinima",
"Fan Works", "Fanfic", "Fanfiction", "Fan Fiction", "Film", "Folklore", "Literature",
"Manga", "Manga and Anime", "Music", "Musicians",
"Live Action Television", "Live Action TV", "Machinima",
"New Media", "Professional Wrestling", "Puppet Shows", "Radio", "Tabletop Games", "Television", "TV",
"Manga", "Manga and Anime", "Music", "Musicians", "New Media",
"Opera", "Professional Wrestling", "Puppet Shows", "Radio", "Tabletop Games", "Tabletop RPGs",
"Television", "TV",
"Theatre", "Theater", "Toys",
"Theatre", "Theater", "Toys",
"Webcomics", "Video Games", "Visual Art", "Visual Novels",
"Webcomics", "Video Games", "Visual Art", "Visual Novels",
Line 30: Line 32:
["creator"] = "Crowners"
["creator"] = "Crowners"
}
}

-- localize functions
local insert = table.insert
local concat = table.concat
local find = string.find
local len = string.len
local sub = string.sub
local rep = string.rep
local listlevel = { '*', '**', '***', '****', '*****' }


function sp.menu ( frame )
function sp.menu ( frame )
local page = frame.args[1] or frame:callParserFunction('TOPLEVELPAGE')
local page = frame.args[1] or frame:callParserFunction('TOPLEVELPAGE')
local template = frame.args[2] or "work"
local template = frame.args[2] or "work"
local wppage = frame.args[3] or page
local wppage = frame.args[3] or page
local haswp = true --(frame.args[4] == '')
local haswp = true --(frame.args[4] == '')
if wppage == "" then wppage = page end
if wppage == "" then wppage = page end


-- create variant top level page variables
local mainpage = page;
local mainpage = page;
if template == "index" then
if template == "index" then
template = "trope"
template = "trope"
crowners["trope"] = {}
mainpage = ":" .. page
mainpage = ":" .. page
page = page:gsub("Category:", "", 1)
page = page:gsub("Category:", "", 1)
end
end
local pageslash = page..'/'


if pageorder[template] == nil then return "Module error: invalid template argument" end
if pageorder[template] == nil then return "Module error: invalid template argument" end
local pagelenoffset = string.len(page) + 2


local allsptxt = frame:callParserFunction('SUBPAGES', { page, sep = "|" })
local allsptxt = frame:callParserFunction('SUBPAGES', { page, sep = "|" })
local subpage_list = split(allsptxt, "|")
local subpage_list = split(allsptxt, "|")
local tree = build_tree(subpage_list)


local tree = build_tree(page, subpage_list)
-- initialize result with Main link
local linkout = { '* [[' .. mainpage .. '|<span id="tm-main"></span>Main]]\n' }
local crownerout = { }
local otherout = { }
local missingout = { }


-- initialize result with Main link
for _, subpage in ipairs(pageorder[template]) do
local linkout = { '* [[' .. mainpage .. '|<span id="tm-main"></span>Main]]\n' }
text = build_list( { [subpage] = tree[subpage] }, page, 1 )
local crownerout = { }
if text ~= '' then
local otherout = { }
table.insert(linkout, text)
local missingout = { }
else
table.insert(missingout, subpage)
end
tree[subpage] = nil
end
for _, subpage in ipairs(crowners[template]) do
text = build_list( { [subpage] = tree[subpage] }, page, 2 )
if text ~= '' then
table.insert(crownerout, text)
else
table.insert(missingout, subpage)
end
tree[subpage] = nil
end
--deal with the leftover subpages
if next(tree) then
local ordered_keys = {}
for k in pairs(arr) do
table.insert(ordered_keys, k)
end
table.sort(ordered_keys)


for _, subpage in ipairs(pageorder[template]) do
for i = 1, #ordered_keys do
text = build_list( { [subpage] = tree[subpage] }, pageslash, 1 )
local subpage = ordered_keys[i]
if text ~= '' then
text = build_list( { [subpage] = tree[subpage] }, page, 2 )
insert(linkout, text)
if text ~= '' then
else
table.insert(otherout, text)
insert(missingout, subpage)
end
end
end
tree[subpage] = nil
end
end
for _, subpage in ipairs(crowners[template]) do
text = build_list( { [subpage] = tree[subpage] }, pageslash, 2 )
if text ~= '' then
insert(crownerout, text)
else
insert(missingout, subpage)
end
tree[subpage] = nil
end
--deal with the leftover subpages
if next(tree) then
local ordered_keys = {}
for k in pairs(tree) do
insert(ordered_keys, k)
end
table.sort(ordered_keys)


for i = 1, #ordered_keys do
local subpage = ordered_keys[i]
text = build_list( { [subpage] = tree[subpage] }, pageslash, 2 )
if text ~= '' then
insert(otherout, text)
end
end
end


local jsdata = '<div id="tm-data" style="display:none" '
.. 'data-toplevelpage="' .. page .. '" '
.. 'data-wanted="' .. table.concat( missingout, "\t" ) .. '" '
.. 'data-templatetype="' .. template .. '" '
.. '></div>'
local crowner_link = next(crownerout)
and '* [[#top|<span id="tm-' .. crownername[template].. '"></span>'..crownername[template]..']]\n'
or ''
local wikipedia_link = haswp
and "* [[wikipedia:" .. wppage .. '|<span id="tm-Wikipedia"></span>Wikipedia]]\n'
or ''
local all_sp_link = '* [[Special:PrefixIndex/' .. page .. '/|<span id="tm-Allothers"></span>All Subpages]]\n'


local jsdata = '<div id="tm-data" style="display:none" '
.. 'data-toplevelpage="' .. page .. '" '
.. 'data-wanted="' .. concat( missingout, "&#9;" ) .. '" '
.. 'data-templatetype="' .. template .. '" '
.. '></div>'
local crowner_link = next(crownerout)
and '* [[#top|<span id="tm-' .. crownername[template].. '"></span>'..crownername[template]..']]\n'
or ''
local wikipedia_link = haswp
and "* [[wikipedia:" .. wppage .. '|<span id="tm-Wikipedia"></span>Wikipedia]]\n'
or ''
local all_sp_link = '* [[Special:PrefixIndex/' .. pageslash .. '|<span id="tm-Allothers"></span>All Subpages]]\n'



return table.concat(linkout)
return concat(linkout)
.. crowner_link
.. crowner_link
.. table.concat(crownerout)
.. concat(crownerout)
.. wikipedia_link
.. wikipedia_link
.. all_sp_link
.. all_sp_link
.. table.concat(otherout)
.. concat(otherout)
.. '* <strong><span id="tm-New"></span>Create New</strong><ul id="tm-wantedpages"></ul>\n'
.. '* <strong><span id="tm-New"></span>Create New</strong><ul id="tm-wantedpages"></ul>\n'
.. jsdata
.. jsdata
end
end


function sp.tree ( frame )
local page = frame.args[1] or frame:callParserFunction('TOPLEVELPAGE')


local allsptxt = frame:callParserFunction('SUBPAGES', { page, sep = "|" })
function build_tree ( subpage_list )
local path_tree = { }
local subpage_list = split(allsptxt, "|")

for _, path in ipairs(subpage_list) do
local tree = build_tree(page, subpage_list)

local list = split('/', path)
if page:match("^Category:") then page = ":"..page end
local lastdir = path_tree
return "; [["..page.."]]\n" .. build_list( tree, page..'/', 1 )
end
for i, dir in ipairs(list) do

if i ~= 1 then

lastdir[dir] = { }
function build_tree ( page, subpage_list )
lastdir = lastdir[dir]
local path_tree = { }
end
local pagelenoffset = len(page) + 2
end

end
for _, fullpath in ipairs(subpage_list) do
return path_tree
local path = sub(fullpath, pagelenoffset)
local list = split(path, '/')
local lastdir = path_tree
for i, dir in ipairs(list) do
if lastdir[dir] == nil then lastdir[dir] = { } end
lastdir = lastdir[dir]
end
end
return path_tree
end
end


function build_list ( tree, prefix, level )
function build_list ( tree, prefix, level )
local libase = listlevel[level] or rep('*', level)
prefix = prefix or ''
level = level or 1
local ul = {}

local libase = mw.ustring.rep('*', level)
local ordered_sp = {}
for page in pairs(tree) do
local ul = ''
insert(ordered_sp, page)
for page, branch in pairs(tree) do
end
local li = libase
table.sort(ordered_sp)

if level <= 2 then
for i = 1, #ordered_sp do
li = libase .. " [["..prefix..page'|<span class="tm-'..page..'">'..page..']]\n'
local page = ordered_sp[i]
-- elseif sp.special[prefix..page] then
local branch = tree[page]
-- li = libase ..' <strong>'..page..'</strong>\n'
local li
else
li = libase ..' [['..prefix..page..'|'..page..']]\n'
if level <= 2 then
end
li = libase .. " [["..prefix..page..'|<span id="tm-'..page..'"></span>'..page..']]\n'
-- elseif sp.pathexists[prefix..page] then
else
-- li = libase ..' [['..prefix..page..'|'..page..']]\n'
li = libase ..' [['..prefix..page..'|'..page..']]\n'
-- else
end
-- li = libase ..' <strong>'..page..'</strong>\n'
insert(ul, li)

if next(branch) then
if next(branch) then
local nextprefix = (prefix..page..'/')
local nextprefix = (prefix..page..'/')
li = li .. build_list(branch, nextprefix, level+1)
insert(ul, build_list(branch, nextprefix, level+1))
end
end
ul = ul .. li
end
end
return ul
return concat(ul)
end
end


function split(str, sSeparator, nMax, bRegexp)
function split(str, sSeparator, nMax, bRegexp)
assert(sSeparator ~= '')
assert(sSeparator ~= '')
assert(nMax == nil or nMax >= 1)
assert(nMax == nil or nMax >= 1)


local aRecord = {}
local aRecord = {}


if mw.ustring.len(str) > 0 then
if len(str) > 0 then
local bPlain = not bRegexp
local bPlain = not bRegexp
nMax = nMax or -1
nMax = nMax or -1


local nField=1 nStart=1
local nField=1 nStart=1
local nFirst,nLast = mw.ustring.find(str, sSeparator, nStart, bPlain)
local nFirst,nLast = find(str, sSeparator, nStart, bPlain)
while nFirst and nMax ~= 0 do
while nFirst and nMax ~= 0 do
aRecord[nField] = mw.ustring.sub(str, nStart, nFirst-1)
aRecord[nField] = sub(str, nStart, nFirst-1)
nField = nField+1
nField = nField+1
nStart = nLast+1
nStart = nLast+1
nFirst,nLast = mw.ustring.find(str, sSeparator, nStart, bPlain)
nFirst,nLast = find(str, sSeparator, nStart, bPlain)
nMax = nMax-1
nMax = nMax-1
end
end
aRecord[nField] = mw.ustring.sub(str, nStart)
aRecord[nField] = sub(str, nStart)
end
end


return aRecord
return aRecord
end
end



Latest revision as of 18:59, 27 July 2023

Documentation for this module may be created at Module:Subpages 3/doc

local sp = {}

local pageorder = {
	["trope"] = {
		"Analysis", "Haiku", "Headscratchers", "Image Links", "Laconic", "Playing With",
		"Quotes", "Useful Notes"
	},
	["work"] = {
		"Analysis", "Characters", "Laconic", "Recap", "Trivia", "Setting", "YMMV",
		"Fridge", "Fanfic Recs", "Haiku", "Headscratchers", "Ho Yay", "Image Links",
		"Memes", "Radar", "Quotes", "Reviews", "Source", "Useful Notes", "WMG"
	},
	["creator"] = {"YMMV", "Quotes", "Fanfic Recs", "Analysis", "Trivia", "WMG", "Image Links", "Haiku", "Laconic"}
}
local crowners = {
	["trope"] = { "Animation", "Animated Films", "Anime", "Anime and Manga", "Board Games", "Comics", "Comic Books",
		"Fan Works", "Fanfic", "Fanfiction", "Fan Fiction", "Film", "Folklore", "Literature",
		"Live Action Television", "Live Action TV", "Machinima",
		"Manga", "Manga and Anime", "Music", "Musicians", "New Media",
		"Opera", "Professional Wrestling", "Puppet Shows", "Radio", "Tabletop Games", "Tabletop RPGs",
		"Television", "TV",
		"Theatre", "Theater", "Toys", 
		"Webcomics", "Video Games", "Visual Art", "Visual Novels",
		"Web Original", "Web Animation", "Web Comics", "Websites",
		"Western Animation", "Wikis", "Wrestling", "Other", "Real Life" },
	["work"] = { "Awesome", "Funny", "Heartwarming", "Nightmare Fuel", "Tear Jerker" },
	["creator"] = { "Awesome", "Funny", "Heartwarming", "Nightmare Fuel", "Tear Jerker" }
}
local crownername = {
	["trope"] = "By Medium",
	["work"] = "Crowners",
	["creator"] = "Crowners"
}

-- localize functions
local insert = table.insert
local concat = table.concat
local find = string.find
local len  = string.len
local sub  = string.sub
local rep  = string.rep
local listlevel = { '*', '**', '***', '****', '*****' }

function sp.menu ( frame )
	local page	 = frame.args[1] or frame:callParserFunction('TOPLEVELPAGE')
	local template = frame.args[2] or "work"
	local wppage   = frame.args[3] or page
	local haswp	= true --(frame.args[4] == '')
	if wppage == "" then wppage = page end

	-- create variant top level page variables
	local mainpage = page;
	if template == "index" then
		template = "trope"
		crowners["trope"] = {}
		mainpage = ":" .. page
		page = page:gsub("Category:", "", 1)
	end
	local pageslash = page..'/'

	if pageorder[template] == nil then return "Module error: invalid template argument" end

	local allsptxt = frame:callParserFunction('SUBPAGES', { page, sep = "|" })
	local subpage_list = split(allsptxt, "|")

	local tree = build_tree(page, subpage_list)

	-- initialize result with Main link
	local linkout = { '* [[' .. mainpage .. '|<span id="tm-main"></span>Main]]\n' }
	local crownerout = { }
	local otherout   = { }
	local missingout = { }

	for _, subpage in ipairs(pageorder[template]) do
		text = build_list( { [subpage] = tree[subpage] }, pageslash, 1 )
		if text ~= '' then
			insert(linkout, text)
		else
			insert(missingout, subpage)
		end
		tree[subpage] = nil
	end
	for _, subpage in ipairs(crowners[template]) do
		text = build_list( { [subpage] = tree[subpage] }, pageslash, 2 )
		if text ~= '' then
			insert(crownerout, text)
		else
			insert(missingout, subpage)
		end
		tree[subpage] = nil
	end
	--deal with the leftover subpages
	if next(tree) then
		local ordered_keys = {}
		for k in pairs(tree) do
			insert(ordered_keys, k)
		end
		table.sort(ordered_keys)

		for i = 1, #ordered_keys do
			local subpage = ordered_keys[i]
			text = build_list( { [subpage] = tree[subpage] }, pageslash, 2 )
			if text ~= '' then
				insert(otherout, text)
			end
		end
	end


	local jsdata = '<div id="tm-data" style="display:none" '
		.. 'data-toplevelpage="' .. page .. '" '
		.. 'data-wanted="'	   .. concat( missingout, "&#9;" ) .. '" '
		.. 'data-templatetype="' .. template .. '" '
		.. '></div>'
	local crowner_link = next(crownerout)
		and '* [[#top|<span id="tm-' .. crownername[template].. '"></span>'..crownername[template]..']]\n'
		or ''
	local wikipedia_link = haswp
		and "* [[wikipedia:" .. wppage .. '|<span id="tm-Wikipedia"></span>Wikipedia]]\n'
		or ''
	local all_sp_link = '* [[Special:PrefixIndex/' .. pageslash .. '|<span id="tm-Allothers"></span>All Subpages]]\n'


	return concat(linkout)
		.. crowner_link
		.. concat(crownerout)
		.. wikipedia_link
		.. all_sp_link
		.. concat(otherout)
		.. '* <strong><span id="tm-New"></span>Create New</strong><ul id="tm-wantedpages"></ul>\n'
		.. jsdata
end

function sp.tree ( frame )
	local page	 = frame.args[1] or frame:callParserFunction('TOPLEVELPAGE')

	local allsptxt = frame:callParserFunction('SUBPAGES', { page, sep = "|" })
	local subpage_list = split(allsptxt, "|")

	local tree = build_tree(page, subpage_list)

	if page:match("^Category:") then page = ":"..page end
	return "; [["..page.."]]\n" ..	build_list( tree, page..'/', 1 )
end


function build_tree ( page, subpage_list )
	local path_tree = { }
	local pagelenoffset = len(page) + 2

	for _, fullpath in ipairs(subpage_list) do
		local path = sub(fullpath, pagelenoffset)
		local list = split(path, '/')
		local lastdir = path_tree
		
		for i, dir in ipairs(list) do
			if lastdir[dir] == nil then lastdir[dir] = { } end
			lastdir = lastdir[dir]
		end
	end
	return path_tree
end

function build_list ( tree, prefix, level )
	local libase = listlevel[level] or rep('*', level)
	
	local ul = {}

	local ordered_sp = {}
	for page in pairs(tree) do
		insert(ordered_sp, page)
	end
	table.sort(ordered_sp)

	for i = 1, #ordered_sp do
		local page = ordered_sp[i]
		local branch = tree[page]
		local li
		
		if level <= 2 then
			li = libase .. " [["..prefix..page..'|<span id="tm-'..page..'"></span>'..page..']]\n'
		else
			li = libase ..' [['..prefix..page..'|'..page..']]\n'
		end
		insert(ul, li)

		if next(branch) then
			local nextprefix = (prefix..page..'/')
			insert(ul, build_list(branch, nextprefix, level+1))
		end
	end
	return concat(ul)
end

function split(str, sSeparator, nMax, bRegexp)
	assert(sSeparator ~= '')
	assert(nMax == nil or nMax >= 1)

	local aRecord = {}

	if len(str) > 0 then
		local bPlain = not bRegexp
		nMax = nMax or -1

		local nField=1 nStart=1
		local nFirst,nLast = find(str, sSeparator, nStart, bPlain)
		while nFirst and nMax ~= 0 do
			aRecord[nField] = sub(str, nStart, nFirst-1)
			nField = nField+1
			nStart = nLast+1
			nFirst,nLast = find(str, sSeparator, nStart, bPlain)
			nMax = nMax-1
		end
		aRecord[nField] = sub(str, nStart)
	end

	return aRecord
end

return sp