From cd440acdc41c4c97578a4fe7f0543297d8a1290e Mon Sep 17 00:00:00 2001 From: Galunid Date: Wed, 17 Jun 2020 23:51:02 +0200 Subject: [PATCH] Add chapter title when exporting notes in Evernote (JSON/HTML, remote) (#6146) closes #4566 closes #6138 --- .../apps/reader/modules/readerbookmark.lua | 34 +++++++++++++++++++ .../apps/reader/modules/readerhighlight.lua | 8 +++++ frontend/apps/reader/modules/readertoc.lua | 34 ++++++++++++++++++- plugins/evernote.koplugin/clip.lua | 2 +- plugins/evernote.koplugin/note.tpl | 24 ++++++++++--- 5 files changed, 96 insertions(+), 6 deletions(-) diff --git a/frontend/apps/reader/modules/readerbookmark.lua b/frontend/apps/reader/modules/readerbookmark.lua index fe42e9c78..ad4dbff28 100644 --- a/frontend/apps/reader/modules/readerbookmark.lua +++ b/frontend/apps/reader/modules/readerbookmark.lua @@ -215,7 +215,38 @@ function ReaderBookmark:gotoBookmark(pn_or_xp) end end +-- This function adds "chapter" property to highlights already saved in the document +function ReaderBookmark:updateHighlightsIfNeeded() + local version = self.ui.doc_settings:readSetting("bookmarks_version") or 0 + if version >= 20200615 then + return + end + + for page, highlights in pairs(self.view.highlight.saved) do + for _, highlight in pairs(highlights) do + local pg_or_xp = self.ui.document.info.has_pages and + page or highlight.pos0 + local chapter_name = self.ui.toc:getTocTitleByPage(pg_or_xp) + highlight.chapter = chapter_name + end + end + + for _, bookmark in ipairs(self.bookmarks) do + if bookmark.pos0 then + local pg_or_xp = self.ui.document.info.has_pages and + bookmark.page or bookmark.pos0 + local chapter_name = self.ui.toc:getTocTitleByPage(pg_or_xp) + bookmark.chapter = chapter_name + elseif bookmark.page then -- dogear bookmark + local chapter_name = self.ui.toc:getTocTitleByPage(bookmark.page) + bookmark.chapter = chapter_name + end + end + self.ui.doc_settings:saveSetting("bookmarks_version", 20200615) +end + function ReaderBookmark:onShowBookmark() + self:updateHighlightsIfNeeded() -- build up item_table for k, v in ipairs(self.bookmarks) do local page = v.page @@ -462,6 +493,7 @@ function ReaderBookmark:updateBookmark(item) new_text, item.updated_highlight.datetime) self.bookmarks[i].datetime = item.updated_highlight.datetime + self.bookmarks[i].chapter = item.updated_highlight.chapter self:onSaveSettings() end end @@ -538,6 +570,7 @@ function ReaderBookmark:toggleBookmark(pn_or_xp) else -- build notes from TOC local notes = self.ui.toc:getTocTitleByPage(pn_or_xp) + local chapter_name = notes if notes ~= "" then notes = "in "..notes end @@ -545,6 +578,7 @@ function ReaderBookmark:toggleBookmark(pn_or_xp) page = pn_or_xp, datetime = os.date("%Y-%m-%d %H:%M:%S"), notes = notes, + chapter = chapter_name }) end end diff --git a/frontend/apps/reader/modules/readerhighlight.lua b/frontend/apps/reader/modules/readerhighlight.lua index ba8438448..e5aa12ee6 100644 --- a/frontend/apps/reader/modules/readerhighlight.lua +++ b/frontend/apps/reader/modules/readerhighlight.lua @@ -323,7 +323,9 @@ function ReaderHighlight:updateHighlight(page, index, side, direction, move_by_c local new_beginning = self.view.highlight.saved[page][index].pos0 local new_end = self.view.highlight.saved[page][index].pos1 local new_text = self.ui.document:getTextFromXPointers(new_beginning, new_end) + local new_chapter = self.ui.toc:getTocTitleByPage(new_beginning) self.view.highlight.saved[page][index].text = new_text + self.view.highlight.saved[page][index].chapter = new_chapter local new_highlight = self.view.highlight.saved[page][index] self.ui.bookmark:updateBookmark({ page = highlight_beginning, @@ -1123,6 +1125,7 @@ function ReaderHighlight:getHighlightBookmarkItem() local datetime = os.date("%Y-%m-%d %H:%M:%S") local page = self.ui.document.info.has_pages and self.hold_pos.page or self.selected_text.pos0 + local chapter_name = self.ui.toc:getTocTitleByPage(page) return { page = page, pos0 = self.selected_text.pos0, @@ -1130,6 +1133,7 @@ function ReaderHighlight:getHighlightBookmarkItem() datetime = datetime, notes = self.selected_text.text, highlighted = true, + chapter = chapter_name, } end end @@ -1144,6 +1148,9 @@ function ReaderHighlight:saveHighlight() self.view.highlight.saved[page] = {} end local datetime = os.date("%Y-%m-%d %H:%M:%S") + local pg_or_xp = self.ui.document.info.has_pages and + self.hold_pos.page or self.selected_text.pos0 + local chapter_name = self.ui.toc:getTocTitleByPage(pg_or_xp) local hl_item = { datetime = datetime, text = self.selected_text.text, @@ -1151,6 +1158,7 @@ function ReaderHighlight:saveHighlight() pos1 = self.selected_text.pos1, pboxes = self.selected_text.pboxes, drawer = self.view.highlight.saved_drawer, + chapter = chapter_name } table.insert(self.view.highlight.saved[page], hl_item) local bookmark_item = self:getHighlightBookmarkItem() diff --git a/frontend/apps/reader/modules/readertoc.lua b/frontend/apps/reader/modules/readertoc.lua index 06dafeefa..eb55afee7 100644 --- a/frontend/apps/reader/modules/readertoc.lua +++ b/frontend/apps/reader/modules/readertoc.lua @@ -203,7 +203,7 @@ function ReaderToc:getTocIndexByPage(pn_or_xp) if #self.toc == 0 then return end local pageno = pn_or_xp if type(pn_or_xp) == "string" then - pageno = self.ui.document:getPageFromXPointer(pn_or_xp) + return self:getAccurateTocIndexByXPointer(pn_or_xp) end local pre_index = 1 for _k,_v in ipairs(self.toc) do @@ -215,6 +215,38 @@ function ReaderToc:getTocIndexByPage(pn_or_xp) return pre_index end +function ReaderToc:getAccurateTocIndexByXPointer(xptr) + local pageno = self.ui.document:getPageFromXPointer(xptr) + -- get toc entry(index) on for the current page + -- we don't get infinite loop, because the this call is not + -- with xpointer, but with page + local index = self:getTocIndexByPage(pageno) + if not index or not self.toc[index] then return end + local initial_comparison = self.ui.document:compareXPointers(self.toc[index].xpointer, xptr) + if initial_comparison and initial_comparison < 0 then + local i = index - 1 + while self.toc[i] do + local toc_xptr = self.toc[i].xpointer + local cmp = self.ui.document:compareXPointers(toc_xptr, xptr) + if cmp and cmp >= 0 then -- toc_xptr is before xptr(xptr >= toc_xptr) + return i + end + i = i - 1 + end + else + local i = index + 1 + while self.toc[i] do + local toc_xptr = self.toc[i].xpointer + local cmp = self.ui.document:compareXPointers(toc_xptr, xptr) + if cmp and cmp < 0 then -- toc_xptr is after xptr(xptr < toc_xptr) + return i - 1 + end + i = i + 1 + end + end + return index +end + function ReaderToc:getTocTitleByPage(pn_or_xp) local index = self:getTocIndexByPage(pn_or_xp) if index then diff --git a/plugins/evernote.koplugin/clip.lua b/plugins/evernote.koplugin/clip.lua index 917106f78..755923f52 100644 --- a/plugins/evernote.koplugin/clip.lua +++ b/plugins/evernote.koplugin/clip.lua @@ -235,6 +235,7 @@ function MyClipping:parseHighlight(highlights, bookmarks, book) clipping.sort = "highlight" clipping.time = self:getTime(item.datetime or "") clipping.text = self:getText(item.text) + clipping.chapter = item.chapter for _, bookmark in pairs(bookmarks) do if bookmark.datetime == item.datetime and bookmark.text then local tmp = string.gsub(bookmark.text, "Page %d+ ", "") @@ -324,4 +325,3 @@ function MyClipping:parseCurrentDoc(view) end return MyClipping - diff --git a/plugins/evernote.koplugin/note.tpl b/plugins/evernote.koplugin/note.tpl index ed592eda8..069055c21 100644 --- a/plugins/evernote.koplugin/note.tpl +++ b/plugins/evernote.koplugin/note.tpl @@ -1,4 +1,21 @@ #{ + -- helper function to convert decimal value to hex value(with trailing zeros) + function rgb_to_hex(r, g, b) + r = string.format("%x", r) + g = string.format("%x", g) + b = string.format("%x", b) + -- add trailing zeros + if #r == 1 then + r = "0" .. r + end + if #g == 1 then + g = "0" .. g + end + if #b == 1 then + b = "0" .. b + end + return "#" .. r .. g .. b + end -- helper function to map time to JET color function timecolor(time) local r,g,b @@ -18,7 +35,7 @@ b = b > 255 and 255 or math.floor(b) b = b < 0 and 0 or math.floor(b) - return r..','..g..','..b + return rgb_to_hex(r, g, b) end function htmlescape(text) @@ -38,8 +55,8 @@ #{ for index, clipping in ipairs(chapter) do }#
-
- #{= os.date("%x", clipping.time) }##{= clipping.page }# +
+ #{= os.date("%x", clipping.time) }##{ if clipping.chapter then }##{= clipping.chapter }#: #{ end }# #{= clipping.page }#
#{= htmlescape(clipping.text) }# @@ -57,4 +74,3 @@ #{ end }# #{ end }#
-