Difference between revisions of "Module:Main"

From Shipbucket Wiki
Jump to: navigation, search
(Removed extraclasses option; those classes appear to be deprecated)
m (1 revision imported)
 
(3 intermediate revisions by 3 users not shown)
Line 9: Line 9:
  
 
local mHatnote = require('Module:Hatnote')
 
local mHatnote = require('Module:Hatnote')
local mHatlist = require('Module:Hatnote list')
+
local mTableTools -- lazily initialise
 
local mArguments -- lazily initialise
 
local mArguments -- lazily initialise
 +
 
local p = {}
 
local p = {}
  
 
function p.main(frame)
 
function p.main(frame)
 +
mTableTools = require('Module:TableTools')
 
mArguments = require('Module:Arguments')
 
mArguments = require('Module:Arguments')
 
local args = mArguments.getArgs(frame, {parentOnly = true})
 
local args = mArguments.getArgs(frame, {parentOnly = true})
Line 19: Line 21:
 
for k, v in pairs(args) do
 
for k, v in pairs(args) do
 
if type(k) == 'number' then
 
if type(k) == 'number' then
local display = args['label ' .. k] or args['l' .. k]
+
local display = args['l' .. tostring(k)]
local page = display and
+
local page = {v, display}
string.format('%s|%s', string.gsub(v, '|.*$', ''), display) or v
+
pages[k] = page
pages[#pages + 1] = page
 
 
end
 
end
 
end
 
end
if #pages == 0 and mw.title.getCurrentTitle().namespace == 0 then
+
pages = mTableTools.compressSparseArray(pages)
return mHatnote.makeWikitextError(
 
'no page names specified',
 
'Template:Main#Errors',
 
args.category
 
)
 
end
 
 
local options = {
 
local options = {
 
selfref = args.selfref
 
selfref = args.selfref
 
}
 
}
return p._main(pages, options)
+
return p._main(options, unpack(pages))
 
end
 
end
  
function p._main(args, options)
+
function p._main(options, ...)
 
-- Get the list of pages. If no first page was specified we use the current
 
-- Get the list of pages. If no first page was specified we use the current
 
-- page name.
 
-- page name.
 +
local pages = {...}
 
local currentTitle = mw.title.getCurrentTitle()
 
local currentTitle = mw.title.getCurrentTitle()
if #args == 0 then args = {currentTitle.text} end
+
local firstPageTable = pages[1]
local firstPage = string.gsub(args[1], '|.*$', '')
+
local firstPage
 +
if firstPageTable then
 +
firstPage = firstPageTable[1]
 +
else
 +
firstPage = currentTitle.text
 +
firstPageTable = {firstPage}
 +
pages[1] = firstPageTable
 +
end
 +
 
 
-- Find the pagetype.
 
-- Find the pagetype.
local pageType = mHatnote.findNamespaceId(firstPage) == 0 and 'article' or 'page'
+
local firstPageNs = mHatnote.findNamespaceId(firstPage)
 +
local pagetype = firstPageNs == 0 and 'article' or 'page'
 +
 
 
-- Make the formatted link text
 
-- Make the formatted link text
list = mHatlist.andList(args, true)
+
local links = mHatnote.formatPageTables(unpack(pages))
 +
links = mw.text.listToText(links)
 +
 
 
-- Build the text.
 
-- Build the text.
local isPlural = #args > 1
+
local isPlural = #pages > 1
local mainForm
+
local currentNs = currentTitle.namespace
local curNs = currentTitle.namespace
+
local isCategoryNamespace = currentNs - currentNs % 2 == 14
if (curNs == 14) or (curNs == 15) then --category/talk namespaces
+
local stringToFormat
mainForm = isPlural and
+
if isCategoryNamespace then
'The main %ss for this [[Wikipedia:Categorization|category]] are %s'
+
if isPlural then
or
+
stringToFormat = 'The main %ss for this'
'The main %s for this [[Wikipedia:Categorization|category]] is %s'
+
.. ' [[Wikipedia:Categorization|category]] are %s'
 +
else
 +
stringToFormat = 'The main %s for this'
 +
.. ' [[Wikipedia:Categorization|category]] is %s'
 +
end
 
else
 
else
mainForm = isPlural and 'Main %ss: %s' or 'Main %s: %s'
+
if isPlural then
 +
stringToFormat = 'Main %ss: %s'
 +
else
 +
stringToFormat = 'Main %s: %s'
 +
end
 
end
 
end
local text = string.format(mainForm, pageType, list)
+
local text = string.format(stringToFormat, pagetype, links)
 +
 
 
-- Process the options and pass the text to the _rellink function in
 
-- Process the options and pass the text to the _rellink function in
 
-- [[Module:Hatnote]].
 
-- [[Module:Hatnote]].
 
options = options or {}
 
options = options or {}
 
local hnOptions = {
 
local hnOptions = {
 +
extraclasses = 'relarticle mainarticle',
 
selfref = options.selfref
 
selfref = options.selfref
 
}
 
}

Latest revision as of 20:46, 9 February 2018

This module produces a link to a main article or articles. It implements the {{main}} template. Normally, it produces a link like "Main article: A". If used in the category namespace, it produces a link like "The main article for this category is A". It is possible to specify multiple articles, and in this case plural wording is used automatically. If the first link is not an article, the module uses the wording "Main page" instead of "Main article".

Usage from wikitext

This module cannot be accessed directly from #invoke. Instead, it can only be used through the {{main}} template. Please see the template page for documentation.

Usage from other Lua modules

Load the module:

local mMain = require('Module:Main')

You can then use the _main function like this:

mMain._main(options, ...)

The parameters following options are a list of link/display tables, used to generate the links. The first value in each table is the link, and is required. The second value in each table is the display value, and is optional. Category or file links are automatically escaped using the colon trick. If a link includes a section name, and no display value is set, links are automatically formatted as page § section, rather than the MediaWiki default of page#section.

The options table can be used to configure the function's output. At current, the only option available is "selfref", which is used when the output is a self-reference to Wikipedia. to set this option, use {selfref = true}. (See the {{selfref}} template for more details on self-references.)

Example 1

mMain._main(nil, {'A'})

Produces:

<div role="note" class="hatnote relarticle mainarticle">Main article: [[A]]</div>

Displays as:

Main article: A

Example 2

mMain._main(nil, {'Wikipedia:Categorization'}, {'Help:Category'}, {'Category:Wikipedia categories'})

Produces:

<div role="note" class="hatnote relarticle mainarticle">Main pages: [[Wikipedia:Categorization]], [[Help:Category]] and [[:Category:Wikipedia categories]]</div>

Displays as:

Example 3

mMain._main(nil, {'A', 'the letter "A"'}, {'B', 'the letter "B"'}, {'C', 'the letter "C"'})

Produces:

<div role="note" class="hatnote relarticle mainarticle">Main articles: [[A|the letter "A"]], [[B|the letter "B"]] and [[C|the letter "C"]]</div>

Displays as:

Example 4

mMain._main({selfref = true}, {'Wikipedia:Verifiability#Burden'})

Produces:

<div role="note" class="hatnote relarticle mainarticle selfref">Main article: [[Wikipedia:Verifiability#Burden|Wikipedia:Verifiability § Burden]]</div>

Displays as:

Example 5 (if used in the category namespace)

mMain._main(nil, {'A'})

Produces:

<div role="note" class="hatnote relarticle mainarticle">The main article for this [[Wikipedia:Categorization|category]] is [[A]]</div>

Displays as:

The main article for this category is A

Technical details

This module uses Module:Hatnote to format the hatnote text, Module:TableTools to process the list of links, and Module:Arguments to fetch the arguments from wikitext.


--[[
-- This module produces a link to a main article or articles. It implements the
-- template {{main}}.
-- 
-- If the module is used in category or category talk space, it produces "The
-- main article for this category is xxx". Otherwise, it produces
-- "Main article: xxx".
--]]

local mHatnote = require('Module:Hatnote')
local mTableTools -- lazily initialise
local mArguments -- lazily initialise

local p = {}

function p.main(frame)
	mTableTools = require('Module:TableTools')
	mArguments = require('Module:Arguments')
	local args = mArguments.getArgs(frame, {parentOnly = true})
	local pages = {}
	for k, v in pairs(args) do
		if type(k) == 'number' then
			local display = args['l' .. tostring(k)]
			local page = {v, display}
			pages[k] = page
		end
	end
	pages = mTableTools.compressSparseArray(pages)
	local options = {
		selfref = args.selfref
	}
	return p._main(options, unpack(pages))
end

function p._main(options, ...)
	-- Get the list of pages. If no first page was specified we use the current
	-- page name.
	local pages = {...}
	local currentTitle = mw.title.getCurrentTitle()
	local firstPageTable = pages[1]
	local firstPage
	if firstPageTable then
		firstPage = firstPageTable[1]
	else
		firstPage = currentTitle.text
		firstPageTable = {firstPage}
		pages[1] = firstPageTable
	end

	-- Find the pagetype.
	local firstPageNs = mHatnote.findNamespaceId(firstPage)
	local pagetype = firstPageNs == 0 and 'article' or 'page'

	-- Make the formatted link text
	local links = mHatnote.formatPageTables(unpack(pages))
	links = mw.text.listToText(links)

	-- Build the text.
	local isPlural = #pages > 1
	local currentNs = currentTitle.namespace
	local isCategoryNamespace = currentNs - currentNs % 2 == 14
	local stringToFormat
	if isCategoryNamespace then
		if isPlural then
			stringToFormat = 'The main %ss for this'
				.. ' [[Wikipedia:Categorization|category]] are %s'
		else
			stringToFormat = 'The main %s for this'
				.. ' [[Wikipedia:Categorization|category]] is %s'
		end
	else
		if isPlural then
			stringToFormat = 'Main %ss: %s'
		else
			stringToFormat = 'Main %s: %s'
		end
	end
	local text = string.format(stringToFormat, pagetype, links)

	-- Process the options and pass the text to the _rellink function in
	-- [[Module:Hatnote]].
	options = options or {}
	local hnOptions = {
		extraclasses = 'relarticle mainarticle',
		selfref = options.selfref
	}
	return mHatnote._hatnote(text, hnOptions)
end

return p