Warning: this is an htmlized version!
The original is here, and
the conversion rules are here.
-- This file:
--   http://anggtwu.net/LUA/Subtitles.lua.html
--   http://anggtwu.net/LUA/Subtitles.lua
--          (find-angg "LUA/Subtitles.lua")
-- Author: Eduardo Ochs <eduardoochs@gmail.com>
--
-- See: http://anggtwu.net/SUBTITLES/
--             (find-angg "SUBTITLES/")
--             (find-angg "LUA/Subtitles2.lua")
--             (find-angg "LUA/Subtitles3.lua")
--      
-- (defun e () (interactive) (find-angg "LUA/Subtitles.lua"))
-- (find-angg "LUA/lua50init.lua" "youtube_make_url")
--
-- TODO: TeX output. See: (find-SUBSfile "2023etel.lua")

-- «.SplitTime»		(to "SplitTime")
-- «.SplitTime-tests»	(to "SplitTime-tests")
-- «.Subtitles»		(to "Subtitles")
-- «.Subtitles-tests»	(to "Subtitles-tests")
-- «.mnu4-eu-leg»	(to "mnu4-eu-leg")

-- (find-es "lpeg" "re-quickref")



-- «SplitTime»  (to ".SplitTime")
SplitTime = Class {
  type  = "SplitTime",
  from  = function (time)
      local mm,ss = time:match("^(%d?%d):(%d%d)$")
      if ss then return SplitTime {mm=mm, ss=ss} end
      local hh,mm,ss = time:match("^(%d?%d):(%d%d):(%d%d)$")
      if ss then return SplitTime {hh=hh, mm=mm, ss=ss} end
      -- See: (find-angg "LUA/lua50init.lua" "youtube_make_url")
      -- TODO: add more formats
    end,
  __tostring = function (st) return mytostringp(st) end,
  __index = {
    pad = function (st, len, o)
        o = o or ""
        if #o < len then return ("0"):rep(len-#o)..o end
        return o
      end,
    nn  = function (st, o) return st:pad(2, o) end,
    nnn = function (st, o) return st:pad(3, o) end,
    HH  = function (st) return st:nn(st.hh) end,
    MM  = function (st) return st:nn(st.mm) end,
    SS  = function (st) return st:nn(st.ss) end,
    MSS = function (st) return st:nnn(st.mss) end,
    vtttime = function (st)
        return format("%s:%s:%s.%s", st:HH(), st:MM(), st:SS(), st:MSS())
      end,
  },
}

-- «SplitTime-tests»  (to ".SplitTime-tests")
--[[
* (eepitch-lua51)
* (eepitch-kill)
* (eepitch-lua51)
dofile "Subtitles.lua"
= SplitTime.from("12:34")
= SplitTime.from("12:34"):pad(3, "4")
= SplitTime.from("12:34"):pad(3, "4567")
= SplitTime.from("12:34"):vtttime()
= SplitTime.from("2:34"):vtttime()

--]]


