Module:Main article

Revision as of 20:30, 22 March 2024 by Habblet (talk | contribs) (Created page with "-- <pre> local p = {} function p.main(frame) local hat = require('Module:Hatnote') local args = frame:getParent().args local ret = mw.html.create('') -- list of all articles local params = {} -- total arguments counted counted local ttl = 0 for _, v in ipairs(args) do ttl = ttl + 1 table.insert(params,v) end -- main return string ret:wikitext('Main article'..(#params > 1 and 's' or '')..': ') -- helper function local function link(article) article...")
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Module documentation
This documentation is transcluded from Module:Main article/doc. [edit] [history] [purge]
This module does not have any documentation. Please consider adding documentation at Module:Main article/doc. [edit]
Module:Main article's function main is invoked by Template:Main.
Module:Main article requires Module:Hatnote.

-- <pre>
local p = {}

function p.main(frame)
	local hat = require('Module:Hatnote')
	local args = frame:getParent().args
	local ret = mw.html.create('')

	-- list of all articles
	local params = {}
	-- total arguments counted counted
	local ttl = 0
	for _, v in ipairs(args) do
		ttl = ttl + 1
		table.insert(params,v)
	end

	-- main return string
	ret:wikitext('Main article'..(#params > 1 and 's' or '')..': ')

	-- helper function
	local function link(article)
		article = string.gsub(article,'_',' ')
		article = mw.text.trim(article)
		-- replace anchors with section symbol
		if article:find('#',1,true) then
			local article_t = mw.text.split(article,'#')
			article_t = table.concat(article_t,' § ')
			-- article|alttext
			article = article..'|'..article_t
		end
		-- link article
		return '[['..article..']]'
	end

	-- additional articles
	for i, v in ipairs(params) do
		-- add article link
		if i < ttl and ttl > 1 then
			ret:wikitext(link(v))
		-- if last argument
		elseif i == ttl then
			-- only one argument, just add link
			if ttl == 1 then
				ret:wikitext(link(v))
			-- otherwise finish with "and"
			else
				ret:wikitext(' and '..link(v))
			end
		end

		-- if more to come, add commas
		if i < ttl and ttl > 2 then
			ret:wikitext(', ')
		end
	end

	-- return finished string
	return hat._hatnote(tostring(ret))
end

return p