comparison src/lib/Bbcode.luan @ 28:d9d7aa2a79db

more video types
author Franklin Schmidt <fschmidt@gmail.com>
date Thu, 21 Jul 2022 23:44:49 -0600
parents 6871eec2cf4c
children a1db5223ced1
comparison
equal deleted inserted replaced
27:6871eec2cf4c 28:d9d7aa2a79db
1 local Luan = require "luan:Luan.luan" 1 local Luan = require "luan:Luan.luan"
2 local error = Luan.error 2 local error = Luan.error
3 local type = Luan.type or error() 3 local type = Luan.type or error()
4 local ipairs = Luan.ipairs or error() 4 local ipairs = Luan.ipairs or error()
5 local pairs = Luan.pairs or error()
5 local stringify = Luan.stringify or error() 6 local stringify = Luan.stringify or error()
6 local Io = require "luan:Io.luan" 7 local Io = require "luan:Io.luan"
7 local output_of = Io.output_of or error() 8 local output_of = Io.output_of or error()
8 local Parsers = require "luan:Parsers.luan" 9 local Parsers = require "luan:Parsers.luan"
9 local bbcode_parse = Parsers.bbcode_parse or error() 10 local bbcode_parse = Parsers.bbcode_parse or error()
99 to_html(bbcode.contents,options) 100 to_html(bbcode.contents,options)
100 %></blockquote><% 101 %></blockquote><%
101 options.strip_newline = true 102 options.strip_newline = true
102 end 103 end
103 104
104 local youtube_ptn1 = [[https://youtu.be/([a-zA-Z0-9_-]+)(?:\?t=([0-9]+))?]] 105 local function video_iframe(url)
105 local youtube_ptn2 = [[https://www.youtube.com/watch\?v=([a-zA-Z0-9_-]+)(?:&t=([0-9]+)s)?]] 106 %><iframe width="560" height="315" frameborder="0" allowfullscreen src="<%=url%>"></iframe><%
106 local bitchute_ptn = [[https://www.bitchute.com/video/([a-zA-Z0-9]+)/]] 107 end
107 local rumble_ptn = [[https://rumble.com/embed/[a-z0-9]+/\?pub=[a-z0-9]+]] 108
109 local video_handlers = {}
110 do
111 local ptn1 = [[https://youtu.be/([a-zA-Z0-9_-]+)(?:\?t=([0-9]+))?]]
112 local ptn2 = [[https://www.youtube.com/watch\?v=([a-zA-Z0-9_-]+)(?:&t=([0-9]+)s)?]]
113 function video_handlers.youtube(url)
114 local id, start = match(url,ptn1)
115 if id == nil then
116 id, start = match(url,ptn2)
117 end
118 if id == nil then
119 return false
120 end
121 url = "https://www.youtube.com/embed/"..id
122 if start ~= nil then
123 url = url.."?start="..start
124 end
125 video_iframe(url)
126 return true
127 end
128 end
129 do
130 local ptn = [[https://rumble.com/embed/[a-z0-9]+/\?pub=[a-z0-9]+]]
131 function video_handlers.rumble(url)
132 if not matches(url,ptn) then
133 return false
134 end
135 video_iframe(url)
136 return true
137 end
138 end
139 do
140 local ptn = [[https://www.bitchute.com/video/([a-zA-Z0-9]+)/]]
141 function video_handlers.bitchute(url)
142 local id = match(url,ptn)
143 if id == nil then
144 return false
145 end
146 url = "https://www.bitchute.com/embed/"..id.."/"
147 video_iframe(url)
148 return true
149 end
150 end
151 do
152 local ptn = [[https://vimeo.com/([0-9]+)]]
153 function video_handlers.vimeo(url)
154 local id = match(url,ptn)
155 if id == nil then
156 return false
157 end
158 url = "https://player.vimeo.com/video/"..id
159 video_iframe(url)
160 return true
161 end
162 end
163 do
164 local ptn = [[https://dai.ly/([a-z0-9]+)]]
165 function video_handlers.dailymotion(url)
166 local id = match(url,ptn)
167 if id == nil then
168 return false
169 end
170 url = "https://www.dailymotion.com/embed/video/"..id
171 video_iframe(url)
172 return true
173 end
174 end
175 do
176 local ptn = [[https://www.tiktok.com/[^/]+/video/([0-9]+)]]
177 function video_handlers.tiktok(url)
178 local id = match(url,ptn)
179 if id == nil then
180 return false
181 end
182 %><blockquote class="tiktok-embed" data-video-id="<%=id%>" style="max-width: 560px; margin-left: 0;"><section></section></blockquote><%
183 %><script async src="https://www.tiktok.com/embed.js"></script><%
184 return true
185 end
186 end
187 do
188 local ptn = [[\.[a-zA-Z0-9]+$]]
189 function video_handlers.file(url)
190 if not matches(url,ptn) then
191 return false
192 end
193 %><video controls width="560"><source src="<%=html_encode(url)%>"></video><%
194 return true
195 end
196 end
108 197
109 function html.video(bbcode,options) 198 function html.video(bbcode,options)
110 local url = bbcode.contents 199 local url = bbcode.contents
111 local id, start = match(url,youtube_ptn1) 200 for _, handle in pairs(video_handlers) do
112 if id == nil then 201 if handle(url) then return end
113 id, start = match(url,youtube_ptn2)
114 end
115 if id ~= nil then
116 %><iframe width="560" height="315" src="https://www.youtube.com/embed/<%=id%><%
117 if start ~= nil then
118 %>?start=<%=start%><%
119 end
120 %>" frameborder="0" allowfullscreen></iframe><%
121 return
122 end
123 id = match(url,bitchute_ptn)
124 if id ~= nil then
125 %><iframe width="560" height="315" scrolling="no" frameborder="0" style="border: none;" src="https://www.bitchute.com/embed/<%=id%>/"></iframe><%
126 return
127 end
128 if matches(url,rumble_ptn) then
129 %><iframe width="560" height="315" frameborder="0" allowfullscreen src="<%=url%>"></iframe><%
130 return
131 end 202 end
132 url = html_encode(url) 203 url = html_encode(url)
133 %><a href="<%=url%>"><%=url%></a><% 204 %><a href="<%=url%>"><%=url%></a><%
134 end 205 end
135 206