Commit: 5b14d134f198cfd96a62daeffbef638c8bb0c976
Parent: 62b2f37b2c48374587d402f82e200d34ff59e86d
Author: Ren Tatsumoto
Date: Thu, 27 Aug 2020 13:40:18 +0300
avoid duplilcate subs and sort subs before exporting.
Diffstat:
| M | subs2srs.lua | | | 47 | +++++++++++++++++++++++++++++++++++++++++++---- |
1 file changed, 43 insertions(+), 4 deletions(-)
diff --git a/subs2srs.lua b/subs2srs.lua
@@ -35,11 +35,15 @@ local mpopt = require('mp.options')
mpopt.read_options(config, "subs2srs")
+-- namespaces
local subs
local clip_autocopy
local ffmpeg
local ankiconnect
+-- classes
+local Subtitle
+
------------------------------------------------------------
-- utility functions
@@ -47,6 +51,15 @@ function string:endswith(suffix)
return self:match(string.format('%s$', suffix))
end
+function table.contains(table, element)
+ for _, value in pairs(table) do
+ if value == element then
+ return true
+ end
+ end
+ return false
+end
+
local function check_config_sanity()
if not config.collection_path:endswith('/') then
-- The user forgot to add a slash at the end of the collection path
@@ -402,7 +415,7 @@ subs.get_current = function()
local sub_delay = mp.get_property_native("sub-delay")
- return {
+ return Subtitle:new{
['text'] = trim(sub_text),
['start'] = mp.get_property_number("sub-start") + sub_delay,
['end'] = mp.get_property_number("sub-end") + sub_delay
@@ -414,7 +427,9 @@ subs.get = function()
return subs.get_current()
end
- local sub = {
+ table.sort(subs.list)
+
+ local sub = Subtitle:new{
['text'] = '',
['start'] = subs.list[1]['start'],
['end'] = subs.list[#subs.list]['end'],
@@ -429,12 +444,13 @@ subs.get = function()
sub['text'] = sub['text'] .. value['text']
end
+ subs.clear()
return sub
end
subs.append = function()
local sub = subs.get_current()
- if sub ~= nil then
+ if sub ~= nil and not table.contains(subs.list, sub) then
table.insert(subs.list, sub)
end
end
@@ -490,11 +506,34 @@ clip_autocopy.toggle = function()
end
------------------------------------------------------------
+-- Subtitle class provides methods for comparing subtitle lines
+
+Subtitle = {
+ ['text'] = '',
+ ['start'] = 0,
+ ['end'] = 0,
+}
+
+function Subtitle:new(o)
+ o = o or {}
+ setmetatable(o, self)
+ self.__index = self
+ return o
+end
+
+Subtitle.__eq = function(lhs, rhs)
+ return lhs['text'] == rhs['text']
+end
+
+Subtitle.__lt = function (lhs, rhs)
+ return lhs['start'] < rhs['start']
+end
+
+------------------------------------------------------------
-- main
local function export_to_anki()
local sub = subs.get()
- subs.clear()
if sub ~= nil then
local filename = construct_filename(sub)