Jump to content

Module:Italic title

From Consumer Rights Wiki

This is the {{high-use}} message box.

It is meant to be put at the top of the documentation subpage for templates transcluded onto more than 2,000 pages. For templates transcluded onto more than 100,000 pages, or if the first parameter is set to risk, a stronger wording is given. See Wikipedia:High-risk templates for background information.

Note: It is normal for some of the links in the message box to be red.

{{High-use}}

{{High-use|2=discussion page, or use + notation|info=additional text}}

all parameters
{{High-use |1= |2= |info= |demo= |form= |expiry= }}

The template can be used as is, and will automatically use bot-updated transclusion counts from subpages of Module:Transclusion_count/data/, when available. It can also take some parameters:

  • 1=number of transclusions: (deprecated) The first parameter is either a static number of times the template has been transcluded, or the word "risk" (without quotes) to display "a very large number of" instead of the actual value. This value will be ignored, if transclusion data is available for the current page (generally, for templates with more than 2,000 transclusions).
  • 2=discussion page, or use + notation: The second parameter is overloaded. It will cause the number of transclusions to display as "#,###+" instead of "approximately #,###" when set equal to "yes" (without quotes). When used in this manner, values will be rounded down, instead of rounded to the nearest number with the appropriate number of significant figures. When set to any other non-blank value, it will replace the link to the template's talk page to the value of the parameter (for example, 2=WP:VPT will insert a link to WP:VPT),
  • <syntaxhighlight lang="wikitext" inline style="display: none;" />|info=<Template:Var>: When set to non-blank, will insert <Template:Var> into the template text.
  • <syntaxhighlight lang="wikitext" inline style="display: none;" />|demo=<Template:Var>: Will use the transclusion count for the template at [[Template:<Template:Var>]] instead of detecting what template it is being used on. Capitalization must exactly match the value used in Special:PrefixIndex/Module:Transclusion_count/data/.
  • <syntaxhighlight lang="wikitext" inline style="display: none;" />|form=: When set to "editnotice", will display the message using {{Editnotice }} instead of {{Ombox }}.
  • <syntaxhighlight lang="wikitext" inline style="display: none;" />|expiry=: Sets the <syntaxhighlight lang="wikitext" inline style="display: none;" />|expiry= parameter for {{Editnotice }}.
  • <syntaxhighlight lang="wikitext" inline style="display: none;" />|no-percent=yes: suppresses automatic 'percent of all pages' annotation; 'percent of all pages' annotation is automatically added when template is used in more than 1% of all pages (currently {{NUMBEROFPAGES}} is 11,902 pages so 1% is 119 pages)

Examples

[edit source]

The full code for a /doc page top usually looks like this:

{{documentation subpage}}
<!-- Add categories where indicated at the bottom of this page and interwikis at Wikidata -->
{{high-use}}

Standard form

[edit source]
{{high-use}}
{{high-use | |Wikipedia talk:High-risk templates }}

Rounding and + notation

[edit source]
{{high-use |49,500 |demo=A template that does not exist }}
{{high-use |49,500+ |demo=A template that does not exist }}

Editnotice form

[edit source]
{{high-use |form=editnotice }}

Lua error: expandTemplate: template "editnotice" does not exist.

High risk

[edit source]
{{high-use |demo=Yesno }}
{{high-use |demo=Yesno |no-percent=yes}}
{{high-use |risk |demo=High-use }}
{{high-use |risk |Wikipedia talk:High-risk templates |info=This is a very large number! |demo=Yesno}}

Technical details

[edit source]

The /sandbox and /testcases links are the standard names for such subpages. If those pages are created, then the green /doc box for the template will detect them and link to them in its heading. For instance, see the top of this documentation.

Ahechtbot compiles usage statistics for all templates with 2,000 or more transclusions, using User:Ahechtbot/transclusioncount.py, and writes them to subpages of Module:Transclusion count/data (see Wikipedia:Bots/Requests for approval/Ahechtbot 6). These pages are usually updated every Sunday, but since running the query is resource intensive, it may be delayed or skipped if Wikipedia server usage is high. Important: If a transclusion count is available in Module:Transclusion count/data, any manually input values will be ignored by this template.

Supporting templates and modules

[edit source]

Redirects

[edit source]

See also

[edit source]

Template counters

[edit source]

TemplateData

[edit source]

Script error: No such module "Template translation".

This is the {{high-use}} message box. It is meant to be put at the top of the documentation subpage for templates transcluded onto more than 2,000 pages. For templates transcluded onto more than 100,000 pages, or if the first parameter is set to risk, then a stronger wording is given. Note: It is normal that some of the links in the message box are red.

Template parameters[Edit template data]

This template prefers inline formatting of parameters.

ParameterDescriptionTypeStatus
number of transclusions (DEPRECATED)1

either a static number of times the template has been transcluded, or the word "risk" (without quotes) to display "a very large number of" instead of the actual value.

Suggested values
risk
Stringdeprecated
discussion page, or use + notation2

The second parameter is overloaded. It will cause the number of transclusions to display as "#,###+" instead of "approximately #,###" when set equal to "yes" (without quotes). When used in this manner, values will be rounded down, instead of rounded to the nearest number with the appropriate number of significant figures. When set to any other non-blank value, it will replace the link to the template's talk page to the value of the parameter (for example, 2=WP:VPT will insert a link to WP:VPT)

Default
Auto value
Stringoptional
Extra informationinfo

When set to non-blank, will insert the value of this parameter into the template text.

Contentoptional
template name (intended for demo purposes)demo

Will use the transclusion count for the template at [[Template:<Template_name>]] instead of detecting what template it is being used on. Capitalization must exactly match the value used in Special:PrefixIndex/Module:Transclusion_count/data/.

Stringoptional
form for this templateform

When set to "editnotice", will display the message using {{editnotice}} instead of {{ombox}}.

Suggested values
editnotice
Stringoptional
Expiry for {{editnotice}}expiry

if `form=editnotice`, this will set the expiry parameter of the editnotice.

Dateoptional
systemsystem

Please directly use {{Used in system}} instead

Unknowndeprecated

This module implements {{Italic title }} and {{Italic disambiguation }}. Please see the template pages for documentation.


-- This module implements {{italic title}}.

require('strict')
local libraryUtil = require('libraryUtil')
local checkType = libraryUtil.checkType
local checkTypeForNamedArg = libraryUtil.checkTypeForNamedArg
local yesno = require('Module: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('Module: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, 'Template: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, 'Template:Italic dab'))
end


return p