模块:Sprite:修订间差异
无编辑摘要 |
无编辑摘要 标签:手工回退 |
||
(未显示同一用户的12个中间版本) | |||
第1行: | 第1行: | ||
local p = {} | local p = {} | ||
function p.sprite( frame ) | |||
local args = frame:getParent().args | |||
-- Default settings | |||
local default = { | |||
scale = 1, | |||
size = 24, | |||
align = 'text-top' | |||
} | |||
local link = ( args.link or '' ) | |||
if mw.ustring.lower( link ) == 'none' then | |||
link = '' | |||
elseif link ~= '' and not link:find( '//' ) then | |||
link = args.linkprefix or '' .. link | |||
end | |||
local scale = args.scale or default.scale | |||
local height = ( args.height or args.size or default.size ) * scale | |||
local width = ( args.width or args.size or default.size ) * scale | |||
local size = width .. 'x' .. height .. 'px' | |||
local styles = {} | |||
if height ~= default.size then | |||
styles[#styles + 1] = 'height:' .. height .. 'px' | |||
end | |||
if width ~= default.size then | |||
styles[#styles + 1] = 'width:' .. width .. 'px' | |||
end | |||
local name = args.name | |||
local file = name .. '.png' | |||
local altText = file | |||
if link ~= '' then | |||
altText = altText .. ', link to ' .. link | |||
end | |||
local sprite = mw.html.create( 'span' ):addClass( 'sprite-file' ) | |||
local img = '[[File:' .. file .. '|' .. size .. '|link=' .. link .. '|alt=' .. altText .. '|class=pixel-image|' .. ( args.title or '' ) .. ']]' | |||
sprite:node( img ) | |||
local align = args.align or default.align | |||
if align ~= default.align then | |||
styles[#styles + 1] = '--vertical-align:' .. align | |||
end | |||
styles[#styles + 1] = args.css | |||
sprite:cssText( table.concat( styles, ';' ) ) | |||
local root | |||
local spriteText | |||
if args.text then | |||
if not args['wrap'] then | |||
root = mw.html.create( 'span' ):addClass( 'nowrap' ) | |||
end | |||
spriteText = mw.html.create( 'span' ):addClass( 'sprite-text' ):wikitext( args.text ) | |||
if args.title then | |||
spriteText:attr( 'title', args.title ) | |||
end | |||
if link ~= '' then | |||
-- External link | |||
if link:find( '//' ) then | |||
spriteText = '[' .. link .. ' ' .. tostring( spriteText ) .. ']' | |||
else | |||
spriteText = '[[' .. link .. '|' .. tostring( spriteText ) .. ']]' | |||
end | |||
end | |||
end | |||
if not root then | |||
root = mw.html.create( '' ) | |||
end | |||
root:node( sprite ) | |||
if spriteText then | |||
root:node( spriteText ) | |||
end | |||
return tostring( root ) | |||
end | end | ||
return p | return p |
2024年4月20日 (六) 16:47的最新版本
可在模块:Sprite/doc创建此模块的帮助文档
local p = {}
function p.sprite( frame )
local args = frame:getParent().args
-- Default settings
local default = {
scale = 1,
size = 24,
align = 'text-top'
}
local link = ( args.link or '' )
if mw.ustring.lower( link ) == 'none' then
link = ''
elseif link ~= '' and not link:find( '//' ) then
link = args.linkprefix or '' .. link
end
local scale = args.scale or default.scale
local height = ( args.height or args.size or default.size ) * scale
local width = ( args.width or args.size or default.size ) * scale
local size = width .. 'x' .. height .. 'px'
local styles = {}
if height ~= default.size then
styles[#styles + 1] = 'height:' .. height .. 'px'
end
if width ~= default.size then
styles[#styles + 1] = 'width:' .. width .. 'px'
end
local name = args.name
local file = name .. '.png'
local altText = file
if link ~= '' then
altText = altText .. ', link to ' .. link
end
local sprite = mw.html.create( 'span' ):addClass( 'sprite-file' )
local img = '[[File:' .. file .. '|' .. size .. '|link=' .. link .. '|alt=' .. altText .. '|class=pixel-image|' .. ( args.title or '' ) .. ']]'
sprite:node( img )
local align = args.align or default.align
if align ~= default.align then
styles[#styles + 1] = '--vertical-align:' .. align
end
styles[#styles + 1] = args.css
sprite:cssText( table.concat( styles, ';' ) )
local root
local spriteText
if args.text then
if not args['wrap'] then
root = mw.html.create( 'span' ):addClass( 'nowrap' )
end
spriteText = mw.html.create( 'span' ):addClass( 'sprite-text' ):wikitext( args.text )
if args.title then
spriteText:attr( 'title', args.title )
end
if link ~= '' then
-- External link
if link:find( '//' ) then
spriteText = '[' .. link .. ' ' .. tostring( spriteText ) .. ']'
else
spriteText = '[[' .. link .. '|' .. tostring( spriteText ) .. ']]'
end
end
end
if not root then
root = mw.html.create( '' )
end
root:node( sprite )
if spriteText then
root:node( spriteText )
end
return tostring( root )
end
return p