Модуль:Loc/Marking: различия между версиями
Материал из Space Station 14 Вики
Pok (обсуждение | вклад) Новая страница: «local p = {} local function trim(s) return (s:gsub("^%s*(.-)%s*$", "%1")) end local function transform(s) if not s or s == "" then return "" end local pattern = "%[([%w_]+)(=([^%]]+))?%](.-)%[/%1%]" local changed = true while changed do changed = false s = s:gsub(pattern, function(tag, _, param, inner) changed = true local t = mw.ustring.lower(tag) if t == "bold" then ret...» |
Pok (обсуждение | вклад) мНет описания правки |
||
| Строка 1: | Строка 1: | ||
local p = {} | local p = {} | ||
local function trim(s) return (s:gsub("^%s*(.-)%s*$", "%1")) end | local function trim(s) | ||
return (s:gsub("^%s*(.-)%s*$", "%1")) | |||
end | |||
local function transform(s) | local function transform(s) | ||
if not s or s == "" then return "" end | if not s or s == "" then return "" end | ||
local pattern = "%[([%w_]+)(=([^%]]+))?%]( | local pattern = "%[([%w_]+)(=([^%]]+))?%]([%s%S]-)%[/%1%]" | ||
local changed = true | local changed = true | ||
| Строка 14: | Строка 16: | ||
changed = true | changed = true | ||
local t = mw.ustring.lower(tag) | local t = mw.ustring.lower(tag) | ||
if t == "bold" then | if t == "bold" then | ||
return "'''" .. inner .. "'''" | return "'''" .. inner .. "'''" | ||
elseif t == "italic" then | elseif t == "italic" then | ||
return "''" .. inner .. "''" | return "''" .. inner .. "''" | ||
elseif t == "head" then | |||
elseif t == "head | |||
local lvl = tonumber(param) or 1 | local lvl = tonumber(param) or 1 | ||
if lvl < 1 then lvl = 1 end | if lvl < 1 then lvl = 1 end | ||
if lvl > 6 then lvl = 6 end | if lvl > 6 then lvl = 6 end | ||
local eq = mw.ustring.rep("=", lvl) | local eq = mw.ustring.rep("=", lvl) | ||
return eq .. " " .. trim(inner) .. " " .. eq .. "\n\n" | |||
return eq .. " " .. | |||
elseif t == "color" then | elseif t == "color" then | ||
return '<span style="color:' .. | return '<span style="color:' .. param .. ';">' .. inner .. '</span>' | ||
else | else | ||
return inner | return inner | ||
| Строка 43: | Строка 41: | ||
function p.main(frame) | function p.main(frame) | ||
local | local text = frame.args.text or frame.args[1] or "" | ||
local out = transform(text) | local out = transform(text) | ||
return out | return frame:preprocess(out) | ||
end | end | ||
return p | return p | ||
Версия от 06:55, 12 января 2026
Для документации этого модуля может быть создана страница Модуль:Loc/Marking/doc
local p = {}
local function trim(s)
return (s:gsub("^%s*(.-)%s*$", "%1"))
end
local function transform(s)
if not s or s == "" then return "" end
local pattern = "%[([%w_]+)(=([^%]]+))?%]([%s%S]-)%[/%1%]"
local changed = true
while changed do
changed = false
s = s:gsub(pattern, function(tag, _, param, inner)
changed = true
local t = mw.ustring.lower(tag)
if t == "bold" then
return "'''" .. inner .. "'''"
elseif t == "italic" then
return "''" .. inner .. "''"
elseif t == "head" then
local lvl = tonumber(param) or 1
if lvl < 1 then lvl = 1 end
if lvl > 6 then lvl = 6 end
local eq = mw.ustring.rep("=", lvl)
return eq .. " " .. trim(inner) .. " " .. eq .. "\n\n"
elseif t == "color" then
return '<span style="color:' .. param .. ';">' .. inner .. '</span>'
else
return inner
end
end)
end
s = s:gsub("%[/?[%w_]+[^%]]*%]", "")
return s
end
function p.main(frame)
local text = frame.args.text or frame.args[1] or ""
local out = transform(text)
return frame:preprocess(out)
end
return p