Zelda Top Logo.jpg

共建WIKI编辑招募
如果你热爱《塞尔达传说》系列并且愿意为之添砖添瓦的话
可以加群一起共同建设,招募详情页见攻略组招募公告

全站通知:

模块:File

来自塞尔达传说 王国之泪WIKI_BWIKI_哔哩哔哩
跳到导航 跳到搜索

此模块的文档可以在模块:File/doc创建

-- Define the File module
local File = {}

--[[
    image - Generates HTML markup for an image.

    url (string): The URL of the image file.
    options (table): An optional table of image options:
        - size (string): The width and height of the image in pixels (e.g. "320x240").
        - align (string): The alignment of the image ("left", "right", "center", or "none").
        - caption (string): A caption for the image.

    Returns: An HTML string representing the image.
]]
function File.image(url, options)
    local attrs = {
        -- Initialize the attrs table as an array of attribute strings
        src = url
    }
    
    -- Set the image size if specified
    if options.size then
        local width, height = options.size:match("(%d+)x(%d+)")
    	attrs.width = width
    	attrs.height = height
    end
    
    -- Set the image alignment if specified
    if options.align then
        attrs.class = "align-" .. options.align
    end
    
    -- Add a caption if specified
    if options.caption then
        -- Generate HTML markup for the image and the caption
		local imgHtml = mw.html.create('img'):attr(attrs):done()
		local captionHtml = mw.html.create('figcaption'):wikitext(options.caption):done()

		-- Combine the HTML markups into a figure element
		local figure = mw.html.create('figure')
		figure:node(imgHtml)
		figure:node(captionHtml)

		return tostring(figure)
    else
        -- If there is no caption, just generate HTML markup for the image
        return mw.html.create('img')
            :attr(attrs)
            :done()
    end
end


--[[
    audio - Generates HTML markup for an audio file.

    url (string): The URL of the audio file.
    options (table): An optional table of audio options:
        - autoplay (boolean): Whether to automatically start playing the audio.
        - loop (boolean): Whether to loop the audio.

    Returns: An HTML string representing the audio player.
]]
function File.audio(url, options)
    local attrs = { controls = 'controls' }
    
    -- Set autoplay and loop attributes if specified
    if options.autoplay then
        attrs.autoplay = 'autoplay'
    end
    
    if options.loop then
        attrs.loop = 'loop'
    end
    
    -- Generate HTML markup for the audio player
    return mw.html.create('audio')
        :attr(attrs)
        :wikitext(mw.html.create('source')
            :attr('src', url)
            :done())
        :done()
end

--[[
    video - Generates HTML markup for a video file.

    url (string): The URL of the video file.
    options (table): An optional table of video options:
        - autoplay (boolean): Whether to automatically start playing the video.
        - loop (boolean): Whether to loop the video.

    Returns: An HTML string representing the video player.
]]
function File.video(url, options)
    local attrs = { controls = 'controls' }
    
    -- Set autoplay and loop attributes if specified
    if options.autoplay then
        attrs.autoplay = 'autoplay'
    end
    
    if options.loop then
        attrs.loop = 'loop'
    end
    
    -- Generate HTML markup for the video player
    return mw.html.create('video')
        :attr(attrs)
        :wikitext(mw.html.create('source')
            :attr('src', url)
            :done())
        :done()
end

--[[
    embed - Generates HTML markup for an embedded media type.

    mediaType (string): The type of media to embed ("youtube" or "soundcloud").
    url (string): The URL of the media.
    options (table): An optional table of media options:
        - width (number): The width of the embed in pixels.
        - height (number): The height of the embed in pixels.
        - autoplay (boolean): Whether to automatically start playing the media.

    Returns: An HTML string representing the embedded media.
]]
function File.embed(mediaType, url, options)
    if mediaType == "youtube" then
        local attrs = { 
            width = options.width or 640,
            height = options.height or 360
        }
        
        -- Add autoplay parameter if specified
        if options.autoplay then
            attrs.src = url .. "?autoplay=1"
        else
            attrs.src = url
        end
        
        -- Generate HTML markup for the YouTube embed
        return mw.html.create('iframe')
            :attr(attrs)
            :done()
    elseif mediaType == "soundcloud" then
        local attrs = {
            width = "100%",
        	height = "166",
        	scrolling = "no",
        	frameborder = "no"
    	}
    
    	-- Add auto_play parameter if specified
    	if options.auto_play then
        	url = url .. "?auto_play=true"
    	end
    
		 -- Generate HTML markup for the SoundCloud embed
    	 return mw.html.create('iframe')
        	:attr(attrs)
        	:wikitext(mw.html.create('a')
            	:attr('href', url)
            	:wikitext("Listen on SoundCloud")
            	:done())
        	:done()
	else
    	error("Unsupported media type: " .. mediaType)
    end
end

return File