Module:Logoform: Difference between revisions
From Neodyland Wiki
More actions
m Reverted edits by 197.120.162.43 (talk) to last revision by Sarang |
m optimize |
||
| Line 1: | Line 1: | ||
-- Module for: Formatting Logo | |||
local p = {} | |||
function p.format (frame) | local concat = table.concat | ||
local gpar = frame.args | local split = mw.text.split | ||
local | local trim = mw.text.trim | ||
local upper = mw.ustring.upper | |||
local | local sub = mw.ustring.sub | ||
if | function p.ucfirst(s) | ||
return upper(sub(s, 1, 1)) .. sub(s, 2) | |||
end | |||
function p.format(frame) | |||
local gpar = frame.args -- global | |||
local fname = ucfirst(trim(gpar[1] or '')) | |||
if fname == '' then return '' end -- if file name is "unspecified" | |||
local size = trim(gpar[2] or '') -- or '0' for "unspecified" | |||
if size:sub(-2) == 'px' then size = size:sub(1, -3) end -- discard 'px' unit if present | |||
size = split(size, 'x') -- {width, height} or {height} | |||
size = size[2] and size[2] or size[1] -- keep only the specified height (in px) | |||
local style = trim(gpar[3] or '') -- or '-' for "unspecified" | |||
if style == '-' then style = '' end | |||
local title = trim(gpar[4] or '')-- or '' for "unspecified" | |||
if title == '' then title = fname--[[default title]] end | |||
local what = 'F' -- assume displaying a file | |||
if fname:find('.', 1, true--[[plain, not regexp]]) then | |||
if size == '' or size == '0' then | |||
size = '36' -- default file size (in px) | |||
end | end | ||
elseif fname:sub(1, 2) == 'U+' or fname:sub(1, 2) == '&#' then | |||
if size == '0' then | what = 'U' -- displaying an Unicode code point instead of a file | ||
size = ' | if size == '' or size == '0' then | ||
size = '15' -- Unicode default font-size (in px) | |||
end | end | ||
title = trim(title | |||
:gsub('U+([0-9A-Fa-f]+) ?', function(x) return 'U+' .. upper(x) .. ' ' end) | |||
:gsub('&#([0-9A-Fa-f]+);?', function(x) return 'U+' .. upper(x) .. ' ' end)) | |||
fname = fname | |||
:gsub('&#([0-9A-Fa-f]+);?', function(x) return '&#x' .. upper(x) .. ';' end) | |||
:gsub('U+([0-9A-Fa-f]+) ?', function(x) return '&#x' .. upper(x) .. ';' end) | |||
end | end | ||
if | if what == 'F' then -- displaying a file | ||
if style ~= '' then | |||
else | style = '|' .. style | ||
return '<span style="font-size:' | end | ||
if title ~= '' then | |||
title = '|' .. title | |||
:gsub('[', '[', true--[[plain]]) | |||
:gsub(']', ']', true--[[plain]]) | |||
:gsub('|', '{', true--[[plain]]) | |||
end | |||
return concat{ '[[File:', fname, '|', size, 'x22px', style--[[allow linking]], title, ']]' } | |||
else -- displaying an Unicode code point | |||
if title ~= '' then | |||
title = concat{ ' title="', title:gsub('"', '"', true--[[plain]]), '"' } | |||
end | |||
return concat{ '<span style="font-size:', size, 'px;line-height:22px;vertical-align:middle', style, '"', title, '>', fname, '</span>' } | |||
end | end | ||
end -- function format | end -- function format | ||
return p | return p | ||
Revision as of 18:47, 22 May 2024
This documentation is transcluded from Module:Logoform/doc.
-- Module for: Formatting Logo
local p = {}
local concat = table.concat
local split = mw.text.split
local trim = mw.text.trim
local upper = mw.ustring.upper
local sub = mw.ustring.sub
function p.ucfirst(s)
return upper(sub(s, 1, 1)) .. sub(s, 2)
end
function p.format(frame)
local gpar = frame.args -- global
local fname = ucfirst(trim(gpar[1] or ''))
if fname == '' then return '' end -- if file name is "unspecified"
local size = trim(gpar[2] or '') -- or '0' for "unspecified"
if size:sub(-2) == 'px' then size = size:sub(1, -3) end -- discard 'px' unit if present
size = split(size, 'x') -- {width, height} or {height}
size = size[2] and size[2] or size[1] -- keep only the specified height (in px)
local style = trim(gpar[3] or '') -- or '-' for "unspecified"
if style == '-' then style = '' end
local title = trim(gpar[4] or '')-- or '' for "unspecified"
if title == '' then title = fname--[[default title]] end
local what = 'F' -- assume displaying a file
if fname:find('.', 1, true--[[plain, not regexp]]) then
if size == '' or size == '0' then
size = '36' -- default file size (in px)
end
elseif fname:sub(1, 2) == 'U+' or fname:sub(1, 2) == '&#' then
what = 'U' -- displaying an Unicode code point instead of a file
if size == '' or size == '0' then
size = '15' -- Unicode default font-size (in px)
end
title = trim(title
:gsub('U+([0-9A-Fa-f]+) ?', function(x) return 'U+' .. upper(x) .. ' ' end)
:gsub('&#([0-9A-Fa-f]+);?', function(x) return 'U+' .. upper(x) .. ' ' end))
fname = fname
:gsub('&#([0-9A-Fa-f]+);?', function(x) return '&#x' .. upper(x) .. ';' end)
:gsub('U+([0-9A-Fa-f]+) ?', function(x) return '&#x' .. upper(x) .. ';' end)
end
if what == 'F' then -- displaying a file
if style ~= '' then
style = '|' .. style
end
if title ~= '' then
title = '|' .. title
:gsub('[', '[', true--[[plain]])
:gsub(']', ']', true--[[plain]])
:gsub('|', '{', true--[[plain]])
end
return concat{ '[[File:', fname, '|', size, 'x22px', style--[[allow linking]], title, ']]' }
else -- displaying an Unicode code point
if title ~= '' then
title = concat{ ' title="', title:gsub('"', '"', true--[[plain]]), '"' }
end
return concat{ '<span style="font-size:', size, 'px;line-height:22px;vertical-align:middle', style, '"', title, '>', fname, '</span>' }
end
end -- function format
return p