Для документации этого модуля может быть создана страница Модуль:Util/doc

--- Functions ---

-- UTF-8 Reference:
-- 0xxxxxxx - 1 byte UTF-8 codepoint (ASCII character)
-- 110yyyxx - First byte of a 2 byte UTF-8 codepoint
-- 1110yyyy - First byte of a 3 byte UTF-8 codepoint
-- 11110zzz - First byte of a 4 byte UTF-8 codepoint
-- 10xxxxxx - Inner byte of a multi-byte UTF-8 codepoint
 
local function chsize(char)
    if not char then
        return 0
    elseif char > 240 then
        return 4
    elseif char > 225 then
        return 3
    elseif char > 192 then
        return 2
    else
        return 1
    end
end
 
-- This function can return a substring of a UTF-8 string, properly handling
-- UTF-8 codepoints.  Rather than taking a start index and optionally an end
-- index, it takes the string, the starting character, and the number of
-- characters to select from the string.
 
local function utf8sub(str, startChar, numChars)
  local startIndex = 1
  while startChar > 1 do
      local char = string.byte(str, startIndex)
      startIndex = startIndex + chsize(char)
      startChar = startChar - 1
  end
 
  local currentIndex = startIndex
 
  while numChars > 0 and currentIndex <= #str do
    local char = string.byte(str, currentIndex)
    currentIndex = currentIndex + chsize(char)
    numChars = numChars -1
  end
  return str:sub(startIndex, currentIndex - 1)
end

---
function explode(div,str) -- credit: http://richard.warburton.it
  if (div=='') then return false end
  local pos,arr = 0,{}
  -- for each divider found
  for st,sp in function() return string.find(str,div,pos,true) end do
    table.insert(arr,string.sub(str,pos,st-1)) -- Attach chars left of current divider
    pos = sp + 1 -- Jump past current divider
  end
  table.insert(arr,string.sub(str,pos)) -- Attach chars right of last divider
  return arr
end


function tablelength(T)
  local count = 0
  for _ in pairs(T) do count = count + 1 end
  return count
end









--- Body ---

local p = {}
function p.dialog(frame)
  data = '<table cellspacing="0" cellpadding="0" class="Dialog">'

  frame = (frame:getParent() or frame)

  if (frame.args[1]:gsub("%s*",'') ~= "") then
      
    data = data .. 
    '<caption>' .. (frame.args[1] or 
    "| Заголовок Диалога, пжлст")
    :match( "^%s*(.-)%s*$" ) .. '</caption>'
    
  end

  for i = 2, tablelength(frame.args), 2 do
      
    data = data .. '<tr valign="top"><td><strong>'
    .. (frame.args[i] or "| Говорящий | Фраза, пжлст")
    :match( "^%s*(.-)%s*$" ) .. ':</strong></td><td>'
    .. (frame.args[i+1] or ""):match( "^%s*(.-)%s*$" )
    .. '</td></tr>'
    
  end

  return data .. '</table><br /><br />'
end



