• Welcome to Touhou Wiki!
  • Registering is temporarily disabled. Check in our Discord server to request an account and for assistance of any kind.

Module:Main: Difference between revisions

From Touhou Wiki
Jump to navigation Jump to search
No edit summary
m (1 revision: updates for most recent version of Scribunto)
 

Latest revision as of 16:10, 8 August 2013

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

-- Port of [[Template:Main]] to Lua
-- by DennouNeko

local common = require("Module:Common")


-- == Helper functions ==

--[[ scan frame.args, looking for unnamed params with links and matching 'l#' with labels ]]
local function get_params(frame)
  local ret = {}
  local idx = {}

  for k,v in frame:argumentPairs() do
    local tmp, label
    -- only for unnamed parameters
    if type(k) == 'number' then idx[#idx+1] = k end
  end

  table.sort(idx)

  for k,v in pairs(idx) do
    tmp = {['link'] = mw.text.trim(frame.args[v])}
    label = frame.args['l' .. v]
    if common.isset(label) then tmp['label'] = label end
    ret[#ret+1] = tmp
  end

  -- probably template preview, make sure there's at least one param
  if #ret < 1 then ret[#ret+1] = {['link'] = frame:preprocess('{{FULLPAGENAME}}'), ['label'] = frame:preprocess('{{PAGENAME}}')} end
  return ret
end


-- == Exported functions ==

--[[ Output generated links ]]
local function buildMain(frame)
  local par = get_params(frame)
  local ret = {}

  ret[#ret+1] = '<div class="rellink">'

  ret[#ret+1] = common.cv(#par < 2, 'Main article', 'Main articles')

  ret[#ret+1] = ': '

  for k,v in pairs(par) do
    if k > 1 then ret[#ret+1] = common.cv(k < #par, ', ', ' and ') end
    ret[#ret+1] = '[['
    ret[#ret+1] = v['link']
    if common.isset(v['label']) then ret[#ret+1] = '|' .. v['label'] end
    ret[#ret+1] = ']]'
  end

  ret[#ret+1] = '</div>'

  return table.concat(ret)
end


-- == Export functions ==
return {
  ['buildMain'] = buildMain,
  ['buildMainTemplate'] = function(frame) return buildMain(frame:getParent()) end,
}

-- [[Category:Lua Scripts|{{PAGENAME}}]]