Module:Video

From Downtime Wiki
Revision as of 04:44, 25 April 2025 by Orashgle (talk | contribs) (1 revision imported)
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)

This module implements {{#if:{{#switch: yes

| y | yes | t | true  | on  | 1 = yes
| n | no  | f | false | off | 0 | = 
| =  
| #default = yes

}}|}}{{{{#if: |subst: }}[[{{#if: {{#pos: YouTube | : }} || Template: }}YouTube|{{ #ifeq: | Template | YouTube | YouTube }}]]{{ #if: | | }}{{ #if: | | }}{{ #if: | | }}{{ #if: | | }}{{ #if: | | }}{{ #if: | | }}{{ #if: | | }}{{ #if: | | }}}}{{#if:{{#switch: yes

| y | yes | t | true  | on  | 1 = yes
| n | no  | f | false | off | 0 | = 
| =  
| #default = yes

}}|}}{{#if: {{#pos: YouTube | File: }}{{#pos: YouTube | Category: }} | }} and {{#if:{{#switch: yes

| y | yes | t | true  | on  | 1 = yes
| n | no  | f | false | off | 0 | = 
| =  
| #default = yes

}}|}}{{{{#if: |subst: }}[[{{#if: {{#pos: Vimeo | : }} || Template: }}Vimeo|{{ #ifeq: | Template | Vimeo | Vimeo }}]]{{ #if: | | }}{{ #if: | | }}{{ #if: | | }}{{ #if: | | }}{{ #if: | | }}{{ #if: | | }}{{ #if: | | }}{{ #if: | | }}}}{{#if:{{#switch: yes

| y | yes | t | true  | on  | 1 = yes
| n | no  | f | false | off | 0 | = 
| =  
| #default = yes

}}|}}{{#if: {{#pos: Vimeo | File: }}{{#pos: Vimeo | Category: }} | }}, and allows invocations from other modules.

See their documentations to learn how to use through a template.

Functions

Other modules can invoke this module, to implement more advanced features.

Exported functions listed below.

genVideo

This function generates a pure video instance.

Syntax: genVideo( VideoServiceProvider, VideoID, VideoWidth, VideoHeight, VideoAlign, VideoCaption, URLArgs )

  • VideoServiceProvider: Optional. The video service provider. If not set, youtube will used.
  • VideoID: A video ID. If not set, this function will directly return a nil value.
  • VideoWidth: Optional. The width of the video instance, non-number will be ignored, defaults to 425.
  • VideoHeight: Optional. The height of the video instance, non-number will be ignored. If not set, it will be calculated by width automatically to keep aspect ratio to 16:9.
  • VideoAlign: Optional. The type of video alignment.
  • VideoCaption: Optional. The small text below the video.
  • URLArgs: Optional. The arguments which should append to the URL of the video.

main

This function is used for template invocation, and without any extra parameters accepted, and should not be invocated by other modules.

Dependencies

{{#if:{{#pos:Video|/sandbox}}|| pt:Módulo:Vídeo ru:Модуль:Видеоролик uk:Модуль:Відеоролик zh:Module:Video }}


local p = {}

local i18n = {
	trackCategory = '[[Category:Pages with%s videos]]',
	services = {	-- when video embeded with a new service provider, add it here
		youtube = ' YouTube',
		youtubeplaylist = ' YouTube',
		vimeo = ' Vimeo',
		bilibili = ' Bilibili',
	}
}

function p.genVideo( provider, id, width, height, align, caption, urlArgs )
	if not id then
		return nil
	end
	provider = provider and provider:lower() or 'youtube'
	local dimensions = tonumber( width ) or 425
	height = tonumber( height )
	if height then
		dimensions = dimensions .. 'x' .. height
	end
	if urlArgs then
		return mw.getCurrentFrame():callParserFunction{ name = '#ev', args = { provider, id = id, dimensions = dimensions, alignment = align, description = caption, urlArgs = urlArgs } }
	else
		return mw.getCurrentFrame():extensionTag{ name = provider, content = id, args = { dimensions = dimensions, alignment = align, description = caption } }
	end
end

function p.main( f )
	local args = f
	if f == mw.getCurrentFrame() then
		args = require( 'Module:ProcessArgs' ).merge( true )
	end

	local result = p.genVideo( args.provider, args[ 1 ], args[ 2 ], args[ 3 ], args.align, args.caption, args.urlargs )
	if result then
		--result = mw.html.create( 'div' ):wikitext( result )
		if not args.nocat then
			result = tostring( result ) .. i18n.trackCategory:format( i18n.services[ args.provider and args.provider:lower() or '' ] or '' )
		end
	end

	return result
end

return p