Export highlights: export backlinks to markdown (#14233)

This commit is contained in:
hius07 authored and GitHub committed 2025-09-02 08:54:55 +03:00
1 parent ec79e6169c
commit 2facbef59a
5 files changed
+92 -71

No files matched your search

+57 -34
View File
@@ -6,7 +6,6 @@ local BD = require("ui/bidi")
local ButtonDialog = require("ui/widget/buttondialog")
local ConfirmBox = require("ui/widget/confirmbox")
local Device = require("device")
local DocumentRegistry = require("document/documentregistry")
local Event = require("ui/event")
local InfoMessage = require("ui/widget/infomessage")
local InputContainer = require("ui/widget/container/inputcontainer")
@@ -14,13 +13,13 @@ local LinkBox = require("ui/widget/linkbox")
local Notification = require("ui/widget/notification")
local QRMessage = require("ui/widget/qrmessage")
local UIManager = require("ui/uimanager")
local ffiutil = require("ffi/util")
local ffiUtil = require("ffi/util")
local lfs = require("libs/libkoreader-lfs")
local logger = require("logger")
local util = require("util")
local _ = require("gettext")
local Screen = Device.screen
local T = ffiutil.template
local T = ffiUtil.template
local function is_wiki_page(link_url)
if not link_url then
@@ -897,36 +896,7 @@ function ReaderLink:onGotoLink(link, neglect_current_location, allow_footnote_po
end
-- Check if it is a link to a local file
local linked_filename = link_url:gsub("^file:(//)?", "") -- remove local file protocol if any
local anchor
if linked_filename:find("?") then -- remove any query string (including any following anchor)
linked_filename, anchor = linked_filename:match("^(.-)(%?.*)$")
elseif linked_filename:find("#") then -- remove any anchor
linked_filename, anchor = linked_filename:match("^(.-)(#.*)$")
end
linked_filename = ffiutil.joinPath(self.document_dir, linked_filename) -- get full path
linked_filename = ffiutil.realpath(linked_filename) -- clean full path from ./ or ../
if linked_filename and lfs.attributes(linked_filename, "mode") == "file" then
if DocumentRegistry:hasProvider(linked_filename) then
-- Display filename with anchor or query string, so the user gets
-- this information and can manually go to the appropriate place
local display_filename = linked_filename
if anchor then
display_filename = display_filename .. anchor
end
UIManager:show(ConfirmBox:new{
text = T(_("Would you like to read this local document?\n\n%1\n"), BD.filepath(display_filename)),
ok_callback = function()
UIManager:scheduleIn(0.1, function()
self.ui:switchDocument(linked_filename)
end)
end
})
else
UIManager:show(InfoMessage:new{
text = T(_("Link to unsupported local file:\n%1"), BD.url(link_url)),
})
end
if self:openFileFromLink(link_url) then
return true
end
@@ -939,6 +909,59 @@ function ReaderLink:onGotoLink(link, neglect_current_location, allow_footnote_po
return true
end
function ReaderLink:openFileFromLink(link_url)
local linked_filename, anchor, after_open_callback
if link_url:find("?") then -- remove any query string (including any following anchor)
linked_filename, anchor = link_url:match("^(.-)(%?.*)$")
if anchor then
-- If anchor contains position (page number or xpointer),
-- go to the position after opening the document
local pn_xp, count = anchor:gsub("^?pos=", "")
if count > 0 then
after_open_callback = function(ui)
ui.link:addCurrentLocationToStack()
if ui.rolling then
ui.rolling:onGotoXPointer(pn_xp, pn_xp)
else
pn_xp = tonumber(pn_xp)
if pn_xp then
ui.paging:onGotoPage(pn_xp)
end
end
end
end
end
elseif link_url:find("#") then -- remove any anchor
linked_filename, anchor = link_url:match("^(.-)(#.*)$")
else
linked_filename = link_url
end
linked_filename = linked_filename:gsub("^file:", "") -- remove local file protocol if any
local __, slash_nb = linked_filename:find("^/*") -- 0...3 leading slashes
linked_filename = linked_filename:gsub("^//", "") -- keep 1 slash for absolute path
if slash_nb == 0 or slash_nb == 2 then -- relative path
linked_filename = ffiUtil.joinPath(self.document_dir, linked_filename)
end
linked_filename = ffiUtil.realpath(linked_filename) -- clean full path from ./ or ../
if linked_filename and lfs.attributes(linked_filename, "mode") == "file" then
local display_filename = linked_filename
if anchor and after_open_callback == nil then
-- Display filename with anchor or query string, so the user gets
-- this information and can manually go to the appropriate place
display_filename = display_filename .. anchor
end
UIManager:show(ConfirmBox:new{
text = T(_("Would you like to read this local document?\n\n%1\n"), BD.filepath(display_filename)),
ok_callback = function()
UIManager:scheduleIn(0.1, function()
self.ui:switchDocument(linked_filename, nil, after_open_callback)
end)
end,
})
return true
end
end
function ReaderLink:onGoToExternalLink(link_url)
local buttons, title = self:getButtonsForExternalLinkDialog(link_url)
self.external_link_dialog = ButtonDialog:new{
@@ -1589,7 +1612,7 @@ function ReaderLink:getButtonsForExternalLinkDialog(link_url)
local default_title = T(_("External link:\n\n%1"), BD.url(link_url))
local title = default_title
for idx, fn_button in ffiutil.orderedPairs(self._external_link_buttons) do
for idx, fn_button in ffiUtil.orderedPairs(self._external_link_buttons) do
local button = fn_button(self, link_url)
local show, button_title
+10 -2
View File
@@ -496,6 +496,10 @@ function ReaderUI:init()
end
self.postReaderReadyCallback = nil
self.reloading = nil
if self.after_open_callback then
self:after_open_callback()
self.after_open_callback = nil
end
Device:setIgnoreInput(false) -- Allow processing of events (on Android).
Input:inhibitInputUntil(0.2)
@@ -714,7 +718,10 @@ function ReaderUI:doShowReader(file, provider, seamless)
covers_fullscreen = true, -- hint for UIManager:_repaint()
document = document,
reloading = self.reloading,
after_open_callback = self.after_open_callback,
}
self.reloading = nil
self.after_open_callback = nil
Screen:setWindowTitle(reader.doc_props.display_title)
Device:notifyBookState(reader.doc_props.display_title, document)
@@ -882,7 +889,6 @@ function ReaderUI:reloadDocument(after_close_callback, seamless)
-- Mimic onShowingReader's refresh optimizations
self.tearing_down = true
self.dithered = nil
self.reloading = true
self:handleEvent(Event:new("CloseReaderMenu"))
self:handleEvent(Event:new("CloseConfigMenu"))
@@ -894,10 +900,11 @@ function ReaderUI:reloadDocument(after_close_callback, seamless)
after_close_callback(file, provider)
end
self.reloading = true
self:showReader(file, provider, seamless)
end
function ReaderUI:switchDocument(new_file, seamless)
function ReaderUI:switchDocument(new_file, seamless, after_open_callback)
if not new_file then return end
-- Mimic onShowingReader's refresh optimizations
@@ -909,6 +916,7 @@ function ReaderUI:switchDocument(new_file, seamless)
self.highlight:onClose() -- close highlight dialog if any
self:onClose(false)
self.after_open_callback = after_open_callback
self:showReader(new_file, nil, seamless)
end
+2
View File
@@ -241,6 +241,7 @@ function MyClipping:parseAnnotations(annotations, book)
chapter = item.chapter,
drawer = item.drawer,
color = item.color,
pn_xp = item.page,
}
table.insert(book, { clipping })
end
@@ -270,6 +271,7 @@ function MyClipping:parseHighlight(highlights, bookmarks, book)
text = self:getText(item.text),
chapter = item.chapter,
drawer = item.drawer,
pn_xp = item.page,
}
local bookmark_found = false
for _, bookmark in pairs(bookmarks) do
+16 -32
View File
@@ -1,3 +1,4 @@
local ReaderHighlight = require("apps/reader/modules/readerhighlight")
local UIManager = require("ui/uimanager")
local md = require("template/md")
local _ = require("gettext")
@@ -58,33 +59,6 @@ function MarkdownExporter:editFormatStyle(drawer_style, label, touchmenu_instanc
})
end
function MarkdownExporter:onInit()
local changed = false
if self.settings.formatting_options == nil then
self.settings.formatting_options = {
lighten = "italic",
underscore = "underline_markdownit",
strikeout = "strikethrough",
invert = "bold",
}
changed = true
end
if self.settings.highlight_formatting == nil then
self.settings.highlight_formatting = true
changed = true
end
if changed then
self:saveSettings()
end
end
local highlight_style = {
{ _("Lighten"), "lighten" },
{ _("Underline"), "underscore" },
{ _("Strikeout"), "strikeout" },
{ _("Invert"), "invert" },
}
function MarkdownExporter:getMenuTable()
local menu = {
text = _("Markdown"),
@@ -95,15 +69,24 @@ function MarkdownExporter:getMenuTable()
checked_func = function() return self:isEnabled() end,
callback = function() self:toggleEnabled() end,
},
{
text = _("Export backlinks"),
checked_func = function() return self.settings.export_backlinks end,
callback = function() self.settings.export_backlinks = not self.settings.export_backlinks end,
},
{
text = _("Format highlights based on style"),
checked_func = function() return self.settings.highlight_formatting end,
callback = function() self.settings.highlight_formatting = not self.settings.highlight_formatting end,
},
}
},
hold_callback = function(touchmenu_instance)
self:toggleEnabled()
touchmenu_instance:updateItems()
end,
}
for _idx, entry in ipairs(highlight_style) do
for _, entry in ipairs(ReaderHighlight.getHighlightStyles()) do
table.insert(menu.sub_item_table, {
text_func = function()
return entry[1] .. ": " .. md.formatters[self.settings.formatting_options[entry[2]]].label
@@ -121,14 +104,15 @@ function MarkdownExporter:getMenuTable()
end
function MarkdownExporter:export(t)
local path = self:getFilePath(t)
local path = self:getFilePath()
local file = io.open(path, "w")
if not file then return false end
for idx, book in ipairs(t) do
local tbl = md.prepareBookContent(book, self.settings.formatting_options, self.settings.highlight_formatting)
local tbl = md.prepareBookContent(book,
self.settings.formatting_options, self.settings.highlight_formatting, self.settings.export_backlinks)
file:write(table.concat(tbl, "\n"))
end
file:write("\n\n_Generated at: " .. self:getTimeStamp() .. "_")
file:write("\n## \n_" .. T("Generated at: %1", self:getTimeStamp()) .. "_")
file:close()
return true
end
+7 -3
View File
@@ -1,4 +1,5 @@
local _ = require("gettext")
local T = require("ffi/util").template
local formatters = {
none = {
@@ -35,13 +36,13 @@ local formatters = {
},
}
local function prepareBookContent(book, formatting_options, highlight_formatting)
local function prepareBookContent(book, formatting_options, highlight_formatting, export_backlinks)
local tbl = {}
local current_chapter = nil
table.insert(tbl, "# " .. book.title)
local author = book.author or _("N/A")
table.insert(tbl, "##### " .. author:gsub("\n", ", ") .. "\n")
for _, note in ipairs(book) do
for __, note in ipairs(book) do
local entry = note[1]
if entry.chapter ~= current_chapter then
current_chapter = entry.chapter
@@ -56,11 +57,14 @@ local function prepareBookContent(book, formatting_options, highlight_formatting
if entry.note then
table.insert(tbl, "\n---\n" .. entry.note)
end
if export_backlinks then
table.insert(tbl, "\n" .. T("[%1](<file://%2?pos=%3>)", _("View in book"), book.file, entry.pn_xp))
end
end
return tbl
end
return {
prepareBookContent = prepareBookContent,
formatters = formatters
formatters = formatters,
}