-- «Subtitles»  (to ".Subtitles")
Subtitles = Class {
  type      = "Subtitles",
  new       = function () return Subtitles {} end,
  fromsexps = function (bigstr) return Subtitles.new():addsexps(bigstr) end,
  splitsexp = function (line)
      -- local line1 = line:gsub("\\.", "__")
      if line == "" or line:match("^[ \t]") then return end
      local time,text = line:match('"(.-)".-"(.+)"')
      text = text and text:gsub("\\(.)", "%1")
      return time,text
    end,
  --
  __index = {
    lang  = "pt-BR",
    lang  = "en-GB",
    addtt = function (sts, time, text)
        local splittime = SplitTime.from(time)
        if not splittime then printf("Warning! Bad time: %q\n", time) end
        local entry = {time=time, text=text, splittime=splittime}
        table.insert(sts, entry)
        return sts
      end,
    addtime = function (sts, time)
        return sts:addtt(time, nil)
      end,
    addsexp = function (sts, line)
        local time,text = Subtitles.splitsexp(line)
        if time then sts:addtt(time, text) end
      end,
    addsexps = function (sts, bigstr)
        for _,line in ipairs(splitlines(bigstr)) do sts:addsexp(line) end
        return sts
      end,
    encode = function (sts, text)
        local translations = {["<"]="&lt;", [">"]="&gt;"}
        return (text:gsub("[<>]", translations))
      end,
    vttentry = function (sts, i)
        local thistime = sts[i]  .splittime:vtttime()
        local nexttime = sts[i+1].splittime:vtttime()
        local text     = sts:encode(sts[i].text)
        return format("%s --> %s\n%s", thistime, nexttime, text)
      end,
    vttbody = function (sts)
        local f = function (i) return sts:vttentry(i) end
        return mapconcat(f, seq(1, #sts-1), "\n\n")
      end,
    vttheader = function (sts)
        return "WEBVTT\nKind: captions\nLanguage: "..sts.lang.."\n\n"
      end,
    vtt = function (sts)
        return sts:vttheader() .. sts:vttbody()
      end,
    --
    par = function (sts, i)
        local st = sts[i]
        local text = st.text:gsub("([%%_^])", "\\%1")
        return "\\par "..st.time.." "..text
      end,
  },
}


-- «Subtitles-tests»  (to ".Subtitles-tests")
--[==[
* (eepitch-lua51)
* (eepitch-kill)
* (eepitch-lua51)
dofile "Subtitles.lua"

infname  = "/tmp/o"
outfname = "/tmp/edrx-pict2e/2022-pict2e-lua.vtt"
outfname = "$S/http/anggtwu.net/eev-videos/2022jul10-apresentacao-C2.vtt"
sts = Subtitles.fromsexps(ee_readfile(infname)):addtime("8:13")
out = sts:vtt().."\n\n"
ee_writefile(outfname, out)

# (find-fline "/tmp/edrx-pict2e/2022-pict2e-lua.vtt")
# (find-fline "$S/http/anggtwu.net/eev-videos/2022jul10-apresentacao-C2.vtt")

* (eepitch-lua51)
* (eepitch-kill)
* (eepitch-lua51)
dofile "Subtitles.lua"

bigstr = [[
% (find-2022pict2eluavideo "06:45" "file - with the core definitions - and `b'")
% (find-2022pict2eluavideo "06:48" "defines b is the function that opens the")
% (find-2022pict2eluavideo "06:50" "second Lua file")
% (find-2022pict2eluavideo "06:52" "with some extra classes and some")
% (find-2022pict2eluavideo "07:89" "foo \"bar\" plic")
]]

sts = Subtitles.fromsexps(bigstr):addtime("8:13")
= sts:vtt()

for _,line in ipairs(splitlines(lines)) do
  sts:addsexp(line)
end

= sts:vttbody()

sts:addtt("foo", "bar")
sts:addtt(222, "bar")
= sts
PPPV(sts)
= sts:vttentry(1)
= sts:vttentry(2)


--]==]


-- «mnu4-eu-leg»  (to ".mnu4-eu-leg")
-- (find-es "ead" "mat-na-univ-4-eu-leg")
-- (find-TH "2021aulas-por-telegram" "legendas")
-- (find-fline "/sda5/videos/Math/" "Matematica_na_Universidade_-_Reuniao_4_-_11_jun_2021_-_Eduardo_Ochs-OofZQYkNVhg.webm")
-- (find-sh "cp -v /sda5/videos/Math/Matematica_na_Universidade_-_Reuniao_4_-_11_jun_2021_-_Eduardo_Ochs-OofZQYkNVhg.webm /tmp/mnu4eu.webm")
--[==[
* (eepitch-lua51)
* (eepitch-kill)
* (eepitch-lua51)
dofile "Subtitles.lua"
infname  = "/tmp/o"
outfname = "/tmp/mnu4eu.vtt"
sts = Subtitles.fromsexps(ee_readfile(infname)):addtime("50:35")
out = sts:vtt().."\n\n"
ee_writefile(outfname, out)

-- (find-fline "/tmp/" "mnu4eu.webm")
-- (find-video "/tmp/mnu4eu.webm")
-- (find-fline "/tmp/mnu4eu.vtt")

--]==]







-- Local Variables:
-- coding:  utf-8-unix
-- End: