Module:Sidebar: Difference between revisions
fix |
m 44 revisions imported from wikipedia:Module:Sidebar |
||
(10 intermediate revisions by 3 users not shown) | |||
Line 1: | Line 1: | ||
require('strict') | |||
require(' | |||
local cfg = mw.loadData('Module:Sidebar/configuration') | local cfg = mw.loadData('Module:Sidebar/configuration') | ||
Line 47: | Line 44: | ||
--[[ | --[[ | ||
Finds whether a sidebar has a subgroup sidebar. | Finds whether a sidebar has a subgroup sidebar. | ||
]] | ]] | ||
local function hasSubgroup(s) | local function hasSubgroup(s) | ||
if mw.ustring.find(s, cfg.i18n.pattern.subgroup | if mw.ustring.find(s, cfg.i18n.pattern.subgroup) then | ||
return true | return true | ||
else | else | ||
return false | return false | ||
end | end | ||
end | |||
local function has_navbar(navbar_mode, sidebar_name) | |||
return navbar_mode ~= cfg.i18n.navbar_none and | |||
navbar_mode ~= cfg.i18n.navbar_off and | |||
( | |||
sidebar_name or | |||
mw.getCurrentFrame():getParent():getTitle():gsub(cfg.i18n.pattern.sandbox, '') ~= | |||
cfg.i18n.title_not_to_add_navbar | |||
) | |||
end | |||
local function has_list_class(args, htmlclass) | |||
local patterns = { | |||
'^' .. htmlclass .. '$', | |||
'%s' .. htmlclass .. '$', | |||
'^' .. htmlclass .. '%s', | |||
'%s' .. htmlclass .. '%s' | |||
} | |||
for arg, value in pairs(args) do | |||
if type(arg) == 'string' and mw.ustring.find(arg, 'class') then | |||
for _, pattern in ipairs(patterns) do | |||
if mw.ustring.find(args[arg] or '', pattern) then | |||
return true | |||
end | |||
end | |||
end | |||
end | |||
return false | |||
end | |||
-- there are a lot of list classes in the wild, so we add their TemplateStyles | |||
local function add_list_styles(args) | |||
local frame = mw.getCurrentFrame() | |||
local function add_list_templatestyles(htmlclass, templatestyles) | |||
if has_list_class(args, htmlclass) then | |||
return frame:extensionTag{ | |||
name = 'templatestyles', args = { src = templatestyles } | |||
} | |||
else | |||
return '' | |||
end | |||
end | |||
local plainlist_styles = add_list_templatestyles('plainlist', cfg.i18n.plainlist_templatestyles) | |||
local hlist_styles = add_list_templatestyles('hlist', cfg.i18n.hlist_templatestyles) | |||
-- a second workaround for [[phab:T303378]] | |||
-- when that issue is fixed, we can actually use has_navbar not to emit the | |||
-- tag here if we want | |||
if has_navbar(args.navbar, args.name) and hlist_styles == '' then | |||
hlist_styles = frame:extensionTag{ | |||
name = 'templatestyles', args = { src = cfg.i18n.hlist_templatestyles} | |||
} | |||
end | |||
-- hlist -> plainlist is best-effort to preserve old Common.css ordering. [hlist_note] | |||
return hlist_styles .. plainlist_styles | |||
end | |||
-- work around [[phab:T303378]] | |||
-- for each arg: find all the templatestyles strip markers, insert them into a | |||
-- table. then remove all templatestyles markers from the arg | |||
local function move_hiding_templatestyles(args) | |||
local gfind = string.gfind | |||
local gsub = string.gsub | |||
local templatestyles_markers = {} | |||
local strip_marker_pattern = '(\127[^\127]*UNIQ%-%-templatestyles%-%x+%-QINU[^\127]*\127)' | |||
for k, arg in pairs(args) do | |||
for marker in gfind(arg, strip_marker_pattern) do | |||
table.insert(templatestyles_markers, marker) | |||
end | |||
args[k] = gsub(arg, strip_marker_pattern, '') | |||
end | |||
return templatestyles_markers | |||
end | end | ||
Line 68: | Line 138: | ||
args = getArgs(frame) | args = getArgs(frame) | ||
end | end | ||
local hiding_templatestyles = table.concat(move_hiding_templatestyles(args)) | |||
local root = mw.html.create() | local root = mw.html.create() | ||
local child = args.child and mw.text.trim(args.child) == cfg.i18n.child_yes | local child = args.child and mw.text.trim(args.child) == cfg.i18n.child_yes | ||
Line 77: | Line 148: | ||
-- force collapsibleclass to be sidebar-collapse otherwise output nothing | -- force collapsibleclass to be sidebar-collapse otherwise output nothing | ||
:addClass(collapsibleClass == cfg.i18n.class.collapse and cfg.i18n.class.collapse or nil) | :addClass(collapsibleClass == cfg.i18n.class.collapse and cfg.i18n.class.collapse or nil) | ||
:addClass('nomobile') | :addClass('nomobile') | ||
:addClass(args.float == cfg.i18n.float_none and cfg.i18n.class.float_none or nil) | :addClass(args.float == cfg.i18n.float_none and cfg.i18n.class.float_none or nil) | ||
:addClass(args.float == cfg.i18n.float_left and cfg.i18n.class.float_left or nil) | :addClass(args.float == cfg.i18n.float_left and cfg.i18n.class.float_left or nil) | ||
:addClass(args.wraplinks | :addClass(args.wraplinks ~= cfg.i18n.wrap_true and cfg.i18n.class.wraplinks or nil) | ||
:addClass(args.bodyclass or args.class) | :addClass(args.bodyclass or args.class) | ||
:css('width', args.width or nil) | :css('width', args.width or nil) | ||
Line 239: | Line 309: | ||
end | end | ||
if not child | if not child and has_navbar(args.navbar, args.name) then | ||
root | |||
:tag('tr') | |||
:tag('td') | |||
:addClass(cfg.i18n.class.navbar) | |||
:cssText(args.navbarstyle) | |||
:wikitext(require('Module:Navbar')._navbar{ | |||
args.name, | |||
mini = 1, | |||
fontstyle = args.navbarfontstyle | |||
}) | |||
end | end | ||
local | local base_templatestyles = frame:extensionTag{ | ||
name = 'templatestyles', args = { src = cfg.i18n.templatestyles } | name = 'templatestyles', args = { src = cfg.i18n.templatestyles } | ||
} | } | ||
Line 262: | Line 328: | ||
local templatestyles = '' | local templatestyles = '' | ||
if args['templatestyles'] and args['templatestyles'] ~= '' then | if args['templatestyles'] and args['templatestyles'] ~= '' then | ||
templatestyles = | templatestyles = frame:extensionTag{ | ||
name = 'templatestyles', args = { src = args['templatestyles'] } | name = 'templatestyles', args = { src = args['templatestyles'] } | ||
} | } | ||
Line 269: | Line 335: | ||
local child_templatestyles = '' | local child_templatestyles = '' | ||
if args['child templatestyles'] and args['child templatestyles'] ~= '' then | if args['child templatestyles'] and args['child templatestyles'] ~= '' then | ||
child_templatestyles = | child_templatestyles = frame:extensionTag{ | ||
name = 'templatestyles', args = { src = args['child templatestyles'] } | name = 'templatestyles', args = { src = args['child templatestyles'] } | ||
} | } | ||
Line 276: | Line 342: | ||
local grandchild_templatestyles = '' | local grandchild_templatestyles = '' | ||
if args['grandchild templatestyles'] and args['grandchild templatestyles'] ~= '' then | if args['grandchild templatestyles'] and args['grandchild templatestyles'] ~= '' then | ||
grandchild_templatestyles = | grandchild_templatestyles = frame:extensionTag{ | ||
name = 'templatestyles', args = { src = args['grandchild templatestyles'] } | name = 'templatestyles', args = { src = args['grandchild templatestyles'] } | ||
} | } | ||
Line 282: | Line 348: | ||
return table.concat({ | return table.concat({ | ||
add_list_styles(args), -- see [hlist_note] above about ordering | |||
base_templatestyles, | |||
templatestyles, | templatestyles, | ||
child_templatestyles, | child_templatestyles, | ||
grandchild_templatestyles, | grandchild_templatestyles, | ||
hiding_templatestyles, | |||
tostring(root), | tostring(root), | ||
(child and cfg.i18n.category.child or ''), | (child and cfg.i18n.category.child or ''), | ||
categorizeTemplatesWithInlineStyles(args) | categorizeTemplatesWithInlineStyles(args) | ||
}) | }) | ||
end | |||
local function list_title(args, is_centered_list_titles, num) | |||
local title_text = trimAndAddAutomaticNewline(args['list' .. num .. 'title'] | |||
or cfg.i18n.default_list_title) | |||
local title | |||
if is_centered_list_titles then | |||
-- collapsible can be finicky, so provide some CSS/HTML to support | |||
title = mw.html.create('div') | |||
:addClass(cfg.i18n.class.list_title_centered) | |||
:wikitext(title_text) | |||
else | |||
title = mw.html.create() | |||
:wikitext(title_text) | |||
end | |||
local title_container = mw.html.create('div') | |||
:addClass(cfg.i18n.class.list_title) | |||
-- don't /need/ a listnumtitleclass because you can do | |||
-- .templateclass .listnumclass .sidebar-list-title | |||
:addClass(args.listtitleclass) | |||
:cssText(args.basestyle) | |||
:cssText(args.listtitlestyle) | |||
:cssText('color: var(--color-base)') | |||
:cssText(args['list' .. num .. 'titlestyle']) | |||
:node(title) | |||
:done() | |||
return title_container | |||
end | end | ||
Line 306: | Line 405: | ||
local contentArgs = {} | local contentArgs = {} | ||
local is_centered_list_titles = false | |||
if args['centered list titles'] and args['centered list titles'] ~= '' then | |||
is_centered_list_titles = true | |||
end | |||
for k, v in pairs(args) do | for k, v in pairs(args) do | ||
Line 320: | Line 424: | ||
:cssText(args.listframestyle) | :cssText(args.listframestyle) | ||
:cssText(args['list' .. num .. 'framestyle']) | :cssText(args['list' .. num .. 'framestyle']) | ||
: | :node(list_title(args, is_centered_list_titles, num)) | ||
:tag('div') | :tag('div') | ||
:addClass(cfg.i18n.class.list_content) | :addClass(cfg.i18n.class.list_content) |