Module:Main article

Revision as of 14:03, 20 January 2024 by OtherXAdmin (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 Template:No documentation/doc. [edit] [history] [purge]

This {{#switch:Module

|=article |User=user page |RuneScape=project page |File=image |Template=template |Help=help page |Category=category |Map=map page |Forum=forum thread |Exchange=exchange page |Update=update page |Calculator=calculator |Transcript=transcript |Property=property |Module=module |Guide=guide

|Talk=talk page |User talk=talk page |RuneScape talk=talk page |File talk=talk page |Template talk=talk page |Help talk=talk page |Category talk=talk page |Map talk=map talk page |Forum talk=talk page |Exchange talk=talk page |Update talk=talk page |Calculator talk=talk page |Transcript talk=talk page |Property talk=talk page |Module talk=talk page |Guide talk=talk page

|#default=<page type>

}} does not have any documentation. Please consider adding documentation at [[{{#replace:Module:Main article|/doc}}/doc]]. [[{{fullurl:{{#replace:Module:Main article|/doc}}/doc|action=edit}} edit]]{{#If:||
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