Module:Logoform

From Wikimedia Commons, the free media repository
Jump to navigation Jump to search
Lua

CodeDiscussionEditHistoryLinksLink countSubpages:DocumentationTestsResultsSandboxLive codeAll modules

global function "format"

is invoked from e.g.Created with/layoutwhen

  • either the name of thelogostarts with U+ or &#
  • and/oraniconsizeisspecified.
  1. analizelogo:when it contains a dot, it cannot be a codepoint
    no further checks are performed whether is is a valid file name, e.g. "U+2861.svg";
    without a dot it is assumed to be a codepoint, as e.g. ⡡
    no further checks are performed whether is is a valid codepoint,
    but the notation U+ is converted to &#x... and the display becomestooltipped
  2. analizeiconsize:when it ismissing,useful defaults are set: 33px for Unicode, 36x22px for files
    when it isspecified,the absence/presence of thepxdimension is verified
  3. either the file, or the codepoint is displayed

With other file names and no iconsize, no invocation occurs

Code

-- Module for: Formatting Logo
localp={}

localconcat=table.concat
localsplit=mw.text.split
localtrim=mw.text.trim
localupper=mw.ustring.upper
localsub=mw.ustring.sub

localfunctionucfirst(s)
returnupper(sub(s,1,1))..sub(s,2)
end

functionp.format(frame)
localgpar=frame.args-- global

localfname=ucfirst(trim(gpar[1]or''))
iffname==''thenreturn''end-- if file name is "unspecified"

localsize=trim(gpar[2]or'')-- or '0' for "unspecified"
ifsize:sub(-2)=='px'thensize=size:sub(1,-3)end-- discard 'px' unit if present
size=split(size,'x')-- {width, height} or {height}
size=size[2]andsize[2]orsize[1]-- keep only the specified height (in px)

localstyle=trim(gpar[3]or'')-- or '-' for "unspecified"
ifstyle=='-'thenstyle=''end

localtitle=trim(gpar[4]or'')-- or '' for "unspecified"
localwhat=split(fname,'.',true--[[plain, not regexp]])--[[detect a filename extension]]
--[[if there's a non-empty part before a supported extension, with 1 to 4 characters: 'jpg', 'jpeg', 'gif', 'png', 'svg', 'webp', 'djvu', 'tif', 'tiff'...]]
if#what>1and#(what[#what])>0and#(what[#what])<5then
iftitle==''then
title=concat(what,'.',1,-2):gsub('_',' ')--[[default title from fname without this extension, underscores replaced by spaces]]
end
what='F'-- displaying a file
ifsize==''orsize=='0'then
size='36'-- default file size (in px)
end
elseiffname:sub(1,2)=='U+'orfname:sub(1,2)=='&#'then
what='U'-- displaying an Unicode code point instead of a file
ifsize==''orsize=='0'then
size='15'-- Unicode default font-size (in px)
end
iftitle==''then
title=fname--[[default title]]
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)..'&#xFE0F;'--[[Emoji-style variation selector 16]]
end

ifwhat=='F'then-- displaying a file
ifstyle~=''then
style='|'..style
end
iftitle~=''then
title='|'..title
:gsub('%[','&#91;')
:gsub('%]','&#93;')
:gsub('%|','&#123;')
end
returnconcat{'[[File:',fname,'|',size,'x22px',style--[[allow linking]],title,']]'}
else-- displaying an Unicode code point
iftitle~=''then
title=concat{' title= "',title:gsub(' "','&#34;'),' "'}
end
returnconcat{'<div style= "display:inline-block;position:relative;font-size:',size,'px;line-height:22px;vertical-align:middle;top:-1px',style,' "',title,'>',fname,'</div>'}
end
end-- function format

returnp