Narutopedia
Register
Advertisement

Documentation for this module may be created at Module:HF/doc

-- HF stands for High Frequency.
-- This Module augments the built-in HF
local HF = mw.InfoboxBuilderHF
----------------------------
-- Libraries of functions --
----------------------------
-----------------------
-- Libraries of data --
-----------------------
------------------------------------------------
-- Local functions (used only in this Module) --
------------------------------------------------
----------------------------------------------------------
-- Public functions (called from a Template or article) --
----------------------------------------------------------
-- eliminates the link portion wikitext of a File: call, leaving only the bare filename.
function HF.stripFileWrapper(frame)
  local args = getArgs(frame)
  return HF._stripFileWrapper(args[1])
end

---------------------------------------------------------
-- Internal functions (used in this and other Modules) --
---------------------------------------------------------
-- eliminates whitespace from the front and back of a string
function HF.trim(s)
  if type(s) == 'string' then
    return (s:gsub("^%s*(.-)%s*$", '%1'))
  else
    return false
  end
end

-- This creates an external link.
function HF.ExternalLink( target, label, plain )
  local output = string.format('[%s %s]', target, label)

  if plain == true then
    output = string.format('<span class="plainlinks">%s</span>', output)
  end

  return output
end

-- This creates a link to a category, as well as placing it in that category.
-- `sortkey` and `label` are optional
-- If there's no `label` given, it will only place it in the category,
-- which is what HF.Category is for.
function HF.CategoryLink( category, sortkey, label )
  if not HF.isempty( label ) then
    return HF.LinkToCategory( category, label ) ..
    HF.Category( category, sortkey )
  else
    return HF.Category( category, sortkey )
  end
end

-- Adds a Category
-- `sortkey` is optional
function HF.Category( category, sortkey )
  if sortkey == nil then sortkey = '' else sortkey = '|' .. sortkey end
  return string.format('%s'..'Category:%s%s]]', '[[', category, sortkey)
end

-- Adds a link to a Category
function HF.LinkToCategory( category, label )
  return string.format('%s'..'Category:%s|%s]]', '[[:', category,
  label or 'Category:' .. category )
end

-- Adds an internal link
-- `label` is optional
function HF.Link( link, text )
  if not HF.isempty( text ) then
    return string.format('%s'..'%s|%s]]', '[[', link, text)
  else
    return string.format('%s'..'%s]]', '[[', link)
  end
end

-- eliminates the link portion wikitext of a File: call, leaving only the bare filename.
function HF._stripFileWrapper (s)
  if type(s) == 'string' then
    return (s
    :gsub("%[*(.*)", '%1') -- leading brackets
    :gsub("([%a]*)|+.*", '%1') -- arguments
    :gsub("([%a]*)%]*", '%1') -- closing brackets
    )
  else
    return false
  end
end

-- Unique table items
function HF.unique( raw_table )
  local hash = {}
  local unique_results = {}

  for _,v in ipairs( raw_table ) do
    if (not hash[v] and v ~= '') then
      table.insert( unique_results, mw.text.trim(v) )
      hash[v] = true
    end
  end

  return unique_results
end

-- Dequalifies page titles (see {{#explode:{{PAGENAME}}|(}} )
function HF.dequalify ( pagetitle )
    if type(pagetitle) == 'string' then
        return pagetitle:match("[^(]*")
    elseif type(pagetitle) == 'table' then
        return pagetitle[1]:match("[^(]*")
    else
        return nil
    end
end

-- A class intended to serve as a set to quickly test whether an element belongs to a list or set
local Set = {} -- the table representing the class, which will double as the metatable for the instances
Set.__index = Set -- failed table lookups on the instances should fallback to the class table, to get methods

function Set:new(init, o)
    local obj = o or {}
    setmetatable(obj, self)

    obj.value = init
    obj.prop_set = {}

    for _, val in pairs(init) do
			obj.prop_set[val] = true
    end

    return obj
end

function Set:is_in(key)
    return self.prop_set[key] ~= nil
end

HF.Set = Set
-------------------------------------------------
-- Output (send it back to whatever called it) --
-------------------------------------------------
return HF
Advertisement