Mòideal:Italic title
Coltas
(deasbaireachd⧼tpt-languages-separator⧽ ⧼tpt-languages-separator⧽eachdraidh⧼tpt-languages-separator⧽ceanglaichean⧼tpt-languages-separator⧽doc⧼tpt-languages-separator⧽bogsa-gainmhich⧼tpt-languages-separator⧽cùisean deuchainn)
This module is currently protected from editing. See the protection policy and protection log for more details. Please discuss any changes on the talk page; you may submit an edit request to ask an administrator to make an edit if it is uncontroversial or supported by consensus. You may also request that this page be unprotected. |
This module is subject to page protection. It is a highly visible module in use by a very large number of pages, or is substituted very frequently. Because vandalism or mistakes would affect many pages, and even trivial editing might cause substantial load on the servers, it is protected from editing. |
This template is used on a very large number of pages. To avoid large-scale disruption and unnecessary server load, any changes to this template should first be tested in its /sandbox or /testcases subpages, or in your own user space. The tested changes can then be added to this page in one single edit. Please consider discussing any changes on the talk page before implementing them. |
This module implements {{Italic title}} and {{Italic dab}}. Please see the template pages for documentation.
-- This module implements {{Italic title}}.
require('Mòideal:No globals')
local libraryUtil = require('libraryUtil')
local checkType = libraryUtil.checkType
local checkTypeForNamedArg = libraryUtil.checkTypeForNamedArg
local yesno = require('Mòideal:Yesno')
--------------------------------------------------------------------------------
-- ItalicTitle class
--------------------------------------------------------------------------------
local ItalicTitle = {}
do
----------------------------------------------------------------------------
-- Class attributes and functions
-- Things that belong to the class are here. Things that belong to each
-- object are in the constructor.
----------------------------------------------------------------------------
-- Keys of title parts that can be Italicized.
local ItalicizableKeys = {
namespace = true,
title = true,
dab = true,
}
----------------------------------------------------------------------------
-- ItalicTitle constructor
-- This contains all the dynamic attributes and methods.
----------------------------------------------------------------------------
function ItalicTitle.new()
local obj = {}
-- Function for checking self variable in methods.
local checkSelf = libraryUtil.makeCheckSelfFunction(
'ItalicTitle',
'obj',
obj,
'ItalicTitle object'
)
-- Checks a key is present in a lookup table.
-- Param: name - the function name.
-- Param: argId - integer position of the key in the argument list.
-- Param: key - the key.
-- Param: lookupTable - the table to look the key up in.
local function checkKey(name, argId, key, lookupTable)
if not lookupTable[key] then
error(string.format(
"bad argument #%d to '%s' ('%s' is not a valid key)",
argId,
name,
key
), 3)
end
end
-- Set up object structure.
local parsed = false
local categories = {}
local ItalicizedKeys = {}
local ItalicizedSubstrings = {}
-- Parses a title object into its namespace text, title, and
-- disambiguation text.
-- Param: options - a table of options with the following keys:
-- title - the title object to parse
-- ignoreDab - ignore any disambiguation parentheses
-- Returns the current object.
function obj:parseTitle(options)
checkSelf(self, 'parseTitle')
checkType('parseTitle', 1, options, 'table')
checkTypeForNamedArg('parseTitle', 'title', options.title, 'table')
local title = options.title
-- Title and dab text
local prefix, parentheses
if not options.ignoreDab then
prefix, parentheses = mw.ustring.match(
title.text,
'^(.+) %(([^%(%)]+)%)$'
)
end
if prefix and parentheses then
self.title = prefix
self.dab = parentheses
else
self.title = title.text
end
-- Namespace
local namespace = mw.site.namespaces[title.namespace].name
if namespace and #namespace >= 1 then
self.namespace = namespace
end
-- Register the object as having parsed a title.
parsed = true
return self
end
-- Italicizes part of the title.
-- Param: key - the key of the title part to be Italicized. Possible
-- keys are contained in the ItalicizableKeys table.
-- Returns the current object.
function obj:Italicize(key)
checkSelf(self, 'Italicize')
checkType('Italicize', 1, key, 'string')
checkKey('Italicize', 1, key, ItalicizableKeys)
ItalicizedKeys[key] = true
return self
end
-- Un-Italicizes part of the title.
-- Param: key - the key of the title part to be un-Italicized. Possible
-- keys are contained in the ItalicizableKeys table.
-- Returns the current object.
function obj:unItalicize(key)
checkSelf(self, 'unItalicize')
checkType('unItalicize', 1, key, 'string')
checkKey('unItalicize', 1, key, ItalicizableKeys)
ItalicizedKeys[key] = nil
return self
end
-- Italicizes a substring in the title. This only affects the main part
-- of the title, not the namespace or the disambiguation text.
-- Param: s - the substring to be Italicized.
-- Returns the current object.
function obj:ItalicizeSubstring(s)
checkSelf(self, 'ItalicizeSubstring')
checkType('ItalicizeSubstring', 1, s, 'string')
ItalicizedSubstrings[s] = true
return self
end
-- Un-Italicizes a substring in the title. This only affects the main
-- part of the title, not the namespace or the disambiguation text.
-- Param: s - the substring to be un-Italicized.
-- Returns the current object.
function obj:unItalicizeSubstring(s)
checkSelf(self, 'unItalicizeSubstring')
checkType('unItalicizeSubstring', 1, s, 'string')
ItalicizedSubstrings[s] = nil
return self
end
-- Renders the object into a page name. If no title has yet been parsed,
-- the current title is used.
-- Returns string
function obj:renderTitle()
checkSelf(self, 'renderTitle')
-- Italicizes a string
-- Param: s - the string to Italicize
-- Returns string.
local function Italicize(s)
assert(type(s) == 'string', 's was not a string')
assert(s ~= '', 's was the empty string')
return string.format('<i>%s</i>', s)
end
-- Escape characters in a string that are magic in Lua patterns.
-- Param: pattern - the pattern to escape
-- Returns string.
local function escapeMagicCharacters(s)
assert(type(s) == 'string', 's was not a string')
return s:gsub('%p', '%%%0')
end
-- If a title hasn't been parsed yet, parse the current title.
if not parsed then
self:parseTitle{title = mw.title.getCurrentTitle()}
end
-- Italicize the different parts of the title and store them in a
-- titleParts table to be joined together later.
local titleParts = {}
-- Italicize the Italicizable keys.
for key in pairs(ItalicizableKeys) do
if self[key] then
if ItalicizedKeys[key] then
titleParts[key] = Italicize(self[key])
else
titleParts[key] = self[key]
end
end
end
-- Italicize substrings. If there are any substrings to be
-- Italicized then start from the raw title, as this overrides any
-- Italicization of the main part of the title.
if next(ItalicizedSubstrings) then
titleParts.title = self.title
for s in pairs(ItalicizedSubstrings) do
local pattern = escapeMagicCharacters(s)
local ItalicizedTitle, nReplacements = titleParts.title:gsub(
pattern,
Italicize
)
titleParts.title = ItalicizedTitle
-- If we didn't make any replacements then it means that we
-- have been passed a bad substring or that the page has
-- been moved to a bad title, so add a tracking category.
if nReplacements < 1 then
categories['Pages using Italic title with no matching string'] = true
end
end
end
-- Assemble the title together from the parts.
local ret = ''
if titleParts.namespace then
ret = ret .. titleParts.namespace .. ':'
end
ret = ret .. titleParts.title
if titleParts.dab then
ret = ret .. ' (' .. titleParts.dab .. ')'
end
return ret
end
-- Returns an expanded DISPLAYTITLE parser function called with the
-- result of obj:renderTitle, plus any other optional arguments.
-- Returns string
function obj:renderDisplayTitle(...)
checkSelf(self, 'renderDisplayTitle')
return mw.getCurrentFrame():callParserFunction(
'DISPLAYTITLE',
self:renderTitle(),
...
)
end
-- Returns an expanded DISPLAYTITLE parser function called with the
-- result of obj:renderTitle, plus any other optional arguments, plus
-- any tracking categories.
-- Returns string
function obj:render(...)
checkSelf(self, 'render')
local ret = self:renderDisplayTitle(...)
for cat in pairs(categories) do
ret = ret .. string.format(
'[[Category:%s]]',
cat
)
end
return ret
end
return obj
end
end
--------------------------------------------------------------------------------
-- Exports
--------------------------------------------------------------------------------
local p = {}
local function getArgs(frame, wrapper)
assert(type(wrapper) == 'string', 'wrapper was not a string')
return require('Mòideal:Arguments').getArgs(frame, {
wrappers = wrapper
})
end
-- Main function for {{Italic title}}
function p._main(args)
checkType('_main', 1, args, 'table')
local ItalicTitle = ItalicTitle.new()
ItalicTitle:parseTitle{
title = mw.title.getCurrentTitle(),
ignoreDab = yesno(args.all, false)
}
if args.string then
ItalicTitle:ItalicizeSubstring(args.string)
else
ItalicTitle:Italicize('title')
end
return ItalicTitle:render(args[1])
end
function p.main(frame)
return p._main(getArgs(frame, 'Teamplaid:Italic title'))
end
function p._dabonly(args)
return ItalicTitle.new()
:Italicize('dab')
:render(args[1])
end
function p.dabonly(frame)
return p._dabonly(getArgs(frame, 'Teamplaid:Italic dab'))
end
return p