2
0
mirror of https://github.com/koreader/koreader synced 2024-11-18 03:25:46 +00:00
koreader/plugins/exporter.koplugin/target/json.lua
Utsob Roy bc0a55f093
Refactor exporter.koplugin (#8944)
Changed:
  - select multiple targets and export to them in a single click.
  - local targets (html, json and text) now are timestamped. Exporting booknotes on already exported documents will generate a new file with all the highlights present at export time. Previous files won't be deleted.

Fixed:
  - chapters are now correctly represented in html output.
  - json issues when exporting the whole history.
  - joplin and readwise crashes when they're unable to reach the server
  - joplin update notes mechanism.
  - joplin is able to recreate the notebook if the user deletes or renames its current one.
  - highlights of read-only documents are also added when exporting the whole history (affects mostly android, might affect desktop targets)

Co-authored-by: Utsob Roy <roy@utsob.me>
2022-05-06 22:44:28 +02:00

50 lines
1.2 KiB
Lua

local json = require("json")
-- json exporter
local JsonExporter = require("base"):new {
name = "json",
}
local function format(booknotes)
local t = {
title = booknotes.title,
author = booknotes.author,
entries = {},
exported = booknotes.exported,
file = booknotes.file
}
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
exportable = format(t[1])
exportable.created_on = timestamp
exportable.version = self:getVersion()
else
local documents = {}
for _, booknotes in ipairs(t) do
table.insert(documents, format(booknotes))
end
exportable = {
created_on = timestamp,
version = self:getVersion(),
documents = documents
}
end
local file = io.open(path, "w")
if not file then return false end
file:write(json.encode(exportable))
file:write("\n")
file:close()
return true
end
return JsonExporter