Module:Motifs: Difference between revisions
From The Seven Sages of Rome
No edit summary |
No edit summary |
||
| Line 1: | Line 1: | ||
local p = {} | local p = {} | ||
local function getParent(motif) | |||
local data = mw.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 | |||
return parent or '' | |||
end | |||
function p.renderBadges(frame) | function p.renderBadges(frame) | ||
| Line 7: | Line 27: | ||
end | end | ||
local motifs = {} | local motifs = {} | ||
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) | ||
if motif ~= '' then | if motif ~= '' then | ||
local | local parent = getParent(motif) | ||
table.insert(motifs, { | table.insert(motifs, { | ||
label = motif, | label = motif, | ||
parent = parent | parent = parent | ||
}) | }) | ||
end | end | ||
end | end | ||
table.sort(motifs, function(a, b) | table.sort(motifs, function(a, b) | ||
local keyA = mw.ustring.lower(a.parent .. '|' .. a.label) | local keyA = mw.ustring.lower(a.parent .. '|' .. a.label) | ||
| Line 44: | Line 48: | ||
end) | end) | ||
local html = mw.html.create() | local html = mw.html.create() | ||
| Line 58: | Line 61: | ||
if item.parent ~= '' then | if item.parent ~= '' then | ||
badge:addClass('has-parent') | badge:addClass('has-parent') | ||
badge | |||
:tag('span') | |||
:addClass('motif-parent') | |||
:wikitext(item.parent .. ' › ') | |||
:done() | |||
end | end | ||
Revision as of 20:05, 24 April 2026
Documentation for this module may be created at Module:Motifs/doc
local p = {}
local function getParent(motif)
local data = mw.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
return parent or ''
end
function p.renderBadges(frame)
local raw = frame.args[1] or ''
if raw == '' then
return ''
end
local motifs = {}
for motif in mw.text.gsplit(raw, ';', true) do
motif = mw.text.trim(motif)
if motif ~= '' then
local parent = getParent(motif)
table.insert(motifs, {
label = motif,
parent = parent
})
end
end
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)
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')
badge
:tag('span')
:addClass('motif-parent')
:wikitext(item.parent .. ' › ')
:done()
end
badge:wikitext(frame:preprocess(
'{{#formredlink:target=' .. item.label .. '|form=Content}}'
))
end
return tostring(html)
end
return p