Модуль:Loc/Marking: различия между версиями
Pok (обсуждение | вклад) мНет описания правки |
Pok (обсуждение | вклад) мНет описания правки |
||
| Строка 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 | |||
-- Найти первую непустую строку и её "распакованную" версию (  -> ' ') | |||
for | for i, line in ipairs(lines) do | ||
if line and line:match("%S") then | if line and line:match("%S") then | ||
first_non_empty = line | |||
first_non_empty_unescaped = mw.ustring.gsub(line, " ", " ") | |||
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 | local line_unescaped = mw.ustring.gsub(line, " ", " ") | ||
if enable_numeric then | if enable_numeric then | ||
local rest = mw.ustring.match( | local leading, num, rest = mw.ustring.match(line_unescaped, "^(%s*)(%d+)%.%s*(.*)$") | ||
if | 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) | ||
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( | 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) | ||
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, | table.insert(out_lines, processed) | ||
end | end | ||
end | end | ||