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

мНет описания правки
мНет описания правки
Строка 76: Строка 76:
     if not s or s == "" then return s end
     if not s or s == "" then return s end
     local lines = mw.text.split(s, "\n")
     local lines = mw.text.split(s, "\n")
    local first_non_empty = nil
    local first_non_empty_unescaped = nil


     local enable_numeric = false
     -- Найти первую непустую строку и её "распакованную" версию (  -> ' ')
     for _, line in ipairs(lines) do
     for i, line in ipairs(lines) do
         if line and line:match("%S") then
         if line and line:match("%S") then
             if mw.ustring.match(line, "^(?: )*%d+%. *") then
             first_non_empty = line
                enable_numeric = true
            first_non_empty_unescaped = mw.ustring.gsub(line, " ", " ")
            end
             break
             break
         end
         end
    end
    local enable_numeric = false
    -- Используем распакованную строку для проверки цифрового списка
    if first_non_empty_unescaped and mw.ustring.match(first_non_empty_unescaped, "^%s*%d+%.") then
        enable_numeric = true
     end
     end


     local out_lines = {}
     local out_lines = {}
     for _, line in ipairs(lines) do
     for _, line in ipairs(lines) do
        local processed = line
         local handled = false
         local handled = false
 
        -- Для распознавания отступов/маркеров интерпретируем только последовательность ' ' как пробел
         local leading = mw.ustring.match(line, "^((?: )*)") or ""
         local line_unescaped = mw.ustring.gsub(line, " ", " ")
        local indent_spaces = select(2, leading:gsub(" ", ""))
        local level = math.floor(indent_spaces / 2)


         if enable_numeric then
         if enable_numeric then
             local rest = mw.ustring.match(line, "^(?: )*%d+%. *(.*)$")
             local leading, num, rest = mw.ustring.match(line_unescaped, "^(%s*)(%d+)%.%s*(.*)$")
             if rest then
             if num then
                local indent = mw.ustring.len(leading or "")
                local level = math.floor(indent / 2)
                 local hashes = string.rep('#', 1 + level)
                 local hashes = string.rep('#', 1 + level)
                 table.insert(out_lines, hashes .. (rest ~= "" and (" " .. rest) or ""))
                 processed = hashes .. (rest ~= "" and (" " .. rest) or "")
                table.insert(out_lines, processed)
                 handled = true
                 handled = true
             end
             end
Строка 106: Строка 114:


         if not handled then
         if not handled then
             local rest = mw.ustring.match(line, "^(?: )*%- +(.*)$")
             local leading, rest = mw.ustring.match(line_unescaped, "^(%s*)%-%s+(.*)$")
             if rest then
             if rest then
                local indent = mw.ustring.len(leading or "")
                local level = math.floor(indent / 2)
                 local stars = string.rep('*', 1 + level)
                 local stars = string.rep('*', 1 + level)
                 table.insert(out_lines, stars .. (rest ~= "" and (" " .. rest) or ""))
                 processed = stars .. (rest ~= "" and (" " .. rest) or "")
                table.insert(out_lines, processed)
                 handled = true
                 handled = true
             end
             end
Строка 115: Строка 126:


         if not handled then
         if not handled then
             table.insert(out_lines, line)
             table.insert(out_lines, processed)
         end
         end
     end
     end