Jump to content

Module:Vital article/sandbox

From Wikipedia, the free encyclopedia
This is an old revision of this page, as edited by MSGJ (talk | contribs) at 08:41, 12 December 2023 (simp). The present address (URL) is a permanent link to this revision, which may differ significantly from the current revision.
local p = {}

local getLink = function(page)
	local subpage = mw.ustring.upper(mw.ustring.sub(page, 1, 1)) -- get first letter of article name
	local codepoint = mw.ustring.codepoint(page, 1, 1)
	if codepoint<65 or codepoint>90 then --first letter is not between A-Z
		subpage = 'others'
	end
	local data_page = 'Wikipedia:Vital articles/data/' .. subpage .. '.json'
	local data = mw.loadJsonData(data_page)[page]
	if data then
		local level = tostring(data.level)
		if level then
			local trimmed_link = data.trimmed_link
			local link
			if trimmed_link then
				link = 'Wikipedia:Vital articles' .. trimmed_link
			else
				link = 'Wikipedia:Vital articles/Level/' .. level
				if (level=='4' or level=='5') and data.topic then
					link = link .. '/' .. data.topic
				end
				if data.sublist then
					link = link .. '/' .. data.sublist
				end
				--if data.section then -- not used until we have stable section anchors to link to
				--	link = link .. '#' .. data.section
				--end
			end
			return link, level
		end
	end
end

p.link = function(frame)
	local page = frame.args.page~='' and frame.args.page or mw.title.getCurrentTitle().subjectPageTitle.text
	local link, level = getLink(page)
	return '[[' .. page .. ']]' .. (link and '&nbsp;[[File:Círculos Concéntricos.svg|11px]]&nbsp;[[' .. link .. '|' .. level .. ']]' or '')
end

local top = function(container, frame)
	return (container and '' or frame:expandTemplate{title = 'Possibly empty category'})
		.. frame:expandTemplate{title = 'Wikipedia category', args = {container = container}}
		.. frame:expandTemplate{title = 'CatAutoTOC'}
end

local parse = function(cat, pattern)
	local str, cat1, cat2 = pattern[1], pattern[2], pattern[3], pattern[4], pattern[5]
	local regex = '^' .. str:gsub('_CLASS', '([%%%a-]+)'):gsub('_LEVEL', '(%%%w+)'):gsub('_TOPIC', '([%%%a ]+)') .. '$'
	local match1, match2 = cat:match(regex)
	local pos1, pos2 = str:match('_%a%a%a%a%a')
	if match1 then
		local t = {[pos1] = match1}
		--if match2 then
		--	t[pos2] = match2
		--end
		t['_TOPIC'] = t['_TOPIC']=='an unknown topic' and 'Unknown' or t['_TOPIC']
		cat1 = cat1 and ('[[Category:' .. cat1:gsub(pos1, t[pos1]) .. ']]') or ''
		cat2 = cat2 and ('[[Category:' .. cat2:gsub(pos1, t[pos1]) .. ']]') or ''
		--:gsub(pos2, t[pos2])
		return cat1 .. cat2
	end
end

p.header = function(frame)
	local pargs = frame:getParent().args
	local cat = pargs.category~='' and pargs.category or mw.title.getCurrentTitle().subjectPageTitle.text
	local patterns = {
		{'Wikipedia vital articles in _TOPIC by class', 'Wikipedia vital articles in _TOPIC|Class', 'Wikipedia vital articles by topic by class|_TOPIC'},
		{'_CLASS vital articles by topic', '_CLASS vital articles|Topic]]', 'Wikipedia vital articles by class by topic|_CLASS'},
		{'Wikipedia vital articles in _TOPIC by level', 'Wikipedia vital articles in _TOPIC|Level', 'Wikipedia vital articles by topic by level|_TOPIC'},
		{'Wikipedia level_LEVEL vital articles by topic', 'Wikipedia level-_LEVEL vital articles|Topic', 'Wikipedia vital articles by level by topic|_LEVEL'},
		{'Wikipedia _CLASS vital articles by level', '_CLASS vital articles|Level]]', '[[Wikipedia vital articles by class by level|_CLASS'},
		{'Wikipedia level_LEVEL vital articles by class', 'Wikipedia level-_LEVEL vital articles|Class', 'Wikipedia vital articles by level by class|_LEVEL'},
		{'Wikipedia vital articles in _TOPIC', 'Wikipedia vital articles by topic|_TOPIC'},
		{'Wikipedia level_LEVEL vital articles', 'Wikipedia vital articles by level|_LEVEL'},
		{'_CLASS level_LEVEL vital articles', 'Wikipedia _CLASS vital articles by level|_LEVEL', 'Wikipedia level-_LEVEL vital articles by class|_CLASS'},
		{'_CLASS vital articles', '_CLASS articles', 'Vital articles by quality|_CLASS'},
		{'Wikipedia level_LEVEL vital articles in _TOPIC', 'Wikipedia vital articles in _TOPIC by level|_LEVEL', 'Wikipedia level-_LEVEL vital articles by topic|_TOPIC'},
		{'_CLASS vital articles in _TOPIC', 'Wikipedia vital articles in _TOPIC by class|_CLASS', '_CLASS vital articles by topic|_TOPIC'}
	}
	--local parsed = {cat}
	for _, pattern in ipairs(patterns) do
		local parsed = parse(cat, pattern)
		if parsed then return parsed end
	end
	--return table.concat(parsed, '<br>')
end

return p