Mercurial Hosting > freedit
comparison src/lib/Bbcode.luan @ 29:a1db5223ced1
luan changes
| author | Franklin Schmidt <fschmidt@gmail.com> |
|---|---|
| date | Mon, 25 Jul 2022 21:28:10 -0600 |
| parents | d9d7aa2a79db |
| children | 8ff35379cc89 |
comparison
equal
deleted
inserted
replaced
| 28:d9d7aa2a79db | 29:a1db5223ced1 |
|---|---|
| 13 local html_parse = Html.parse or error() | 13 local html_parse = Html.parse or error() |
| 14 local Table = require "luan:Table.luan" | 14 local Table = require "luan:Table.luan" |
| 15 local is_list = Table.is_list or error() | 15 local is_list = Table.is_list or error() |
| 16 local concat = Table.concat or error() | 16 local concat = Table.concat or error() |
| 17 local String = require "luan:String.luan" | 17 local String = require "luan:String.luan" |
| 18 local gsub = String.gsub or error() | 18 local regex = String.regex or error() |
| 19 local matches = String.matches or error() | 19 local ends_with = String.ends_with or error() |
| 20 local match = String.match or error() | |
| 21 local User = require "site:/lib/User.luan" | 20 local User = require "site:/lib/User.luan" |
| 22 local Shared = require "site:/lib/Shared.luan" | 21 local Shared = require "site:/lib/Shared.luan" |
| 23 local list_to_set = Shared.list_to_set or error() | 22 local list_to_set = Shared.list_to_set or error() |
| 24 local to_list = Shared.to_list or error() | 23 local to_list = Shared.to_list or error() |
| 25 local Logging = require "luan:logging/Logging.luan" | 24 local Logging = require "luan:logging/Logging.luan" |
| 26 local logger = Logging.logger "Bbcode" | 25 local logger = Logging.logger "Bbcode" |
| 27 | 26 |
| 28 | 27 |
| 29 local Bbcode = {} | 28 local Bbcode = {} |
| 29 | |
| 30 local starting_cr_regex = regex[[^\n]] | |
| 30 | 31 |
| 31 local to_html | 32 local to_html |
| 32 local html = {} | 33 local html = {} |
| 33 | 34 |
| 34 function html.b(bbcode,options) | 35 function html.b(bbcode,options) |
| 65 %><a href="<%=url%>"><% to_html(bbcode.contents,options) %></a><% | 66 %><a href="<%=url%>"><% to_html(bbcode.contents,options) %></a><% |
| 66 end | 67 end |
| 67 end | 68 end |
| 68 | 69 |
| 69 function html.code(bbcode,options) | 70 function html.code(bbcode,options) |
| 70 local s = gsub(bbcode.contents,[[^\n]],"") | 71 local s = starting_cr_regex.gsub(bbcode.contents,"") |
| 71 %><code><%= html_encode(s) %></code><% | 72 %><code><%= html_encode(s) %></code><% |
| 72 options.strip_newline = true | 73 options.strip_newline = true |
| 73 end | 74 end |
| 74 | 75 |
| 75 function html.img(bbcode,options) | 76 function html.img(bbcode,options) |
| 106 %><iframe width="560" height="315" frameborder="0" allowfullscreen src="<%=url%>"></iframe><% | 107 %><iframe width="560" height="315" frameborder="0" allowfullscreen src="<%=url%>"></iframe><% |
| 107 end | 108 end |
| 108 | 109 |
| 109 local video_handlers = {} | 110 local video_handlers = {} |
| 110 do | 111 do |
| 111 local ptn1 = [[https://youtu.be/([a-zA-Z0-9_-]+)(?:\?t=([0-9]+))?]] | 112 local ptn1 = regex[[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 local ptn2 = regex[[https://www.youtube.com/watch\?v=([a-zA-Z0-9_-]+)(?:&t=([0-9]+)s)?]] |
| 113 function video_handlers.youtube(url) | 114 function video_handlers.youtube(url) |
| 114 local id, start = match(url,ptn1) | 115 local id, start = ptn1.match(url) |
| 115 if id == nil then | 116 if id == nil then |
| 116 id, start = match(url,ptn2) | 117 id, start = ptn2.match(url) |
| 117 end | 118 end |
| 118 if id == nil then | 119 if id == nil then |
| 119 return false | 120 return false |
| 120 end | 121 end |
| 121 url = "https://www.youtube.com/embed/"..id | 122 url = "https://www.youtube.com/embed/"..id |
| 125 video_iframe(url) | 126 video_iframe(url) |
| 126 return true | 127 return true |
| 127 end | 128 end |
| 128 end | 129 end |
| 129 do | 130 do |
| 130 local ptn = [[https://rumble.com/embed/[a-z0-9]+/\?pub=[a-z0-9]+]] | 131 local ptn = regex[[https://rumble.com/embed/[a-z0-9]+/\?pub=[a-z0-9]+]] |
| 131 function video_handlers.rumble(url) | 132 function video_handlers.rumble(url) |
| 132 if not matches(url,ptn) then | 133 if not ptn.matches(url) then |
| 133 return false | 134 return false |
| 134 end | 135 end |
| 135 video_iframe(url) | 136 video_iframe(url) |
| 136 return true | 137 return true |
| 137 end | 138 end |
| 138 end | 139 end |
| 139 do | 140 do |
| 140 local ptn = [[https://www.bitchute.com/video/([a-zA-Z0-9]+)/]] | 141 local ptn = regex[[https://www.bitchute.com/video/([a-zA-Z0-9]+)/]] |
| 141 function video_handlers.bitchute(url) | 142 function video_handlers.bitchute(url) |
| 142 local id = match(url,ptn) | 143 local id = ptn.match(url) |
| 143 if id == nil then | 144 if id == nil then |
| 144 return false | 145 return false |
| 145 end | 146 end |
| 146 url = "https://www.bitchute.com/embed/"..id.."/" | 147 url = "https://www.bitchute.com/embed/"..id.."/" |
| 147 video_iframe(url) | 148 video_iframe(url) |
| 148 return true | 149 return true |
| 149 end | 150 end |
| 150 end | 151 end |
| 151 do | 152 do |
| 152 local ptn = [[https://vimeo.com/([0-9]+)]] | 153 local ptn = regex[[https://vimeo.com/([0-9]+)]] |
| 153 function video_handlers.vimeo(url) | 154 function video_handlers.vimeo(url) |
| 154 local id = match(url,ptn) | 155 local id = ptn.match(url) |
| 155 if id == nil then | 156 if id == nil then |
| 156 return false | 157 return false |
| 157 end | 158 end |
| 158 url = "https://player.vimeo.com/video/"..id | 159 url = "https://player.vimeo.com/video/"..id |
| 159 video_iframe(url) | 160 video_iframe(url) |
| 160 return true | 161 return true |
| 161 end | 162 end |
| 162 end | 163 end |
| 163 do | 164 do |
| 164 local ptn = [[https://dai.ly/([a-z0-9]+)]] | 165 local ptn = regex[[https://dai.ly/([a-z0-9]+)]] |
| 165 function video_handlers.dailymotion(url) | 166 function video_handlers.dailymotion(url) |
| 166 local id = match(url,ptn) | 167 local id = ptn.match(url) |
| 167 if id == nil then | 168 if id == nil then |
| 168 return false | 169 return false |
| 169 end | 170 end |
| 170 url = "https://www.dailymotion.com/embed/video/"..id | 171 url = "https://www.dailymotion.com/embed/video/"..id |
| 171 video_iframe(url) | 172 video_iframe(url) |
| 172 return true | 173 return true |
| 173 end | 174 end |
| 174 end | 175 end |
| 175 do | 176 do |
| 176 local ptn = [[https://www.tiktok.com/[^/]+/video/([0-9]+)]] | 177 local ptn = regex[[https://www.tiktok.com/[^/]+/video/([0-9]+)]] |
| 177 function video_handlers.tiktok(url) | 178 function video_handlers.tiktok(url) |
| 178 local id = match(url,ptn) | 179 local id = ptn.match(url) |
| 179 if id == nil then | 180 if id == nil then |
| 180 return false | 181 return false |
| 181 end | 182 end |
| 182 %><blockquote class="tiktok-embed" data-video-id="<%=id%>" style="max-width: 560px; margin-left: 0;"><section></section></blockquote><% | 183 %><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 %><script async src="https://www.tiktok.com/embed.js"></script><% |
| 184 return true | 185 return true |
| 185 end | 186 end |
| 186 end | 187 end |
| 187 do | 188 do |
| 188 local ptn = [[\.[a-zA-Z0-9]+$]] | 189 local ptn = regex[[\.[a-zA-Z0-9]+$]] |
| 189 function video_handlers.file(url) | 190 function video_handlers.file(url) |
| 190 if not matches(url,ptn) then | 191 if not ptn.matches(url) then |
| 191 return false | 192 return false |
| 192 end | 193 end |
| 193 %><video controls width="560"><source src="<%=html_encode(url)%>"></video><% | 194 %><video controls width="560"><source src="<%=html_encode(url)%>"></video><% |
| 194 return true | 195 return true |
| 195 end | 196 end |
| 227 end | 228 end |
| 228 | 229 |
| 229 function to_html(bbcode,options) | 230 function to_html(bbcode,options) |
| 230 if options.strip_newline then | 231 if options.strip_newline then |
| 231 if type(bbcode) == "string" then | 232 if type(bbcode) == "string" then |
| 232 bbcode = gsub(bbcode,[[^\n]],"") | 233 bbcode = starting_cr_regex.gsub(bbcode,"") |
| 233 end | 234 end |
| 234 options.strip_newline = false | 235 options.strip_newline = false |
| 235 end | 236 end |
| 236 if type(bbcode) == "string" then | 237 if type(bbcode) == "string" then |
| 237 %><%= html_encode(bbcode) %><% | 238 %><%= html_encode(bbcode) %><% |
| 261 "code" | 262 "code" |
| 262 "img" | 263 "img" |
| 263 "video" | 264 "video" |
| 264 } | 265 } |
| 265 | 266 |
| 267 local url_regex = regex[[(^|\s)(https?://\S+)]] | |
| 268 | |
| 266 local function preprocess(bbcode) | 269 local function preprocess(bbcode) |
| 267 if type(bbcode) == "string" then | 270 if type(bbcode) == "string" then |
| 268 bbcode = gsub( bbcode, [[(^|\s)(https?://\S+)]], "$1[url]$2[/url]" ) | 271 bbcode = url_regex.gsub( bbcode, "$1[url]$2[/url]" ) |
| 269 %><%= bbcode %><% | 272 %><%= bbcode %><% |
| 270 else | 273 else |
| 271 type(bbcode) == "table" or error() | 274 type(bbcode) == "table" or error() |
| 272 if is_list(bbcode) then | 275 if is_list(bbcode) then |
| 273 for _, v in ipairs(bbcode) do | 276 for _, v in ipairs(bbcode) do |
| 301 preprocess(bbcode) | 304 preprocess(bbcode) |
| 302 end) | 305 end) |
| 303 end | 306 end |
| 304 | 307 |
| 305 function Bbcode.remove_html(text) | 308 function Bbcode.remove_html(text) |
| 306 local ends_with_newline = matches(text,[[\n$]]) | 309 local ends_with_newline = ends_with(text,"\n") |
| 307 local t = {} | 310 local t = {} |
| 308 local html = html_parse(text) | 311 local html = html_parse(text) |
| 309 local is_first = true | 312 local is_first = true |
| 310 for _, v in ipairs(html) do | 313 for _, v in ipairs(html) do |
| 311 if type(v) == "string" then | 314 if type(v) == "string" then |
