La documentación para este módulo puede ser creada en Módulo:HF/doc
local p = {}
local getArgs = require('Dev:Arguments').getArgs
-- returns content of the page
function p.lua_getContent(pagename)
local output = ''
if not p.isempty(pagename)
then output = mw.title.new(p.lua_breaklink(pagename,1)):getContent() or ''
end
return output
end
-- returns value of 'field' from 'page_content'
function p.lua_getFieldValue(page_content, field)
local output = ''
if not p.isempty(page_content) and not p.isempty(field)
then
output = string.match(page_content, '|%s-'..field..'%s-=%s-(.-)\n|')
if not p.isempty(output)
then output = p.trim(output)
end
end
return output
end
-- Check if 'text' is part of the 'list' or not
function p.inlist(list, text)
local output = ''
if p.isempty(list) == true or p.isempty(text) == true
then output = false
else
for i, v in ipairs( list ) do
if v == text
then
output = true
break
else output = false
end
end
end
return output
end
-- adds all elements from table2 into table1
function p.lua_concatTables(table1, table2)
local output = {}
if not p.isempty(table1)
then
if type(table1) == "table"
then output = table1
else table.insert(output, table1)
end
end
if not p.isempty(table2) and type(table2) == "table"
then
for i = 1, #table2 do
table.insert(output, table2[i])
end
end
return output
end
--checks if 'page' exists or not
function p.lua_isexists(page)
if not p.isempty(page) and mw.title.new(page).exists
then return true
else return false
end
end
--returns true if text is a wikilink or false otherwise
function p.lua_isLink(link)
local i
local j
local output = false
if not p.isempty(link)
then
i = string.find(link, "[[",1,true)
j = string.find(link, "]]",1,true)
if i ~= nil and j ~= nil
then output = true
end
end
return output
end
--Check if 'link' is a wikilink. If yes, then check if it has | inside. If yes, then return 'part' part of it. If 'link' is not a wikilink return 'link'.
function p.lua_breaklink(link, part)
local i
local j
local k
local output = ''
if not p.isempty(link) and p.lua_isLink(link)
then
i = string.find(link, "[[",1,true)
j = string.find(link, "|",1,true)
k = string.find(link, "]]",1,true)
if j == nil
then output = string.sub(link, i+2, k-1)
elseif j < k
then
if part == 2
then output = string.sub(link, j+1, k-1)
else output = string.sub(link, i+2, j-1)
end
else output = string.sub(link, i+2, k-1)
end
else output = link
end
return output
end
function p.breaklink (frame)
return p.lua_breaklink(frame.args[1], tonumber(frame.args[2]))
end
-- return number of pages in the "category", "pagetype" can be one of '*', 'all', 'pages', 'subcats', or 'files'
function p.lua_PagesInCategory(category, pagetype)
local output = 0
pagetype = pagetype or 'pages'
if not p.isempty(category)
then
category = string.gsub(category,''',"'")
output = mw.site.stats.pagesInCategory( category, pagetype )
end
return output
end
function p.explode( sep, text )
local sep, fields = sep or "::", {}
local pattern = string.format("([^%s]+)", sep)
text:gsub(pattern, function(c) fields[#fields+1] = c end)
return fields
end
function p.trim(s)
if type(s) == "string" then
return (s:gsub("^%s*(.-)%s*$", "%1"))
else
return false
end
end
function p.isempty(s)
local result = false
if type(s) == "nil" then
result = true
elseif type(s) == "string" then
if s == "" then
result = true
end
elseif type(s) == "table" then
if next(s) == nil then
result = true
end
end
return result
end
function p.print_r( t, name, indent )
local cart -- a container
local autoref -- for self references
--[[ counts the number of elements in a table
local function tablecount(t)
local n = 0
for _, _ in pairs(t) do n = n+1 end
return n
end
]]
-- (RiciLake) returns true if the table is empty
local function isemptytable(t) return next(t) == nil end
local function basicSerialize (o)
local so = tostring(o)
if type(o) == "function" then
local info = debug.getinfo(o, "S")
-- info.name is nil because o is not a calling level
if info.what == "C" then
return string.format("%q", so .. ", C function")
else
-- the information is defined through lines
return string.format("%q", so .. ", defined in (" ..
info.linedefined .. "-" .. info.lastlinedefined ..
")" .. info.source)
end
elseif type(o) == "number" or type(o) == "boolean" then
return so
else
return string.format("%q", so)
end
end
local function addtocart (value, name, indent, saved, field)
indent = indent or ""
saved = saved or {}
field = field or name
cart = cart .. indent .. field
if type(value) ~= "table" then
cart = cart .. " = " .. basicSerialize(value) .. ";\n"
else
if saved[value] then
cart = cart .. " = {}; -- " .. saved[value]
.. " (self reference)\n"
autoref = autoref .. name .. " = " .. saved[value] .. ";\n"
else
saved[value] = name
--if tablecount(value) == 0 then
if isemptytable(value) then
cart = cart .. " = {};\n"
else
cart = cart .. " = {\n"
for k, v in pairs(value) do
k = basicSerialize(k)
local fname = string.format("%s[%s]", name, k)
field = string.format("[%s]", k)
-- three spaces between levels
addtocart(v, fname, indent .. " ", saved, field)
end
cart = cart .. indent .. "};\n"
end
end
end
end
name = name or "__unnamed__"
if type(t) ~= "table" then
return name .. " = " .. basicSerialize(t)
end
cart, autoref = "", ""
addtocart(t, name, indent)
return cart .. autoref
end
function p.firstToUpper( str )
return (str:gsub("^%l", string.upper))
end
function p.round(num, idp)
local mult = 10^(idp or 0)
return math.floor(num * mult + 0.5) / mult
end
function p.AddZeros( s, len )
local output = ""
local sLength = string.len( tostring( s ) )
local diff = tonumber( len ) - tonumber( sLength )
if diff > 0 then
for i = 1, diff do
output = output .. "0"
end
end
output = output .. s
return output
end
function p.ExternalLink( link, text, plain )
local output = "[" .. link .. " " .. text .. "]"
if plain == true then
output = "<span class=\"plainlinks\">" .. output .. "</span>"
end
return output
end
function p.CategoryLink( category, sort, text )
local output = ""
if not p.isempty( text ) then
output = "[[:Category:" .. category .. "|" .. text .. "]][[Category:" .. category .. "|" .. sort .. "]]"
else
output = "[[Category:" .. category .. "|" .. sort .. "]]"
end
return output
end
function p.Link( link, text )
local output = ''
if not p.isempty(link)
then
if not p.isempty(text)
then text = '|'..text
else text = ''
end
output = '[['..link..text..']]'
end
return output
end
function p.HelpButton( frame )
local args = getArgs( frame )
return p._HelpButton( args )
end
function p._HelpButton( args )
if p.isempty( args.buttonsize ) then args.buttonsize = "10px" end
local target = args.ArticleTarget or "Click here for help with this field"
local link = string.format(
"[[File:Information-silk.png|%s|link=Click here for help with this field#%s]] %s",
args.buttonsize,
args.Section or args.Label or '',
args.Label or ''
)
return link
end
--------------------------------------------------------------------------------------------------
function p.lua_link_to_category(category, text)
local output = ''
if not p.isempty(category)
then
if not p.isempty(text)
then text = '|'..text
else text = '|'..category
end
output = '[[:Category:'..category..text..']]'
end
return output
end
--------------------------------------------------------------------------------------------------
function p.lua_add_category(category, sortname)
local output = ''
if not p.isempty(category)
then
if not p.isempty(sortname)
then sortname = '|'..sortname
else sortname = ''
end
output = '[[Category:'..category..sortname..']]'
end
return output
end
--------------------------------------------------------------------------------------------------
function p.lua_create_list_of_categories(categories, sortname)
local i
local output = {}
if not p.isempty(categories)
then
for i = 1,#categories do
if not p.isempty(categories[i])
then table.insert( output, p.lua_add_category(categories[i], sortname) )
end
end
end
return table.concat(output)
end
return p