2022-06-05 14:52:05 +00:00
|
|
|
local rapidjson = require("rapidjson")
|
2022-10-12 21:50:44 +00:00
|
|
|
local md5 = require("ffi/MD5")
|
|
|
|
local _ = require("gettext")
|
2022-06-05 14:52:05 +00:00
|
|
|
|
2022-05-06 20:44:28 +00:00
|
|
|
-- json exporter
|
|
|
|
local JsonExporter = require("base"):new {
|
|
|
|
name = "json",
|
2022-10-26 09:18:27 +00:00
|
|
|
-- the proper mimetype is "application/json" as stated in
|
|
|
|
-- https://www.iana.org/assignments/media-types/application/json
|
|
|
|
-- but we follow google recommendations because we just share on android.
|
|
|
|
-- https://developer.android.com/training/sharing/send#using-the-right-mimetype
|
|
|
|
mimetype = "text/json",
|
2022-05-06 20:44:28 +00:00
|
|
|
}
|
|
|
|
|
2022-10-12 21:50:44 +00:00
|
|
|
function JsonExporter:getMenuTable()
|
|
|
|
return {
|
|
|
|
text = _("Json"),
|
|
|
|
checked_func = function() return self:isEnabled() end,
|
|
|
|
sub_item_table = {
|
|
|
|
{
|
|
|
|
text = _("Export to Json"),
|
|
|
|
checked_func = function() return self:isEnabled() end,
|
|
|
|
callback = function() self:toggleEnabled() end,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
text = _("Include book checksum in export"),
|
|
|
|
checked_func = function() return self.settings.bookChecksum end,
|
|
|
|
callback = function()
|
|
|
|
self.settings.bookChecksum = not self.settings.bookChecksum
|
|
|
|
self:saveSettings()
|
|
|
|
end,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
end
|
|
|
|
|
|
|
|
local function format(booknotes, settings)
|
2022-05-06 20:44:28 +00:00
|
|
|
local t = {
|
|
|
|
title = booknotes.title,
|
|
|
|
author = booknotes.author,
|
|
|
|
entries = {},
|
|
|
|
exported = booknotes.exported,
|
2022-06-16 11:54:14 +00:00
|
|
|
file = booknotes.file,
|
2022-10-12 21:50:44 +00:00
|
|
|
md5sum = settings.bookChecksum and md5.sumFile(booknotes.file) or nil,
|
2022-06-16 11:54:14 +00:00
|
|
|
number_of_pages = booknotes.number_of_pages
|
2022-05-06 20:44:28 +00:00
|
|
|
}
|
|
|
|
for _, entry in ipairs(booknotes) do
|
|
|
|
table.insert(t.entries, entry[1])
|
|
|
|
end
|
|
|
|
return t
|
|
|
|
end
|
|
|
|
|
|
|
|
function JsonExporter:export(t)
|
|
|
|
local exportable
|
|
|
|
local timestamp = self.timestamp or os.time()
|
|
|
|
local path = self:getFilePath(t)
|
|
|
|
if #t == 1 then
|
2022-10-12 21:50:44 +00:00
|
|
|
exportable = format(t[1], self.settings)
|
2022-05-06 20:44:28 +00:00
|
|
|
exportable.created_on = timestamp
|
|
|
|
exportable.version = self:getVersion()
|
|
|
|
else
|
|
|
|
local documents = {}
|
|
|
|
for _, booknotes in ipairs(t) do
|
2022-10-12 21:50:44 +00:00
|
|
|
table.insert(documents, format(booknotes, self.settings))
|
2022-05-06 20:44:28 +00:00
|
|
|
end
|
|
|
|
exportable = {
|
|
|
|
created_on = timestamp,
|
|
|
|
version = self:getVersion(),
|
|
|
|
documents = documents
|
|
|
|
}
|
|
|
|
end
|
|
|
|
local file = io.open(path, "w")
|
|
|
|
if not file then return false end
|
2022-06-05 14:52:05 +00:00
|
|
|
file:write(rapidjson.encode(exportable, {pretty = true}))
|
2022-05-06 20:44:28 +00:00
|
|
|
file:write("\n")
|
|
|
|
file:close()
|
|
|
|
return true
|
|
|
|
end
|
|
|
|
|
2022-06-03 17:56:10 +00:00
|
|
|
function JsonExporter:share(t)
|
|
|
|
local content = format(t)
|
|
|
|
content.created_on = self.timestamp or os.time()
|
|
|
|
content.version = self:getVersion()
|
2022-10-26 09:18:27 +00:00
|
|
|
self:shareText(rapidjson.encode(content, {pretty = true}))
|
2022-06-03 17:56:10 +00:00
|
|
|
end
|
|
|
|
|
2022-05-06 20:44:28 +00:00
|
|
|
return JsonExporter
|