Difference between revisions of "Module:Hatnote group"

From blackwiki
Jump to navigation Jump to search
test>Manabimasu
(Undid revision 969327312 by Manabimasu (talk) reverted vandalism)
m (1 revision imported)
Line 1: Line 1:
local mHatnote = require('Module:Hatnote')
+
<includeonly>{{#invoke:hatnote group|group}}</includeonly><noinclude>{{documentation}}</noinclude>
local p = {}
 
 
 
--Collates key-based table of classes into usable class list
 
function collateClassList (listTable)
 
local list = {}
 
for k, v in pairs(listTable) do
 
if v and type(k) == "string" then table.insert(list, k) end
 
end
 
return table.concat(list, " ")
 
end
 
 
 
--Passes through single argument from parent frame
 
function p.group (frame)
 
return p._group(frame:getParent().args[1], frame:getParent().args.category)
 
end
 
 
 
function p._group (inputText, category)
 
--If there's an error element, pass everything through unchanged for easier
 
--error resolution
 
if string.find(inputText, '<%a- class="error"', 1, true) then return inputText end
 
 
--Heavily reused hatnote data capture pattern
 
local hatnotePattern = '(<div role="note" class="hatnote navigation%-not%-searchable%s?(.-)">(.-)</div>)'
 
 
--Capture hatnote divs and "loose" categories; we'll ignore everything else
 
local rawDivs = {}
 
local looseCategories = ''
 
for x in string.gmatch(inputText, hatnotePattern) do
 
table.insert(rawDivs, x)
 
end
 
for x in string.gmatch(inputText, '%[%[Category:.-%]%]') do
 
looseCategories = looseCategories .. x
 
end
 
 
 
--if no inner hatnotes, return an error
 
if not rawDivs[1] then
 
return mHatnote.makeWikitextError(
 
'no inner hatnotes detected',
 
'Template:Hatnote group',
 
category
 
)
 
end
 
 
 
--Preprocess divs into strings and classes
 
local innerHatnotes = {}
 
for k, v in pairs(rawDivs) do
 
row = {}
 
row.text = string.gsub(v, hatnotePattern, '%3')
 
--Here we set class names as keys for easier intersection later
 
row.classes = {}
 
for m, w in ipairs(
 
mw.text.split(
 
string.gsub(v, hatnotePattern, '%2'),
 
' ',
 
true
 
)
 
) do
 
row.classes[mw.text.trim(w)] = true
 
end
 
 
table.insert(innerHatnotes, row)
 
end
 
 
--Identify any universal classes ("hatnote" ignored by omission earlier)
 
local universalClasses = {}
 
--clone first classes table to force passing by value rather than reference
 
for k, v in pairs(innerHatnotes[1].classes) do universalClasses[k] = v end
 
for k, v in ipairs(innerHatnotes) do
 
for m, w in pairs(universalClasses) do
 
universalClasses[m] = (universalClasses[m] and v.classes[m])
 
end
 
end
 
 
--Remove universal classes from div items, then create class strings per row
 
for k, v in ipairs(innerHatnotes) do
 
for m, w in pairs(v.classes) do
 
if universalClasses[m] then v.classes[m] = nil end
 
end
 
v.classString = collateClassList(v.classes)
 
end
 
 
--Process div items into classed span items
 
local innerSpans = {}
 
for k, v in ipairs(innerHatnotes) do
 
table.insert(
 
innerSpans,
 
(v.classString ~= '') and
 
string.format('<span class="%s">%s</span>', v.classString, v.text) or
 
string.format('<span>%s</span>', v.text)
 
)
 
end
 
 
 
--Concatenate spans and categories, and return wrapped as a single hatnote
 
local outputText = table.concat(innerSpans, " ") .. looseCategories
 
local hnOptions = {extraclasses = collateClassList(universalClasses)}
 
return mHatnote._hatnote(outputText, hnOptions)
 
end
 
 
 
return p
 

Revision as of 16:10, 6 September 2020

Module documentation[view] [edit] [history] [purge]

This experimental template applies Module:Hatnote group to merge hatnotes it is given as input; this has the net effect of making them formatted inline relative to one another. For example:

{{about|USE1||PAGE1}}
{{redirect|REDIRECT|USE2|PAGE2}}

would normally display as:

Script error: No such module "about".

but, when wrapped in this template:

{{hatnote group|
{{about|USE1||PAGE1}}
{{redirect|REDIRECT|USE2|PAGE2}}
}}

displays as

Script error: No such module "hatnote group".

Technically, it changes the hatnotes by converting their div wrappers into spans, wrapping the group in a new hatnote div, and migrating classes appropriately. Classes are migrated to the appropriate span, unless every hatnote in the group includes that class, in which case the class will be migrated to the new div wrapper. For example:

{{hatnote|extraclasses=example demonstration |Hatnote 1}}
{{hatnote|extraclasses=example |Hatnote 2}}
{{hatnote|extraclasses=example |Hatnote 3}}

would normally be rendered to:

<div role="note" class="hatnote navigation-not-searchable example demonstration">Hatnote 1</div>
<div role="note" class="hatnote navigation-not-searchable example">Hatnote 2</div>
<div role="note" class="hatnote navigation-not-searchable example">Hatnote 3</div>

but, when wrapped in this template, becomes:

<div role="note" class="hatnote navigation-not-searchable example">
<span class="demonstration">Hatnote 1</span> 
<span>Hatnote 2</span> 
<span>Hatnote 3</span> 
</div>