مندرجات کا رخ کریں

ماڈیول:pages

ویکی لغت سے

"اس ماڈیول کی دستاویز ماڈیول:pages/دستاویز پر بنائی جاسکتی ہے"

local require = require
local require_when_needed = require("ماڈیول:require when needed")

local decode_entities = require_when_needed("ماڈیول:string utilities", "decode_entities")
local find = string.find
local format = string.format
local get_current_section -- معرفة القسم الحالي
local gsub = string.gsub
local is_valid_title -- التحقق من صحة العنوان
local lower = string.lower
local match = string.match
local new_title = mw.title.new
local sub = string.sub
local tonumber = tonumber
local trim = require_when_needed("ماڈیول:string utilities", "trim")
local type = type
local unstrip_nowiki = mw.text.unstripNoWiki

local export = {}

--[==[
إرجاع true إذا كان العنوان صحيحًا وليس رابطًا بين الويكيات]==]
function export.is_valid_title(title)
    return title and #title.prefixedText > 0 and #title.interwiki == 0
end
is_valid_title = export.is_valid_title

--[==[
إرجاع true إذا كان اسم الصفحة صالحًا وليس رابطًا بين الويكيات]==]
function export.is_valid_page_name(pagename)
    return is_valid_title(new_title(pagename))
end

do
    local function is_sandbox(text)
        return (find(lower(text), "sandbox", 1, true) or sub(text, 1, 5) == "صارف:") and true or false
    end
    
    local function is_documentation(text)
        return match(text, "./documentation$") and true or false
    end
    
    local function is_testcase_page(text)
        return match(text, "./[Tt]estcases?%f[%L]") and true or false
    end
    
    --[==[
    إرجاع نوع الصفحة مثل "وحدة" أو "ملعب وحدة"]==]
    function export.pagetype(title)
        if not is_valid_title(title) then
            error(mw.dumpObject(title.fullText) .. " ليس اسم صفحة صحيح.")
        end
        local content_model = title.contentModel
        if content_model == "css" or content_model == "sanitized-css" then
            return "stylesheet"
        elseif content_model == "javascript" then
            return "script"
        elseif content_model == "json" then
            return "صفحة JSON"
        elseif content_model == "MassMessageListContent" then
            return "قائمة توصيل الرسائل الجماعية"
        elseif content_model == "Scribunto" then
            local title_text = title.text
            if is_sandbox(title_text) then
                return "ملعب وحدة"
            elseif is_testcase_page(title_text) then
                return "ملعب وحدة"
            end
            return "ماڈیول"
        elseif content_model == "text" then
            return "صفحہ"
        elseif title.isTalkPage then
            return "نقاش الصفحة"
        end
        local ns = title.namespace
        if ns == 0 then
            return "صفحہ"
        elseif ns == 4 then
            return "صفحة مشروع"
        elseif ns == 8 or ns == 710 then
            return title.nsText .. " صفحہ"
        elseif ns == 10 then
            local title_text = title.text
            if is_sandbox(title_text) then
                return "ملعب قالب"
            elseif is_documentation(title_text) then
                return "توثيق قالب"
            elseif is_testcase_page(title_text) then
                return "ملعب قالب"
            end
            return "template"
        elseif ns == 828 then
            local title_text = title.text
            if is_sandbox(title_text) then
                return "ملعب وحدة"
            elseif is_documentation(title_text) then
                return "توثيق وحدة"
            end
        end
        local ns_text = lower(title.nsText)
        if ns == 14 or ns == 100 then
            return ns_text
        elseif ns == 110 or ns == 118 then
            return ns_text .. " entry"
        end
        return gsub(ns_text, "_", " ") .. " صفحة"
    end
end

do
    local function check_level(lvl)
        if type(lvl) ~= "number" then
            error("مستويات العناوين يجب أن تكون أرقامًا.")
        elseif lvl < 1 or lvl > 6 or lvl % 1 ~= 0 then
            error("مستويات العناوين يجب أن تكون أعدادًا صحيحة بين 1 و6.")
        end
        return lvl
    end

    local function find_headings(text, a, b)
        a = a and check_level(a) or nil
        b = b and check_level(b) or a or nil
        local start, loc, lvl, sec = 1

        return function()
            repeat
                loc, lvl, sec, start = match(text, "()%f[^%z\n](==?=?=?=?=?)([^\n]+)%2[\t ]*%f[%z\n]()", start)
                lvl = lvl and #lvl
            until not (sec and a) or (lvl >= a and lvl <= b)
            return sec and trim(decode_entities(sec)) or nil, lvl, loc
        end
    end

    local function _get_section(content, name, level)
        if not (content and name) then
            return nil
        elseif find(name, "\n", 1, true) then
            error("اسم العنوان لا يمكن أن يحتوي على سطر جديد.")
        end
        level = level and check_level(level) or nil
        name = trim(decode_entities(name))
        local start
        for sec, lvl, loc in find_headings(content, level and 1 or nil, level) do
            if start and lvl <= level then
                return sub(content, start, loc - 1)
            elseif not start and (not level or lvl == level) and sec == name then
                start, level = loc, lvl
            end
        end
        return start and sub(content, start)
    end

    function export.get_section(content, names, level)
        if type(names) ~= "table" then
            return _get_section(content, names, level)
        end
        local i = 1
        local name = names[i]
        if not name then
            error("يجب تحديد عنوان واحد على الأقل.")
        end
        while true do
            local nxt_i = i + 1
            local nxt = names[nxt_i]
            if nxt == nil then
                return _get_section(content, name, level)
            end
            content = _get_section(content, name)
            if content == nil then
                return nil
            elseif i == 6 then
                error("لا يمكن تحديد أكثر من 6 أقسام: العناوين تصل حتى المستوى 6.")
            end
            i = nxt_i
            name = names[i]
        end
        return content
    end
end

do
    local current_section
    function export.get_current_section()
        if current_section then
            return current_section
        end
        local frame = mw.getCurrentFrame()
        local extension_tag = frame.extensionTag
        local nowiki_marker = extension_tag(frame, "nowiki")
        local h = tonumber(match(
            frame:preprocess("=" .. nowiki_marker .. "="),
            "\127'\"`UNIQ%-%-h%-(%d+)%-%-QINU`\"'\127"
        ))
        if not h then
            return 0
        end
        local n, offset = tonumber(match(
            nowiki_marker,
            "\127'\"`UNIQ%-%-nowiki%-([%dA-F]+)%-QINU`\"'\127"
        ), 16)
        while not offset and n > 0 do
            n = n - 1
            offset = match(
                unstrip_nowiki(format("\127'\"`UNIQ--nowiki-%08X-QINU`\"'\127", n)),
                "HEADING\1(%d+)"
            )
        end
        offset = offset and (offset + 1) or 0
        extension_tag(frame, "nowiki", "HEADING\1" .. offset)
        current_section = h - offset
        return current_section
    end
    get_current_section = export.get_current_section
end

do
    local L2_sections
    function export.get_current_L2()
        local section = get_current_section()
        if section == 0 then
            return
        end
        L2_sections = L2_sections or mw.loadData("Module:headword/data").page.L2_sections
        while section > 0 do
            local L2 = L2_sections[section]
            if L2 then
                return L2
            end
            section = section - 1
        end
    end
end

return export