Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Highlights share #9153

Merged
merged 10 commits into from Jun 3, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
49 changes: 42 additions & 7 deletions plugins/exporter.koplugin/main.lua
Expand Up @@ -127,8 +127,11 @@ function Exporter:isReady()
end

function Exporter:isDocReady()
local docless = self.ui == nil or self.ui.document == nil or self.view == nil
return not docless and self:isReady()
return self.ui and self.ui.document and self.view
end

function Exporter:isReadyToExport()
return self:isDocReady() and self:isReady()
end

function Exporter:requiresNetwork()
Expand All @@ -141,8 +144,12 @@ function Exporter:requiresNetwork()
end
end

function Exporter:getDocumentClippings()
return self.parser:parseCurrentDoc(self.view) or {}
end

function Exporter:exportCurrentNotes()
local clippings = self.parser:parseCurrentDoc(self.view)
local clippings = self:getDocumentClippings()
self:exportClippings(clippings)
end

Expand Down Expand Up @@ -193,20 +200,34 @@ end

function Exporter:addToMainMenu(menu_items)
local submenu = {}
local sharemenu = {}
for k, v in pairs(self.targets) do
submenu[#submenu + 1] = v:getMenuTable()
if v.shareable then
sharemenu[#sharemenu + 1] = { text = _("Share as " .. v.name), callback = function()
local clippings = self:getDocumentClippings()
local document
for _, notes in pairs(clippings) do
document = notes or {}
end

if #document > 0 then
v:share(document)
end
end
}
end
end
table.sort(submenu, function(v1, v2)
return v1.text < v2.text
end)

menu_items.exporter = {
local menu = {
text = _("Export highlights"),
sub_item_table = {
{
text = _("Export all notes in this book"),
enabled_func = function()
return self:isDocReady()
return self:isReadyToExport()
end,
callback = function()
self:exportCurrentNotes()
Expand All @@ -220,7 +241,7 @@ function Exporter:addToMainMenu(menu_items)
callback = function()
self:exportAllNotes()
end,
separator = true,
separator = #sharemenu == 0,
},
{
text = _("Choose formats and services"),
Expand All @@ -229,6 +250,20 @@ function Exporter:addToMainMenu(menu_items)
},
}
}
if #sharemenu > 0 then
table.sort(sharemenu, function(v1, v2)
return v1.text < v2.text
end)
table.insert(menu.sub_item_table, 3, {
text = _("Share all notes in this book"),
enabled_func = function()
return self:isDocReady()
end,
sub_item_table = sharemenu,
separator = true,
})
end
menu_items.exporter = menu
end

return Exporter
20 changes: 16 additions & 4 deletions plugins/exporter.koplugin/target/html.lua
@@ -1,9 +1,11 @@
local Device = require("device")
local logger = require("logger")
local slt2 = require("template/slt2")

-- html exporter
local HtmlExporter = require("base"):new {
name = "html",
shareable = Device:canShareText(),
}

local function format(booknotes)
Expand Down Expand Up @@ -32,31 +34,41 @@ local function format(booknotes)
return booknotes
end

function HtmlExporter:export(t)
function HtmlExporter:getRenderedContent(t)
local title
local path = self:getFilePath(t)
if #t == 1 then
title = t[1].title
else
title = "All Books"
end
local file = io.open(path, "w")
local template = slt2.loadfile(self.path .. "/template/note.tpl")
local clipplings = {}
for _, booknotes in ipairs(t) do
table.insert(clipplings, format(booknotes))
end
if not file then return false end
local content = slt2.render(template, {
clippings=clipplings,
document_title = title,
version = self:getVersion(),
timestamp = self:getTimeStamp(),
logger = logger
})
return content
end

function HtmlExporter:export(t)
local path = self:getFilePath(t)
local file = io.open(path, "w")
if not file then return false end
local content = self:getRenderedContent(t)
file:write(content)
file:close()
return true
end

function HtmlExporter:share(t)
local content = self:getRenderedContent({t})
Device:doShareText(content)
end

return HtmlExporter
10 changes: 9 additions & 1 deletion plugins/exporter.koplugin/target/json.lua
@@ -1,8 +1,9 @@
local json = require("json")

local Device = require("device")
-- json exporter
local JsonExporter = require("base"):new {
name = "json",
shareable = Device:canShareText(),
}

local function format(booknotes)
Expand Down Expand Up @@ -46,4 +47,11 @@ function JsonExporter:export(t)
return true
end

function JsonExporter:share(t)
local content = format(t)
content.created_on = self.timestamp or os.time()
content.version = self:getVersion()
Device:doShareText(content)
end

return JsonExporter
7 changes: 7 additions & 0 deletions plugins/exporter.koplugin/target/markdown.lua
@@ -1,4 +1,5 @@
local UIManager = require("ui/uimanager")
local Device = require("device")
local md = require("template/md")
local _ = require("gettext")
local T = require("ffi/util").template
Expand All @@ -7,6 +8,7 @@ local T = require("ffi/util").template
local MarkdownExporter = require("base"):new {
name = "markdown",
extension = "md",
shareable = Device:canShareText(),
init_callback = function(self, settings)
local changed = false
if not settings.formatting_options or settings.highlight_formatting == nil then
Expand Down Expand Up @@ -132,4 +134,9 @@ function MarkdownExporter:export(t)
return true
end

function MarkdownExporter:share(t)
local content = md.prepareBookContent(t, self.settings.formatting_options, self.settings.highlight_formatting) .. "\n\n_Generated at: " .. self:getTimeStamp() .. "_"
Device:doShareText(content)
end

return MarkdownExporter
63 changes: 39 additions & 24 deletions plugins/exporter.koplugin/target/text.lua
@@ -1,3 +1,4 @@
local Device = require("device")
local util = require("ffi/util")
local T = util.template
local _ = require("gettext")
Expand All @@ -6,41 +7,55 @@ local _ = require("gettext")
local TextExporter = require("base"):new {
name = "text",
extension = "txt",
shareable = Device:canShareText(),
}

local function format(booknotes)
local wide_space = "\227\128\128"
local content = ""
if booknotes.title then
content = content .. wide_space .. booknotes.title .. "\n" .. wide_space .. "\n"
end
for ___, entry in ipairs(booknotes) do
for ____, clipping in ipairs(entry) do
if clipping.chapter then
content = content .. wide_space .. clipping.chapter .. "\n" .. wide_space .. "\n"
end
local text = T(_("-- Page: %1, added on %2\n"), clipping.page, os.date("%c", clipping.time))
content = content .. wide_space .. wide_space .. text
if clipping.text then
content = content .. clipping.text
end
if clipping.note then
content = content .. "\n---\n" .. clipping.note
end
if clipping.image then
content = content .. _("<An image>")
end
content = content .. "\n-=-=-=-=-=-\n"
end
end
content = content .. "\n"
return content
end

function TextExporter:export(t)
-- Use wide_space to avoid crengine to treat it specially.
local wide_space = "\227\128\128"

local path = self:getFilePath(t)
local file = io.open(path, "a")
if not file then return false end
for __, booknotes in ipairs(t) do
if booknotes.title then
file:write(wide_space .. booknotes.title .. "\n" .. wide_space .. "\n")
end
for ___, entry in ipairs(booknotes) do
for ____, clipping in ipairs(entry) do
if clipping.chapter then
file:write(wide_space .. clipping.chapter .. "\n" .. wide_space .. "\n")
end
local text = T(_("-- Page: %1, added on %2\n"), clipping.page, os.date("%c", clipping.time))
file:write(wide_space .. wide_space .. text)
if clipping.text then
file:write(clipping.text)
end
if clipping.note then
file:write("\n---\n" .. clipping.note)
end
if clipping.image then
file:write(_("<An image>"))
end
file:write("\n-=-=-=-=-=-\n")
end
end
file:write("\n")
local content = format(booknotes)
file:write(content)
end
file:close()
return true
end

function TextExporter:share(t)
local content = format(t)
Device:doShareText(content)
end

return TextExporter