-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.lua
More file actions
85 lines (71 loc) · 2.36 KB
/
main.lua
File metadata and controls
85 lines (71 loc) · 2.36 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
--[[
---------------------------------------------------
Copyright (C) wst.pub, All Rights Reserved.
---------------------------------------------------
]]
---@param s string string has redundant spaces
local function delete_spaces(s)
local del = 0
for i = 1, #s do
if string.sub(s, -i, -i) == " " then
del = del + 1
else
break
end
end
return string.sub(s, 1, -del - 1)
end
---@param str string
---@return table
local function split(str)
local t = {}
for chunk in string.gmatch(str, "[^\n]+") do
table.insert(t, chunk)
end
return t
end
local function parse_srt(srt_raw)
local inBlock
local last_line
local last_index
local parsed_srt = {}
local next_line_to_be_processed = -1
local srt_raw = split(srt_raw)
for i = 1, #srt_raw do
if i >= next_line_to_be_processed then
if inBlock ~= true then
local index = tonumber(srt_raw[i])
if index then
inBlock = true
last_line = "index"
last_index = index
parsed_srt[index] = {}
end
else
if last_line == "index" then
parsed_srt[last_index]["time"] = srt_raw[i]
last_line = "time"
else
if last_line == "time" then
parsed_srt[last_index]["text"] = delete_spaces(srt_raw[i])
local skip = 1
local exit = false
repeat
local line = srt_raw[i + skip]
if line ~= nil and line ~= "" then
parsed_srt[last_index]["text"] = parsed_srt[last_index]["text"] .. " " .. delete_spaces(line)
skip = skip + 1
else
exit = true
end
until exit == true
next_line_to_be_processed = i + skip + 1
inBlock = false
end
end
end
end
end
return parsed_srt, tonumber(srt_raw[1]), last_index
end
return parse_srt