mirror of
https://github.com/koreader/koreader.git
synced 2025-12-13 20:36:53 +01:00
[Android] Highlights share (#9153)
This commit is contained in:
@@ -127,8 +127,11 @@ function Exporter:isReady()
|
|||||||
end
|
end
|
||||||
|
|
||||||
function Exporter:isDocReady()
|
function Exporter:isDocReady()
|
||||||
local docless = self.ui == nil or self.ui.document == nil or self.view == nil
|
return self.ui and self.ui.document and self.view
|
||||||
return not docless and self:isReady()
|
end
|
||||||
|
|
||||||
|
function Exporter:isReadyToExport()
|
||||||
|
return self:isDocReady() and self:isReady()
|
||||||
end
|
end
|
||||||
|
|
||||||
function Exporter:requiresNetwork()
|
function Exporter:requiresNetwork()
|
||||||
@@ -141,8 +144,12 @@ function Exporter:requiresNetwork()
|
|||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
function Exporter:getDocumentClippings()
|
||||||
|
return self.parser:parseCurrentDoc(self.view) or {}
|
||||||
|
end
|
||||||
|
|
||||||
function Exporter:exportCurrentNotes()
|
function Exporter:exportCurrentNotes()
|
||||||
local clippings = self.parser:parseCurrentDoc(self.view)
|
local clippings = self:getDocumentClippings()
|
||||||
self:exportClippings(clippings)
|
self:exportClippings(clippings)
|
||||||
end
|
end
|
||||||
|
|
||||||
@@ -193,20 +200,34 @@ end
|
|||||||
|
|
||||||
function Exporter:addToMainMenu(menu_items)
|
function Exporter:addToMainMenu(menu_items)
|
||||||
local submenu = {}
|
local submenu = {}
|
||||||
|
local sharemenu = {}
|
||||||
for k, v in pairs(self.targets) do
|
for k, v in pairs(self.targets) do
|
||||||
submenu[#submenu + 1] = v:getMenuTable()
|
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
|
end
|
||||||
table.sort(submenu, function(v1, v2)
|
table.sort(submenu, function(v1, v2)
|
||||||
return v1.text < v2.text
|
return v1.text < v2.text
|
||||||
end)
|
end)
|
||||||
|
local menu = {
|
||||||
menu_items.exporter = {
|
|
||||||
text = _("Export highlights"),
|
text = _("Export highlights"),
|
||||||
sub_item_table = {
|
sub_item_table = {
|
||||||
{
|
{
|
||||||
text = _("Export all notes in this book"),
|
text = _("Export all notes in this book"),
|
||||||
enabled_func = function()
|
enabled_func = function()
|
||||||
return self:isDocReady()
|
return self:isReadyToExport()
|
||||||
end,
|
end,
|
||||||
callback = function()
|
callback = function()
|
||||||
self:exportCurrentNotes()
|
self:exportCurrentNotes()
|
||||||
@@ -220,7 +241,7 @@ function Exporter:addToMainMenu(menu_items)
|
|||||||
callback = function()
|
callback = function()
|
||||||
self:exportAllNotes()
|
self:exportAllNotes()
|
||||||
end,
|
end,
|
||||||
separator = true,
|
separator = #sharemenu == 0,
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
text = _("Choose formats and services"),
|
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
|
end
|
||||||
|
|
||||||
return Exporter
|
return Exporter
|
||||||
|
|||||||
@@ -1,9 +1,11 @@
|
|||||||
|
local Device = require("device")
|
||||||
local logger = require("logger")
|
local logger = require("logger")
|
||||||
local slt2 = require("template/slt2")
|
local slt2 = require("template/slt2")
|
||||||
|
|
||||||
-- html exporter
|
-- html exporter
|
||||||
local HtmlExporter = require("base"):new {
|
local HtmlExporter = require("base"):new {
|
||||||
name = "html",
|
name = "html",
|
||||||
|
shareable = Device:canShareText(),
|
||||||
}
|
}
|
||||||
|
|
||||||
local function format(booknotes)
|
local function format(booknotes)
|
||||||
@@ -32,21 +34,18 @@ local function format(booknotes)
|
|||||||
return booknotes
|
return booknotes
|
||||||
end
|
end
|
||||||
|
|
||||||
function HtmlExporter:export(t)
|
function HtmlExporter:getRenderedContent(t)
|
||||||
local title
|
local title
|
||||||
local path = self:getFilePath(t)
|
|
||||||
if #t == 1 then
|
if #t == 1 then
|
||||||
title = t[1].title
|
title = t[1].title
|
||||||
else
|
else
|
||||||
title = "All Books"
|
title = "All Books"
|
||||||
end
|
end
|
||||||
local file = io.open(path, "w")
|
|
||||||
local template = slt2.loadfile(self.path .. "/template/note.tpl")
|
local template = slt2.loadfile(self.path .. "/template/note.tpl")
|
||||||
local clipplings = {}
|
local clipplings = {}
|
||||||
for _, booknotes in ipairs(t) do
|
for _, booknotes in ipairs(t) do
|
||||||
table.insert(clipplings, format(booknotes))
|
table.insert(clipplings, format(booknotes))
|
||||||
end
|
end
|
||||||
if not file then return false end
|
|
||||||
local content = slt2.render(template, {
|
local content = slt2.render(template, {
|
||||||
clippings=clipplings,
|
clippings=clipplings,
|
||||||
document_title = title,
|
document_title = title,
|
||||||
@@ -54,9 +53,22 @@ function HtmlExporter:export(t)
|
|||||||
timestamp = self:getTimeStamp(),
|
timestamp = self:getTimeStamp(),
|
||||||
logger = logger
|
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:write(content)
|
||||||
file:close()
|
file:close()
|
||||||
return true
|
return true
|
||||||
end
|
end
|
||||||
|
|
||||||
|
function HtmlExporter:share(t)
|
||||||
|
local content = self:getRenderedContent({t})
|
||||||
|
Device:doShareText(content)
|
||||||
|
end
|
||||||
|
|
||||||
return HtmlExporter
|
return HtmlExporter
|
||||||
|
|||||||
@@ -1,8 +1,9 @@
|
|||||||
local json = require("json")
|
local json = require("json")
|
||||||
|
local Device = require("device")
|
||||||
-- json exporter
|
-- json exporter
|
||||||
local JsonExporter = require("base"):new {
|
local JsonExporter = require("base"):new {
|
||||||
name = "json",
|
name = "json",
|
||||||
|
shareable = Device:canShareText(),
|
||||||
}
|
}
|
||||||
|
|
||||||
local function format(booknotes)
|
local function format(booknotes)
|
||||||
@@ -46,4 +47,11 @@ function JsonExporter:export(t)
|
|||||||
return true
|
return true
|
||||||
end
|
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
|
return JsonExporter
|
||||||
|
|||||||
@@ -1,4 +1,5 @@
|
|||||||
local UIManager = require("ui/uimanager")
|
local UIManager = require("ui/uimanager")
|
||||||
|
local Device = require("device")
|
||||||
local md = require("template/md")
|
local md = require("template/md")
|
||||||
local _ = require("gettext")
|
local _ = require("gettext")
|
||||||
local T = require("ffi/util").template
|
local T = require("ffi/util").template
|
||||||
@@ -7,6 +8,7 @@ local T = require("ffi/util").template
|
|||||||
local MarkdownExporter = require("base"):new {
|
local MarkdownExporter = require("base"):new {
|
||||||
name = "markdown",
|
name = "markdown",
|
||||||
extension = "md",
|
extension = "md",
|
||||||
|
shareable = Device:canShareText(),
|
||||||
init_callback = function(self, settings)
|
init_callback = function(self, settings)
|
||||||
local changed = false
|
local changed = false
|
||||||
if not settings.formatting_options or settings.highlight_formatting == nil then
|
if not settings.formatting_options or settings.highlight_formatting == nil then
|
||||||
@@ -132,4 +134,9 @@ function MarkdownExporter:export(t)
|
|||||||
return true
|
return true
|
||||||
end
|
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
|
return MarkdownExporter
|
||||||
|
|||||||
@@ -1,3 +1,4 @@
|
|||||||
|
local Device = require("device")
|
||||||
local util = require("ffi/util")
|
local util = require("ffi/util")
|
||||||
local T = util.template
|
local T = util.template
|
||||||
local _ = require("gettext")
|
local _ = require("gettext")
|
||||||
@@ -6,41 +7,55 @@ local _ = require("gettext")
|
|||||||
local TextExporter = require("base"):new {
|
local TextExporter = require("base"):new {
|
||||||
name = "text",
|
name = "text",
|
||||||
extension = "txt",
|
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)
|
function TextExporter:export(t)
|
||||||
-- Use wide_space to avoid crengine to treat it specially.
|
-- Use wide_space to avoid crengine to treat it specially.
|
||||||
local wide_space = "\227\128\128"
|
|
||||||
local path = self:getFilePath(t)
|
local path = self:getFilePath(t)
|
||||||
local file = io.open(path, "a")
|
local file = io.open(path, "a")
|
||||||
if not file then return false end
|
if not file then return false end
|
||||||
for __, booknotes in ipairs(t) do
|
for __, booknotes in ipairs(t) do
|
||||||
if booknotes.title then
|
local content = format(booknotes)
|
||||||
file:write(wide_space .. booknotes.title .. "\n" .. wide_space .. "\n")
|
file:write(content)
|
||||||
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")
|
|
||||||
end
|
end
|
||||||
file:close()
|
file:close()
|
||||||
return true
|
return true
|
||||||
end
|
end
|
||||||
|
|
||||||
|
function TextExporter:share(t)
|
||||||
|
local content = format(t)
|
||||||
|
Device:doShareText(content)
|
||||||
|
end
|
||||||
|
|
||||||
return TextExporter
|
return TextExporter
|
||||||
|
|||||||
Reference in New Issue
Block a user