Module:Hatnote: Difference between revisions
From Amaranth Legacy, available at amaranth-legacy.community
More actions
Content deleted Content added
Revert |
Tesinormed (talk | contribs) No edit summary |
||
| 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('noexcerpt') |
|||
:addClass('template__hatnote') |
|||
:attr('role', 'note') |
|||
if args['additional_class'] then |
|||
local function getArgs(frame) |
|||
wrapper:addClass(additional_class) |
|||
-- Fetches the arguments from the parent frame. Whitespace is trimmed and |
|||
end |
|||
-- 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. |
|||
return s:match('^:?(.*)') |
|||
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('file2 is required when file1 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('--color-amaranth-legacy-scope', args['color']) |
|||
if yesno(args['color_applies_to_links'] or true) then |
|||
wrapper:css('--color-link', args['color']) |
|||
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 |
|||
if args['file1'] and args['file2'] then |
|||
and title.namespace ~= 2 -- Don't categorise userspace |
|||
content:attr('class', 'hatnote2') |
|||
and yesno(addTrackingCategory) ~= false -- Allow opting out |
|||
elseif args['file1'] then |
|||
then |
|||
content:attr('class', 'hatnote1') |
|||
category = 'Hatnote templates with errors' |
|||
category = mw.ustring.format( |
|||
'[[%s:%s]]', |
|||
mw.site.namespaces[14].name, |
|||
category |
|||
) |
|||
else |
else |
||
content:attr('class', '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 file_text = '[[' |
|||
--Default missing target category, exported for use in related modules |
|||
.. 'File:' .. args['file1'] |
|||
((curNs == 0) or (curNs == 14)) and |
|||
.. '|' .. (args['file_size'] or '80px') |
|||
'Articles with hatnote templates targeting a nonexistent page' or nil |
|||
.. '|link=' |
|||
.. '|alt=' |
|||
function p.quote(title) |
|||
if yesno(args['file_invert_in_dark_mode'] or false) then file_text = file_text .. '|class=skin-invert-image' end |
|||
--Wraps titles in quotation marks. If the title starts/ends with a quotation |
|||
file_text = file_text .. ']]' |
|||
--mark, kerns that side as with {{-'}} |
|||
content:tag('div'):wikitext(file_text) |
|||
local quotationMarks = { |
|||
["'"]=true, ['"']=true, ['“']=true, ["‘"]=true, ['”']=true, ["’"]=true |
|||
} |
|||
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') |
|||
-- 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 |
local primary_text = text:tag('span') |
||
:css('display', 'inline-block') |
|||
local s = args[1] |
|||
-- primary text linear-gradient |
|||
if not s then |
|||
if args['primary_gradient'] then |
|||
return p.makeWikitextError( |
|||
primary_text:css('color', 'transparent') |
|||
'no text specified', |
|||
:css('background-image', 'linear-gradient(' .. args['primary_gradient'] .. ')') |
|||
'Template:Hatnote#Errors', |
|||
:css('-webkit-background-clip', 'text') |
|||
args.category |
|||
:css('background-clip', 'text') |
|||
) |
|||
else |
|||
primary_text:css('color', 'var(--color-amaranth-legacy-scope)') |
|||
end |
end |
||
-- primary text font-size |
|||
return p._hatnote(s, { |
|||
primary_text:css('font-size', 'var(--font-size-' .. (args['primary_font_size'] or 'xx-large') .. ')') |
|||
extraclasses = args.extraclasses, |
|||
-- primary text font-family |
|||
selfref = args.selfref |
|||
primary_text:css('font-family', args['font'] or 'var(--font-family-serif)') |
|||
}) |
|||
-- primary text text-transform |
|||
end |
|||
primary_text:css('text-transform', args['primary_transform'] or 'none') |
|||
-- primary text |
|||
primary_text:wikitext(args['primary']) |
|||
-- 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['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('<br/>' .. 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') |
|||
content:tag('div'):wikitext('[[' |
|||
:addClass(p.defaultClasses(inline)) |
|||
.. 'File:' .. args['file2'] |
|||
:addClass(extraclasses) |
|||
.. '|' .. (args['file_size'] or '80px') |
|||
:addClass(options.selfref and 'selfref' or nil) |
|||
.. '|class=notpageimage' |
|||
:wikitext(s) |
|||
.. '|link=' |
|||
.. '|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 |
||
Revision as of 19:06, March 31, 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('noexcerpt')
:addClass('template__hatnote')
:attr('role', 'note')
if args['additional_class'] then
wrapper:addClass(additional_class)
end
-- backwards compatibility
if args['file'] then args['file1'] = args['file'] end
-- input validation
if not args['file1'] and args['file2'] then
error('file2 is required when file1 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('--color-amaranth-legacy-scope', args['color'])
if yesno(args['color_applies_to_links'] or true) then
wrapper:css('--color-link', args['color'])
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')
if args['file1'] and args['file2'] then
content:attr('class', 'hatnote2')
elseif args['file1'] then
content:attr('class', 'hatnote1')
else
content:attr('class', 'hatnote0')
end
-- left file
if args['file1'] then
local file_text = '[['
.. 'File:' .. args['file1']
.. '|' .. (args['file_size'] or '80px')
.. '|link='
.. '|alt='
if yesno(args['file_invert_in_dark_mode'] or false) then file_text = file_text .. '|class=skin-invert-image' end
file_text = file_text .. ']]'
content:tag('div'):wikitext(file_text)
end
-- center text
local text = content:tag('div')
-- primary text wrapper
local primary_text = text:tag('span')
:css('display', 'inline-block')
-- primary text linear-gradient
if args['primary_gradient'] then
primary_text:css('color', 'transparent')
:css('background-image', 'linear-gradient(' .. args['primary_gradient'] .. ')')
:css('-webkit-background-clip', 'text')
:css('background-clip', 'text')
else
primary_text:css('color', 'var(--color-amaranth-legacy-scope)')
end
-- primary text font-size
primary_text:css('font-size', 'var(--font-size-' .. (args['primary_font_size'] or 'xx-large') .. ')')
-- primary text font-family
primary_text:css('font-family', args['font'] or 'var(--font-family-serif)')
-- primary text text-transform
primary_text:css('text-transform', args['primary_transform'] or 'none')
-- primary text
primary_text:wikitext(args['primary'])
-- secondary text
if args['secondary'] then
-- italic secondary text
if yesno(args['italic_secondary'] or true) then
args['secondary'] = '\'\'' .. args['secondary'] .. '\'\''
end
text:wikitext('<br/>' .. args['secondary'])
end
-- right file
if args['file2'] then
content:tag('div'):wikitext('[['
.. 'File:' .. args['file2']
.. '|' .. (args['file_size'] or '80px')
.. '|class=notpageimage'
.. '|link='
.. '|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