exporter: fix remaining OOM (#12126)

This commit is contained in:
Martín Fernández
2024-07-03 21:55:42 +02:00
committed by GitHub
parent 6d73442ecd
commit 4f75e41636
3 changed files with 30 additions and 30 deletions

View File

@@ -125,10 +125,8 @@ function MarkdownExporter:export(t)
local file = io.open(path, "w")
if not file then return false end
for idx, book in ipairs(t) do
file:write(md.prepareBookContent(book, self.settings.formatting_options, self.settings.highlight_formatting))
if idx < #t then
file:write("\n")
end
local tbl = md.prepareBookContent(book, self.settings.formatting_options, self.settings.highlight_formatting)
file:write(table.concat(tbl, "\n"))
end
file:write("\n\n_Generated at: " .. self:getTimeStamp() .. "_")
file:close()
@@ -136,8 +134,9 @@ function MarkdownExporter:export(t)
end
function MarkdownExporter:share(t)
local content = md.prepareBookContent(t, self.settings.formatting_options, self.settings.highlight_formatting) .. "\n\n_Generated at: " .. self:getTimeStamp() .. "_"
self:shareText(content)
local tbl = md.prepareBookContent(t, self.settings.formatting_options, self.settings.highlight_formatting)
table.insert(tbl, "\n_Generated at: " .. self:getTimeStamp() .. "_")
self:shareText(table.concat(tbl, "\n"))
end
return MarkdownExporter

View File

@@ -10,43 +10,45 @@ local TextExporter = require("base"):new {
}
local function format(booknotes)
local tbl = {}
-- Use wide_space to avoid crengine to treat it specially.
local wide_space = "\227\128\128"
local content = ""
if booknotes.title then
content = content .. wide_space .. booknotes.title .. "\n" .. wide_space .. "\n"
table.insert(tbl, wide_space .. booknotes.title)
table.insert(tbl, wide_space)
end
for ___, entry in ipairs(booknotes) do
for ____, clipping in ipairs(entry) do
if clipping.chapter then
content = content .. wide_space .. clipping.chapter .. "\n" .. wide_space .. "\n"
table.insert(tbl, wide_space .. clipping.chapter)
table.insert(tbl, wide_space)
end
local text = T(_("-- Page: %1, added on %2\n"), clipping.page, os.date("%c", clipping.time))
content = content .. wide_space .. wide_space .. text
table.insert(tbl, wide_space .. wide_space .. text)
if clipping.text then
content = content .. clipping.text
table.insert(tbl, clipping.text)
end
if clipping.note then
content = content .. "\n---\n" .. clipping.note
table.insert(tbl, "\n---\n" .. clipping.note)
end
if clipping.image then
content = content .. _("<An image>")
table.insert(tbl, _("<An image>"))
end
content = content .. "\n-=-=-=-=-=-\n"
table.insert(tbl, "-=-=-=-=-=-")
end
end
content = content .. "\n"
return content
return tbl
end
function TextExporter:export(t)
-- Use wide_space to avoid crengine to treat it specially.
local path = self:getFilePath(t)
local file = io.open(path, "a")
if not file then return false end
for __, booknotes in ipairs(t) do
local content = format(booknotes)
file:write(content)
local tbl = format(booknotes)
file:write(table.concat(tbl, "\n"))
end
file:close()
return true

View File

@@ -36,29 +36,28 @@ local formatters = {
}
local function prepareBookContent(book, formatting_options, highlight_formatting)
local content = ""
local tbl = {}
local current_chapter = nil
content = content .. "# " .. book.title .. "\n"
table.insert(tbl, "# " .. book.title)
local author = book.author or _("N/A")
content = content .. "##### " .. author:gsub("\n", ", ") .. "\n\n"
table.insert(tbl, "##### " .. author:gsub("\n", ", ") .. "\n")
for _, note in ipairs(book) do
local entry = note[1]
if entry.chapter ~= current_chapter then
current_chapter = entry.chapter
content = content .. "## " .. current_chapter .. "\n"
table.insert(tbl, "## " .. current_chapter)
end
content = content .. "### Page " .. entry.page .. " @ " .. os.date("%d %B %Y %I:%M:%S %p", entry.time) .. "\n"
table.insert(tbl, "### Page " .. entry.page .. " @ " .. os.date("%d %B %Y %I:%M:%S %p", entry.time))
if highlight_formatting then
content = content .. string.format(formatters[formatting_options[entry.drawer]].formatter, entry.text) .."\n"
table.insert(tbl, string.format(formatters[formatting_options[entry.drawer]].formatter, entry.text))
else
content = content .. entry.text .. "\n"
table.insert(tbl, entry.text)
end
if entry.note then
content = content .. "\n---\n" .. entry.note .. "\n"
table.insert(tbl, "\n---\n" .. entry.note)
end
content = content .. "\n"
end
return content
return tbl
end
return {