Module:Sidebar: Difference between revisions
Appearance
use require('strict') instead of require('Module:No globals') |
find vanishing TemplateStyles in args, find plainlist in class args |
||
Line 1: | Line 1: | ||
require('strict') | require('strict') | ||
local cfg = mw.loadData('Module:Sidebar/configuration') | local cfg = mw.loadData('Module:Sidebar/configuration') | ||
Line 54: | Line 51: | ||
return false | return false | ||
end | end | ||
end | |||
-- do we want a navbar | |||
-- will be wanted later for finding hlist | |||
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 function add_list_templatestyles(htmlclass, templatestyles) | |||
local frame = mw.getCurrentFrame() | |||
if has_list_class(args, htmlclass) then | |||
return frame:extensionTag{ | |||
name = 'templatestyles', args = { src = templatestyles } | |||
} | |||
else | |||
return '' | |||
end | |||
end | |||
-- TODO: get hlist to the point where we can turn this on | |||
-- see [[MediaWiki talk:Common.css/to do#Hlist]] | |||
local plainlist_styles = add_list_templatestyles('plainlist', 'Plainlist/styles.css') | |||
return 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 65: | Line 130: | ||
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 235: | Line 301: | ||
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 | ||
Line 282: | Line 344: | ||
child_templatestyles, | child_templatestyles, | ||
grandchild_templatestyles, | grandchild_templatestyles, | ||
add_list_styles(args), | |||
hiding_templatestyles, | |||
tostring(root), | tostring(root), | ||
(child and cfg.i18n.category.child or ''), | (child and cfg.i18n.category.child or ''), | ||
Line 333: | Line 397: | ||
local contentArgs = {} | local contentArgs = {} | ||
local is_centered_list_titles | local is_centered_list_titles = false | ||
if args['centered list titles'] and args['centered list titles'] ~= '' then | if args['centered list titles'] and args['centered list titles'] ~= '' then | ||
is_centered_list_titles = true | is_centered_list_titles = true | ||
end | end | ||