FANDOM


local HF = {}
HF.getArgs = require('Dev:Arguments').getArgs
HF.L = require('Dev:List')
 
-- String Functions
function HF.lettersequencenumber(str)
  str = str:upper()
  local sct = string.tochartable(str)
  local tot = 0
  for i=1, #sct do
    local q, l = string.byte(sct[i])-64, #sct-i
    tot = tot + q*(26^l)
  end
  return tot
end
 
function HF.lettersequence(number)
  if not number then
    local obj = {_lower = false}
    obj.lower = function(self, bool)
      if bool and bool==false then self._lower = false
      else self._lower = true end
      return self
    end
    obj.__index = function(self, key)
      if rawget(obj, key) then return rawget(obj, key) end
      if type(key)=="number" then
        local x = HF.lettersequence(key)
        if self._lower then return x:lower() end
        return x
      else return HF.lettersequencenumber(key)
      end
    end
    setmetatable(obj, obj)
    return obj
  end
  if number < 1 then return nil end
  number = number - 1
  local nad = {}
  while number > 25 do
    table.insert(nad, 1, (number % 26))
    number = math.floor(number/26)-1
  end
  table.insert(nad, 1, number)
  local o = ""
  for i=1, #nad do o = o .. string.char(nad[i]+65) end
  return o
end
 
-- Date Functions
function HF.formatDate( field ) -- InfoboxBuilder
    return HF.__formatDate( field.Value )
end
function HF._formatDate( frame ) -- invoke
    return HF.__formatDate( frame.args[1] )
end
function HF.__formatDate( var ) -- internal
   return language:formatDate("F j, Y", var)
end
 
-- List Functions
function HF.list( field, vars ) -- InfoboxBuilder
    return HF.__list( field.ListType, field.Value )
end
function HF._list( frame ) -- invoked
    local listtype = frame.args['type'] or frame.args[1]
    local divisible = frame.args
    return HF.__list( listtype, divisible )
end
function HF.__list( listtype, divisible ) -- internal
    local items = HF.explode( ';', divisible )
    if (listtype == 'horizontal') then items.class = 'hwrap plainlinks' end
    return HF.L.makeList( listtype , items )
end
function HF.horizontal( frame ) -- invoked
    return HF.__list('horizontal', frame.args[1])
end
function HF.bulleted( frame ) -- invoked
    return HF.__list('bulleted', frame.args[1])
end
function HF.unbulleted( frame ) -- invoked
    return HF.__list('unbulleted', frame.args[1])
end
function HF.elementlookup( frame ) -- invoked
    local args = HF.getArgs(frame, { trim = true, removeBlanks = true })
    return HF._elementlookup( args )
end
function HF.statuslookup( frame ) -- invoked
    local args = HF.getArgs(frame, { trim = true, removeBlanks = true })
    return HF._statuslookup( args )
end
-- Wikitext parsers
-- mw.uri.encode( s, 'WIKI' ) should be used instead.
function HF.tounderscore(val)
  return string.gsub(val, " ", "_") .. "";
end
 
-- Conditionals
---Returns true if the value is not an empty string.
--@param val The string to be tested.
--@return Boolean: whether val is not an empty string.
function HF.ifnotblank(val)
  return val ~= nil and val ~= "";
end
 
-- Debugging
---Generates an error message. TODO: Change to mw.html.
function HF.err(str)
  return '<strong class="error">ERROR: ' .. str .. '</strong>';
end
 
-- frame
---Convert frame.args to #lengthable table
--@param f - frame.args
--@return table: Standard table form of frame.args.
function HF.reargs(f)
  local n = {}
  for k, v in pairs(f) do n[k] = v end
  return n
end
 
---Turn blank values into nil
--@param f - frame.args
--@return table: frame.args with all empty strings turned into nil
function HF.emptystring(f)
  local n = {}
  for k, v in pairs(f) do
    if mw.text.trim(v) ~= "" then n[k] = v end
  end
  return n
end
function HF.expandTemplate(...)
  --this function doesn't work in console
  local t
  if type(arg[1])~="table" then
    if type(arg[2])~="table" then
      t = { title = table.remove(arg, 1), args = arg }
    else
      t = { title = arg[1], args = arg[2] }
    end
  else
    t = arg[1]
  end
  return mw.getCurrentFrame():expandTemplate(t)      
end
function HF.toromannumeral(num)
  local rn = { i = 1, v = 5, x = 10 , l = 50, c = 100, d = 500, m = 1000 }
  local nD = math.floor(num/500)
  num = num%500
  local nL = math.floor(num/50)
  num = num%50
  local nV = math.floor(num/5)
  num = num%5
 
  local nI = num
 
  local nX = math.floor(nV/2)
  nV = nV%2
 
  local nC = math.floor(nL/2)
  nL = nL%2
 
  local nM = math.floor(nD/2)
  nD = nD%2
 
  local output = ""
 
  output = ("m"):rep(nM)
  if nC == 4 then output = output .. "c" .. (nD==0 and "d" or "m")
  else output = output .. ("d"):rep(nD) .. ("c"):rep(nC)
  end
  if nX == 4 then output = output .. "x" .. (nL==0 and "l" or "c")
  else output = output .. ("l"):rep(nL) .. ("x"):rep(nX)
  end
  if nI == 4 then output = output .. "i" .. (nV==0 and "v" or "x")
  else output = output .. ("v"):rep(nV) .. ("i"):rep(nI)
  end
  return output:upper()
end
function HF.fromromannumeral(str)--designed to be fairly accepting of invalid RN strings
  local rn = { i = 1, v = 5, x = 10 , l = 50, c = 100, d = 500, m = 1000 }
  local char = str:lower():tochartable()
  local rank = {0,0,0,0,0}
  local split = {""}
  local currank, lasrank = 0, 0
  for i=1, #char do
    lasrank = currank
    currank = rn[char[i]]
    if currank <= lasrank then
      split[#split+1] = ""
    end
    split[#split] = split[#split] .. char[i]
  end
  for i=1, #split do
    local x = split[i]:tochartable()
    split[i] = rn[x[#x]]
    for j=#x-1, 1, -1 do
      split[i] = split[i] - rn[x[j]]
    end
  end
  local total = 0
  for i=1, #split do
    total = total + split[i]
  end
  return total
end
 
return HF
I contenuti della comunità sono disponibili sotto la licenza CC-BY-SA a meno che non sia diversamente specificato.