2
0
mirror of https://github.com/koreader/koreader synced 2024-11-11 19:11:14 +00:00

Exporter: choose highlight styles to be exported (#12431)

This commit is contained in:
hius07 2024-08-31 19:28:48 +03:00 committed by GitHub
parent 38c8d66b5b
commit 49e885321a
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
3 changed files with 96 additions and 61 deletions

View File

@ -1866,6 +1866,10 @@ function ReaderHighlight:onCycleHighlightAction()
return true
end
function ReaderHighlight.getHighlightStyles()
return highlight_style
end
function ReaderHighlight:getHighlightStyleString(style) -- for bookmark list
for _, v in ipairs(highlight_style) do
if v[2] == style then

View File

@ -236,8 +236,9 @@ function MyClipping:getImage(image)
end
function MyClipping:parseAnnotations(annotations, book)
local settings = G_reader_settings:readSetting("exporter")
for _, item in ipairs(annotations) do
if item.drawer then
if item.drawer and not (settings.highlight_styles and settings.highlight_styles[item.drawer] == false) then
local clipping = {
sort = "highlight",
page = item.pageref or item.pageno,
@ -246,6 +247,7 @@ function MyClipping:parseAnnotations(annotations, book)
note = item.note and self:getText(item.note),
chapter = item.chapter,
drawer = item.drawer,
color = item.color,
}
table.insert(book, { clipping })
end
@ -264,49 +266,52 @@ function MyClipping:parseHighlight(highlights, bookmarks, book)
"%d%d%d%d%-%d%d%-%d%d %d%d:%d%d:%d%d") .. "$"
local orphan_highlights = {}
local settings = G_reader_settings:readSetting("exporter")
for page, items in pairs(highlights) do
for _, item in ipairs(items) do
local clipping = {
sort = "highlight",
page = page,
time = self:getTime(item.datetime or ""),
text = self:getText(item.text),
chapter = item.chapter,
drawer = item.drawer,
}
local bookmark_found = false
for _, bookmark in pairs(bookmarks) do
if bookmark.datetime == item.datetime then
if bookmark.text then
local bookmark_quote = bookmark.text:match(pattern)
if bookmark_quote ~= clipping.text and bookmark.text ~= clipping.text then
-- use modified quoted text or entire bookmark text if it's not a match
clipping.note = bookmark_quote or bookmark.text
if not (settings.highlight_styles and settings.highlight_styles[item.drawer] == false) then
local clipping = {
sort = "highlight",
page = page,
time = self:getTime(item.datetime or ""),
text = self:getText(item.text),
chapter = item.chapter,
drawer = item.drawer,
}
local bookmark_found = false
for _, bookmark in pairs(bookmarks) do
if bookmark.datetime == item.datetime then
if bookmark.text then
local bookmark_quote = bookmark.text:match(pattern)
if bookmark_quote ~= clipping.text and bookmark.text ~= clipping.text then
-- use modified quoted text or entire bookmark text if it's not a match
clipping.note = bookmark_quote or bookmark.text
end
end
bookmark_found = true
break
end
bookmark_found = true
break
end
end
if not bookmark_found then
table.insert(orphan_highlights, { clipping })
end
if item.text == "" and item.pos0 and item.pos1 and
item.pos0.x and item.pos0.y and
item.pos1.x and item.pos1.y then
-- highlights in reflowing mode don't have page in pos
if item.pos0.page == nil then item.pos0.page = page end
if item.pos1.page == nil then item.pos1.page = page end
local image = {}
image.file = book.file
image.pos0, image.pos1 = item.pos0, item.pos1
image.pboxes = item.pboxes
image.drawer = item.drawer
clipping.image = self:getImage(image)
end
--- @todo Store chapter info when exporting highlights.
if (bookmark_found and clipping.text and clipping.text ~= "") or clipping.image then
table.insert(book, { clipping })
if not bookmark_found then
table.insert(orphan_highlights, { clipping })
end
if item.text == "" and item.pos0 and item.pos1 and
item.pos0.x and item.pos0.y and
item.pos1.x and item.pos1.y then
-- highlights in reflowing mode don't have page in pos
if item.pos0.page == nil then item.pos0.page = page end
if item.pos1.page == nil then item.pos1.page = page end
local image = {}
image.file = book.file
image.pos0, image.pos1 = item.pos0, item.pos1
image.pboxes = item.pboxes
image.drawer = item.drawer
clipping.image = self:getImage(image)
end
--- @todo Store chapter info when exporting highlights.
if (bookmark_found and clipping.text and clipping.text ~= "") or clipping.image then
table.insert(book, { clipping })
end
end
end
end

