From d266d320e2cb71b1077805a254148848564d84fe Mon Sep 17 00:00:00 2001 From: Utsob Roy Date: Sat, 19 Mar 2022 14:59:04 +0600 Subject: [PATCH] [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. --- plugins/exporter.koplugin/clip.lua | 1 + plugins/exporter.koplugin/main.lua | 39 +++++++++++++++++++++++++++++- 2 files changed, 39 insertions(+), 1 deletion(-) diff --git a/plugins/exporter.koplugin/clip.lua b/plugins/exporter.koplugin/clip.lua index 195a2f1a4..64f3e130d 100644 --- a/plugins/exporter.koplugin/clip.lua +++ b/plugins/exporter.koplugin/clip.lua @@ -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) diff --git a/plugins/exporter.koplugin/main.lua b/plugins/exporter.koplugin/main.lua index bf7b93a5c..02cff0502 100644 --- a/plugins/exporter.koplugin/main.lua +++ b/plugins/exporter.koplugin/main.lua @@ -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