function p.links(frame)
    
    linklist = {}
    link = {}
    title = {}
    
    qwert = explode(",", (frame.args[1] or ""))
    
    
    --qwert = mw.text.split("Вивек, eeee", ",")
    
    
    if qwert ~= "" then
        
        data = ""
        
        for i = 1, tablelength(qwert) do
            
            qwert[i] = qwert[i]:gsub("%[", ""):gsub("%]", "")
            linklist = explode("|", qwert[i])
            link[i]  = (linklist[1] or qwert[i]):match("^%s*(.-)%s*$")
            title[i] = (linklist[2] or qwert[i]):match("^%s*(.-)%s*$")
        end
        
        ----
        
       -- if mw.title.new(link).exists == true then
      --      data = '[[' .. qwert[1]
      --      :match("^%s*(.-)%s*$") .. ']]'
    --    else
     --       data = (title[1] or link[1])
         --       :match("^%s*(.-)%s*$")
     --   end
        
        ----
        
        for i = 1, tablelength(link) do
          
            if i > 1 then
                data = data .. ', '
            end
          
            if mw.title.new(link[i]).exists == true then
                data = data .. '[[' .. link[i]
                
                if title[i] ~= "" then
                    data = data .. "|" .. title[i]
                end
                
                data = data .. ']]'
            elseif string.match(link[i], "^https?:%/%/") then
                
                title[i] = ""
                
                title[i] = explode(" ", link[i])[2]
                link[i] = explode(" ", link[i])[1]
                
                data = data .. '[' .. link[i]
                
                if title[i] ~= "" then
                    data = data .. " " .. title[i]
                end
                
                data = data .. ']'
                
            else
                data = data .. (title[i] or link[i]):match("^%s*(.-)%s*$")
            end
        end
      
      data = data:gsub("%[%[%[",'[')
      :gsub("%]%]%]",']')
    end

return data
end



function p.gender(frame)
    
    qwert = (frame.args[1] or "")
    qwert = utf8sub(qwert,1,1):lower()
    
    if (qwert == "f" or qwert == "ж" or qwert == "Ж") then
      data = '♀'
    elseif (qwert == "m" or qwert == "м" or qwert == "М") then
      data = '♂'
    else
      data = qwert
    end
    
    return data
end




function p.dragonscript(frame)
    
    frame = (frame:getParent() or frame)
    
    tag = {}
    
    tag[1]  =  '<' .. (frame.args[3] or "span") 
     .. ' class="Dragonic-font" style="font-size: '
     .. (frame.args[2] or "20px")  
     .. '; line-height: 1.1; word-spacing: .5em;">'
    
    tag[2]  =  '</'.. (frame.args[3] or "span") .. '>'
    
    
    
    string = (frame.args[1] or "")
    
    :match( "^%s*(.-)%s*$" ) -- Trim space ??
    
    :lower() -- text lowercase!
    
    :gsub("\n" ,'<br />') -- prevent paragraph division
    
    
    -- dict
    :gsub('aa','1'):gsub('ah','4')
    :gsub('ei','2'):gsub('ey','9')
    :gsub('ii','3'):gsub('ir','7')
    :gsub('oo','8'):gsub('uu','5')
    :gsub('ur','6'):gsub('_' ,' ')
    
    
    
    
    return tag[1] .. string .. tag[2]
end






function p.cat(frame)
 
    qwert = mw.text.split((frame.args[1] or "")
    
    :gsub("%[%[Category:" , ""):gsub("%[%[category:" , "")
    :gsub("%[%[Категория:", ""):gsub("%[%[категория:", "")
    
    :gsub("%]%]", ""), ",")
    -- explode(",", (frame.args[1] or ""):gsub("%[%[", ""):gsub("%]%]", ""))
 
    if qwert ~= "" then
      data = '[[Category:' .. qwert[1]
      :match( "^%s*(.-)%s*$" ) .. ']]'
 
      for i = 2, tablelength(qwert) do
        data = data .. ', [[Category:' .. qwert[i]
        :match( "^%s*(.-)%s*$" ) .. ']]'
      end
 
     -- data = data:gsub("%[%[%[",'[')
     -- :gsub("%]%]%]",']')
    end
 
return data
end


function p.nobr(frame)
    
    frame = frame:getParent()
    
    --
    tag = {}
    
    tag[1] = '<'  .. (frame.args[2] or "span")
                  .. ' style="white-space: nowrap">'
    tag[2] = '</' .. (frame.args[2] or "span") .. '>'
    
    --
    data = frame.args[1]
    
    :gsub("\n", '<br />')
    :gsub(" ", '&nbsp;')
    :gsub("-", '&#8209;')
    
    
    return tag[1] .. data .. tag[2]
end

return p
Материалы сообщества доступны в соответствии с условиями лицензии CC-BY-SA, если не указано иное.