Toggle menu
Toggle preferences menu
Toggle personal menu
Not logged in
Your IP address will be publicly visible if you make any edits.

Module:Navbox: Difference between revisions

From Gensopedia
mNo edit summary
Tag: Reverted
mNo edit summary
Tag: Manual revert
Line 1: Line 1:
-- version 1.1.1


local canonicalName = {
-- config table for RANGER.
-- If you want to change the default config, DO NOT change it here,
-- please do it via the `onLoadConfig` hook in [[Module:Navbox/Hooks]].
local config = {
default_navbox_class = "navigation-not-searchable",  -- Base value of the `class` parameter.
default_title_class = nil,    -- Base value of the `title_class` parameter.
default_above_class = nil,    -- Base value of the `above_class` parameter.
default_below_class = nil,    -- Base value of the `below_class` parameter.
default_section_class =nil,  -- Base value of the `section_class` parameter.
default_header_class = nil,  -- Base value of the `header_class` parameter.
default_group_class = nil,    -- Base value of the `group_class` parameter.
default_list_class = 'hlist', -- Base value of the `list_class` parameter.
default_header_state = nil, -- Base value of the `state` parameter.
 
editlink_hover_message_key = 'Navbox-edit-hover', -- The system message name for hover text of the edit icon.
auto_flatten_top_level = true, -- If true, when a section has only one list with no content and no corresponding group but has sublists, these sublists will be moved to top level.
-- This helps make the hierarchy of sections and content clearer.
-- An example:
-- {{navbox
-- ...
--  |header-1 = Items
--  | group-1.1 = Weapons
--  |  list-1.1 = Swords · Guns · Wands
--  | group-1.2 = Armors
--  |  list-1.2 = Head pieces · Capes
--  |header-2 = NPCs
--  | group-2.1 = Town NPCs
--  |  list-2.1 = Guide · Witch
-- ...
-- }}
-- will be equal to:
-- {{navbox
-- ...
--  |header-1 = Items
--  | group-2 = Weapons
--  |  list-2 = Swords · Guns · Wands
--  | group-3 = Armors
--  |  list-3 = Head pieces · Capes
--  |header-5 = NPCs
--  | group-6 = Town NPCs
--  |  list-6 = Guide · Witch
-- ...
-- }}
custom_render_handle = nil, -- usually for debugging purposes only. if set, it should be a function accept 2 parameters: `dataTree` and `args`, and return a string as module output.
}
 
---------------------------------------------------------------------
 
-- Argument alias.
local CANONICAL_NAMES = {
['titlestyle'] = 'title_style',
['titlestyle'] = 'title_style',
['listclass'] = 'list_class',
['listclass'] = 'list_class',
Line 18: Line 71:
}
}


local STATES = {
['no'] = '',
['off'] = '',
['plain'] = '',
['collapsed'] = 'mw-collapsible mw-collapsed',
}


local BOOL_FALSE = {
['no'] = true,
['off'] = true,
['false'] = true,
}


local STRIPED = {
['odd'] = 'striped-odd',
['swap'] = 'striped-odd',
['y'] = 'striped-even',
['yes'] = 'striped-even',
['on'] = 'striped-even',
['even'] = 'striped-even',
['striped'] = 'striped-even',
}


local config = {
local DEFAULT_ARGS = {
['default_list_class'] = 'hlist', -- base value of the `list_class` parameter.
['meta'] = true,
['editlink_hover_message_key'] = 'Navbox-edit-hover', -- The system message name for hover text of the edit icon.
}
}


local NAVBOX_CHILD_INDICATOR = '!!C$H$I$L$D!!'
local NAVBOX_CHILD_INDICATOR_LENGTH = string.len( NAVBOX_CHILD_INDICATOR )


local args = {} -- store nomalized args
local CLASS_PREFIX = 'ranger-'
local tree = {}


local hooks = {}
---------------------------------------------------------------------


local listClass -- default class for lists
local p = {}
local listCss
local h = {} -- non-public
local groupClass -- default class for groups
local hooks = mw.title.new('Module:Navbox/Hooks').exists and require('Module:Navbox/Hooks') or {}
local groupCss
local subgroupClass -- default class for subgroups
local subgroupCss
local headerClass -- default class for headers
local headerCss


local headerState -- default state for all headers
---------------------------------------------------------------------
 
