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
3 changed files with 96 additions and 61 deletions

View File

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

View File

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

View File

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