模块:Sprite:修订间差异

来自迫真战记中文WIKI
无编辑摘要
无编辑摘要
标签手工回退
 
(未显示同一用户的12个中间版本)
第1行: 第1行:
local p = {}
local p = {}
function p.base( f )
local args = f
if f == mw.getCurrentFrame() then
args = require( 'Module:ProcessArgs' ).merge( true )
else
f = mw.getCurrentFrame()
end
local data = args.data and mw.loadData( 'Module:' .. args.data ) or {}
local settings = data.settings


-- Default settings
function p.sprite( frame )
local default = {
    local args = frame:getParent().args
scale = 1,
sheetsize = 256,
size = 16,
pos = 1,
align = 'text-top'
}
local defaultStyle = default
if settings then
if not settings.stylesheet then
-- Make a separate clone of the current default settings
defaultStyle = {
scale = 1,
sheetsize = 256,
size = 16,
pos = 1,
align = 'text-top'
}
end
for k, v in pairs( settings ) do
default[k] = v
end
end
setmetatable( args, { __index = default } )
local sprite = mw.html.create( 'span' ):addClass( 'sprite' )
-- mw.html's css method performs very slow escaping, which doubles the time it takes
-- to run, so we'll construct the styles manually, and put them in the cssText
-- method, which only does html escaping (which isn't slow)
local styles = {}
-- for tint
local classname = args['classname'] or mw.ustring.lower( args['name'] :gsub( ' ', '-' ) ) .. '-sprite'
local css_image = "background"
if args['formask'] then
classname = classname .. '-mask'
css_image = "mask"
end
sprite:addClass( classname )
local class = args['class']
if class then
sprite:addClass( class )
end
local width = args['width'] or args['size']
local height = args['height'] or args['size']
local sheetWidth = args['sheetsize']
local tiles = sheetWidth / width
local pos = args['pos'] - 1
local scale = args['scale']
local autoScale = args['autoscale']
if pos then
local left = pos % tiles * width * scale
local top = math.floor( pos / tiles ) * height * scale
if css_image == 'mask' then
styles[#styles + 1] = '-webkit-mask-position:-' .. left .. 'px -' .. top .. 'px'
end
styles[#styles + 1] = css_image .. '-position:-' .. left .. 'px -' .. top .. 'px'
end
if not autoScale and scale ~= defaultStyle.scale then
if css_image == 'mask' then
styles[#styles + 1] = '-webkit-mask-size:' .. sheetWidth * scale .. 'px auto'
end
styles[#styles + 1] = css_image .. '-size:' .. sheetWidth * scale .. 'px auto'
end
if height ~= defaultStyle.size or width ~= defaultStyle.size or ( not autoScale and scale ~= defaultStyle.scale ) then
styles[#styles + 1] = 'height:' .. height * scale .. 'px'
styles[#styles + 1] = 'width:' .. width * scale .. 'px'
end
local align = args['align']
if align ~= defaultStyle.align then
styles[#styles + 1] = 'vertical-align:' .. align
end
styles[#styles + 1] = args['css']
sprite:cssText( table.concat( styles, ';' ) )
local text = args['text']
local root
local spriteText
if text then
if not args['wrap'] then
root = mw.html.create( 'span' ):addClass( 'nowrap' )
end
local autolinkText
if args['no-autolink'] then
autolinkText = text
else
autolinkText = text
end
spriteText = mw.html.create( 'span' ):addClass( 'sprite-text' ):wikitext( autolinkText )
end
local title = args['title']
if title then
( root or sprite ):attr( 'title', title )
end
if not root then
root = mw.html.create( '' )
end
root:node( sprite )
if spriteText then
root:node( spriteText )
end
local link = args['link'] or ''
if link ~= '' and mw.ustring.lower( link ) ~= 'none' then
-- External link
if link:find( '//' ) then
return '[' .. link .. ' ' .. tostring( root ) .. ']'
end
-- Internal link  Autolink.invlink( link, 'linkonly', args.data )
local linkPrefix = args['linkprefix'] or ''
return '[[' .. linkPrefix .. link .. args.data .. '|' .. tostring( root ) .. ']]'
end
return tostring( root )
end


function p.sprite( f )
    -- Default settings
local args = f
    local default = {
if f == mw.getCurrentFrame() then
        scale = 1,
args = require( 'Module:ProcessArgs' ).merge( true )
        size = 24,
else
        align = 'text-top'
f = mw.getCurrentFrame()
    }
end
local data = args.data and mw.loadData( 'Module:' .. args.data ) or {}
local categories = {}
local idData = args.iddata
if not idData then
local name = args.name or data.settings.name
local id = mw.text.trim( tostring( args[1] or '' ) )
idData = data.ids[id] or data.ids[mw.ustring.lower( id ):gsub( '[%s%+]', '-' )]
end
local title = mw.title.getCurrentTitle()
-- Remove categories on language pages, talk pages, and in User/UserWiki/UserProfile namespaces
local disallowCats = args.nocat or title.isTalkPage or title.nsText:find( '^User' )
if idData then
if idData.deprecated then
args.class = ( args.class or '' ) .. ' sprite-deprecated'
if not disallowCats then
categories[#categories + 1] = '[[Category:使用已弃用Sprite的页面]]'
end
end
args.pos = idData.pos
elseif not disallowCats then
categories[#categories + 1] = '[[Category:含有错误Sprite的页面]]'
end
return p.base( args ), table.concat( categories )
end


function p.link( f )
 
local args = f
    local link = ( args.link or '' )
if f == mw.getCurrentFrame() then
    if mw.ustring.lower( link ) == 'none' then
args = require( 'Module:ProcessArgs' ).merge( true )
        link = ''
end
    elseif link ~= '' and not link:find( '//' ) then
        link = args.linkprefix or '' .. link
local link = args[1]
    end
if args[1] and not args.id then
 
link = args[1]:match( '^(.-)%+' ) or args[1]
    local scale = args.scale or default.scale
end
    local height = ( args.height or args.size or default.size ) * scale
local text
    local width = ( args.width or args.size or default.size ) * scale
if not args.notext then
    local size = width .. 'x' .. height .. 'px'
text = args.text or args[2] or link
 
end
    local styles = {}
    if height ~= default.size then
args[1] = args.id or args[1]
        styles[#styles + 1] = 'height:' .. height .. 'px'
args.link = args.link or link
    end
args.text = text
    if width ~= default.size then
        styles[#styles + 1] = 'width:' .. width .. 'px'
return p.sprite( args )
    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