FANDOM


-- This Module augments the built-in HF
local HF = mw.InfoboxBuilderHF
local L = require('Module:List')
local language = mw.language.new('en')
 
local data = mw.loadData( 'Module:HF/data' )
local ordinals = data.ordinals
local StoryNames = data.StoryNames
local Categories = data.Categories
 
 
function HF.trim(s)
  if type(s) == "string" then
    return (s:gsub("^%s*(.-)%s*$", "%1"))
  else
    return false
  end
end
 
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
 
function HF.episodeCount( var )
    local episodecount = 0
    local episodesbyname = HF.explode( ";", var )
    for i, episode in ipairs( episodesbyname ) do episodecount = episodecount + 1 end
    return episodecount
end
 
function HF.numtoordinal( var )
    local number = var
    if ordinals[var] ~= nil then
        ordinal = ordinals[var]
     else ordinal = ""
    end
    return ordinal
end
 
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 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.parseStory( var )
    local StoryToParse = var
     if StoryNames[var] ~= nil then
         StoryToParse = StoryNames[var]
      else StoryToParse = ''
     end
    return StoryToParse
end
 
function HF._parseStory(frame)
    return HF.parseStory(frame.args[1])
end
 
function HF.parseCategory( var )
    local CatToParse = var
     if Categories[var] ~= nil then
         CatToParse = Categories[var]
      else CatToParse = ''
     end
    return CatToParse
end
 
function HF.parseIndexedFields( args ) 
  local input = {}
        input.fields   = {}   -- Actual data
        input.vars     = {}   -- "Global" variables
 
  -- Distribute args to vars and fields.
  local indexes = {} -- Table used to sort indexes of fields
  local fields = {}  -- Temporary fields table
  local f = 0        -- Fields consecutive indice
 
  for k, v in pairs(args) do
    local keySplit = HF.explode( ":", tostring(k) )
    local index  = tonumber( HF.trim( keySplit[1] ) )
    local key    = tostring( HF.trim( keySplit[2] ) )
    local value  = tostring( HF.trim( v )           )
    if index > 0 then
      if fields[index] == nil then
        table.insert( indexes, index )
        fields[index] = {}
      end
      fields[index][key] = value
    else
      if not HF.isempty( HF.trim( value ) ) then input.vars[key] = value end
    end
  end
 
  table.sort( indexes )
  for i, index in ipairs(indexes) do
    f = f + 1
    input.fields[f] = {}
    input.fields[f] = fields[index]
  end
  return input
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('[['..'Category:%s%s]]', category, sortkey)
end
 
-- Adds a link to a Category
function HF.LinkToCategory( category, label )
    return string.format('[[:'..'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]]', link, text)
    else
        return string.format('[['..'%s]]', link)
    end
end
return HF

Ad blocker interference detected!


Wikia is a free-to-use site that makes money from advertising. We have a modified experience for viewers using ad blockers

Wikia is not accessible if you’ve made further modifications. Remove the custom ad blocker rule(s) and the page will load as expected.