Module:Main

The printable version is no longer supported and may have rendering errors. Please update your browser bookmarks and please use the default browser print function instead.

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}}]]