Module:NavboxUpgraded: Difference between revisions
EnWikiAdmin (talk | contribs) No edit summary |
EnWikiAdmin (talk | contribs) No edit summary |
||
(13 intermediate revisions by the same user not shown) | |||
Line 1: | Line 1: | ||
-- Module:NavboxUpgraded | -- Module:NavboxUpgraded | ||
require('strict') | |||
local p = {} | local p = {} | ||
local | local cfg = mw.loadData('Module:Navbox/configuration') | ||
local | local inArray = require("Module:TableTools").inArray | ||
local getArgs -- lazily initialized | |||
local format = string.format | |||
Line 44: | Line 45: | ||
container:wikitext(nestedSubgroup) | container:wikitext(nestedSubgroup) | ||
else | else | ||
-- | -- Process the list into proper <li> elements | ||
local | local listHtml = mw.html.create('ul') | ||
:addClass('navbox-list') | :addClass('navbox-list') | ||
:css('padding-left', '1.5em') | :css('padding-left', '1.5em') | ||
:css('list-style', 'disc') | :css('list-style', 'disc') | ||
for item in sublist:gmatch("[^\n]+") do | |||
for item in sublist:gmatch("%*%s* | -- Remove leading '*' or similar list markers | ||
if | local cleanItem = item:gsub("^%s*[%*#]+%s*", "") | ||
if cleanItem ~= '' then | |||
listHtml:tag('li') | |||
:wikitext(cleanItem) | |||
end | end | ||
end | end | ||
container:node(listHtml) | |||
end | end | ||
end | end | ||
Line 63: | Line 68: | ||
return tostring(html) | return tostring(html) | ||
end | end | ||
Line 84: | Line 87: | ||
local titleRow = navbox:tag('tr') | local titleRow = navbox:tag('tr') | ||
local titleCell = titleRow:tag('th') | local titleCell = titleRow:tag('th') | ||
titleCell | |||
:attr('colspan', '2') | :attr('colspan', '2') | ||
:addClass('navbox-title') | :addClass('navbox-title') | ||
Line 95: | Line 99: | ||
local aboveRow = navbox:tag('tr') | local aboveRow = navbox:tag('tr') | ||
local aboveCell = aboveRow:tag('td') | local aboveCell = aboveRow:tag('td') | ||
aboveCell | |||
:attr('colspan', '2') | :attr('colspan', '2') | ||
:addClass('navbox-abovebelow') | :addClass('navbox-abovebelow') | ||
Line 121: | Line 126: | ||
if group then | if group then | ||
local groupCell = row:tag('th') | local groupCell = row:tag('th') | ||
groupCell | |||
:addClass('navbox-group') | :addClass('navbox-group') | ||
:css('text-align', 'right') | :css('text-align', 'right') | ||
Line 151: | Line 157: | ||
listCell:wikitext(subgroupContent) | listCell:wikitext(subgroupContent) | ||
else | else | ||
listCell:tag('ul') | |||
:addClass('navbox-list') | :addClass('navbox-list') | ||
:css('list-style', 'disc') | :css('list-style', 'disc') | ||
:css('padding-left', '1.5em') | :css('padding-left', '1.5em') | ||
:wikitext(list) | |||
end | end | ||
end | end | ||
Line 172: | Line 171: | ||
local belowRow = navbox:tag('tr') | local belowRow = navbox:tag('tr') | ||
local belowCell = belowRow:tag('td') | local belowCell = belowRow:tag('td') | ||
belowCell | |||
:attr('colspan', '2') | :attr('colspan', '2') | ||
:addClass('navbox-abovebelow') | :addClass('navbox-abovebelow') | ||
Line 181: | Line 181: | ||
return tostring(navbox) | return tostring(navbox) | ||
end | end | ||
Line 254: | Line 253: | ||
return tostring(navbox) | return tostring(navbox) | ||
end | end | ||
local function add_navbox_styles() | |||
local html = mw.html.create() | |||
-- Helper function to add a stylesheet link | |||
local function add_stylesheet(path) | |||
html:tag('link') | |||
:attr('rel', 'stylesheet') | |||
:attr('href', path) | |||
:attr('type', 'text/css') | |||
end | |||
-- Assuming cfg.templatestyles contains space-separated CSS file paths | |||
if type(cfg.templatestyles) == 'string' and cfg.templatestyles ~= '' then | |||
for stylePath in cfg.templatestyles:gmatch("[^%s]+") do | |||
add_stylesheet(stylePath) | |||
end | |||
end | |||
if type(cfg.hlist_templatestyles) == 'string' and cfg.hlist_templatestyles ~= '' then | |||
for stylePath in cfg.hlist_templatestyles:gmatch("[^%s]+") do | |||
add_stylesheet(stylePath) | |||
end | |||
end | |||
if type(cfg.plainlist_templatestyles) == 'string' and cfg.plainlist_templatestyles ~= '' then | |||
for stylePath in cfg.plainlist_templatestyles:gmatch("[^%s]+") do | |||
add_stylesheet(stylePath) | |||
end | |||
end | |||
return html:done() | |||
end | |||
-- Main function | -- Main function | ||
function p.navbox(frame) | function p.navbox(frame) | ||
if not getArgs then | |||
getArgs = require('Module:Arguments').getArgs | |||
end | |||
local args = getArgs(frame) | local args = getArgs(frame) | ||
local desktopNavbox = p._navboxDesktop(args) | local desktopNavbox = p._navboxDesktop(args) | ||
local mobileNavbox = p._navboxMobile(args) | local mobileNavbox = p._navboxMobile(args) | ||
local res = mw.html.create() | local res = mw.html.create('div') | ||
res:node(add_navbox_styles()) -- Add styles at the beginning of the div | |||
res | res | ||
:tag('div') | :tag('div') | ||
:addClass('nomobile') | :addClass('nomobile') | ||
:wikitext(desktopNavbox) | :wikitext(desktopNavbox) -- Do not include `add_navbox_styles()` here | ||
:done() | :done() | ||
:tag('div') | :tag('div') | ||
:addClass('onlymobile') | :addClass('onlymobile') | ||
:wikitext(mobileNavbox) | :wikitext(mobileNavbox) -- Do not include `add_navbox_styles()` here | ||
:done() | :done() | ||
return tostring(res) | return tostring(res) | ||
end | end | ||
return p | return p |