Add chapter title when exporting notes in Evernote (JSON/HTML, remote) (#6146)

closes #4566
closes #6138
reviewable/pr6282/r1
Galunid 4 years ago committed by GitHub
parent 5aab341ed0
commit cd440acdc4
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

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

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

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

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

@ -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 }#
<div style="padding-top:0.5em; padding-bottom:0.5em;#{ if index > 1 then }# border-top:1px dotted lightgray;#{ end }#">
<div style="font-size:10pt; margin-bottom:0.2em; color:darkgray">
<div style="display:inline-block; width:0.2em; height:0.9em; margin-right:0.2em; background-color:rgb(#{= timecolor(clipping.time)}#);"></div>
<span>#{= os.date("%x", clipping.time) }#</span><span style="float:right">#{= clipping.page }#</span>
<div style="display:inline-block; width:0.2em; height:0.9em; margin-right:0.2em; background-color:#{= timecolor(clipping.time)}#;"></div>
<span>#{= os.date("%x", clipping.time) }#</span><span style="float:right">#{ if clipping.chapter then }#<b>#{= clipping.chapter }#</b>: #{ end }# #{= clipping.page }#</span>
</div>
<div style="font-size:12pt">
<span>#{= htmlescape(clipping.text) }#</span>
@ -57,4 +74,3 @@
#{ end }#
#{ end }#
</div>

Loading…
Cancel
Save