Модуль:Videogallery2 🔗


Материал из VEDA Wiki
Перейти к: навигация, поиск

Для документации этого модуля может быть создана страница Модуль:Videogallery2/doc

-- Module for replacing of combination of EmbedVideo-ext + my Ev-template with more faster lua-implementation
-- Test page: https://lahwiki.sphynkx.org.ua/Sandbox/Videogallery
-- v.1: Supported services are YouTube, RuTube, EMTube, OK
-- v.2: Added optional collapsibility. 
-- v.3: function for replacement youtube timestamps into URL in wiki-format. Timestamp is beginning part of line that starts with ':' and after placed hours (optionally), again ':',minutes (one- or twodigit), ':', seconds (todigit, leading zero if need). After that is have to be one space and minus. And next is rest of comment etc.


local p = {} -- p stands for package

ytid=''

function get_embed(u)
    if     string.match(u,'youtube') then 
                                    id = string.gsub(u,'.*youtube.com%/watch%?v%=(.*)','%1')
                                    ytid = '<span class="'.. '{{#ifexist: Субтитры:'.. id .. ' | | tech }}' .. '"><br>[[Субтитры:' .. id .. '|(Субтитры)]]</span>'
                                    return '<html><iframe src="https://www.youtube.com/embed/'..id..'" width="300" height="250" frameborder="1" allowfullscreen="true" style="white-space: nowrap; display: -webkit-flex; -webkit-flex-direction: row; display: flex; flex-direction: row;" align="baseline z-index:999;" seamless></iframe></html>[[File:Youtube.png|24px|left|link=]]'
    elseif     string.match(u,'yurtube') then 
                                    id = string.gsub(u,'.*yurtube.-/watch%?v%=(.*)','%1')
                                    ytid=''
                                    return '<html><iframe src="https://yurtube.sphynkx.org.ua/embed?vid='..id..'&width=300&height=250&autoplay=no" frameborder="1" allowfullscreen scrolling="no" width="300" height="250" seamless></iframe></html><br>[[File:yurtube.png|24px|left|link=]]'
    elseif     string.match(u,'rutube') then 
                                    id = string.gsub(u,'.*rutube.ru/video/(.*)/','%1')
                                    ytid=''
                                    return '<html><iframe width="300" height="250" src="https://rutube.ru/pl/?pl_video='..id..'" frameborder="0" allow="clipboard-write" webkitAllowFullScreen mozallowfullscreen allowfullscreen></iframe></html><br>[[File:rutube.png|24px|left|link=]]'
    else return '<div style="display: block; border: 1px solid red; width:300px; max-width:300px; height:250px;"> </div>[[File:Unknown.png|24px|left|link=]]'
    end
end


--TODO: Here need to convert in comment (c) any sequences of "XX:YY:ZZ" into "XXhYYmZZs" and generate videoURL with this timestamp
function format_timestamp(u, c)
local outp=''
--for each line in comment..
for line in string.gmatch(c, "([^\n]+)") do
--check if timestamp exists in line
    t=string.match(line, ':(.-%d:%d%d) ')
    if t==nil then 
	t="" 
	else
--working line backup--	    line=string.gsub(line, '(:)(%d?:?.-):(%d%d)(.-)', '%1['..u..'&t=%2m%3s '..t..']%4')
-- reformat to wiki markup and set seconds
	    line=string.gsub(line, ':(.-%d%d)( %- .-)', ':['..u..'&t=%1s '..t..']%2')
-- format munutes part
	    line=string.gsub(line, ':(%d%ds)', 'm%1')
-- format hours part
	    line=string.gsub(line, '(%d:?):(%d%dm%d%ds)', '%1h%2')
	end -- IF
-- concatenate everything back
    outp=outp..line..'\n'
    end --FOR
    return outp
end


--format final code with iframe
function format_embed(d, u, t, c)
  e=get_embed(u)
  if string.match(c,'<nosub>') then 
     c = string.gsub(c,'(.-)(<nosub>)(.*)','%1%3')
     ytid = '<br><span class="tech" style="color: lightgray;">(Без субтитров)</span>'
  end
  c=format_timestamp(u, c)
  return '<b>'..e..d..'<br>'..' ['..u..' '..t..']</b>'..ytid..'<br>'..c
end


function p.videos( frame )
local vidlist = frame.args['VIDEOS']
local vidlist_rows = frame.args['VIDEO_ROWS']
local vidlist_border = frame.args['VIDEO_BORDER']
local collapsed  = frame.args['COLLAPSED']=='1' and ' mw-collapsed' or ''
local collapseHead = frame.args['COLLAPSIBLE']=='1' and frame:preprocess('<html><span class="toccolours mw-collapsible' .. collapsed ..'" style="width:8000px; display: table-cell; margin: 0.5em 0 0 2em; clear: both"></html>') or ''
local collapseBody = frame.args['COLLAPSIBLE']=='1' and frame:preprocess('<html><span class="mw-collapsible-content"></html>') or ''
local collapseEnd  = frame.args['COLLAPSIBLE']=='1' and frame:preprocess('<html></span></span></html>') or ''

-- escape STAR sign inside the text, that is not a list identifier
vidlist=string.gsub(vidlist,'([^\n])%*', '%1&#42;')
vidcnt=1
vidtable = {}
for i in string.gmatch(vidlist, '%*[^\*]*') do
    i=string.gsub(i,'%*(.-)%[(.-) (.-)%](.*)', format_embed)
    tr_start = math.fmod( vidcnt, vidlist_rows ) == 1 and ''  or ""
    tr_end   = math.fmod( vidcnt, vidlist_rows ) == 0 and '' or ""
    table.insert(vidtable, tr_start.. '<span style="display: inline-block; border: 1px solid red; width:300px; max-width:300px; height:550px; padding: 5px;">' .. i .. '</span>' ..tr_end)
    vidcnt=vidcnt+1
    end

-- here i reused as identifier of nonempty videogallery (for setting of category)
i = (vidcnt>1) and "[[Category:Страницы с видео]]" or ""
return collapseHead .. table.getn(vidtable) .. ' videos' .. collapseBody .. '<span style="display: -webkit-flex; -webkit-flex-direction: row; display: flex; flex-direction: row; box-sizing: content-box; width: 100%; padding: 5px; flex-wrap: wrap; align-items: flex-start; justify-content: flex-start;">' .. frame:preprocess(table.concat(vidtable)) .. '</span>SPAN'.. i .. collapseEnd
end

return p