Better meta parsing for exporter (#9220)

* Get title, author and number of pages from document's metadata
* Add exportable_title parsed from document's title, used in export file generation.
* JSON: export number of pages
reviewable/pr9230/r1
Utsob Roy 2 years ago committed by GitHub
parent 2fb45b2c6d
commit 74ca6fb317
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

@ -89,7 +89,7 @@ function BaseExporter:getFilePath(t)
if not self.is_remote then
local filename = string.format("%s-%s.%s",
self:getTimeStamp(),
#t == 1 and t[1].title or "all-books",
#t == 1 and t[1].exportable_title or "all-books",
self.extension)
return self.clipping_dir .. "/" .. getSafeFilename(filename)
end

@ -105,14 +105,7 @@ local extensions = {
-- remove file extensions added by former KOReader
-- extract author name in "Title(Author)" format
-- extract author name in "Title - Author" format
function MyClipping:getTitle(line, path)
if path then
local props = self:getProps(path)
if props and props.title ~= "" then
return props.title, props.authors or props.author
end
end
function MyClipping:parseTitleFromPath(line)
line = line:match("^%s*(.-)%s*$") or ""
if extensions[line:sub(-4):lower()] then
line = line:sub(1, -5)
@ -361,18 +354,39 @@ function MyClipping:getProps(file)
return book_props
end
function MyClipping:parseCurrentDoc(view)
local clippings = {}
local function isEmpty(s)
return s == nil or s == ""
end
function MyClipping:getDocMeta(view)
local props = self:getProps(view.document.file)
local number_of_pages = view.document.info.number_of_pages
local title = props.title
local author = props.author or props.authors
local path = view.document.file
local _, _, docname = path:find(".*/(.*)")
local title, author = self:getTitle(docname, path)
clippings[title] = {
file = view.document.file,
local parsed_title, parsed_author = self:parseTitleFromPath(docname)
if isEmpty(title) then
title = isEmpty(parsed_title) and "Unknown Book" or parsed_title
end
if isEmpty(author) then
author = isEmpty(parsed_author) and "Unknown Author" or parsed_author
end
return {
title = title,
-- To make sure that export doesn't fail due to unsupported charchters.
exportable_title = parsed_title,
author = author,
number_of_pages = number_of_pages,
file = view.document.file,
}
self:parseHighlight(view.highlight.saved, view.ui.bookmark.bookmarks, clippings[title])
end
function MyClipping:parseCurrentDoc(view)
local clippings = {}
local meta = self:getDocMeta(view)
clippings[meta.title] = meta
self:parseHighlight(view.highlight.saved, view.ui.bookmark.bookmarks, clippings[meta.title])
return clippings
end

@ -13,7 +13,8 @@ local function format(booknotes)
author = booknotes.author,
entries = {},
exported = booknotes.exported,
file = booknotes.file
file = booknotes.file,
number_of_pages = booknotes.number_of_pages
}
for _, entry in ipairs(booknotes) do
table.insert(t.entries, entry[1])

Loading…
Cancel
Save