Jump to content

Module:TOC001

Permanently protected module
From Wikipedia, the free encyclopedia
This is an old revision of this page, as edited by Tom.Reding (talk | contribs) at 00:13, 10 November 2023 (Simplify/readability). The present address (URL) is a permanent link to this revision, which may differ significantly from the current revision.

local p = {}

function p.TOC001( frame )
	local title = mw.title.getCurrentTitle()
	local pn = title.text
	local pagebase = mw.ustring.match(pn, '^(List of minor planets: )%d') or
					 mw.ustring.match(pn, '^(Meanings of minor planet names: )%d') or
					 'testcase'
	local parent_from = mw.ustring.match(pn, '(%d+)–') or 0 --en dash; 1, 1001, 2001, ...
	local prefix = (parent_from - 1)/1000 --0, 1, 2, ...
	local left = parent_from - 5000 --left-most 'from'
	local left_prefix = (left - 1)/1000
	local hlist, anchor
	local nav_upper, nav_lower = '', ''
	
	--hlist
	if pagebase == 'List of minor planets: ' then
		hlist = frame:expandTemplate{
					title = 'Hlist',
					args = { 
						'[[LOMP#Main index|Index]]',
						'[[LOMP#100K|100K]]',
						'[[LOMP#200K|200K]]',
						'[[LOMP#300K|300K]]',
						'[[LOMP#400K|400K]]',
						'[[LOMP#500K|500K]]',
						'[[LOMP#600K|600K]]',
						'[[List of minor planets#Orbital groups|color code]]',
					}
				}
	elseif pagebase == 'Meanings of minor planet names: ' then
		hlist = frame:expandTemplate{
					title = 'Hlist',
					args = { 
						'[[MOMP#Index|Index]]',
						'[[MOMP#100K|100K]]',
						'[[MOMP#200K|200K]]',
						'[[MOMP#300K|300K]]',
						'[[MOMP#400K|400K]]',
						'[[MOMP#500K|500K]]',
						'[[MOMP#600K|600K]]',
					}
				}
	else
		return --tbd
	end
	
	--TOC top
	local TOC_top = frame:expandTemplate{
						title = 'TOC top',
						args = { 
							align = 'center',
							title = '<div style="background-color: #eaecf0; margin-bottom: 8px; padding: 2px;">'..
							hlist..
							'</div>'
						}
					}
	
	--nav_upper
	if left < 1 then left = 1 end
	local i = 0
	while i <= 10 do
		local from = left + 1000*i
		local to   = left-1 + 1000*(i+1)
		nav_upper = nav_upper..'\n* [['..pagebase..from..'–'..to..'|'..
					mw.getContentLanguage():formatNum(from - 1)..'s]]'
		i = i + 1
	end
	
	--nav_lower
	if left_prefix < 1 then left_prefix = '' end
	i = 0
	while i <= 9 do
		anchor = i..'01' --001, 002, ...
		nav_lower = nav_lower..'\n* [[#'..anchor..'|'..
					mw.getContentLanguage():formatNum(tonumber(prefix..anchor))..'…]]'
		i = i + 1
	end
	
	--cleanup nav_ (do this once after the whiles, instead of conditionals inside)
	nav_upper = string.gsub(nav_upper, '|0s]]', '|1–1000]]')
	nav_lower = string.gsub(nav_lower, '|001…]]', '|1–100]]')
	
	--TOC bottom
	local TOC_bottom = frame:expandTemplate{ title = 'TOC bottom', args = {} }
	
	--concat all
	return TOC_top..nav_upper..'<hr />\n<div style="text-align: center">\n'..nav_lower..'\n</div>\n'..TOC_bottom
end

return p