[Android] Highlights share (#9153)

reviewable/pr9165/r1
Utsob Roy 2 years ago committed by GitHub
parent ee6197efff
commit 71c7a8a042
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

@ -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()
@ -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
@ -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()
@ -220,7 +241,7 @@ function Exporter:addToMainMenu(menu_items)
callback = function()
self:exportAllNotes()
end,
separator = true,
separator = #sharemenu == 0,
},
{
text = _("Choose formats and services"),
@ -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

@ -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)
@ -32,21 +34,18 @@ 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,
@ -54,9 +53,22 @@ function HtmlExporter:export(t)
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

@ -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)
@ -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

@ -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
@ -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
@ -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

@ -1,3 +1,4 @@
local Device = require("device")
local util = require("ffi/util")
local T = util.template
local _ = require("gettext")
@ -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

Loading…
Cancel
Save