Модуль:Loc/Marking: различия между версиями

мНет описания правки
Нет описания правки
 
(не показана 41 промежуточная версия этого же участника)
Строка 53: Строка 53:
     if attr_str == "" then return nil end
     if attr_str == "" then return nil end
     local params = {}
     local params = {}
    local function unquote_val(v)
        if not v then return v end
        v = trim(v)
        v = mw.ustring.gsub(v, "^"(.-)"$", "%1")
        v = mw.ustring.gsub(v, '^"(.-)"$', "%1")
        return v
    end
     for k, v in mw.ustring.gmatch(attr_str, "([%w_%-%:]+)%s*=%s*\"(.-)\"") do
     for k, v in mw.ustring.gmatch(attr_str, "([%w_%-%:]+)%s*=%s*\"(.-)\"") do
         params[mw.ustring.lower(k)] = v
         params[mw.ustring.lower(k)] = unquote_val(v)
     end
     end
     attr_str = mw.ustring.gsub(attr_str, "([%w_%-%:]+)%s*=%s*\"(.-)\"", "")
     attr_str = mw.ustring.gsub(attr_str, "([%w_%-%:]+)%s*=%s*\"(.-)\"", "")
     for k, v in mw.ustring.gmatch(attr_str, "([%w_%-%:]+)%s*=%s*([^%s%]]+)") do
     for k, v in mw.ustring.gmatch(attr_str, "([%w_%-%:]+)%s*=%s*([^%s%]]+)") do
         params[mw.ustring.lower(k)] = v
         params[mw.ustring.lower(k)] = unquote_val(v)
     end
     end
     attr_str = mw.ustring.gsub(attr_str, "([%w_%-%:]+)%s*=%s*([^%s%]]+)", "")
     attr_str = mw.ustring.gsub(attr_str, "([%w_%-%:]+)%s*=%s*([^%s%]]+)", "")
     local unnamed = mw.ustring.match(attr_str, "%s*\"(.-)\"")
 
     local unnamed = mw.ustring.match(attr_str, "%s*"(.-)"") or mw.ustring.match(attr_str, "%s*\"(.-)\"")
     if unnamed then
     if unnamed then
         params._value = unnamed
         params._value = unquote_val(unnamed)
        attr_str = mw.ustring.gsub(attr_str, "%s*"(.-)"", "", 1)
         attr_str = mw.ustring.gsub(attr_str, "%s*\"(.-)\"", "", 1)
         attr_str = mw.ustring.gsub(attr_str, "%s*\"(.-)\"", "", 1)
     end
     end
     if not params._value then
     if not params._value then
         local v = mw.ustring.match(attr_str, "%s*([^%s%]]+)")
         local v = mw.ustring.match(attr_str, "%s*([^%s%]]+)")
         if v then params._value = v end
         if v then params._value = unquote_val(v) end
     end
     end
     return params
     return params
end
end


local function convert_lists(s)
local function convert_lists(s, indent_size)
    indent_size = indent_size or 2
     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 out_lines = {}
    local stack = {}


     local enable_numeric = false
     local function make_prefix_from_stack()
    for _, line in ipairs(lines) do
         if #stack == 0 then return "" end
         if line and line:match("%S") then
        return table.concat(stack, "")
            if mw.ustring.match(line, "^(?: )*%d+%. *") then
                enable_numeric = true
            end
            break
        end
     end
     end
    local out_lines = {}


     for _, line in ipairs(lines) do
     for _, line in ipairs(lines) do
         local handled = false
         local line_unescaped = mw.ustring.gsub(line, " ", " ")
 
         local leading, num, rest_num = mw.ustring.match(line_unescaped, "^(%s*)(%d+)%.%s*(.*)$")
         local leading = mw.ustring.match(line, "^((?: )*)") or ""
         local leading2, rest_dash = mw.ustring.match(line_unescaped, "^(%s*)%-%s+(.*)$")
         local indent_spaces = select(2, leading:gsub(" ", ""))
        local level = math.floor(indent_spaces / 2)


         if enable_numeric then
         if num then
             local rest = mw.ustring.match(line, "^(?: )*%d+%. *(.*)$")
             local indent = mw.ustring.len(leading or "")
             if rest then
            local level = math.floor(indent / indent_size)
                 local hashes = string.rep('#', 1 + level)
            while #stack > level do table.remove(stack) end
                 table.insert(out_lines, hashes .. (rest ~= "" and (" " .. rest) or ""))
             if #stack == level then
                handled = true
                 table.insert(stack, "#")
            else
                 while #stack < level do table.insert(stack, "#") end
                table.insert(stack, "#")
             end
             end
        end
            local prefix = make_prefix_from_stack()
 
            local text = rest_num or ""
         if not handled then
            table.insert(out_lines, (prefix ~= "" and (prefix .. (text ~= "" and (" " .. text) or "")) or text))
             local rest = mw.ustring.match(line, "^(?:&#32;)*%-&#32;+(.*)$")
         elseif rest_dash then
             if rest then
             local indent = mw.ustring.len(leading2 or "")
                 local stars = string.rep('*', 1 + level)
            local level = math.floor(indent / indent_size)
                 table.insert(out_lines, stars .. (rest ~= "" and (" " .. rest) or ""))
            while #stack > level do table.remove(stack) end
                handled = true
             if #stack == level then
                 table.insert(stack, "*")
            else
                 while #stack < level do table.insert(stack, "*") end
                table.insert(stack, "*")
             end
             end
        end
            local prefix = make_prefix_from_stack()
 
            local text = rest_dash or ""
         if not handled then
            table.insert(out_lines, (prefix ~= "" and (prefix .. (text ~= "" and (" " .. text) or "")) or text))
         else
            stack = {}
             table.insert(out_lines, line)
             table.insert(out_lines, line)
         end
         end
Строка 134: Строка 153:
             if tip == "" and params._value and visible ~= params._value then tip = params._value end
             if tip == "" and params._value and visible ~= params._value then tip = params._value end
         end
         end
        visible = mw.ustring.gsub(visible, "|", "&#124;")
         return "{{altTooltip|1=" .. (visible or "") .. "|2=<span style='white-space:pre-wrap;'>{{#invoke:Loc|GetRawString|noFormat=1|" .. (tip or "") .. "}}</span>}}"
        tip = mw.ustring.gsub(tip, "|", "&#124;")
         return "{{tooltip|" .. (visible or "") .. "||" .. (tip or "") .. "}}"
     end)
     end)
     while true do
     while true do
Строка 168: Строка 185:
             :gsub('=', '&#61;')
             :gsub('=', '&#61;')
             :gsub(' ', '&#32;')
             :gsub(' ', '&#32;')
            :gsub('{"', '')
            :gsub('"}', '')
             :gsub('{', '&#123;')
             :gsub('{', '&#123;')
             :gsub('"', '&#34;')
             :gsub('"', '&#34;')
Строка 178: Строка 197:
     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 ""
    local noFormat = args.noFormat
     text = mw.text.unstripNoWiki(text)
     text = mw.text.unstripNoWiki(text)
     text = process_nowiki_equals(text)
     text = process_nowiki_equals(text)
     return frame:preprocess(transform(text))
 
    local class = noFormat and "ts-loc-format" or ""
    local content = transform(text)
    if mw.ustring.find(content, '^[ \t]*[#%*;%:]+') then
        content = '\n' .. content
    end
 
     return frame:preprocess(
        frame:extensionTag(
            'templatestyles',
            '',
            { src = 'Модуль:Loc/styles.css' }
        ) ..
        '<span class="list-reset-margin ' .. class .. '">' .. content .. '</span>'
    )
end
end


return p
return p