Jump to content

This site is currently in alpha, so you will run into rough edges.

Please send feedback or ideas to connect@deeptech.wiki

Module:ListUtils: Difference between revisions

From The Deep Tech Wiki
Created page with "-- Module:ListUtils local p = {} -- Helper: trim whitespace local function trim(s) if not s then return '' end return (mw.ustring.gsub(s, '^%s*(.-)%s*$', '%1')) end -- Helper: is already a wikilink like Foo or Bar local function isLinked(s) return mw.ustring.match(s, '%[%[') ~= nil end -- Helper: strip existing ... to normalize before relinking local function stripLinks(s) s = mw.ustring.gsub(s, '%[%[', '') s = mw.ustring.gsub(s, '%]%]', '') retu..."
 
No edit summary
Line 54: Line 54:


return table.concat(out, ', ')
return table.concat(out, ', ')
end
-- Public: for "A, B, [[C|X]]" produce:
-- [[Category:A]] [[HasVertical::A]] [[Category:B]] [[HasVertical::B]] [[Category:C]] [[HasVertical::C]]
function p.verticalCatsAndProps(frame)
    local raw = frame.args[1] or ''
    raw = trim(raw)
    if raw == '' then
        return ''
    end
    local parts = mw.text.split(raw, ',')
    local out = {}
    local seen = {}
    for _, part in ipairs(parts) do
        local s = trim(part)
        if s ~= '' then
            -- Normalize to plain page name
            local plain = stripLinks(s)
            -- Handle links like [[Foo|Label]]
            plain = mw.ustring.match(plain, '([^|]+)') or plain
            plain = trim(plain)
            if plain ~= '' and not seen[plain] then
                table.insert(out, '[[Category:' .. plain .. ']]')
                table.insert(out, '[[HasVertical::' .. plain .. ']]')
                seen[plain] = true
            end
        end
    end
    return table.concat(out, ' ')
end
end


return p
return p

Revision as of 07:47, 13 November 2025

Documentation for this module may be created at Module:ListUtils/doc

-- Module:ListUtils
local p = {}

-- Helper: trim whitespace
local function trim(s)
	if not s then return '' end
	return (mw.ustring.gsub(s, '^%s*(.-)%s*$', '%1'))
end

-- Helper: is already a wikilink like [[Foo]] or [[Foo|Bar]]
local function isLinked(s)
	return mw.ustring.match(s, '%[%[') ~= nil
end

-- Helper: strip existing [[...]] to normalize before relinking
local function stripLinks(s)
	s = mw.ustring.gsub(s, '%[%[', '')
	s = mw.ustring.gsub(s, '%]%]', '')
	return s
end

-- Public: turn "A, B , [[C|X]] , D" into "[[A]], [[B]], [[C|X]], [[D]]"
function p.linkifyCommaList(frame)
	local raw = frame.args[1] or ''
	raw = trim(raw)
	if raw == '' then return '' end

	-- Split on commas (allowing messy spacing)
	local parts = mw.text.split(raw, ',')
	local out = {}
	local seen = {}

	for _, part in ipairs(parts) do
		local s = trim(part)
		if s ~= '' then
			-- Keep already-linked entries as-is
			if isLinked(s) then
				if not seen[s] then
					table.insert(out, s)
					seen[s] = true
				end
			else
				-- Normalize any accidental brackets then relink
				local plain = stripLinks(s)
				plain = trim(plain)
				-- Deduplicate by plain text
				if not seen[plain] then
					table.insert(out, '[[' .. plain .. ']]')
					seen[plain] = true
				end
			end
		end
	end

	return table.concat(out, ', ')
end

-- Public: for "A, B, [[C|X]]" produce:
-- [[Category:A]] [[HasVertical::A]] [[Category:B]] [[HasVertical::B]] [[Category:C]] [[HasVertical::C]]
function p.verticalCatsAndProps(frame)
    local raw = frame.args[1] or ''
    raw = trim(raw)
    if raw == '' then
        return ''
    end

    local parts = mw.text.split(raw, ',')
    local out = {}
    local seen = {}

    for _, part in ipairs(parts) do
        local s = trim(part)
        if s ~= '' then
            -- Normalize to plain page name
            local plain = stripLinks(s)
            -- Handle links like [[Foo|Label]]
            plain = mw.ustring.match(plain, '([^|]+)') or plain
            plain = trim(plain)

            if plain ~= '' and not seen[plain] then
                table.insert(out, '[[Category:' .. plain .. ']]')
                table.insert(out, '[[HasVertical::' .. plain .. ']]')
                seen[plain] = true
            end
        end
    end

    return table.concat(out, ' ')
end

return p