Module:ListUtils: Difference between revisions

No edit summary
No edit summary
 
(2 intermediate revisions by the same user not shown)
Line 87: Line 87:
                     table.insert(out, '[[' .. catNs .. ':' .. plain .. ']]')
                     table.insert(out, '[[' .. catNs .. ':' .. plain .. ']]')
                 end
                 end
                 -- Property link (optional if property is non-empty)
                 -- Property annotation (hidden label so nothing shows)
                 if property and property ~= '' then
                 if property and property ~= '' then
                     table.insert(out, '[[' .. property .. '::' .. plain .. ']]')
                     table.insert(out, '[[' .. property .. '::' .. plain .. '| ]]')
                 end
                 end
                seen[plain] = true
            end
        end
    end
    return table.concat(out, ' ')
end
-- Internal: props only
local function propsFromList(list, property)
    list = trim(list or '')
    if list == '' or not property or property == '' then
        return ''
    end
    local parts = mw.text.split(list, ',')
    local out = {}
    local seen = {}
    for _, part in ipairs(parts) do
        local s = trim(part)
        if s ~= '' then
            local plain = stripLinks(s)
            plain = mw.ustring.match(plain, '([^|]+)') or plain
            plain = trim(plain)
            if plain ~= '' and not seen[plain] then
                table.insert(out, '[[' .. property .. '::' .. plain .. '| ]]')
                 seen[plain] = true
                 seen[plain] = true
             end
             end
Line 114: Line 142:
     return catsAndPropsFromList(list, property, catNs)
     return catsAndPropsFromList(list, property, catNs)
end
end
-- Public: props-only entry point
-- {{#invoke:ListUtils|propsOnly|A, B, C|HasThing}}
-- {{#invoke:ListUtils|propsOnly|list=A, B, C|property=HasThing}}
function p.propsOnly(frame)
    local list = frame.args.list or frame.args[1] or ''
    local property = frame.args.property or frame.args[2] or ''
    return propsFromList(list, property)
end
return p