Loncat ke isi

Modul:descendants/tree

Deri Wikikamus

Dokumentasi untuk modul ini dapat dibuat di Modul:descendants/tree/doc

local p = {}
local descendants = require('Modul:descendants')
local langModule = require('Modul:lang')

-- Get section for a language
local function getLangSection(content, langCode)
    local pattern = "==%s*{{lang|" .. langCode .. "}}%s*==([%s%S]-)(\n==)"
    local section, _ = mw.ustring.match(content, pattern)
    if not section then
        section = mw.ustring.match(content, "==%s*{{lang|" .. langCode .. "}}%s*==([%s%S]+)$")
    end
    return section or ""
end

-- Extract descendant lines (lines containing "descendant")
local function extractDescendants(section)
    local lines = {}
    for line in mw.ustring.gmatch(section, "[^\n]+") do
        if mw.ustring.find(line, "descendant") then
            table.insert(lines, line)
        end
    end
    return lines
end

-- Adjust bullet nesting
local function adjustBullets(lines, extraLevel)
    extraLevel = tonumber(extraLevel) or 0
    local result = {}
    for _, line in ipairs(lines) do
        local starCount = mw.ustring.match(line, "^(%*+)")
        if not starCount then starCount = "" end
        local newLine = string.rep("*", #starCount + extraLevel) .. mw.ustring.sub(line, #starCount + 1)
        table.insert(result, newLine)
    end
    return result
end

-- Main desctree function
function p.desctree(frame)
    local args = frame.args or {}
    local langCode = args[1] or ""
    local pageName = args[2] or ""
    local bor = args.bor or 0

    local title = mw.title.new(pageName)
    if not title or not title.exists then return "" end

    local content = title:getContent()
    if not content then return "" end

    local section = getLangSection(content, langCode)
    if section == "" then return "" end

    local lines = extractDescendants(section)
    if #lines == 0 then return "" end

    local output = adjustBullets(lines, bor)

    return table.concat(output, "\n")
end

return p