Модуль:Loc/Marking: различия между версиями

мНет описания правки
мНет описания правки
Строка 74: Строка 74:


local function convert_lists(s)
local function convert_lists(s)
     local lines = mw.text.split(s or "", "\n", true)
    if not s or s == "" then return s end
    local out = {}
     local lines = mw.text.split(s, "\n")


    local enable_numeric = false
     for _, line in ipairs(lines) do
     for _, line in ipairs(lines) do
         local indentEsc = mw.ustring.match(line, "^((  )*)") or ""
         if line and line:match("%S") then
         local level = 0
            if mw.ustring.match(line, "^(?: )*%d+%. *") then
                enable_numeric = true
            end
            break
         end
    end
 
    local out_lines = {}


         if indentEsc ~= "" then
    for _, line in ipairs(lines) do
            level = mw.ustring.len(indentEsc) / mw.ustring.len("  ")
         local handled = false
 
        local leading = mw.ustring.match(line, "^((?: )*)") or ""
        local indent_spaces = select(2, leading:gsub(" ", ""))
        local level = math.floor(indent_spaces / 2)
 
        if enable_numeric then
            local rest = mw.ustring.match(line, "^(?: )*%d+%. *(.*)$")
            if rest then
                local hashes = string.rep('#', 1 + level)
                table.insert(out_lines, hashes .. (rest ~= "" and (" " .. rest) or ""))
                handled = true
            end
         end
         end


         local content = mw.ustring.sub(line, mw.ustring.len(indentEsc) + 1)
         if not handled then
            local rest = mw.ustring.match(line, "^(?: )*%- +(.*)$")
            if rest then
                local stars = string.rep('*', 1 + level)
                table.insert(out_lines, stars .. (rest ~= "" and (" " .. rest) or ""))
                handled = true
            end
        end


        local numMatch = mw.ustring.match(content, "^%d+%.%s*(.*)")
         if not handled then
         if numMatch then
             table.insert(out_lines, line)
             table.insert(out, string.rep("#", level + 1) .. " " .. numMatch)
        else
            local dashMatch = mw.ustring.match(content, "^%-%s*(.*)")
            if dashMatch then
                table.insert(out, string.rep("*", level + 1) .. " " .. dashMatch)
            else
                table.insert(out, line)
            end
         end
         end
     end
     end


     return table.concat(out, "\n")
     return table.concat(out_lines, "\n")
end
end