Toggle menu
Toggle preferences menu
Toggle personal menu
Not logged in
You must create an account or log in to edit.

Module:Hatnote: Difference between revisions

From Amaranth Legacy, available at amaranth-legacy.community
Content deleted Content added
Revert
m Removed protection from "Module:Hatnote"
 
(32 intermediate revisions by 2 users not shown)
Line 1: Line 1:
local getArgs = require('Module:Arguments').getArgs
--------------------------------------------------------------------------------
local yesno = require('Module:Yesno')
-- Module:Hatnote --
-- --
-- This module produces hatnote links and links to related articles. It --
-- implements the {{hatnote}} and {{format link}} meta-templates and includes --
-- helper functions for other Lua hatnote modules. --
--------------------------------------------------------------------------------

local libraryUtil = require('libraryUtil')
local checkType = libraryUtil.checkType
local checkTypeForNamedArg = libraryUtil.checkTypeForNamedArg
local mArguments -- lazily initialise [[Module:Arguments]]
local yesno -- lazily initialise [[Module:Yesno]]
local formatLink -- lazily initialise [[Module:Format link]] ._formatLink

local p = {}
local p = {}


function p.main(frame)
--------------------------------------------------------------------------------
local args = getArgs(frame)
-- Helper functions
local wrapper = mw.html.create('div')
--------------------------------------------------------------------------------
:addClass('hatnote-wrapper')


if args['class'] then wrapper:addClass(args['class']) end
local function getArgs(frame)
-- Fetches the arguments from the parent frame. Whitespace is trimmed and
-- blanks are removed.
mArguments = require('Module:Arguments')
return mArguments.getArgs(frame, {parentOnly = true})
end


-- backwards compatibility
local function removeInitialColon(s)
if args['file'] then args['file1'] = args['file'] end
-- Removes the initial colon from a string, if present.
if args['file_class'] then args['file1_class'] = args['file_class'] end
return s:match('^:?(.*)')
if args['file_link'] then args['file1_link'] = args['file_link'] end
end


-- input validation
function p.defaultClasses(inline)
if not args['file1'] and args['file2'] then
-- Provides the default hatnote classes as a space-separated string; useful
error('file1 is required when file2 is specified')
-- for hatnote-manipulation modules like [[Module:Hatnote group]].
return
(inline == 1 and 'hatnote-inline' or 'hatnote') .. ' ' ..
'navigation-not-searchable'
end

function p.disambiguate(page, disambiguator)
-- Formats a page title with a disambiguation parenthetical,
-- i.e. "Example" → "Example (disambiguation)".
checkType('disambiguate', 1, page, 'string')
checkType('disambiguate', 2, disambiguator, 'string', true)
disambiguator = disambiguator or 'disambiguation'
return mw.ustring.format('%s (%s)', page, disambiguator)
end

function p.findNamespaceId(link, removeColon)
-- Finds the namespace id (namespace number) of a link or a pagename. This
-- function will not work if the link is enclosed in double brackets. Colons
-- are trimmed from the start of the link by default. To skip colon
-- trimming, set the removeColon parameter to false.
checkType('findNamespaceId', 1, link, 'string')
checkType('findNamespaceId', 2, removeColon, 'boolean', true)
if removeColon ~= false then
link = removeInitialColon(link)
end
end
if not args['primary'] then
local namespace = link:match('^(.-):')
error('primary is required')
if namespace then
end
local nsTable = mw.site.namespaces[namespace]

if nsTable then
-- set the color so the look stays consistent even outside of the scope
return nsTable.id
if args['color'] then
wrapper:css('--template-arg-color--light', args['color'])
wrapper:css('--template-arg-color--dark', args['color'])
if yesno(args['color_applies_to_links'] or true) then
wrapper:css('--template-arg-color-link--light', args['color'])
wrapper:css('--template-arg-color-link--dark', args['color'])
end
elseif args['color_light'] and args['color_dark'] then
wrapper:css('--template-arg-color--light', args['color_light'])
wrapper:css('--template-arg-color--dark', args['color_dark'])
if yesno(args['color_applies_to_links'] or true) then
wrapper:css('--template-arg-color-link--light', args['color_light'])
wrapper:css('--template-arg-color-link--dark', args['color_dark'])
end
end
end
end
return 0
end


-- top <hr>
function p.makeWikitextError(msg, helpLink, addTrackingCategory, title)
if not yesno(args['remove_hr'] or false) and not yesno(args['remove_hr_top'] or false) then
-- Formats an error message to be returned to wikitext. If
wrapper:wikitext('<hr>')
-- addTrackingCategory is not false after being returned from
-- [[Module:Yesno]], and if we are not on a talk page, a tracking category
-- is added.
checkType('makeWikitextError', 1, msg, 'string')
checkType('makeWikitextError', 2, helpLink, 'string', true)
yesno = require('Module:Yesno')
title = title or mw.title.getCurrentTitle()
-- Make the help link text.
local helpText
if helpLink then
helpText = ' ([[' .. helpLink .. '|help]])'
else
helpText = ''
end
end

