Модуль:FtlMarking: различия между версиями
Материал из Space Station 14 Вики
Pok (обсуждение | вклад) Нет описания правки |
Pok (обсуждение | вклад) Нет описания правки |
||
| Строка 1: | Строка 1: | ||
local p = {} | local p = {} | ||
local function apply_tag(tag, param, inner) | |||
local t = mw.ustring.lower(tag or "") | |||
if t == "bold" then | |||
return "<b>" .. inner .. "</b>" | |||
elseif t == "italic" then | |||
return "<i>" .. inner .. "</i>" | |||
elseif t == "bolditalic" then | |||
return "<i><b>" .. inner .. "</b></i>" | |||
elseif t == "head" then | |||
local n = tonumber(param) or 1 | |||
if n < 1 or n > 6 then n = 1 end | |||
return "<h" .. n .. ">" .. inner .. "</h" .. n .. ">" | |||
elseif t == "color" then | |||
if not param or param == "" then | |||
return '<span>' .. inner .. '</span>' | |||
end | |||
return '<span style="color:' .. param .. ';">' .. inner .. '</span>' | |||
elseif t == "bullet" then | |||
return "· " .. inner | |||
else | |||
return inner | |||
end | |||
end | |||
local function transform(s) | local function transform(s) | ||
s = s or "" | |||
while true do | |||
local c_s, c_e, c_tag = mw.ustring.find(s, "%[/([%w_]+)%]") | |||
if not c_s then break end | |||
local last_o_s, last_o_e = nil, nil | |||
local search_pos = 1 | |||
while true do | |||
local a, b = mw.ustring.find(s, "%[" .. c_tag .. "=?[^%]]*%]", search_pos) | |||
if not a or a > c_s then break end | |||
last_o_s, last_o_e = a, b | |||
search_pos = b + 1 | |||
end | |||
if not last_o_s then | |||
s = mw.ustring.sub(s, 1, c_s - 1) .. mw.ustring.sub(s, c_e + 1) | |||
else | |||
local inner = mw.ustring.sub(s, last_o_e + 1, c_s - 1) | |||
local _, _, param = mw.ustring.find(s, "%[" .. c_tag .. "=([^%]]+)%]", last_o_s) | |||
local replacement = apply_tag(c_tag, param, inner) | |||
s = mw.ustring.sub(s, 1, last_o_s - 1) .. replacement .. mw.ustring.sub(s, c_e + 1) | |||
end | |||
end | |||
end | end | ||
s = | s = mw.ustring.gsub(s, "%[/?[%w_]+[^%]]*%]", "") | ||
return s | return s | ||
| Строка 38: | Строка 59: | ||
local args = frame.args or {} | local args = frame.args or {} | ||
local text = args[1] or args.text or "" | local text = args[1] or args.text or "" | ||
return transform(text) | |||
end | end | ||
return p | return p | ||
Версия от 07:57, 12 января 2026
Для документации этого модуля может быть создана страница Модуль:FtlMarking/doc
local p = {}
local function apply_tag(tag, param, inner)
local t = mw.ustring.lower(tag or "")
if t == "bold" then
return "<b>" .. inner .. "</b>"
elseif t == "italic" then
return "<i>" .. inner .. "</i>"
elseif t == "bolditalic" then
return "<i><b>" .. inner .. "</b></i>"
elseif t == "head" then
local n = tonumber(param) or 1
if n < 1 or n > 6 then n = 1 end
return "<h" .. n .. ">" .. inner .. "</h" .. n .. ">"
elseif t == "color" then
if not param or param == "" then
return '<span>' .. inner .. '</span>'
end
return '<span style="color:' .. param .. ';">' .. inner .. '</span>'
elseif t == "bullet" then
return "· " .. inner
else
return inner
end
end
local function transform(s)
s = s or ""
while true do
local c_s, c_e, c_tag = mw.ustring.find(s, "%[/([%w_]+)%]")
if not c_s then break end
local last_o_s, last_o_e = nil, nil
local search_pos = 1
while true do
local a, b = mw.ustring.find(s, "%[" .. c_tag .. "=?[^%]]*%]", search_pos)
if not a or a > c_s then break end
last_o_s, last_o_e = a, b
search_pos = b + 1
end
if not last_o_s then
s = mw.ustring.sub(s, 1, c_s - 1) .. mw.ustring.sub(s, c_e + 1)
else
local inner = mw.ustring.sub(s, last_o_e + 1, c_s - 1)
local _, _, param = mw.ustring.find(s, "%[" .. c_tag .. "=([^%]]+)%]", last_o_s)
local replacement = apply_tag(c_tag, param, inner)
s = mw.ustring.sub(s, 1, last_o_s - 1) .. replacement .. mw.ustring.sub(s, c_e + 1)
end
end
s = mw.ustring.gsub(s, "%[/?[%w_]+[^%]]*%]", "")
return s
end
function p.main(frame)
local args = frame.args or {}
local text = args[1] or args.text or ""
return transform(text)
end
return p