merge ws/absorb: bmp wav mp3 flac opus zip playlists (+APNG, writers), containers Ogg/Riff/Zip, codecs Url/Text
This commit is contained in:
commit
1a577f8910
201 files changed
+20786
-54
No files matched your search
@@ -0,0 +1,53 @@
|
||||
--!strict
|
||||
--!optimize 2
|
||||
-- codecs/Text: text decoding helpers for the text formats: a UTF-8 BOM is stripped, text that is not valid UTF-8 is
|
||||
-- read as Windows-1252 (classic .m3u / .pls / .ini / .srt files), lines split on CRLF / CR / LF, trim. Absorbed from
|
||||
-- playlist-luau 3fc7fd3 (same author, no third-party code). Never throws.
|
||||
local Text = {}
|
||||
|
||||
-- CP1252 bytes 0x80..0x9F (undefined ones map to U+FFFD)
|
||||
Text.CP1252 = {
|
||||
[0x80] = 0x20AC, [0x82] = 0x201A, [0x83] = 0x0192, [0x84] = 0x201E, [0x85] = 0x2026, [0x86] = 0x2020,
|
||||
[0x87] = 0x2021, [0x88] = 0x02C6, [0x89] = 0x2030, [0x8A] = 0x0160, [0x8B] = 0x2039, [0x8C] = 0x0152,
|
||||
[0x8E] = 0x017D, [0x91] = 0x2018, [0x92] = 0x2019, [0x93] = 0x201C, [0x94] = 0x201D, [0x95] = 0x2022,
|
||||
[0x96] = 0x2013, [0x97] = 0x2014, [0x98] = 0x02DC, [0x99] = 0x2122, [0x9A] = 0x0161, [0x9B] = 0x203A,
|
||||
[0x9C] = 0x0153, [0x9E] = 0x017E, [0x9F] = 0x0178,
|
||||
}
|
||||
|
||||
function Text.trim(s: string): string
|
||||
return (string.match(s, "^%s*(.-)%s*$") :: string)
|
||||
end
|
||||
|
||||
-- toUtf8(text, warnings?) -> text as UTF-8: strips a BOM; text that is not valid UTF-8 is read as Windows-1252
|
||||
function Text.toUtf8(text: string, warnings: { string }?): string
|
||||
if string.sub(text, 1, 3) == "\239\187\191" then
|
||||
text = string.sub(text, 4)
|
||||
end
|
||||
if utf8.len(text) then
|
||||
return text
|
||||
end
|
||||
if warnings then
|
||||
table.insert(warnings, "text is not UTF-8: read as Windows-1252")
|
||||
end
|
||||
local out = table.create(#text)
|
||||
for i = 1, #text do
|
||||
local b = string.byte(text, i)
|
||||
if b < 0x80 then
|
||||
out[i] = string.char(b)
|
||||
else
|
||||
out[i] = utf8.char(if b < 0xA0 then Text.CP1252[b] or 0xFFFD else b)
|
||||
end
|
||||
end
|
||||
return table.concat(out)
|
||||
end
|
||||
|
||||
function Text.lines(text: string): { string }
|
||||
text = string.gsub(text, "\r\n?", "\n")
|
||||
local out = {}
|
||||
for line in string.gmatch(text .. "\n", "([^\n]*)\n") do
|
||||
table.insert(out, line)
|
||||
end
|
||||
return out
|
||||
end
|
||||
|
||||
return Text
|
||||
@@ -0,0 +1,216 @@
|
||||
--!strict
|
||||
--!optimize 2
|
||||
-- codecs/Url: RFC 3986 reference resolution (section 5.2), the classification of a playlist / manifest location (URL,
|
||||
-- Windows / UNC / POSIX local path, relative reference) and percent-encoding of bytes that may not appear in a URI
|
||||
-- (raw spaces and UTF-8 in playlists). Shared by the playlist formats (m3u / HLS, pls, xspf); later DASH / glTF URIs.
|
||||
-- Absorbed from playlist-luau 3fc7fd3 (same author, no third-party code): Url.luau, plus locate / base from its
|
||||
-- init.luau. Never throws.
|
||||
local Url = {}
|
||||
|
||||
export type Location = { url: string, playable: boolean, reason: string?, [string]: any }
|
||||
|
||||
export type Parts = {
|
||||
scheme: string?,
|
||||
authority: string?,
|
||||
path: string,
|
||||
query: string?,
|
||||
fragment: string?,
|
||||
}
|
||||
|
||||
-- Splits a URI reference into its five components (RFC 3986 appendix B).
|
||||
function Url.split(ref: string): Parts
|
||||
local scheme, rest = string.match(ref, "^([%a][%w+%.%-]*):(.*)$")
|
||||
if not scheme then
|
||||
rest = ref
|
||||
end
|
||||
local s = rest :: string
|
||||
local fragment, query, authority
|
||||
local i = string.find(s, "#", 1, true)
|
||||
if i then
|
||||
fragment = string.sub(s, i + 1)
|
||||
s = string.sub(s, 1, i - 1)
|
||||
end
|
||||
i = string.find(s, "?", 1, true)
|
||||
if i then
|
||||
query = string.sub(s, i + 1)
|
||||
s = string.sub(s, 1, i - 1)
|
||||
end
|
||||
if string.sub(s, 1, 2) == "//" then
|
||||
local j = string.find(s, "/", 3, true)
|
||||
if j then
|
||||
authority = string.sub(s, 3, j - 1)
|
||||
s = string.sub(s, j)
|
||||
else
|
||||
authority = string.sub(s, 3)
|
||||
s = ""
|
||||
end
|
||||
end
|
||||
return { scheme = scheme, authority = authority, path = s, query = query, fragment = fragment }
|
||||
end
|
||||
|
||||
-- Recomposes components (RFC 3986 section 5.3).
|
||||
function Url.join(p: Parts): string
|
||||
local out = {}
|
||||
if p.scheme then
|
||||
table.insert(out, p.scheme .. ":")
|
||||
end
|
||||
if p.authority then
|
||||
table.insert(out, "//" .. p.authority)
|
||||
end
|
||||
table.insert(out, p.path)
|
||||
if p.query then
|
||||
table.insert(out, "?" .. p.query)
|
||||
end
|
||||
if p.fragment then
|
||||
table.insert(out, "#" .. p.fragment)
|
||||
end
|
||||
return table.concat(out)
|
||||
end
|
||||
|
||||
-- RFC 3986 section 5.2.4
|
||||
function Url.removeDotSegments(path: string): string
|
||||
local input = path
|
||||
local output = {}
|
||||
while input ~= "" do
|
||||
if string.sub(input, 1, 3) == "../" then
|
||||
input = string.sub(input, 4)
|
||||
elseif string.sub(input, 1, 2) == "./" then
|
||||
input = string.sub(input, 3)
|
||||
elseif string.sub(input, 1, 3) == "/./" then
|
||||
input = string.sub(input, 3)
|
||||
elseif input == "/." then
|
||||
input = "/"
|
||||
elseif string.sub(input, 1, 4) == "/../" then
|
||||
input = string.sub(input, 4)
|
||||
table.remove(output)
|
||||
elseif input == "/.." then
|
||||
input = "/"
|
||||
table.remove(output)
|
||||
elseif input == "." or input == ".." then
|
||||
input = ""
|
||||
else
|
||||
-- move the first segment (with its leading "/", up to the next "/") to the output
|
||||
local j = string.find(input, "/", 2, true)
|
||||
local seg = if j then string.sub(input, 1, j - 1) else input
|
||||
table.insert(output, seg)
|
||||
input = string.sub(input, #seg + 1)
|
||||
end
|
||||
end
|
||||
return table.concat(output)
|
||||
end
|
||||
|
||||
-- Resolves a reference against an absolute base URI (RFC 3986 section 5.2.2, strict).
|
||||
function Url.resolve(ref: string, base: string): string
|
||||
local r, b = Url.split(ref), Url.split(base)
|
||||
local t: Parts = { path = "" }
|
||||
if r.scheme then
|
||||
t.scheme, t.authority, t.path, t.query = r.scheme, r.authority, Url.removeDotSegments(r.path), r.query
|
||||
else
|
||||
if r.authority then
|
||||
t.authority, t.path, t.query = r.authority, Url.removeDotSegments(r.path), r.query
|
||||
else
|
||||
if r.path == "" then
|
||||
t.path = b.path
|
||||
t.query = if r.query then r.query else b.query
|
||||
else
|
||||
if string.sub(r.path, 1, 1) == "/" then
|
||||
t.path = Url.removeDotSegments(r.path)
|
||||
else
|
||||
-- merge (5.2.3)
|
||||
local merged
|
||||
if b.authority and b.path == "" then
|
||||
merged = "/" .. r.path
|
||||
else
|
||||
local k = string.match(b.path, "^.*()/")
|
||||
merged = (if k then string.sub(b.path, 1, k) else "") .. r.path
|
||||
end
|
||||
t.path = Url.removeDotSegments(merged)
|
||||
end
|
||||
t.query = r.query
|
||||
end
|
||||
t.authority = b.authority
|
||||
end
|
||||
t.scheme = b.scheme
|
||||
end
|
||||
t.fragment = r.fragment
|
||||
return Url.join(t)
|
||||
end
|
||||
|
||||
-- Percent-encodes the bytes that may not appear in a URI (controls, space, non-ASCII, "<>\"^`{|}" and "\\"),
|
||||
-- leaving existing %XX escapes and the reserved delimiters alone.
|
||||
function Url.encodeUnsafe(s: string): string
|
||||
return (string.gsub(s, "[%z\1-\32\127-\255<>\"%^`{|}\\]", function(c: string): string
|
||||
return string.format("%%%02X", string.byte(c))
|
||||
end))
|
||||
end
|
||||
|
||||
-- Is a location a local Windows path ("C:\x", "C:/x", "\\server\share", "\x")?
|
||||
function Url.isWindowsPath(s: string): boolean
|
||||
return string.match(s, "^%a:[\\/]") ~= nil
|
||||
or string.match(s, "^%a:$") ~= nil
|
||||
or string.sub(s, 1, 2) == "\\\\"
|
||||
or string.sub(s, 1, 1) == "\\"
|
||||
end
|
||||
|
||||
-- The scheme of an absolute URI (lower case), or nil. Windows drive letters are not schemes.
|
||||
function Url.scheme(s: string): string?
|
||||
if Url.isWindowsPath(s) then
|
||||
return nil
|
||||
end
|
||||
local scheme = string.match(s, "^([%a][%w+%.%-]*):")
|
||||
return if scheme then string.lower(scheme) else nil
|
||||
end
|
||||
|
||||
-- locate(raw, base?, baseScheme?) -> { url, playable, reason? }: an entry location resolved against the playlist's
|
||||
-- base (from Url.base). Windows paths stay as they are and are local; relative paths without a base, file: URLs and
|
||||
-- schemes other than http(s) are not playable (reason says why).
|
||||
function Url.locate(raw: string, base: string?, baseScheme: string?): Location
|
||||
local loc = (string.match(raw, "^%s*(.-)%s*$") :: string)
|
||||
if Url.isWindowsPath(loc) then
|
||||
return { url = loc, playable = false, reason = "local path" }
|
||||
end
|
||||
local scheme = Url.scheme(loc)
|
||||
local url = loc
|
||||
if not scheme and baseScheme == "windows" and base then
|
||||
-- a playlist on a Windows disk: relative entries are paths next to it
|
||||
local dir = string.match(base, "^(.*[\\/])") or ""
|
||||
return { url = dir .. loc, playable = false, reason = "local path" }
|
||||
end
|
||||
if scheme then
|
||||
if scheme == "http" or scheme == "https" then
|
||||
url = Url.encodeUnsafe(Url.resolve(loc, loc))
|
||||
end
|
||||
elseif base and baseScheme then
|
||||
-- a relative reference: Windows separators become URL ones
|
||||
url = Url.resolve(Url.encodeUnsafe((string.gsub(loc, "\\", "/"))), base)
|
||||
scheme = baseScheme
|
||||
else
|
||||
return { url = loc, playable = false, reason = "relative path without a base URL" }
|
||||
end
|
||||
if scheme == "http" or scheme == "https" then
|
||||
return { url = url, playable = true }
|
||||
elseif scheme == "file" then
|
||||
return { url = url, playable = false, reason = "local path" }
|
||||
end
|
||||
return { url = url, playable = false, reason = "scheme " .. tostring(scheme) }
|
||||
end
|
||||
|
||||
-- base(baseUrl?, warnings?) -> base, baseScheme: the base for Url.locate from the playlist's own URL or path ("windows"
|
||||
-- for a Windows path); a non-absolute base is ignored (with a warning)
|
||||
function Url.base(baseUrl: string?, warnings: { string }?): (string?, string?)
|
||||
if type(baseUrl) ~= "string" or baseUrl == "" then
|
||||
return nil, nil
|
||||
end
|
||||
local s = Url.scheme(baseUrl)
|
||||
if s and Url.split(baseUrl).authority ~= nil or s == "file" then
|
||||
return Url.encodeUnsafe(baseUrl), s
|
||||
elseif Url.isWindowsPath(baseUrl) then
|
||||
return baseUrl, "windows"
|
||||
end
|
||||
if warnings then
|
||||
table.insert(warnings, "base URL is not absolute: " .. baseUrl)
|
||||
end
|
||||
return nil, nil
|
||||
end
|
||||
|
||||
return Url
|
||||
Reference in new issue
Block a user