[plugin] Exporter: optimized JSON export (#8904)

I've implemented a better JSON export format that removes redundant lists and objects and introduces the `entries` key containing all the entries.

It also add `drawer` values from highlight so that user can use this piece of metadata to generate desirable output.
reviewable/pr8923/r1
Utsob Roy 2 years ago committed by GitHub
parent fe6f4b68b3
commit d266d320e2
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

@ -256,6 +256,7 @@ function MyClipping:parseHighlight(highlights, bookmarks, book)
clipping.time = self:getTime(item.datetime or "")
clipping.text = self:getText(item.text)
clipping.chapter = item.chapter
clipping.drawer = item.drawer
for _, bookmark in pairs(bookmarks) do
if bookmark.datetime == item.datetime and bookmark.text then
local bookmark_quote = bookmark.text:match(pattern)

@ -609,10 +609,47 @@ function Exporter:exportBooknotesToHTML(title, booknotes)
end
end
function Exporter:prepareBooknotesForJSON(booknotes)
local exportable = {
title = booknotes.title,
author = booknotes.author,
entries = {},
exported = booknotes.exported,
file = booknotes.file
}
for _, entry in ipairs(booknotes) do
table.insert(exportable.entries, entry[1])
end
return exportable
end
-- This function should handle both multidocument export and single exports.
-- For Single Exports, it will create a JSON file with a object ({}) as root node.
-- For Multidocument export, it will create a JSON file with an array ([]) as root node.
function Exporter:exportToJSON(clippings)
local file = io.open(self.json_clipping_file, "a")
if file then
local exportable = {}
if table.getn(clippings) == 1 then
-- We will handle single document export here.
exportable = self:prepareBooknotesForJSON(clippings[0])
else
for _, booknotes in ipairs(clippings) do
table.insert(exportable, self:prepareBooknotesForJSON(booknotes))
end
end
file:write(json.encode(exportable))
file:write("\n")
file:close()
end
end
function Exporter:exportBooknotesToJSON(title, booknotes)
logger.dbg("booknotes", booknotes)
local file = io.open(self.json_clipping_file, "a")
if file then
file:write(json.encode(booknotes))
local exportable = self:prepareBooknotesForJSON(booknotes)
file:write(json.encode(exportable))
file:write("\n")
file:close()
end

Loading…
Cancel
Save