View File

@ -30,6 +30,7 @@ local Device = require("device")
local InfoMessage = require("ui/widget/infomessage")
local MyClipping = require("clip")
local NetworkMgr = require("ui/network/manager")
local ReaderHighlight = require("apps/reader/modules/readerhighlight")
local UIManager = require("ui/uimanager")
local WidgetContainer = require("ui/widget/container/widgetcontainer")
local filemanagerutil = require("apps/filemanager/filemanagerutil")
@ -236,28 +237,49 @@ function Exporter:exportClippings(clippings)
end
function Exporter:addToMainMenu(menu_items)
local submenu = {}
local sharemenu = {}
local formats_submenu, share_submenu, styles_submenu = {}, {}, {}
for k, v in pairs(self.targets) do
submenu[#submenu + 1] = v:getMenuTable()
formats_submenu[#formats_submenu + 1] = v:getMenuTable()
if v.shareable then
sharemenu[#sharemenu + 1] = { text = T(_("Share as %1."), 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
share_submenu[#share_submenu + 1] = {
text = T(_("Share as %1"), 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)
table.sort(formats_submenu, function(v1, v2)
return v1.text < v2.text
end)
local settings = G_reader_settings:readSetting("exporter", {})
for i, v in ipairs(ReaderHighlight.getHighlightStyles()) do
local style = v[2]
styles_submenu[i] = {
text = v[1],
checked_func = function() -- all styles checked by default
return not (settings.highlight_styles and settings.highlight_styles[style] == false)
end,
callback = function()
if settings.highlight_styles and settings.highlight_styles[style] == false then
settings.highlight_styles[style] = nil
if next(settings.highlight_styles) == nil then
settings.highlight_styles = nil
end
else
settings.highlight_styles = settings.highlight_styles or {}
settings.highlight_styles[style] = false
end
end,
}
end
local menu = {
text = _("Export highlights"),
sub_item_table = {
@ -271,18 +293,22 @@ function Exporter:addToMainMenu(menu_items)
end,
},
{
text = _("Export all notes in your library"),
text = _("Export all notes in all books in history"),
enabled_func = function()
return self:isReady()
end,
callback = function()
self:exportAllNotes()
end,
separator = #sharemenu == 0,
separator = #share_submenu == 0,
},
{
text = _("Choose formats and services"),
sub_item_table = submenu,
sub_item_table = formats_submenu,
},
{
text = _("Choose highlight styles"),
sub_item_table = styles_submenu,
},
{
text = _("Choose export folder"),
@ -291,10 +317,10 @@ function Exporter:addToMainMenu(menu_items)
self:chooseFolder()
end,
},
}
},
}
if #sharemenu > 0 then
table.sort(sharemenu, function(v1, v2)
if #share_submenu > 0 then
table.sort(share_submenu, function(v1, v2)
return v1.text < v2.text
end)
table.insert(menu.sub_item_table, 3, {
@ -302,7 +328,7 @@ function Exporter:addToMainMenu(menu_items)
enabled_func = function()
return self:isDocReady()
end,
sub_item_table = sharemenu,
sub_item_table = share_submenu,
separator = true,
})
end