-- Make the category text.
-- content wrapper
local category
local content = wrapper:tag('div')
if not title.isTalkPage -- Don't categorise talk pages
:attr('role', 'note')
and title.namespace ~= 2 -- Don't categorise userspace
:addClass('noexcerpt')
and yesno(addTrackingCategory) ~= false -- Allow opting out
:addClass('hatnote')
then
if args['file1'] and args['file2'] then
category = 'Hatnote templates with errors'
content:addClass('hatnote2')
category = mw.ustring.format(
elseif args['file1'] then
'[[%s:%s]]',
content:addClass('hatnote1')
mw.site.namespaces[14].name,
category
)
else
else
content:addClass('hatnote0')
category = ''
end
end
return mw.ustring.format(
'<strong class="error">Error: %s%s.</strong>%s',
msg,
helpText,
category
)
end


-- left file
local curNs = mw.title.getCurrentTitle().namespace
if args['file1'] then
p.missingTargetCat =
local class = 'notpageimage hatnote-image1 attribute-loading-unset'
--Default missing target category, exported for use in related modules
if args['file1_class'] then
((curNs == 0) or (curNs == 14)) and
class = class .. ' ' .. args['file1_class']
'Articles with hatnote templates targeting a nonexistent page' or nil
end

content:wikitext('[['
function p.quote(title)
.. 'File:' .. args['file1']
--Wraps titles in quotation marks. If the title starts/ends with a quotation
.. '|' .. (args['file_size'] or '80px')
--mark, kerns that side as with {{-'}}
.. '|class=' .. class
local quotationMarks = {
.. '|link=' .. (args['file1_link'] or '')
["'"]=true, ['"']=true, ['“']=true, ["‘"]=true, ['”']=true, ["’"]=true
.. '|alt='
}
.. ']]')
local quoteLeft, quoteRight = -- Test if start/end are quotation marks
quotationMarks[string.sub(title, 1, 1)],
quotationMarks[string.sub(title, -1, -1)]
if quoteLeft or quoteRight then
title = mw.html.create("span"):wikitext(title)
end
end
if quoteLeft then title:css("padding-left", "0.15em") end
if quoteRight then title:css("padding-right", "0.15em") end
return '"' .. tostring(title) .. '"'
end


-- center text
--------------------------------------------------------------------------------
local text = content:tag('div'):addClass('hatnote-text')
-- Hatnote
--
-- Produces standard hatnote text. Implements the {{hatnote}} template.
--------------------------------------------------------------------------------
p[''] = function (frame) return p.hatnote(frame:newChild{ title = "Template:Hatnote" }) end


-- primary text wrapper
function p.hatnote(frame)
local primary_text = text:tag('div'):addClass('hatnote-primary')
local args = getArgs(frame)
-- primary text linear-gradient
local s = args[1]
if not s then
if args['primary_gradient'] then
primary_text
return p.makeWikitextError(
:css('color', 'transparent')
'no text specified',
:css('background-image', 'linear-gradient(' .. args['primary_gradient'] .. ')')
'Template:Hatnote#Errors',
:css('background-clip', 'text')
args.category
end
)
-- primary text font-size
if args['primary_font_size'] then
primary_text:css('font-size', 'var(--font-size-' .. args['primary_font_size'] .. ')')
end
-- primary text font-family
if args['font'] then
primary_text:css('font-family', args['font'])
end
-- primary text text-transform
if args['primary_transform'] then
primary_text:css('text-transform', args['primary_transform'])
end
end
-- primary text
return p._hatnote(s, {
primary_text:wikitext(args['primary'])
extraclasses = args.extraclasses,
selfref = args.selfref
})
end


-- secondary text
function p._hatnote(s, options)
if args['secondary'] then
checkType('_hatnote', 1, s, 'string')
-- italic secondary text
checkType('_hatnote', 2, options, 'table', true)
if yesno(args['secondary_italic'] or true) and yesno(args['italic_secondary'] or true) then
options = options or {}
args['secondary'] = '\'\'' .. args['secondary'] .. '\'\''
local inline = options.inline
end
local hatnote = mw.html.create(inline == 1 and 'span' or 'div')
text:wikitext(args['secondary'])
local extraclasses
if type(options.extraclasses) == 'string' then
extraclasses = options.extraclasses
end
end


-- right file
hatnote
if args['file2'] then
:attr('role', 'note')
local class = 'notpageimage hatnote-image2 attribute-loading-unset'
:addClass(p.defaultClasses(inline))
if args['file2_class'] then
:addClass(extraclasses)
class = class .. ' ' .. args['file2_class']
:addClass(options.selfref and 'selfref' or nil)
end
:wikitext(s)
content:wikitext('[['
.. 'File:' .. args['file2']
.. '|' .. (args['file_size'] or '80px')
.. '|class=' .. class
.. '|link=' .. (args['file2_link'] or '')
.. '|alt='
.. ']]')
end

-- bottom <hr>
if not yesno(args['remove_hr'] or false) and not yesno(args['remove_hr_bottom'] or false) then
wrapper:wikitext('<hr>')
end


return frame:extensionTag { name = 'templatestyles', args = { src = 'Module:Hatnote/styles.css' } }
return mw.getCurrentFrame():extensionTag{
.. tostring(wrapper)
name = 'templatestyles', args = { src = 'Module:Hatnote/styles.css' }
} .. tostring(hatnote)
end
end



Latest revision as of 10:43, September 11, 2025

This module is used for Template:Hatnote. See the template's page for more information.


local getArgs = require('Module:Arguments').getArgs
local yesno = require('Module:Yesno')
local p = {}

function p.main(frame)
	local args = getArgs(frame)
	local wrapper = mw.html.create('div')
		:addClass('hatnote-wrapper')

	if args['class'] then wrapper:addClass(args['class']) end

	-- backwards compatibility
	if args['file'] then args['file1'] = args['file'] end
	if args['file_class'] then args['file1_class'] = args['file_class'] end
	if args['file_link'] then args['file1_link'] = args['file_link'] end

	-- input validation
	if not args['file1'] and args['file2'] then
		error('file1 is required when file2 is specified')
	end
	if not args['primary'] then
		error('primary is required')
	end

	-- set the color so the look stays consistent even outside of the scope
	if args['color'] then
		wrapper:css('--template-arg-color--light', args['color'])
		wrapper:css('--template-arg-color--dark', args['color'])
		if yesno(args['color_applies_to_links'] or true) then
			wrapper:css('--template-arg-color-link--light', args['color'])
			wrapper:css('--template-arg-color-link--dark', args['color'])
		end
	elseif args['color_light'] and args['color_dark'] then
		wrapper:css('--template-arg-color--light', args['color_light'])
		wrapper:css('--template-arg-color--dark', args['color_dark'])
		if yesno(args['color_applies_to_links'] or true) then
			wrapper:css('--template-arg-color-link--light', args['color_light'])
			wrapper:css('--template-arg-color-link--dark', args['color_dark'])
		end
	end

	-- top <hr>
	if not yesno(args['remove_hr'] or false) and not yesno(args['remove_hr_top'] or false) then
		wrapper:wikitext('<hr>')
	end

	-- content wrapper
	local content = wrapper:tag('div')
		:attr('role', 'note')
		:addClass('noexcerpt')
		:addClass('hatnote')
	if args['file1'] and args['file2'] then
		content:addClass('hatnote2')
	elseif args['file1'] then
		content:addClass('hatnote1')
	else
		content:addClass('hatnote0')
	end

	-- left file
	if args['file1'] then
		local class = 'notpageimage hatnote-image1 attribute-loading-unset'
		if args['file1_class'] then
			class = class .. ' ' .. args['file1_class']
		end
		content:wikitext('[['
			.. 'File:' .. args['file1']
			.. '|' .. (args['file_size'] or '80px')
			.. '|class=' .. class
			.. '|link=' .. (args['file1_link'] or '')
			.. '|alt='
		.. ']]')
	end

	-- center text
	local text = content:tag('div'):addClass('hatnote-text')

	-- primary text wrapper
	local primary_text = text:tag('div'):addClass('hatnote-primary')
	-- primary text linear-gradient
	if args['primary_gradient'] then
		primary_text
			:css('color', 'transparent')
			:css('background-image', 'linear-gradient(' .. args['primary_gradient'] .. ')')
			:css('background-clip', 'text')
	end
	-- primary text font-size
	if args['primary_font_size'] then
		primary_text:css('font-size', 'var(--font-size-' .. args['primary_font_size'] .. ')')
	end
	-- primary text font-family
	if args['font'] then
		primary_text:css('font-family', args['font'])
	end
	-- primary text text-transform
	if args['primary_transform'] then
		primary_text:css('text-transform', args['primary_transform'])
	end
	-- primary text
	primary_text:wikitext(args['primary'])

	-- secondary text
	if args['secondary'] then
		-- italic secondary text
		if yesno(args['secondary_italic'] or true) and yesno(args['italic_secondary'] or true) then
			args['secondary'] = '\'\'' .. args['secondary'] .. '\'\''
		end
		text:wikitext(args['secondary'])
	end

	-- right file
	if args['file2'] then
		local class = 'notpageimage hatnote-image2 attribute-loading-unset'
		if args['file2_class'] then
			class = class .. ' ' .. args['file2_class']
		end
		content:wikitext('[['
			.. 'File:' .. args['file2']
			.. '|' .. (args['file_size'] or '80px')
			.. '|class=' .. class
			.. '|link=' .. (args['file2_link'] or '')
			.. '|alt='
		.. ']]')
	end

	-- bottom <hr>
	if not yesno(args['remove_hr'] or false) and not yesno(args['remove_hr_bottom'] or false) then
		wrapper:wikitext('<hr>')
	end

	return frame:extensionTag { name = 'templatestyles', args = { src = 'Module:Hatnote/styles.css' } }
		.. tostring(wrapper)
end

return p