2022-05-06 20:44:28 +00:00
|
|
|
--[[--
|
|
|
|
Base for highlight exporters.
|
|
|
|
|
|
|
|
Each target should inherit from this class and implement *at least* an `export` function.
|
|
|
|
|
|
|
|
@module baseexporter
|
|
|
|
]]
|
|
|
|
|
2022-10-26 09:18:27 +00:00
|
|
|
local Device = require("device")
|
2022-05-25 16:14:05 +00:00
|
|
|
local getSafeFilename = require("util").getSafeFilename
|
2022-10-26 09:18:27 +00:00
|
|
|
local _ = require("gettext")
|
|
|
|
|
2022-05-06 20:44:28 +00:00
|
|
|
local BaseExporter = {
|
|
|
|
clipping_dir = require("datastorage"):getDataDir() .. "/clipboard"
|
|
|
|
}
|
|
|
|
|
|
|
|
function BaseExporter:new(o)
|
|
|
|
o = o or {}
|
|
|
|
assert(type(o.name) == "string", "name is mandatory")
|
|
|
|
setmetatable(o, self)
|
|
|
|
self.__index = self
|
|
|
|
return o:_init()
|
|
|
|
end
|
|
|
|
|
|
|
|
function BaseExporter:_init()
|
|
|
|
self.extension = self.extension or self.name
|
|
|
|
self.is_remote = self.is_remote or false
|
|
|
|
self.version = self.version or "1.0.0"
|
2022-10-26 09:18:27 +00:00
|
|
|
self.shareable = self.is_remote and nil or Device:canShareText()
|
2022-05-06 20:44:28 +00:00
|
|
|
self:loadSettings()
|
2022-05-28 08:32:36 +00:00
|
|
|
if type(self.init_callback) == "function" then
|
|
|
|
local changed, settings = self:init_callback(self.settings)
|
|
|
|
if changed then
|
|
|
|
self.settings = settings
|
|
|
|
self:saveSettings()
|
|
|
|
end
|
|
|
|
end
|
2022-05-06 20:44:28 +00:00
|
|
|
return self
|
|
|
|
end
|
|
|
|
|
|
|
|
--[[--
|
|
|
|
Export timestamp
|
|
|
|
|
|
|
|
@treturn string timestamp
|
|
|
|
]]
|
|
|
|
function BaseExporter:getTimeStamp()
|
|
|
|
local ts = self.timestamp or os.time()
|
2022-05-25 16:14:05 +00:00
|
|
|
return os.date("%Y-%m-%d-%H-%M-%S", ts)
|
2022-05-06 20:44:28 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
--[[--
|
|
|
|
Exporter version
|
|
|
|
|
|
|
|
@treturn string version
|
|
|
|
]]
|
|
|
|
function BaseExporter:getVersion()
|
|
|
|
return self.name .. "/" .. self.version
|
|
|
|
end
|
|
|
|
|
|
|
|
--[[--
|
|
|
|
Loads settings for the exporter
|
|
|
|
]]
|
|
|
|
function BaseExporter:loadSettings()
|
|
|
|
local plugin_settings = G_reader_settings:readSetting("exporter") or {}
|
|
|
|
self.settings = plugin_settings[self.name] or {}
|
2023-05-18 20:11:35 +00:00
|
|
|
if plugin_settings.clipping_dir then
|
|
|
|
self.clipping_dir = plugin_settings.clipping_dir
|
|
|
|
end
|
2022-05-06 20:44:28 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
--[[--
|
|
|
|
Saves settings for the exporter
|
|
|
|
]]
|
|
|
|
function BaseExporter:saveSettings()
|
|
|
|
local plugin_settings = G_reader_settings:readSetting("exporter") or {}
|
|
|
|
plugin_settings[self.name] = self.settings
|
|
|
|
G_reader_settings:saveSetting("exporter", plugin_settings)
|
|
|
|
self.new_settings = true
|
|
|
|
end
|
|
|
|
|
|
|
|
--[[--
|
|
|
|
Exports a table of booknotes to local format or remote service
|
|
|
|
|
|
|
|
@param t table of booknotes
|
|
|
|
@treturn bool success
|
|
|
|
]]
|
|
|
|
function BaseExporter:export(t) end
|
|
|
|
|
|
|
|
--[[--
|
|
|
|
File path where the exporter writes its output
|
|
|
|
|
|
|
|
@param t table of booknotes
|
|
|
|
@treturn string absolute path or nil
|
|
|
|
]]
|
|
|
|
function BaseExporter:getFilePath(t)
|
|
|
|
if not self.is_remote then
|
2022-05-25 16:14:05 +00:00
|
|
|
local filename = string.format("%s-%s.%s",
|
2022-05-06 20:44:28 +00:00
|
|
|
self:getTimeStamp(),
|
2023-04-28 23:19:13 +00:00
|
|
|
#t == 1 and t[1].output_filename or self.all_books_title or "all-books",
|
2022-05-06 20:44:28 +00:00
|
|
|
self.extension)
|
2022-05-25 16:14:05 +00:00
|
|
|
return self.clipping_dir .. "/" .. getSafeFilename(filename)
|
2022-05-06 20:44:28 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
--[[--
|
|
|
|
Configuration menu for the exporter
|
|
|
|
|
|
|
|
@treturn table menu with exporter settings
|
|
|
|
]]
|
|
|
|
function BaseExporter:getMenuTable()
|
|
|
|
return {
|
|
|
|
text = self.name:gsub("^%l", string.upper),
|
|
|
|
checked_func = function()
|
|
|
|
return self:isEnabled()
|
|
|
|
end,
|
|
|
|
callback = function()
|
|
|
|
self:toggleEnabled()
|
|
|
|
end,
|
|
|
|
}
|
|
|
|
end
|
|
|
|
|
|
|
|
--[[--
|
|
|
|
Checks if the exporter is ready to export
|
|
|
|
|
|
|
|
@treturn bool ready
|
|
|
|
]]
|
|
|
|
function BaseExporter:isReadyToExport()
|
|
|
|
return true
|
|
|
|
end
|
|
|
|
|
|
|
|
--[[--
|
|
|
|
Checks if the exporter was enabled by the user and it is ready to export
|
|
|
|
|
|
|
|
@treturn bool enabled
|
|
|
|
]]
|
|
|
|
function BaseExporter:isEnabled()
|
|
|
|
return self.settings.enabled and self:isReadyToExport()
|
|
|
|
end
|
|
|
|
|
|
|
|
--[[--
|
|
|
|
Toggles exporter enabled state if it's ready to export
|
|
|
|
]]
|
|
|
|
function BaseExporter:toggleEnabled()
|
|
|
|
if self:isReadyToExport() then
|
|
|
|
self.settings.enabled = not self.settings.enabled
|
|
|
|
self:saveSettings()
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2022-10-26 09:18:27 +00:00
|
|
|
--[[--
|
|
|
|
Shares text with other apps
|
|
|
|
]]
|
|
|
|
function BaseExporter:shareText(text, title)
|
|
|
|
local msg_reason = _("Share") .. " " .. self.name
|
|
|
|
local msg_text = type(text) == "string" and text
|
|
|
|
local msg_title = type(title) == "string" and title
|
|
|
|
Device:doShareText(msg_text, msg_reason, msg_title, self.mimetype)
|
|
|
|
end
|
|
|
|
|
2022-05-06 20:44:28 +00:00
|
|
|
return BaseExporter
|