Module:Vertical header/sandbox: Difference between revisions
Appearance
Content deleted Content added
copy from Module:Vertical header |
rewrite this to use template styles |
||
Line 1: | Line 1: | ||
local p = {} |
local p = {} |
||
function |
local function getRowCount(text) |
||
local |
local _, count = text:gsub("<[bB][rR] */? *>", "%0") |
||
return count + 1 |
|||
local sortPadding = frame.args.sortPadding |
|||
end |
|||
local vertAlign = frame.args.vertAlign |
|||
local maxWidth = frame.args.maxWidth |
|||
local function formatAttribute(options) |
|||
local noBold = frame.args.noBold |
|||
if #options.attrs == 0 then |
|||
local style = frame.args.style:gsub("\"", """) |
|||
return nil |
|||
local cellStyle = frame.args.cellstyle:gsub("\"", """) |
|||
local wikiText = "class = \"nowrap" |
|||
local normalAlign = "" |
|||
-- local stupidIEAlign = "" |
|||
local rows = 1 |
|||
local width = 0 |
|||
if maxWidth ~= "" then |
|||
width = maxWidth |
|||
else |
|||
for eachMatch in text:gmatch("<[bB][rR] */? *>") do |
|||
rows = rows + 1 |
|||
end |
|||
width = rows * 0.875 |
|||
width = width .. "em" |
|||
end |
|||
if sortPadding == "" then |
|||
wikiText = wikiText .. " unsortable" |
|||
end |
end |
||
local separator = options.separator or "" |
|||
wikiText = wikiText .. "\" style=\"line-height:99%;vertical-align:" .. vertAlign .. ";padding:" |
|||
return string.format( |
|||
if sortPadding == "" then |
|||
'%s="%s"', |
|||
wikiText = wikiText .. ".4em" |
|||
options.attrName, |
|||
table.concat(options.attrs, separator) |
|||
) |
|||
end |
|||
function p._cell(args) |
|||
local text = args[1] |
|||
local sortPadding = args.stp |
|||
local vertAlign = args.va |
|||
local manualWidth = args.mw |
|||
local noBold = args.nb |
|||
local extraThStyles = args.cellstyle and args.cellstyle:gsub("\"", """) |
|||
local extraDivStyles = args.style and args.style:gsub("\"", """) |
|||
local thClasses = {"vertical-header", "nowrap"} |
|||
local thStyles = {} |
|||
local divClasses = {} |
|||
if sortPadding then |
|||
table.insert(thClasses, "vertical-header-sortable") |
|||
else |
else |
||
table.insert(thClasses, "unsortable") |
|||
wikiText = wikiText .. "21px" |
|||
end |
end |
||
wikiText = wikiText .. " .4em .2em;background-position:50% .4em !important;" |
|||
wikiText = wikiText .. "min-width:" .. width .. ";max-width:" .. width .. ";width:" .. width .. ";overflow:hidden;" .. cellStyle .. "\"" |
|||
wikiText = wikiText .. " | <div style=\"" .. frame:preprocess("{{writing-mode|v1}}{{Transform-rotate|180}}") .. "-ms-transform: none \ ;padding-left:1px;text-align:" |
|||
if vertAlign == "top" then |
if vertAlign == "top" then |
||
table.insert(thClasses, "vertical-header-align-top") |
|||
normalAlign = "right" |
|||
-- stupidIEAlign = "left" |
|||
elseif vertAlign == "middle" then |
elseif vertAlign == "middle" then |
||
table.insert(thClasses, "vertical-header-align-middle") |
|||
normalAlign = "center" |
|||
-- stupidIEAlign = "center" |
|||
else |
|||
normalAlign = "left" |
|||
-- stupidIEAlign = "right" |
|||
end |
end |
||
wikiText = wikiText .. normalAlign .. ";" -- text-align:" .. stupidIEAlign .. " \ ;" |
|||
if manualWidth then |
|||
wikiText = wikiText .. style .. "\">" |
|||
table.insert(thStyles, "width:" .. manualWidth) |
|||
if noBold == "" then |
|||
wikiText = wikiText .. text |
|||
else |
else |
||
local rowCount = getRowCount(text) |
|||
wikiText = wikiText .. frame:preprocess("{{nobold|1=" .. text .. "}}") |
|||
-- The styles for 1 row are in the default vertical-header class. For |
|||
-- 2, 3, or 4 rows, we add special classes to get the correct width. |
|||
-- In the rare event that there are more than 4 rows, use inline styles. |
|||
if rowCount >= 2 and rowCount <= 4 then |
|||
table.insert(tableClasses, string.format("vertical-header-%drows", rowCount)) |
|||
elseif rowCount > 4 then |
|||
table.insert(tableStyles, string.format("width:%.3fem", rowCount * 0.875)) |
|||
end |
|||
end |
end |
||
wikiText = wikiText .. "</div>" |
|||
if noBold then |
|||
return wikiText |
|||
table.insert(divClasses, "vertical-header-nobold") |
|||
end |
|||
table.insert(thStyles, extraThStyles) |
|||
result = 'class="%"' |
|||
local div = mw.html.create("div") |
|||
for _, class in ipairs(divClasses) do |
|||
div:addClass(class) |
|||
end |
|||
div:cssText(extraDivStyles) |
|||
div:wikitext(text) |
|||
thAttributes = {} |
|||
table.insert(thAttributes, formatAttribute{attrName = "class", attrs = thClasses, separator = " "}) |
|||
table.insert(thAttributes, formatAttribute{attrName = "style", attrs = thStyles}) |
|||
return string.format("%s | %s", table.concat(thAttributes, " "), tostring(div)) |
|||
end |
|||
function p.cell(frame) |
|||
local args = require("Module:Arguments").getArgs(frame, { |
|||
wrapper="Template:Vertical header" |
|||
}) |
|||
return p._cell(args) |
|||
end |
end |
||
Revision as of 07:07, 2 August 2020
![]() | This is the module sandbox page for Module:Vertical header (diff). |
![]() | This module uses TemplateStyles: |
Implements {{Vertical header}}
local p = {}
local function getRowCount(text)
local _, count = text:gsub("<[bB][rR] */? *>", "%0")
return count + 1
end
local function formatAttribute(options)
if #options.attrs == 0 then
return nil
end
local separator = options.separator or ""
return string.format(
'%s="%s"',
options.attrName,
table.concat(options.attrs, separator)
)
end
function p._cell(args)
local text = args[1]
local sortPadding = args.stp
local vertAlign = args.va
local manualWidth = args.mw
local noBold = args.nb
local extraThStyles = args.cellstyle and args.cellstyle:gsub("\"", """)
local extraDivStyles = args.style and args.style:gsub("\"", """)
local thClasses = {"vertical-header", "nowrap"}
local thStyles = {}
local divClasses = {}
if sortPadding then
table.insert(thClasses, "vertical-header-sortable")
else
table.insert(thClasses, "unsortable")
end
if vertAlign == "top" then
table.insert(thClasses, "vertical-header-align-top")
elseif vertAlign == "middle" then
table.insert(thClasses, "vertical-header-align-middle")
end
if manualWidth then
table.insert(thStyles, "width:" .. manualWidth)
else
local rowCount = getRowCount(text)
-- The styles for 1 row are in the default vertical-header class. For
-- 2, 3, or 4 rows, we add special classes to get the correct width.
-- In the rare event that there are more than 4 rows, use inline styles.
if rowCount >= 2 and rowCount <= 4 then
table.insert(tableClasses, string.format("vertical-header-%drows", rowCount))
elseif rowCount > 4 then
table.insert(tableStyles, string.format("width:%.3fem", rowCount * 0.875))
end
end
if noBold then
table.insert(divClasses, "vertical-header-nobold")
end
table.insert(thStyles, extraThStyles)
result = 'class="%"'
local div = mw.html.create("div")
for _, class in ipairs(divClasses) do
div:addClass(class)
end
div:cssText(extraDivStyles)
div:wikitext(text)
thAttributes = {}
table.insert(thAttributes, formatAttribute{attrName = "class", attrs = thClasses, separator = " "})
table.insert(thAttributes, formatAttribute{attrName = "style", attrs = thStyles})
return string.format("%s | %s", table.concat(thAttributes, " "), tostring(div))
end
function p.cell(frame)
local args = require("Module:Arguments").getArgs(frame, {
wrapper="Template:Vertical header"
})
return p._cell(args)
end
return p