|
Warning: this is an htmlized version!
The original is here, and the conversion rules are here. |
-- This file:
-- https://anggtwu.net/LUA/SrtParser1.lua.html
-- https://anggtwu.net/LUA/SrtParser1.lua
-- (find-angg "LUA/SrtParser1.lua")
-- Author: Eduardo Ochs <eduardoochs@gmail.com>
--
-- (defun e () (interactive) (find-angg "LUA/SrtParser1.lua"))
-- (find-es "subtitles" "pycaption")
-- (find-es "ffmpeg" "rcnnov25")
--
-- «.SrtLine» (to "SrtLine")
-- «.SrtLine-tests» (to "SrtLine-tests")
-- «.SrtSub» (to "SrtSub")
-- «.SrtSub-tests» (to "SrtSub-tests")
-- «.SrtSubs» (to "SrtSubs")
-- «.SrtSubs-tests» (to "SrtSubs-tests")
-- ____ _ _ _
-- / ___| _ __| |_| | (_)_ __ ___
-- \___ \| '__| __| | | | '_ \ / _ \
-- ___) | | | |_| |___| | | | | __/
-- |____/|_| \__|_____|_|_| |_|\___|
--
-- «SrtLine» (to ".SrtLine")
SrtLine = Class {
type = "SrtLine",
from = function (li) return SrtLine {li} end,
__tostring = function (sl) return sl[1] end,
__index = {
kind = function (sl)
if sl[1]:match("^[0-9]+$") then return "n" end
if sl[1]:match("%-%->") then return "time" end
return "text"
end,
start = function (sl) return sl[1]:sub(2,8) end,
},
}
-- «SrtLine-tests» (to ".SrtLine-tests")
--[[
* (eepitch-lua51)
* (eepitch-kill)
* (eepitch-lua51)
dofile "SrtParser1.lua"
= lines
= lines[2]
= slines
= slines[1]:kind()
= slines[2]:kind()
= SrtLine.from(lines[2]):start()
--]]
-- ____ _ ____ _
-- / ___| _ __| |_/ ___| _ _| |__
-- \___ \| '__| __\___ \| | | | '_ \
-- ___) | | | |_ ___) | |_| | |_) |
-- |____/|_| \__|____/ \__,_|_.__/
--
-- «SrtSub» (to ".SrtSub")
SrtSub = Class {
type = "SrtSub",
__tostring = function (ss) return tostring(ss:torect()) end,
__index = {
start = function (ss)
return ss.time and ss.time:start() or ".:..:.."
end,
torect = function (ss)
return ss:start() .. " " .. Rect(copy(ss))
end,
deltrailingblanks = function (ss)
while #ss > 0 and ss[#ss] == "" do ss[#ss] = nil end
return ss
end,
sexptext = function (ss) return concat(ss, " ") end,
sexp = function (ss,c)
c = c or "2025rcnnov"
return format('(find-%svideo "%s" "%s")', c, ss:start(), ss:sexptext())
end,
},
}
-- «SrtSub-tests» (to ".SrtSub-tests")
--[[
* (eepitch-lua51)
* (eepitch-kill)
* (eepitch-lua51)
dofile "SrtParser1.lua"
= lines
ss = SrtSub {}
for i=1,4 do ss:addline(lines[i]) end
= ss
ss.time()
--]]
-- ____ _ ____ _
-- / ___| _ __| |_/ ___| _ _| |__ ___
-- \___ \| '__| __\___ \| | | | '_ \/ __|
-- ___) | | | |_ ___) | |_| | |_) \__ \
-- |____/|_| \__|____/ \__,_|_.__/|___/
--
-- «SrtSubs» (to ".SrtSubs")
SrtSubs = Class {
type = "SrtSubs",
from = function (lines)
if type(lines) == "string" then lines = splitlines(lines) end
local sss = SrtSubs {}
for _,li in ipairs(lines) do sss:addline(li) end
return sss
end,
__tostring = function (sss) return mapconcat(tostring, sss, "\n") end,
__index = {
addline = function (sss,li)
if type(li) == "string" then li = SrtLine.from(li) end
if li:kind() == "n" then table.insert(sss, SrtSub {n=li[1]+0}); return end
if li:kind() == "time" then sss[#sss].time=li; return end
table.insert(sss[#sss], rtrim(li[1]))
end,
delcopyofnextfirstline = function (sss,i,dellast)
local A,B = sss[i],sss[i+1]
local A2,B1 = A[#A],B[1]
if A2 and B1 and A2==B1 then
if dellast then A[#A] = nil else table.remove(B,1) end
end
end,
clean1 = function (sss)
for i=1,#sss do sss[i]:deltrailingblanks() end
return sss
end,
clean2 = function (sss,dellast)
for i=1,#sss-1 do sss:delcopyofnextfirstline(i,dellast) end
return sss
end,
clean3 = function (sss)
for i=#sss,1,-1 do if #(sss[i]) == 0 then table.remove(sss,i) end end
return sss
end,
clean = function (sss,dellast)
return sss:clean1():clean2(dellast):clean3()
end,
sexps = function (sss)
local L = VTable {}
for _,ss in ipairs(sss) do table.insert(L, ss:sexp()) end
return concat(L, "\n")
end,
},
}
bigstr = ee_readfile "/tmp/subs/subs2.srt"
lines = VTable(splitlines(bigstr))
-- while #lines > 100 do lines[#lines] = nil end
slines = VTable {}
for _,li in ipairs(lines) do table.insert(slines, SrtLine.from(li)) end
-- «SrtSubs-tests» (to ".SrtSubs-tests")
--[[
* (eepitch-lua51)
* (eepitch-kill)
* (eepitch-lua51)
dofile "SrtParser1.lua"
sss = SrtSubs.from(lines)
-- sss = SrtSubs.from(lines):clean() -- buggy
sss = SrtSubs.from(lines):clean("dellast")
= sss
= sss:clean1(3)
= sss:clean2(3)
= sss
= sss[3]
PPP(sss[3])
PPP(sss[4])
-- = lines
-- Choose one:
= sss:clean("dellast")
= sss:clean()
= sss:sexps()
= sss
= sss
= sss:clean1()
= sss:clean2()
= sss:clean2("dellast")
= sss:clean3()
= sss:sexps()
= sss[1]
PPP(sss)
PPP(sss[1])
for i=1,#sss-1 do sss[i]:delstutter(sss[i+1][1]) end
for i=1,#sss do if #sss[i] == 0 then table.remove(sss,i) end end
= sss
--]]
-- Local Variables:
-- coding: utf-8-unix
-- End: