--[[
Вспомогательные функции для SummaryII
--]]
local dependencies = require 'Module:SummaryII/dependencies'
local trim, lower = dependencies.trim, dependencies.lc
local match, gmatch, gsub = dependencies.match, dependencies.gmatch, dependencies.gsub
local split, gsplit = dependencies.split, dependencies.gsplit
local strip = dependencies.strip
local concat = table.concat
local set = dependencies.set
local wrapper = dependencies.wrapper
local p = {}
p.strip = wrapper (function (str)
-- Нельзя сразу return: mw.ustring.gsub возвращает два значения:
local ret = gsub (lower (str), '[%s%p_]', '')
return ret
end)
p.strip_number = wrapper (function (str)
-- Нельзя сразу return: mw.ustring.gsub возвращает два значения:
local ret = gsub (lower (str), '[%s%d()%p_]', '')
return ret
end)
p.parse_params = wrapper (function (str, regex, group)
local property = 'Параметр шаблона'
local value_regex = regex and regex ~= '' and regex or '.*'
local list = {
'{| class="wikitable" style="overflow: auto; font-size: 50%;"',
'! префикс !! параметр !! значения !! контекст !! группа !! порядок'
}
for quintuplet in gsplit (str, '%s*\n%s*') do
local name, value, context, section, order = unpack (split (quintuplet, '%s*;%s*'))
name = p.strip_number (name)
local param_regex = 'pcre2/^' .. name .. '\\d*/i_'
local record = name .. ';'
.. param_regex .. ';'
.. trim (value or value_regex) .. ';'
.. trim (context or '*') .. ';'
.. trim (section or group or '') ..';'
.. trim (order or '0')
set {[property] = record}
list[#list + 1] = '|\n<code>' .. name .. '</code>\n|\n<code>'
.. param_regex .. '</code>\n|\n<code>'
.. (value or value_regex) .. '</code>\n|\n<code>'
.. (context or '*') .. '</code>\n|\n<code>'
.. (section or group or '') .. '</code>\n|\n<code>'
.. (order or '0') .. '</code>'
end
return '\n' .. concat (list, '\n|-\n') .. '\n|}'
end)
local function strip_caron_dollar (regex)
return gsub (gsub (regex, '^%^', ''), '%$$', '')
end
-- @TODO: units.
-- @TODO: separators.
-- @TODO: illegal characters.
-- @TODO: [[]].
-- @TODO: type names.
-- @TODO: property values.
p.property_widget = wrapper (function (
prop_name,
prop_type,
prop_regex,
corresponds_to,
template
)
local name = p.strip (prop_name)
local decimal = ','
local value = ''
if prop_regex then
value = strip_caron_dollar (prop_regex)
elseif prop_type == 'Страница' then
value = '.*' -- @TODO: separators. @TODO: illegal chars.
elseif prop_type == 'Число' then
value = '\\d+(' .. decimal .. '\\d+)?'
elseif prop_type == 'Количество' then
value = '\\d+(' .. decimal .. '\\d+)?\\s*'
if corresponds_to then
local units = {}
for _, unit in ipairs (split (corresponds_to, ', ', plain)) do
units[#units + 1] = gsub (gsub (gsub (unit, '[%d,]', ''), '^ ', ''), 'e[+-]', '')
end
value = value .. '%s*(?<unit>' .. concat (units, '|') .. ')?'
end
else -- String.
value = '.*' -- @TODO: separators.
end
return '<<pcre/^' .. name .. '(?<no>\\d*)$/i_ = pcre/^(?<value>' .. value .. ')$/|'
.. '{{' .. template .. '|<<value>>}}|>>'
end)
return p