Modul:Parse
Vzhled
local p = {}
local CHAR, SET, REPLACE = 0x1, 0x2, 0x4
-- https://www.mediawiki.org/wiki/Extension:Scribunto/Lua_reference_manual#Character_class
local escapeChars = {
['^'] = CHAR + SET,
['$'] = CHAR,
['('] = CHAR,
[')'] = CHAR,
['%'] = CHAR + SET + REPLACE,
['.'] = CHAR,
['['] = CHAR + SET,
[']'] = CHAR + SET,
['*'] = CHAR,
['+'] = CHAR,
['-'] = CHAR + SET,
['?'] = CHAR,
}
local function escape(c, context)
local bit32 = require( 'bit32' )
local mask = escapeChars[c] or 0x0
if bit32.btest(mask, context) then
return '%' .. c
else
return c
end
end
function p.splitAndMap(str, data, separator, unified_param, marker)
if not marker or marker == '' then
marker = ','
elseif mw.ustring.len(marker) > 1 then
error('marker must be a single character')
end
local string_to_parse = mw.text.trim(str)
string_to_parse = mw.ustring.gsub(
string_to_parse,
'(' .. string.char(0x7F) .. '.-' .. string.char(0x7F) .. ')',
escape(marker, REPLACE) .. '%1' .. escape(marker, REPLACE))
string_to_parse = mw.ustring.gsub(
string_to_parse,
escape(marker, CHAR) .. '[' .. escape(marker, SET) .. ' ]*',
escape(marker, REPLACE))
local parsed_table = mw.text.split(string_to_parse, marker, true)
for pos, arg in pairs(parsed_table) do
if unified_param == nil then
if data[arg] ~= nil then
parsed_table[pos] = data[arg]
else
parsed_table[pos] = arg
end
else
if string.match(arg, string.char(0x7F)) == nil then
arg = string.gsub(unified_param, '%(hodnota%)', arg)
end
parsed_table[pos] = arg
end
end
return table.concat(parsed_table, separator)
end
function p.horizontalList(context)
return p.splitAndMap(context.args[1], context.args, '', context.args[2])
end
function p.countriesWithFlags(context)
local data = {}
local __index = function (t, key)
if mw.ustring.find(key, '[%[%]|<>]') then
return nil
end
local value = context:expandTemplate{ title = 'Vlajka a název', args = { key } }
t[key] = value
return value
end
setmetatable(data, { __index = __index })
return p.splitAndMap(context.args[1], data, '<br>')
end
function p.regions(context)
local data = {}
local __index = function (t, key)
if mw.ustring.find(key, '[%[%]|<>]') then
return nil
end
local value = '[[okres ' .. key .. '|' .. key .. ']]'
t[key] = value
return value
end
setmetatable(data, { __index = __index })
return p.splitAndMap(context.args[1], data, ', ')
end
function p.ghsStatements(context)
local codes = mw.text.trim(context.args[1])
local marker = '+'
local statements = mw.text.split(codes, marker, true)
local known = context.args
for _, statement in pairs(statements) do
if known[statement] == nil then
return '">' .. context:expandTemplate{title = 'Chyba', args = {'Neznámá věta'}} .. ' <span title="'
end
end
return p.splitAndMap(codes, known, ' ', context.args[2], marker)
end
return p