Module:Motifs: Difference between revisions

From The Seven Sages of Rome
m Noeth moved page Module:MotifTags to Module:Motifs
No edit summary
 
(2 intermediate revisions by the same user not shown)
Line 7: Line 7:
     end
     end


     local smw = mw.smw
     local motifs = {}
    local html = mw.html.create()


     for motif in mw.text.gsplit(raw, ';', true) do
     for motif in mw.text.gsplit(raw, ';', true) do
Line 14: Line 13:


         if motif ~= '' then
         if motif ~= '' then
             local data = smw.ask({
             table.insert(motifs, motif)
                '[[' .. motif .. ']]',
        end
                '?Is Child Of',
    end
                mainlabel = '-'
            })


            local parent = nil
    table.sort(motifs, function(a, b)
            if data and data[1] and data[1]['Is Child Of'] then
        return mw.ustring.lower(a) < mw.ustring.lower(b)
                parent = data[1]['Is Child Of']
    end)
                if type(parent) == 'table' then
                    parent = parent[1]
                end
            end


            local badge = html
    local html = mw.html.create()
                :tag('span')
                :addClass('badge badge-pill badge-motif mr-1')
                :attr('title', parent and ('Child of: ' .. parent) or 'Top-level motif')


            if parent then
    for _, motif in ipairs(motifs) do
                badge
        html
                    :tag('span')
            :tag('span')
                    :addClass('motif-parent')
            :addClass('badge badge-pill badge-motif mr-1')
                    :wikitext(parent .. ' › ')
             :wikitext(frame:preprocess(
                    :done()
            end
 
             badge:wikitext(frame:preprocess(
                 '{{#formredlink:target=' .. motif .. '|form=Content}}'
                 '{{#formredlink:target=' .. motif .. '|form=Content}}'
             ))
             ))
        end
     end
     end



Latest revision as of 20:09, 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 motifs = {}

    for motif in mw.text.gsplit(raw, ';', true) do
        motif = mw.text.trim(motif)

        if motif ~= '' then
            table.insert(motifs, motif)
        end
    end

    table.sort(motifs, function(a, b)
        return mw.ustring.lower(a) < mw.ustring.lower(b)
    end)

    local html = mw.html.create()

    for _, motif in ipairs(motifs) do
        html
            :tag('span')
            :addClass('badge badge-pill badge-motif mr-1')
            :wikitext(frame:preprocess(
                '{{#formredlink:target=' .. motif .. '|form=Content}}'
            ))
    end

    return tostring(html)
end

return p