Module:PageTabber
Overview
PageTabber is a Lua module that functions like Tabber but links to separate pages instead of rendering all content on the same page, e.g.:
- This Module Page
- Documentation
- Hellion
- Undefined Page
- Hellion, but in 3D
(Page content goes here.)
Produced by the code like the following:
{{#invoke:PageTabber|render |Module:PageTabber|This Module Page |- |Module:PageTabber/doc|Documentation |- |Hellion ... }}
See the source code to see how the tabs are generated in each example.
Usage
The module only has a single endpoint, render
. Tabs are specified using a syntax similar to normal wikilinks or tables:
- A tab consists of a
link
, optionally followed by alabel
and someoptions
, for example|link|label|options
. - Tabs are separated by a single
-
character, for example|-
. - Extraneous arguments and leading or trailing white space will be ignored.
Base Pages and Sub-pages
An optional base
parameter may be provided, in which case all pages will be assumed to be sub-pages of a given base page. E.g., the following code:
{{#invoke:PageTabber|render |base=Hellion (Darkest Dungeon II) ... }}
produces the following output:
A blank link argument as in the example above will generate a link to the base page itself.
Highlighting a Tab
Normally the tab corresponding to the current page is highlighted; however, sometimes it is useful to override this behaviour by manually specifying the link to be highlighted using the optional argument highlight
. This is useful when building multiple stacked layers of tabs: in the example below the current page is Pets
, but its containing category, Stagecoach Items
, is also highlighted:
{{#invoke:PageTabber|render |highlight=Stagecoach Items ... }} {{#invoke:PageTabber|render |highlight=Pets ... }}
Output:
Options
Certain flags or options may be added as the third argument of any tab. Currently there is only one option available.
optional
A tab may be marked as optional
, specifying that it should not be rendered if it links to a non-existent page. This is useful when making pages for upcoming patch changes, since the page may be deleted after the patch is released.
Without optional
:
- Hellion (Darkest Dungeon II)
- Undefined Page
(Page content goes here.)
With optional
:
(Page content goes here.)
Miscellaneous
Note that as with all Lua modules, this module should not be called directly from content pages; rather you should use a template wrapper or create one if it does not exist.
local getArgs = require('Module:Arguments').getArgs
local TableParser = require('Module:TableParser')
local rowFmt = '<ul class="page-tabs" role="tablist">%s</ul>'
local linkFmt = '[[%s|%s]]'
local tabFmt = '<li class="%s" role="tab">%s</li>'
local tabClass = "page-tab"
local highlightClass = "page-tab-highlight"
local function _render(args, highlight)
local base = args.base
local tabs = TableParser.parse(args, function (idx, tab_args)
if #tab_args == 0 then
return
end
local link = tab_args[1]
local label = tab_args[2]
local options = string.lower(tab_args[3] or "")
if label == nil or label == '' then
if base ~= nil and link == "" then
-- Special case
label = base
else
label = link
end
end
if base ~= nil then
-- Base page
if link == "" then
link = base
-- Sub-page
else
link = base .. "/" .. link
end
end
local optional = options:find('optional')
-- Highlighted tab
if link == highlight then
return tabFmt:format(highlightClass, label)
-- Other tabs
elseif (not optional) or mw.title.new(link).exists then
return tabFmt:format(tabClass,
linkFmt:format(link, label))
end
end)
return rowFmt:format(table.concat(tabs))
end
local function render(frame)
local args = getArgs(frame, {removeBlanks=false})
-- Highlight current page by default.
local current = mw.title.getCurrentTitle().prefixedText
local highlight = args.highlight or current
return _render(args, highlight)
end
return {
_render=_render,
render=render
}