Module:Motifs: Difference between revisions
From The Seven Sages of Rome
m Noeth moved page Module:MotifTags to Module:Motifs |
No edit summary |
||
| Line 8: | Line 8: | ||
local smw = mw.smw | local smw = mw.smw | ||
local | local motifs = {} | ||
-- collect motifs + parent info | |||
for motif in mw.text.gsplit(raw, ';', true) do | for motif in mw.text.gsplit(raw, ';', true) do | ||
motif = mw.text.trim(motif) | motif = mw.text.trim(motif) | ||
| Line 17: | Line 18: | ||
'[[' .. motif .. ']]', | '[[' .. motif .. ']]', | ||
'?Is Child Of', | '?Is Child Of', | ||
mainlabel = '-' | mainlabel = '-', | ||
limit = 1 | |||
}) | }) | ||
local parent = | local parent = '' | ||
if data and data[1] and data[1]['Is Child Of'] then | if data and data[1] and data[1]['Is Child Of'] then | ||
parent = data[1]['Is Child Of'] | parent = data[1]['Is Child Of'] | ||
| Line 28: | Line 30: | ||
end | end | ||
table.insert(motifs, { | |||
label = motif, | |||
parent = parent or '' | |||
}) | |||
end | |||
end | |||
-- sort: parent + label (case-insensitive) | |||
table.sort(motifs, function(a, b) | |||
local keyA = mw.ustring.lower(a.parent .. '|' .. a.label) | |||
local keyB = mw.ustring.lower(b.parent .. '|' .. b.label) | |||
return keyA < keyB | |||
end) | |||
-- render | |||
local html = mw.html.create() | |||
for _, item in ipairs(motifs) do | |||
local badge = html | |||
:tag('span') | |||
:addClass('badge badge-pill badge-motif mr-1') | |||
:attr( | |||
'title', | |||
item.parent ~= '' and ('Child of: ' .. item.parent) or 'Top-level motif' | |||
) | |||
badge: | if item.parent ~= '' then | ||
badge:addClass('has-parent') | |||
end | end | ||
badge:wikitext(frame:preprocess( | |||
'{{#formredlink:target=' .. item.label .. '|form=Content}}' | |||
)) | |||
end | end | ||
Revision as of 20:04, 24 April 2026
Documentation for this module may be created at Module:Motifs/doc
local p = {}
function p.renderBadges(frame)
local raw = frame.args[1] or ''
if raw == '' then
return ''
end
local smw = mw.smw
local motifs = {}
-- collect motifs + parent info
for motif in mw.text.gsplit(raw, ';', true) do
motif = mw.text.trim(motif)
if motif ~= '' then
local data = smw.ask({
'[[' .. motif .. ']]',
'?Is Child Of',
mainlabel = '-',
limit = 1
})
local parent = ''
if data and data[1] and data[1]['Is Child Of'] then
parent = data[1]['Is Child Of']
if type(parent) == 'table' then
parent = parent[1]
end
end
table.insert(motifs, {
label = motif,
parent = parent or ''
})
end
end
-- sort: parent + label (case-insensitive)
table.sort(motifs, function(a, b)
local keyA = mw.ustring.lower(a.parent .. '|' .. a.label)
local keyB = mw.ustring.lower(b.parent .. '|' .. b.label)
return keyA < keyB
end)
-- render
local html = mw.html.create()
for _, item in ipairs(motifs) do
local badge = html
:tag('span')
:addClass('badge badge-pill badge-motif mr-1')
:attr(
'title',
item.parent ~= '' and ('Child of: ' .. item.parent) or 'Top-level motif'
)
if item.parent ~= '' then
badge:addClass('has-parent')
end
badge:wikitext(frame:preprocess(
'{{#formredlink:target=' .. item.label .. '|form=Content}}'
))
end
return tostring(html)
end
return p