Difference between revisions of "Module:Main/sandbox"
Jump to navigation
Jump to search
test>Nihiltres m (Sync'ed with main module) |
test>Nihiltres (Prototype improvement: offload common stuff onto Module:Labelled list hatnote) |
||
Line 10: | Line 10: | ||
local mHatnote = require('Module:Hatnote') | local mHatnote = require('Module:Hatnote') | ||
local mHatlist = require('Module:Hatnote list') | local mHatlist = require('Module:Hatnote list') | ||
+ | local mLabelledList = require('Module:Labelled list hatnote') | ||
local mArguments -- lazily initialise | local mArguments -- lazily initialise | ||
local p = {} | local p = {} | ||
Line 16: | Line 17: | ||
mArguments = require('Module:Arguments') | mArguments = require('Module:Arguments') | ||
local args = mArguments.getArgs(frame, {parentOnly = true}) | local args = mArguments.getArgs(frame, {parentOnly = true}) | ||
− | local pages = | + | local pages = mLabelledList.preprocessDisplays(args) |
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
local options = { | local options = { | ||
− | selfref = args.selfref | + | selfref = args.selfref, |
+ | category = args.category | ||
} | } | ||
return p._main(pages, options) | return p._main(pages, options) | ||
end | end | ||
− | function p._main( | + | function p._main(pages, 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 currentTitle = mw.title.getCurrentTitle() | local currentTitle = mw.title.getCurrentTitle() | ||
− | if # | + | if #pages == 0 then |
− | local firstPage = string.gsub( | + | if mw.title.getCurrentTitle().namespace == 0 then |
+ | return mHatnote.makeWikitextError( | ||
+ | 'no page names specified', | ||
+ | 'Template:Main#Errors', | ||
+ | options.category | ||
+ | ) | ||
+ | else | ||
+ | pages = {currentTitle.text} | ||
+ | end | ||
+ | end | ||
+ | local firstPage = string.gsub(pages[1], '|.*$', '') | ||
-- Find the pagetype. | -- Find the pagetype. | ||
local pageType = mHatnote.findNamespaceId(firstPage) == 0 and 'article' or 'page' | local pageType = mHatnote.findNamespaceId(firstPage) == 0 and 'article' or 'page' | ||
− | |||
− | |||
− | |||
− | |||
− | |||
local curNs = currentTitle.namespace | local curNs = currentTitle.namespace | ||
if (curNs == 14) or (curNs == 15) then --category/talk namespaces | if (curNs == 14) or (curNs == 15) then --category/talk namespaces | ||
− | mainForm = | + | local mainForm = #pages > 1 and |
− | 'The main %ss for this [[Wikipedia:Categorization|category]] are %s' | + | 'The main %ss for this [[Wikipedia:Categorization|category]] are %s.' |
or | or | ||
− | 'The main %s for this [[Wikipedia:Categorization|category]] is %s' | + | 'The main %s for this [[Wikipedia:Categorization|category]] is %s.' |
+ | return mHatnote._hatnote( | ||
+ | string.format(mainForm, pageType, mHatlist.andList(pages, true)), | ||
+ | {selfref = options.selfref} | ||
+ | ) | ||
else | else | ||
− | + | return mLabelledList._labelledList( | |
+ | pages, | ||
+ | { | ||
+ | string.format('Main %s', pageType), | ||
+ | string.format('Main %ss', pageType) | ||
+ | }, | ||
+ | { | ||
+ | category = options.category, | ||
+ | selfref = options.selfref, | ||
+ | template = 'Template:Main' | ||
+ | } | ||
+ | ) | ||
end | end | ||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
end | end | ||
return p | return p |
Revision as of 19:49, 1 July 2016
Documentation for this module may be created at Module:Main/sandbox/doc
--[[
-- 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 mHatlist = require('Module:Hatnote list')
local mLabelledList = require('Module:Labelled list hatnote')
local mArguments -- lazily initialise
local p = {}
function p.main(frame)
mArguments = require('Module:Arguments')
local args = mArguments.getArgs(frame, {parentOnly = true})
local pages = mLabelledList.preprocessDisplays(args)
local options = {
selfref = args.selfref,
category = args.category
}
return p._main(pages, options)
end
function p._main(pages, options)
-- Get the list of pages. If no first page was specified we use the current
-- page name.
local currentTitle = mw.title.getCurrentTitle()
if #pages == 0 then
if mw.title.getCurrentTitle().namespace == 0 then
return mHatnote.makeWikitextError(
'no page names specified',
'Template:Main#Errors',
options.category
)
else
pages = {currentTitle.text}
end
end
local firstPage = string.gsub(pages[1], '|.*$', '')
-- Find the pagetype.
local pageType = mHatnote.findNamespaceId(firstPage) == 0 and 'article' or 'page'
local curNs = currentTitle.namespace
if (curNs == 14) or (curNs == 15) then --category/talk namespaces
local mainForm = #pages > 1 and
'The main %ss for this [[Wikipedia:Categorization|category]] are %s.'
or
'The main %s for this [[Wikipedia:Categorization|category]] is %s.'
return mHatnote._hatnote(
string.format(mainForm, pageType, mHatlist.andList(pages, true)),
{selfref = options.selfref}
)
else
return mLabelledList._labelledList(
pages,
{
string.format('Main %s', pageType),
string.format('Main %ss', pageType)
},
{
category = options.category,
selfref = options.selfref,
template = 'Template:Main'
}
)
end
end
return p