-- For templates: {{#invoke:navbox|main|...}}
function p.main(frame)
local args = p.mergeArgs(frame)
args = h.parseArgs(args)
return p.build(args)
end


local nvaboxMainClass = 'ranger-navbox'
-- For modules: return require('module:navbox').build(args)
local classPrefix = 'ranger-'
-- By default this method will skip the arguments sanitizing phase
-- (and onSanitizeArgsStart/onSanitizeArgsEnd hooks).
-- Set `doParseArgs` to true to do arguments sanitizing.
-- If `customConfig` table is provided, it will be merged into default config table.
-- If `customHooks` table is provided, all default hook handles will be overrided, unprovided hooks will be empty.
function p.build(args, doParseArgs, customConfig, customHooks)
if customHooks then
hooks = customHooks
end
if customConfig then
for k,v in pairs(customConfig) do
config[k] = v
end
end
if doParseArgs then
args = h.parseArgs(args)
end
 
h.runHook('onLoadConfig', config, args)
--merge default args
for k,v in pairs(DEFAULT_ARGS) do
if args[k] == nil then
args[k] = DEFAULT_ARGS[k]
end
end
 
h.runHook('onBuildTreeStart', args)
local dataTree = h.buildDataTree(args)
h.runHook('onBuildTreeEnd', dataTree, args)
if type(config.custom_render_handle) == 'function' then
return config.custom_render_handle(dataTree, args)
else
return h.render(dataTree)
end
end


local trim = mw.text.trim
-- merge args from frame and frame:getParent()
-- It may be used when creating custom wrapping navbox module.
--
-- For example, Module:PillNavbox
--
-- local RANGER = require('Module:Navbox')
-- local p = {}
-- function p.main(frame)
--    return RANGER.build(RANGER.mergeArgs(frame), true, {
--        default_navbox_class = 'pill', -- use "pill" style by default.
--    })
-- end
-- return p
--
function p.mergeArgs(frame)
local inputArgs = {}
for k, v in pairs(frame.args) do
v = mw.text.trim(tostring(v))
if v ~= '' then
inputArgs[k] = v
end
end
for k, v in pairs(frame:getParent().args) do
v = mw.text.trim(v)
if v ~= '' then
inputArgs[k] = v
end
end
return inputArgs
end


local even -- for zebra stripes
------------------------------------------------------------------------


---Split the `str` on each `div` in it and return the result as a table. Original
function h.parseArgs(inputArgs)
---version credit: http://richard.warburton.it.
---@param div string
h.runHook('onSanitizeArgsStart', inputArgs)
---@param str string
---@return string[]? strExploded Is `nil` if `div` is an empty string
local args = {}
local function explode(div, str)
if (div=='') then return nil end
for k, v in pairs(inputArgs) do
local pos,arr = 0,{}
-- all args have already been trimmed
-- for each divider found
if type(k) == 'string' then
for st,sp in function() return string.find(str,div,pos,true) end do
local key = h.normalizeKey(k)
arr[#arr+1] = string.sub(str,pos,st-1) -- Attach chars left of current divider
args[key] = h.normalizeValue(key, v)
pos = sp+1 -- Jump past current divider
else
args[k] = mw.text.trim(v) -- keep number-index arguments (for {{navbox|child|...}})
end
end
end
arr[#arr+1] = string.sub(str,pos) -- Attach chars right of last divider
return arr
h.runHook('onSanitizeArgsEnd', args, inputArgs)
return args
end
end


-- Normalize the name string of arguments.
-- Normalize the name string of arguments.
-- space character(" ") will be treat as underscore("_"),
-- the normalized form is (index:)?name, in which:
-- and the name string will be converted to lowercase,
-- index is number index such as 1, 1.3, 1.2.45,
-- and support underscore between numbers (n_m_l format),
-- name is in lowercase underscore-case, such as group, group_style
-- and support such as group1/list1 prefix.
-- e.g: header_state, 1.3:list_style
local function normalize(s)
-- the input argument name can be:
-- * camel-case: listStyle, ListStyle
-- * space separated: list style
-- * prefix+index+postfix?, and can be in camel-case or space/hyphen separated or mixed: list 1 style, list1, list1Style, list1_style
-- * index.name: 1.3.list
-- * index_name: 1.3_list (Space separated are treated as underscore separated, therefore 1.3 list are vaild too)
function h.normalizeKey(s)
-- camel-case to lowercase underscore-case
-- camel-case to lowercase underscore-case
s = string.gsub(s, '(%l)(%u)', '%1_%2')  
s = s:gsub('%l%f[%u]', '%0_') -- listStyle to list_style
s = string.lower(string.gsub(s, ' ', '_'))
s = (s:gsub(' ', '_')):lower() -- space to underscore
s = string.gsub(s, '(%l)(%d)', '%1_%2') -- group1* to group_1*
s = s:gsub('%l%f[%d]', '%0_') -- group1* to group_1*
s = string.gsub(s, '(%d)(%l)', '%1_%2') -- *1style to *1_style
s = s:gsub('%d%f[%l]', '%0_') -- *1style to *1_style
-- number format x_y_z to x.y.z
-- number format x_y_z to x.y.z
s = string.gsub(s, '(%d)_%f[%d]', '%1%.')
s = s:gsub('(%d)_%f[%d]', '%1%.')
-- move index to the beginning:
-- group_1.2_style to 1.2:group_style
-- group_1 to 1:group
s = s:gsub('^([%l_]+)_([%d%.]+)', '%2:%1')
-- support index.name and index_name:
-- 1.2.group / 1.2_group to 1.2:group
s = s:gsub('^([%d%.]+)[%._]%f[%l]', '%1:')
-- now the key should be in normalized form, if the origin key is vaild
-- standardize *_css to *_style
-- standardize *_css to *_style
s = string.gsub(s, '_css$', '_style')
s = s:gsub('_css$', '_style')
-- standardize all aliases to the canonical name
-- standardize all aliases to the canonical name
return canonicalName[s] or s
return CANONICAL_NAMES[s] or s
end
end


local function parseArgs(inputArgs)
function h.normalizeValue(k, v)
for k,v in pairs(inputArgs) do
k = tostring(k)
args[normalize(k)] = trim(v)
if k:find('_style$') then
v = (v .. ';'):gsub(';;', ';')
return v
elseif k == 'striped' then
return STRIPED[v]
elseif v:sub(1, 2) == '{|' or v:match('^[*:;#]') then
-- Applying nowrap to lines in a table does not make sense.
-- Add newlines to compensate for trim of x in |parm=x in a template.
return '\n' .. v ..'\n'
elseif k == 'meta' then
return not BOOL_FALSE[v]
end
end
return v
end
-- we need a default value for all empty state_* arguments, so we can not do this in h.normalizeValue()
function h.normalizeStateValue(v)
return STATES[v] or 'mw-collapsible'
end
end


-- Used to traverses a table following the order of its keys:
-- parse arguments, convert them to structured data tree
--  for key, value in pairsByKeys(array) do
function h.buildDataTree(args)
--    print(key, value)
local data = {
--  end
state = h.normalizeStateValue(args.state),
local function pairsByKeys(t, f)
striped = args.striped,
local a = {}
class = h.mergeAttrs(args.navbox_class, config.default_navbox_class),
for n in pairs(t) do table.insert(a, n) end
style = args.navbox_style,
table.sort(a, f)
}
local i = 0      -- iterator variable
local iter = function ()   -- iterator function
if args.title or args.meta or data.state ~= '' then
i = i + 1
data.title = {
if a[i] == nil then return nil
content = args.title,
else return a[i], t[a[i]]
class = h.mergeAttrs(args.title_class, config.default_title_class),
style = args.title_style,
}
if args.meta then
data.metaLinks = {
template = args.name or mw.getCurrentFrame():getParent():getTitle()
}
end
end
end
end
return iter
end
if args.above then
data.above = {
content = args.above,
class= h.mergeAttrs(args.above_class, config.default_above_class),
style = args.above_style,
}
end


local function normalizeStateValue(state)
if args.below then
if state == 'no' or state == 'off' or state == 'plain' then
data.below = {
return nil
content = args.below,
class= h.mergeAttrs(args.below_class, config.default_below_class),
style = args.below_style,
}
end
end
if state == 'collapsed' then
return 'collapsed'
local tree = h.buildTree(args, {
listClass = h.mergeAttrs(args.list_class, config.default_list_class),
listStyle =  args.list_style,
groupClass = h.mergeAttrs(args.group_class, config.default_group_class),
groupStyle = args.group_style,
headerClass = h.mergeAttrs(args.header_class, config.default_header_class),
headerStyle = args.header_style,
})
-- handle {{navbox|child|...}} syntax:
if args[1] == 'child' then
return NAVBOX_CHILD_INDICATOR..mw.text.jsonEncode(tree)
end
end
return true
end
-- normal case
 
local sectionClass = h.mergeAttrs(args.section_class, config.default_section_class)
local function normalizeStripedValue(striped)
local sectionStyle = args.section_style
if striped == 'odd' or striped == 'swap' then
local headerState = args.header_state or config.default_header_state
striped = 'striped-odd'
data.sections = {}
elseif striped == 'y' or striped == 'yes' or striped == 'on' or striped == 'even' or striped == 'striped' then
local section
striped = 'striped-even'
for k, v in h.orderedPairs(tree or {}) do
else
if v.header or not section then
striped = nil
--start a new section
section = {
class = h.mergeAttrs(args[k..':section_class'], sectionClass),
style = h.mergeAttrs(args[k..':section_style'], sectionStyle),
body = {},
}
-- Section header if needed.
-- If the value of a `|header_n=` is two or more consecutive "-" characters (e.g. --, -----),
-- it means start a new section without header, and the new section will be not collapsable.
if v.header and not string.match(v.header.content, '^%-%-+$') then
section.header =v.header
section.state = h.normalizeStateValue(args[k..':state'] or headerState)
end
v.header = nil
data.sections[#data.sections+1] = section
end
-- check for section above/below areas
if v.above then
section.above = v.above
v.above = nil
end
if v.below then
section.below = v.below
v.below = nil
end
if next(v) then -- v is not empty (with group/list/sub)
section.body[#section.body+1] = v
end
end
end
return striped
end
if config.auto_flatten_top_level then
 
for _, sect in ipairs(data.sections) do
local function makeCollapsible(node, state)
if #sect.body == 1 then
if state then
local node = sect.body[1]
node:addClass('mw-collapsible')
if not node.group and not node.list and node.sub then
if state == 'collapsed' then  
sect.body = node.sub
node:addClass('mw-collapsed')
end
end
end
end
end
end
return data
end
end


function h.buildTree(args, defaults)
local tree = {}
local check = function(key, value)
local index, name = string.match(key, '^([%d%.]+):(.+)$')


local function runHook(key, ...)
if not index then return end -- no number index found
if hooks[key] then
if name ~= 'list' and name ~= 'group' and name ~= 'header' and name ~= 'above' and name ~= 'below' then return end -- check only the names we are interested in
hooks[key](...)
if string.match(index, '^%.') or string.match(index, '%.$') or string.match(index, '%.%.') then return end -- invalid number index
-- find the node that matches the index in the tree
local arr = mw.text.split(index, '.', true)
n = tonumber(table.remove(arr))
local node = tree
for _, v in ipairs(arr) do
v = tonumber(v)
if not node[v] then
node[v] = {['sub'] = {}}
elseif not node[v]['sub'] then
node[v]['sub'] = {}
end
node = node[v]['sub']
end
if not node[n] then node[n] = {} end
if name == 'list' and string.sub(value, 1, NAVBOX_CHILD_INDICATOR_LENGTH) == NAVBOX_CHILD_INDICATOR then
-- it is from {{navbox|child| ... }}
node[n]['sub'] = mw.text.jsonDecode(string.sub(value, NAVBOX_CHILD_INDICATOR_LENGTH+1))
else
node[n][name] = {
content = value,
class= h.mergeAttrs(args[key..'_class'], defaults[name..'Class']),
style = h.mergeAttrs(args[key..'_style'], defaults[name..'Style'])
}
end
end
end
end
for k,v in pairs(args) do
 
check(k, v)
local function getArg(name)
if args[name] and args[name] ~= '' then
return args[name]
else
return nil
end
end
return tree
end
end


local function getArgGroup(prefix)
function h.render(data)
if not prefix then
-- handle {{navbox|child|...}} syntax
return tree
if type(data) == 'string' then
return data
end
end
local node = tree
for _, s in ipairs(explode('.', prefix)) do
if not node[s] then error('invaild index: '..prefix) end
node = node[s]['sub']
end
return node
end


local function checkForTreeNode(name, key, value)
-----  normal case -----
local pattern = '^'..name..'_([%.%d]+)$'
local index = string.match(key, pattern)
if not index then return end
if string.match(index, '^%.') or string.match(index, '%.$') or string.match(index, '%.%.') then return end
local arr = explode('.', index)
n = tonumber(table.remove(arr))
local node = tree
for _, v in ipairs(arr) do
v = tonumber(v)
if not node[v] then
node[v] = {['sub'] = {}}
elseif not node[v]['sub'] then
node[v]['sub'] = {}
end
node = node[v]['sub']
end
if not node[n] then node[n] = {} end
if name == 'list' and string.sub(value, 1, 13) == '!!C$H$I$L$D!!' then
local out = mw.html.create()
-- it is from {{navbox|child| ... }}
node[n]['sub'] = mw.text.jsonDecode(string.sub(value, 14))
else
node[n][name] = value
end
return true
-- build navbox container
end
local navbox = out:tag('div')
:attr('role', 'navigation'):attr('aria-label', 'Navbox')
:addClass(CLASS_PREFIX..'navbox')
:addClass(data.class)
:addClass(data.striped)
:addClass(data.state)
:cssText(data.style)


local function buildTree()
--title bar
for k, v in pairs(args) do
if data.title then
local _ = checkForTreeNode('list', k, v) or checkForTreeNode('group', k, v) or checkForTreeNode('header', k, v)
local titlebar = navbox:tag('div'):addClass(CLASS_PREFIX..'title')
titlebar:tag('div'):addClass('mw-collapsible-toggle-placeholder')
if data.metaLinks then
titlebar:node(h.renderMetaLinks(data.metaLinks))
end
if data.title then
titlebar:addClass(data.title.class):tag('div')
:addClass(CLASS_PREFIX..'title-text')
:addClass(data.title.class)
:cssText(data.title.style)
:wikitext(data.title.content)
end
end
end
return tree
end


local function getMergedStr(...)
--above
local s = ''
if data.above then
for i=1, select('#', ...) do
navbox:tag('div')
local v = trim(select(i, ...) or '')
:addClass(CLASS_PREFIX..'above mw-collapsible-content')
local str = string.match(v, '^%-%-+(.*)$')
:addClass(data.above.class)
if str then
:cssText(data.above.style)
s = trim(str..' '..s)
:wikitext(data.above.content)
break
:attr('id', (not data.title) and mw.uri.anchorEncode(data.above.content) or nil) -- id for aria-labelledby attribute, if no title
else
end
s = trim(v..' '..s)
-- sections
for i,sect in ipairs(data.sections) do
--section box
local section = navbox:tag('div')
:addClass(CLASS_PREFIX..'section mw-collapsible-content')
:addClass(sect.class)
:addClass(sect.state)
:cssText(sect.style)
-- section header
if sect.header then
section:tag('div')
:addClass(CLASS_PREFIX..'header')
:addClass(sect.header.class)
:cssText(sect.header.style)
:tag('div'):addClass('mw-collapsible-toggle-placeholder'):done()
:tag('div'):addClass(CLASS_PREFIX..'header-text'):wikitext(sect.header.content)
end
-- above:
if sect.above then
section:tag('div')
:addClass(CLASS_PREFIX..'above mw-collapsible-content')
:addClass(sect.above.class)
:cssText(sect.above.style)
:wikitext(sect.above.content)
end
-- body: groups&lists
local box = section:tag('div'):addClass(CLASS_PREFIX..'section-body mw-collapsible-content')
h.renderBody(sect.body, box, 0, true) -- reset even status each section
-- below:
if sect.below then
section:tag('div')
:addClass(CLASS_PREFIX..'below mw-collapsible-content')
:addClass(sect.below.class)
:cssText(sect.below.style)
:wikitext(sect.below.content)
end
end
end
end
if s == '' then s = nil end
-- Insert a blank section for completely empty navbox to ensure it behaves correctly when collapsed.
return s
if #data.sections == 0 and not data.above and not data.below then
end
navbox:tag('div'):addClass(CLASS_PREFIX..'section mw-collapsible-content')
end


local function getCssArg(prefix)
--below
local css = getArg(prefix..'_style')
if data.below then
if css and (string.sub(css, -1) ~= ';') then
navbox:tag('div')
css = css..';'
:addClass(CLASS_PREFIX..'below mw-collapsible-content')
:addClass(data.below.class)
:cssText(data.below.style)
:wikitext(data.below.content)
end
end
return css
end
-- Applies class/css to the element
local function applyStyle(node, prefix, baseClass, baseCss)
return node:addClass(getMergedStr(getArg(prefix..'_class'), baseClass)):cssText(getMergedStr(getCssArg(prefix), baseCss))
end


local function processItem(item)
return out
if item:sub(1, 2) == '{|' then
-- Applying nowrap to lines in a table does not make sense.
-- Add newlines to compensate for trim of x in |parm=x in a template.
return '\n' .. item ..'\n'
end
if item:match('^[*:;#]') then
return '\n' .. item ..'\n'
end
return item
end
end


local function renderMetaLinks()
function h.renderMetaLinks(info)
local name = getArg('name') or mw.getCurrentFrame():getParent():getTitle()
local title = mw.title.new(mw.text.trim(info.template), 'Template')
local title = mw.title.new(trim(name), 'Template')
if not title then
if not title then
error('Invalid title ' .. name)
error('Invalid title ' .. info.template)
end
end
local msg = mw.message.new(config['editlink_hover_message_key'])
local msg = mw.message.new(config.editlink_hover_message_key)
local hoverText = msg:exists() and msg:plain() or 'View or edit this template'
local hoverText = msg:exists() and msg:plain() or 'View or edit this template'
return mw.html.create('span'):addClass(classPrefix..'meta')
return mw.html.create('span'):addClass(CLASS_PREFIX..'meta')
:tag('span'):addClass('nv nv-view')
:tag('span'):addClass('nv nv-view')
:wikitext('[['..title.fullText..'|')
:wikitext('[['..title.fullText..'|')
Line 265: Line 536:
end
end


local function renderTitleBar(title, collapsible, metaLinks)
function h.renderBody(info, box, level, even)
local titlebar = applyStyle(mw.html.create('div'):addClass(classPrefix..'title'), 'title', config['default_title_class'])
local count = 0
if metaLinks then
for _,v in h.orderedPairs(info) do
titlebar:node(renderMetaLinks())
if v.group or v.list or v.sub then
end
count = count + 1
if title then
-- row container
titlebar:tag('div')
local row = box:tag('div'):addClass(CLASS_PREFIX..'row')
:attr('id', mw.uri.anchorEncode(title) or '') -- id for aria-labelledby attribute
-- group cell
:addClass(classPrefix..'title-text')
if v.group or (v.sub and level > 0 and not v.list) then
:wikitext(processItem(title))
local groupCell = row:tag('div')
end
:addClass(CLASS_PREFIX..'group level-'..level)
return titlebar
:addClass((level > 0) and CLASS_PREFIX..'subgroup' or nil)
end
local groupContentWrap = groupCell:tag('div'):addClass(CLASS_PREFIX..'wrap')
 
if v.group then
local function renderAboveBox(above, id)
groupCell:addClass(v.group.class):cssText(v.group.style)
local node = mw.html.create('div')
groupContentWrap:wikitext(v.group.content)
:addClass(classPrefix..'above mw-collapsible-content')
else
-- id for aria-labelledby attribute, if no title
groupCell:addClass('empty')
:attr('id', id and mw.uri.anchorEncode(above) or nil)
row:addClass('empty-group-list')
:wikitext(processItem(above))
end
return applyStyle(node, 'above', config['default_above_class'])
end
 
local function renderBelowBox(below)
local node = mw.html.create('div')
:addClass(classPrefix..'below mw-collapsible-content')
:wikitext(processItem(below))
return applyStyle(node, 'below', config['default_below_class'])
end
 
local function renderSectionHeader(content, index)
local node = mw.html.create('div'):addClass(classPrefix..'header')
:tag('div'):addClass(classPrefix..'header-text'):wikitext(processItem(content))
:done()
return applyStyle(node, 'header_'..index, headerClass, getCssArg('header'))
end
 
local function renderList(content, index, level)
even = not even -- flip even/odd status
local node = mw.html.create('div'):addClass(classPrefix..'wrap'):addClass(even and classPrefix..'even' or classPrefix..'odd')
:tag('div'):addClass(classPrefix..'list'):wikitext(processItem(content))
:done()
return applyStyle(node, 'list_'..index,
getMergedStr(getArg('list_level_'..level..'_class'), listClass),
getMergedStr(getCssArg('list_level_'..level), config['default_list_level_'..level..'_class'], listCss)
)
end
 
local renderRow, renderSublist
function renderRow(box, v, k, level)
if v['group'] or v['list'] or v['sub'] then
local row = box:tag('div'):addClass(classPrefix..'row')
if v['group'] or (v['sub'] and level > 0 and not v['group'] and not v['list']) then
local groupCell = row:tag('div')
if level == 0 then
groupCell:addClass(classPrefix..'group')
applyStyle(groupCell, 'group_'..k, groupClass, groupCss)
else
else
groupCell:addClass(classPrefix..'subgroup level-'..level)
row:addClass('empty-group')
:addClass(getMergedStr(
  getArg('group_'..k..'_class'),
  getArg('subgroup_'..k..'_class'),
  getArg('subgroup_level_'..level..'_class'),
  config['default_subgroup_level_'..level..'_class'],
  subgroupClass
))
:cssText(getMergedStr(
  getCssArg('group_'..k),
  getCssArg('subgroup_'..k),
  getCssArg('subgroup_level_'..level),
  subgroupCss
))
end
end
groupCell:tag('div'):addClass(classPrefix..'wrap'):wikitext(processItem(v['group'] or ''))
-- list cell
if not v['group'] then
local listCell = row:tag('div'):addClass(CLASS_PREFIX..'listbox')
groupCell:addClass('empty')
if not v.list and not v.sub then
row:addClass('empty-group-list')
listCell:addClass('empty')
row:addClass('empty-list')
end
if v.list or (v.group and not v.sub) then
--listCell:node(h.renderList(v['list'] or '', k, level, args))
even = not even -- flip even/odd status
local cell = listCell:tag('div')
:addClass(CLASS_PREFIX..'wrap')
:addClass(even and CLASS_PREFIX..'even' or CLASS_PREFIX..'odd')
if v.list then
cell:addClass(v.list.class):cssText(v.list.style)
:tag('div'):addClass(CLASS_PREFIX..'list'):wikitext(v.list.content)
end
end
if v.sub then
local sublistBox = listCell:tag('div'):addClass(CLASS_PREFIX..'sublist level-'..level)
even = h.renderBody(v.sub, sublistBox, level+1, even)
end
end
else
row:addClass('empty-group')
end
local listCell = row:tag('div'):addClass(classPrefix..'listbox')
if not v['list'] and not v['sub'] then
listCell:addClass('empty')
row:addClass('empty-list')
end
end
if v['list'] or (v['group'] and not v['sub']) then
listCell:node(renderList(v['list'] or '', k, level))
end
if v['sub'] then
listCell:node(renderSublist(v['sub'], k, level+1))
end
return box
end
end
function renderSublist(l, prefix, level)
local count = 0
local box = mw.html.create('div'):addClass(classPrefix..'sublist level-'..level)
for k,v in pairsByKeys(l) do
count = count + tonumber(renderRow(box, v, prefix..'.'..k, level) and 1 or 0)
end
end
if count > 0 then
if count > 0 then
return box:css('--count', count)
box:css('--count', count) -- for flex-grow
end
end
return even
end
end


local function build(inputArgs)
-- pairs, but sort the keys alphabetically
if mw.title.new('Module:Navbox/Hooks').exists then
function h.orderedPairs(t, f)
hooks = require('Module:Navbox/Hooks')
local a = {}
for n in pairs(t) do table.insert(a, n) end
table.sort(a, f)
local i = 0      -- iterator variable
local iter = function ()   -- iterator function
i = i + 1
if a[i] == nil then return nil
else return a[i], t[a[i]]
end
end
end
return iter
end


runHook('onParseArgs', inputArgs)
-- For cascading parameters, such as style or class, they are merged in exact order (from general to specific).
parseArgs(inputArgs)
-- Any parameter starting with multiple hyphens(minus signs) will terminate the cascade.
-- An example:
buildTree()
-- For group_1.1, its style is affected by parameters |group_1.1_style=... , |subgroup_level_1_style=... , and |subgroup_style=... .
listClass = getMergedStr(getArg('list_class'), config['default_list_class'])
-- If we have |group_1.1_style= color:red; |subgroup_level_1_style= font-weight: bold; and |subgroup_style= color: green; ,
listCss =  getCssArg('list')
-- the style of group_1.1 will be style="color:green; font-weight: bold; color: red;" ;
groupClass = getMergedStr(getArg('group_class'), config['default_group_class'])
-- if we have |group_1.1_style= -- color:red; |subgroup_level_1_style= font-weight: bold; and |subgroup_style= color: green; ,
groupCss = getCssArg('group')
-- the style of group_1.1 will be style="color: red;" only, and the cascade is no longer performed for |subgroup_level_1_style and |subgroup_style.
subgroupClass = getMergedStr(getArg('subgroup_class'), config['default_subgroup_class'])
function h.mergeAttrs(...)
subgroupCss = getCssArg('subgroup')
local trim = mw.text.trim
headerClass = getMergedStr(getArg('header_class'), config['default_header_class'])
local s = ''
headerCss = getCssArg('header')
for i=1, select('#', ...) do
local v = trim(select(i, ...) or '')
headerState = getArg('header_state')
local str = string.match(v, '^%-%-+(.*)$')
if str then
local res = mw.html.create()
s = trim(str..' '..s)
break
local collapsible = normalizeStateValue(getArg('state'))
else
local metaLinks = normalizeStateValue(getArg('meta'))
s = trim(v..' '..s)
local title = getArg('title')
local above = getArg('above')
local below = getArg('below')
local striped = normalizeStripedValue(getArg('striped'))
 
-- build navbox container
local navboxClass = getMergedStr(getArg('navbox_class'), config['default_navbox_class'])
local nav = res:tag('div')
:attr('role', 'navigation')
:addClass(nvaboxMainClass)
:addClass(navboxClass)
:addClass(striped)
:cssText(getCssArg('navbox'))
makeCollapsible(nav, collapsible)
-- aria-labelledby title, otherwise above
if title or above then
nav:attr('aria-labelledby', mw.uri.anchorEncode(title or above))
else
nav:attr('aria-label', 'Navbox')
end
-- title bar
if title or collapsible or metaLinks then
nav:node(renderTitleBar(title, collapsible, metaLinks))
end
-- above
if above then
nav:node(renderAboveBox(above, not title))
end
-- sections
local section, box
local sectionClass = getMergedStr(getArg('section_class'), config['default_section_class'])
for k,v in pairsByKeys(tree) do
--start a new section
if v['header'] or not section then
section = nav:tag('div'):addClass(classPrefix..'section mw-collapsible-content')
applyStyle(section, 'section_'..k, sectionClass)
even = true -- reset even/odd status
if v['header'] then
local state = 'plain'
if getMergedStr(v['header']) then
section:node(renderSectionHeader(v['header'], k))
state = getArg('state_'..k) or headerState
end
makeCollapsible(section, normalizeStateValue(state))
end
box = section:tag('div'):addClass(classPrefix..'section-body mw-collapsible-content')
end
end
renderRow(box, v, k, 0)
end
end
if s == '' then s = nil end
-- Add a blank section for completely empty navbox to ensure it behaves correctly when collapsed.
return s
if not section and not above and not below then  
end
nav:tag('div'):addClass(classPrefix..'section mw-collapsible-content')
 
function h.runHook(key, ...)
if hooks[key] then
hooks[key](...)
end
end
-- below
if below then
nav:node(renderBelowBox(below))
end
return tostring(res)
end
end


---------------------------------------------------------------------
-----------------------------------------------
return {
return p
navbox = function(frame)
local inputArgs = {}
for k, v in pairs(frame.args) do
if type(k) == 'string' then
v = trim(tostring(v))
if v ~= '' then
inputArgs[k] = v
end
end
end
for k, v in pairs(frame:getParent().args) do
if type(k) == 'string' then
v = trim(v)
if v ~= '' then
inputArgs[k] = v
end
end
end
if trim(frame.args[1] or frame:getParent().args[1] or '') == 'child' then
parseArgs(inputArgs)
return '!!C$H$I$L$D!!'..mw.text.jsonEncode(buildTree())
else
return build(inputArgs)
end
end,
build = build, -- for other modules. e.g: return require('module:navbox').build(args)
}
 
-- version: r59 2024.10.28 --

Revision as of 03:25, 1 January 2025

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

-- version 1.1.1

-- config table for RANGER.
-- If you want to change the default config, DO NOT change it here,
-- please do it via the `onLoadConfig` hook in [[Module:Navbox/Hooks]].
local config = {
	default_navbox_class = "navigation-not-searchable",   -- Base value of the `class` parameter.
	default_title_class = nil,    -- Base value of the `title_class` parameter.
	default_above_class = nil,    -- Base value of the `above_class` parameter.
	default_below_class = nil,    -- Base value of the `below_class` parameter.
	default_section_class =nil,   -- Base value of the `section_class` parameter.
	default_header_class = nil,   -- Base value of the `header_class` parameter.
	default_group_class = nil,    -- Base value of the `group_class` parameter.
	default_list_class = 'hlist', -- Base value of the `list_class` parameter.
	
	default_header_state = nil, -- Base value of the `state` parameter.

	editlink_hover_message_key = 'Navbox-edit-hover', -- The system message name for hover text of the edit icon. 
	
	auto_flatten_top_level = true, -- If true, when a section has only one list with no content and no corresponding group but has sublists, these sublists will be moved to top level.
	-- This helps make the hierarchy of sections and content clearer.
	-- An example:
	-- {{navbox
	-- ...
	--   |header-1 = Items
	--   | group-1.1 = Weapons
	--   |  list-1.1 = Swords · Guns · Wands
	--   | group-1.2 = Armors
	--   |  list-1.2 = Head pieces · Capes
	--   |header-2 = NPCs
	--   | group-2.1 = Town NPCs
	--   |  list-2.1 = Guide · Witch
	-- ...
	-- }}
	-- will be equal to:
	-- {{navbox
	-- ...
	--   |header-1 = Items
	--   | group-2 = Weapons
	--   |  list-2 = Swords · Guns · Wands
	--   | group-3 = Armors
	--   |  list-3 = Head pieces · Capes
	--   |header-5 = NPCs
	--   | group-6 = Town NPCs
	--   |  list-6 = Guide · Witch
	-- ...
	-- }}
	
	custom_render_handle = nil, -- usually for debugging purposes only. if set, it should be a function accept 2 parameters: `dataTree` and `args`, and return a string as module output.
}

---------------------------------------------------------------------

-- Argument alias.
local CANONICAL_NAMES = {
	['titlestyle'] = 'title_style',
	['listclass'] = 'list_class',
	['groupstyle'] = 'group_style',
	['collapsible'] = 'state',
	['editlink'] = 'meta',
	['editlinks'] = 'meta',
	['editicon'] = 'meta',
	['edit_link'] = 'meta',
	['edit_links'] = 'meta',
	['edit_icon'] = 'meta',
	['navbar'] = 'meta',
	['evenodd'] = 'striped',
	['class'] = 'navbox_class',
	['css'] = 'navbox_style',
	['style'] = 'navbox_style',
}

local STATES = {
	['no'] = '',
	['off'] = '',
	['plain'] = '',
	['collapsed'] = 'mw-collapsible mw-collapsed',
}

local BOOL_FALSE = {
	['no'] = true,
	['off'] = true,
	['false'] = true,
}

local STRIPED = {
	['odd'] = 'striped-odd',
	['swap'] = 'striped-odd',
	['y'] = 'striped-even',
	['yes'] = 'striped-even',
	['on'] = 'striped-even',
	['even'] = 'striped-even',
	['striped'] = 'striped-even',
}

local DEFAULT_ARGS = {
	['meta'] = true,
}

local NAVBOX_CHILD_INDICATOR = '!!C$H$I$L$D!!'
local NAVBOX_CHILD_INDICATOR_LENGTH = string.len( NAVBOX_CHILD_INDICATOR )

local CLASS_PREFIX = 'ranger-'

---------------------------------------------------------------------

local p = {}
local h = {} -- non-public
local hooks = mw.title.new('Module:Navbox/Hooks').exists and require('Module:Navbox/Hooks') or {}

---------------------------------------------------------------------

-- For templates: {{#invoke:navbox|main|...}}
function p.main(frame)
	local args = p.mergeArgs(frame)
	args = h.parseArgs(args)
	return p.build(args)
end

-- For modules: return require('module:navbox').build(args)
-- By default this method will skip the arguments sanitizing phase 
-- (and onSanitizeArgsStart/onSanitizeArgsEnd hooks).
-- Set `doParseArgs` to true to do arguments sanitizing.
-- If `customConfig` table is provided, it will be merged into default config table.
-- If `customHooks` table is provided, all default hook handles will be overrided, unprovided hooks will be empty.
function p.build(args, doParseArgs, customConfig, customHooks)
	if customHooks then
		hooks = customHooks
	end
	if customConfig then
		for k,v in pairs(customConfig) do
			config[k] = v
		end
	end
	if doParseArgs then 
		args = h.parseArgs(args)
	end

	h.runHook('onLoadConfig', config, args)
	
	--merge default args
	for k,v in pairs(DEFAULT_ARGS) do
		if args[k] == nil then
			args[k] = DEFAULT_ARGS[k]
		end
	end

	h.runHook('onBuildTreeStart', args)
	local dataTree = h.buildDataTree(args)
	h.runHook('onBuildTreeEnd', dataTree, args)
	
	if type(config.custom_render_handle) == 'function' then
		return config.custom_render_handle(dataTree, args)
	else
		return h.render(dataTree)
	end
end

-- merge args from frame and frame:getParent()
-- It may be used when creating custom wrapping navbox module.
--
-- For example, Module:PillNavbox
--
-- local RANGER = require('Module:Navbox')
-- local p = {}
-- function p.main(frame)
--     return RANGER.build(RANGER.mergeArgs(frame), true, {
--         default_navbox_class = 'pill', -- use "pill" style by default.
--     })
-- end
-- return p
--
function p.mergeArgs(frame)
	local inputArgs = {}
	
	for k, v in pairs(frame.args) do
		v = mw.text.trim(tostring(v))
		if v ~= '' then
			inputArgs[k] = v
		end
	end
	
	for k, v in pairs(frame:getParent().args) do
		v = mw.text.trim(v)
		if v ~= '' then
			inputArgs[k] = v
		end
	end
	
	return inputArgs
end

------------------------------------------------------------------------

function h.parseArgs(inputArgs)
	
	h.runHook('onSanitizeArgsStart', inputArgs)
	
	local args = {}
	
	for k, v in pairs(inputArgs) do
		-- all args have already been trimmed
		if type(k) == 'string' then
			local key = h.normalizeKey(k)
			args[key] = h.normalizeValue(key, v)
		else
			args[k] = mw.text.trim(v) -- keep number-index arguments (for {{navbox|child|...}})
		end
	end
	
	h.runHook('onSanitizeArgsEnd', args, inputArgs)
	
	return args
end

-- Normalize the name string of arguments.
-- the normalized form is (index:)?name, in which:
-- index is number index such as 1, 1.3, 1.2.45,
-- name is in lowercase underscore-case, such as group, group_style
-- e.g: header_state, 1.3:list_style
-- the input argument name can be:
-- * camel-case: listStyle, ListStyle
-- * space separated: list style
-- * prefix+index+postfix?, and can be in camel-case or space/hyphen separated or mixed: list 1 style, list1, list1Style, list1_style
-- * index.name: 1.3.list
-- * index_name: 1.3_list (Space separated are treated as underscore separated, therefore 1.3 list are vaild too)
function h.normalizeKey(s)
	-- camel-case to lowercase underscore-case
	s = s:gsub('%l%f[%u]', '%0_') -- listStyle to list_style
	s = (s:gsub(' ', '_')):lower() -- space to underscore 
	s = s:gsub('%l%f[%d]', '%0_') -- group1* to group_1*
	s = s:gsub('%d%f[%l]', '%0_') -- *1style to *1_style
	
	-- number format x_y_z to x.y.z
	s = s:gsub('(%d)_%f[%d]', '%1%.')
	
	-- move index to the beginning:
	-- group_1.2_style to 1.2:group_style
	-- group_1 to 1:group
	s = s:gsub('^([%l_]+)_([%d%.]+)', '%2:%1')
	
	-- support index.name and index_name:
	-- 1.2.group / 1.2_group to 1.2:group
	s = s:gsub('^([%d%.]+)[%._]%f[%l]', '%1:')
	
	-- now the key should be in normalized form, if the origin key is vaild

	-- standardize *_css to *_style
	s = s:gsub('_css$', '_style')
	
	-- standardize all aliases to the canonical name
	return CANONICAL_NAMES[s] or s
end

function h.normalizeValue(k, v)
	k = tostring(k)
	if k:find('_style$') then
		v = (v .. ';'):gsub(';;', ';')
		return v
	elseif k == 'striped' then
		return STRIPED[v]
	elseif v:sub(1, 2) == '{|' or v:match('^[*:;#]') then
		-- Applying nowrap to lines in a table does not make sense.
		-- Add newlines to compensate for trim of x in |parm=x in a template.
		return '\n' .. v ..'\n'
	elseif k == 'meta' then
		return not BOOL_FALSE[v]
	end
	return v
end

-- we need a default value for all empty state_* arguments, so we can not do this in h.normalizeValue()
function h.normalizeStateValue(v)
	return STATES[v] or 'mw-collapsible'
end

-- parse arguments, convert them to structured data tree
function h.buildDataTree(args)
	local data = {
		state = h.normalizeStateValue(args.state),
		striped = args.striped,
		class = h.mergeAttrs(args.navbox_class, config.default_navbox_class),
		style = args.navbox_style,
	}
	
	if args.title or args.meta or data.state ~= '' then
		data.title = {
			content = args.title,
			class = h.mergeAttrs(args.title_class, config.default_title_class),
			style = args.title_style,
		}
		if args.meta then
			data.metaLinks = {
				template = args.name or mw.getCurrentFrame():getParent():getTitle()
			}
		end
	end
	
	if args.above then
		data.above = {
			content = args.above,
			class= h.mergeAttrs(args.above_class, config.default_above_class),
			style = args.above_style,
		}
	end

	if args.below then
		data.below = {
			content = args.below,
			class= h.mergeAttrs(args.below_class, config.default_below_class),
			style = args.below_style,
		}
	end
	
	local tree = h.buildTree(args, {
		listClass = h.mergeAttrs(args.list_class, config.default_list_class),
		listStyle =  args.list_style,
		groupClass = h.mergeAttrs(args.group_class, config.default_group_class),
		groupStyle = args.group_style,
		headerClass = h.mergeAttrs(args.header_class, config.default_header_class),
		headerStyle = args.header_style,
	})
	
	-- handle {{navbox|child|...}} syntax:
	if args[1] == 'child' then
		return NAVBOX_CHILD_INDICATOR..mw.text.jsonEncode(tree)
	end
	
	-- normal case
	local sectionClass = h.mergeAttrs(args.section_class, config.default_section_class)
	local sectionStyle = args.section_style
	local headerState = args.header_state or config.default_header_state
	data.sections = {}
	local section
	for k, v in h.orderedPairs(tree or {}) do
		if v.header or not section then
			--start a new section
			section = { 
				class = h.mergeAttrs(args[k..':section_class'], sectionClass),
				style = h.mergeAttrs(args[k..':section_style'], sectionStyle),
				body = {},
			}
			-- Section header if needed.
			-- If the value of a `|header_n=` is two or more consecutive "-" characters (e.g. --, -----), 
			-- it means start a new section without header, and the new section will be not collapsable.
			if v.header and not string.match(v.header.content, '^%-%-+$') then
				section.header =v.header
				section.state = h.normalizeStateValue(args[k..':state'] or headerState)
			end
			v.header = nil
			data.sections[#data.sections+1] = section
		end
		-- check for section above/below areas
		if v.above then
			section.above = v.above
			v.above = nil
		end
		if v.below then
			section.below = v.below
			v.below = nil
		end
		if next(v) then -- v is not empty (with group/list/sub)
			section.body[#section.body+1] = v
		end
	end
	
	if config.auto_flatten_top_level then
		for _, sect in ipairs(data.sections) do
			if #sect.body == 1 then
				local node = sect.body[1]
				if not node.group and not node.list and node.sub then
					sect.body = node.sub
				end
			end
		end
	end
	
	return data
end

function h.buildTree(args, defaults)
	local tree = {}
	local check = function(key, value)
		local index, name = string.match(key, '^([%d%.]+):(.+)$')

		if not index then return end -- no number index found
		if name ~= 'list' and name ~= 'group' and name ~= 'header' and name ~= 'above' and name ~= 'below' then return end -- check only the names we are interested in
		if string.match(index, '^%.') or string.match(index, '%.$') or string.match(index, '%.%.') then return end -- invalid number index
		
		-- find the node that matches the index in the tree
		local arr = mw.text.split(index, '.', true)
		n = tonumber(table.remove(arr))
		local node = tree
		for _, v in ipairs(arr) do
			v = tonumber(v)
			if not node[v] then 
				node[v] = {['sub'] = {}} 
			elseif not node[v]['sub'] then
				node[v]['sub'] = {}
			end
			node = node[v]['sub']
		end
		if not node[n] then node[n] = {} end
		
		if name == 'list' and string.sub(value, 1, NAVBOX_CHILD_INDICATOR_LENGTH) == NAVBOX_CHILD_INDICATOR then
			-- it is from {{navbox|child| ... }}
			node[n]['sub'] = mw.text.jsonDecode(string.sub(value, NAVBOX_CHILD_INDICATOR_LENGTH+1))
		else
			node[n][name] = {
				content = value,
				class= h.mergeAttrs(args[key..'_class'], defaults[name..'Class']),
				style = h.mergeAttrs(args[key..'_style'], defaults[name..'Style'])
			}
		end
	end
	for k,v in pairs(args) do
		check(k, v)
	end
	return tree
end

function h.render(data)
	-- handle {{navbox|child|...}} syntax
	if type(data) == 'string' then
		return data
	end

	-----  normal case -----
	
	local out = mw.html.create()
	
	-- build navbox container
	local navbox = out:tag('div')
		:attr('role', 'navigation'):attr('aria-label', 'Navbox')
		:addClass(CLASS_PREFIX..'navbox')
		:addClass(data.class)
		:addClass(data.striped)
		:addClass(data.state)
		:cssText(data.style)

	--title bar
	if data.title then
		local titlebar = navbox:tag('div'):addClass(CLASS_PREFIX..'title')
		titlebar:tag('div'):addClass('mw-collapsible-toggle-placeholder')
		if data.metaLinks then
			titlebar:node(h.renderMetaLinks(data.metaLinks))
		end
		if data.title then
			titlebar:addClass(data.title.class):tag('div')
			:addClass(CLASS_PREFIX..'title-text')
			:addClass(data.title.class)
			:cssText(data.title.style)
			:wikitext(data.title.content)
		end
	end

	--above
	if data.above then
		navbox:tag('div')
		:addClass(CLASS_PREFIX..'above mw-collapsible-content')
		:addClass(data.above.class)
		:cssText(data.above.style)
		:wikitext(data.above.content)
		:attr('id', (not data.title) and mw.uri.anchorEncode(data.above.content) or nil) -- id for aria-labelledby attribute, if no title
	end
	
	-- sections
	for i,sect in ipairs(data.sections) do
		--section box
		local section = navbox:tag('div')
			:addClass(CLASS_PREFIX..'section mw-collapsible-content')
			:addClass(sect.class)
			:addClass(sect.state)
			:cssText(sect.style)
		-- section header
		if sect.header then
			section:tag('div')
			:addClass(CLASS_PREFIX..'header')
			:addClass(sect.header.class)
			:cssText(sect.header.style)
			:tag('div'):addClass('mw-collapsible-toggle-placeholder'):done()
			:tag('div'):addClass(CLASS_PREFIX..'header-text'):wikitext(sect.header.content)
		end
		-- above:
		if sect.above then
			section:tag('div')
			:addClass(CLASS_PREFIX..'above mw-collapsible-content')
			:addClass(sect.above.class)
			:cssText(sect.above.style)
			:wikitext(sect.above.content)
		end
		-- body: groups&lists
		local box = section:tag('div'):addClass(CLASS_PREFIX..'section-body mw-collapsible-content')
		h.renderBody(sect.body, box, 0, true) -- reset even status each section
		-- below:
		if sect.below then
			section:tag('div')
			:addClass(CLASS_PREFIX..'below mw-collapsible-content')
			:addClass(sect.below.class)
			:cssText(sect.below.style)
			:wikitext(sect.below.content)
		end
	end
	-- Insert a blank section for completely empty navbox to ensure it behaves correctly when collapsed.
	if #data.sections == 0 and not data.above and not data.below then 
		navbox:tag('div'):addClass(CLASS_PREFIX..'section mw-collapsible-content')
	end

	--below
	if data.below then
		navbox:tag('div')
		:addClass(CLASS_PREFIX..'below mw-collapsible-content')
		:addClass(data.below.class)
		:cssText(data.below.style)
		:wikitext(data.below.content)
	end

	return out
end

function h.renderMetaLinks(info)
	local title = mw.title.new(mw.text.trim(info.template), 'Template')
	if not title then
		error('Invalid title ' .. info.template)
	end
	
	local msg = mw.message.new(config.editlink_hover_message_key)
	local hoverText = msg:exists() and msg:plain() or 'View or edit this template'
	
	return mw.html.create('span'):addClass(CLASS_PREFIX..'meta')
		:tag('span'):addClass('nv nv-view')
			:wikitext('[['..title.fullText..'|')
			:tag('span'):wikitext(hoverText):attr('title', hoverText):done()
			:wikitext(']]')
		:done()
end

function h.renderBody(info, box, level, even)
	local count = 0
	for _,v in h.orderedPairs(info) do
		if v.group or v.list or v.sub then
			count = count + 1
			-- row container
			local row = box:tag('div'):addClass(CLASS_PREFIX..'row')
			-- group cell
			if v.group or (v.sub and level > 0 and not v.list) then
				local groupCell = row:tag('div')
					:addClass(CLASS_PREFIX..'group level-'..level)
					:addClass((level > 0) and CLASS_PREFIX..'subgroup' or nil)
				local groupContentWrap = groupCell:tag('div'):addClass(CLASS_PREFIX..'wrap')
				if v.group then
					groupCell:addClass(v.group.class):cssText(v.group.style)
					groupContentWrap:wikitext(v.group.content)
				else
					groupCell:addClass('empty')
					row:addClass('empty-group-list')
				end
			else
				row:addClass('empty-group')
			end
			-- list cell
			local listCell = row:tag('div'):addClass(CLASS_PREFIX..'listbox')
			if not v.list and not v.sub then
				listCell:addClass('empty')
				row:addClass('empty-list')
			end
			if v.list or (v.group and not v.sub) then
				--listCell:node(h.renderList(v['list'] or '', k, level, args))
				even = not even -- flip even/odd status
				local cell = listCell:tag('div')
				:addClass(CLASS_PREFIX..'wrap')
				:addClass(even and CLASS_PREFIX..'even' or CLASS_PREFIX..'odd')
				if v.list then
					cell:addClass(v.list.class):cssText(v.list.style)
					:tag('div'):addClass(CLASS_PREFIX..'list'):wikitext(v.list.content)
				end
			end
			if v.sub then
				local sublistBox = listCell:tag('div'):addClass(CLASS_PREFIX..'sublist level-'..level)
				even = h.renderBody(v.sub, sublistBox, level+1, even)
			end
		end
	end
	if count > 0 then
		box:css('--count', count) -- for flex-grow
	end
	return even
end

-- pairs, but sort the keys alphabetically
function h.orderedPairs(t, f)
	local a = {}
	for n in pairs(t) do table.insert(a, n) end
	table.sort(a, f)
	local i = 0      -- iterator variable
	local iter = function ()   -- iterator function
		i = i + 1
		if a[i] == nil then return nil
		else return a[i], t[a[i]]
		end
	end
	return iter
end

-- For cascading parameters, such as style or class, they are merged in exact order (from general to specific). 
-- Any parameter starting with multiple hyphens(minus signs) will terminate the cascade.
-- An example:
-- For group_1.1, its style is affected by parameters |group_1.1_style=... , |subgroup_level_1_style=... , and |subgroup_style=... .
-- If we have |group_1.1_style= color:red; |subgroup_level_1_style= font-weight: bold; and |subgroup_style= color: green; ,
-- the style of group_1.1 will be style="color:green; font-weight: bold; color: red;" ;
-- if we have |group_1.1_style= -- color:red; |subgroup_level_1_style= font-weight: bold; and |subgroup_style= color: green; ,
-- the style of group_1.1 will be style="color: red;" only, and the cascade is no longer performed for |subgroup_level_1_style and |subgroup_style.
function h.mergeAttrs(...)
	local trim = mw.text.trim
	local s = ''
	for i=1, select('#', ...) do
		local v = trim(select(i, ...) or '')
		local str = string.match(v, '^%-%-+(.*)$')
		if str then
			s = trim(str..' '..s)
			break
		else
			s = trim(v..' '..s)
		end
	end
	if s == '' then s = nil end
	return s
end

function h.runHook(key, ...)
	if hooks[key] then
		hooks[key](...)
	end
end

-----------------------------------------------
return p