2
0
mirror of https://github.com/koreader/koreader synced 2024-11-18 03:25:46 +00:00
koreader/plugins/exporter.koplugin/target/html.lua

74 lines
1.9 KiB
Lua
Raw Normal View History

local logger = require("logger")
local slt2 = require("template/slt2")
-- html exporter
local HtmlExporter = require("base"):new {
name = "html",
mimetype = "text/html",
}
local function format(booknotes)
local chapters = {}
local curr_chapter = nil
for _, booknote in ipairs(booknotes) do
if curr_chapter == nil then
curr_chapter = {
title = booknote[1].chapter,
entries = {}
}
elseif curr_chapter.title ~= booknote[1].chapter then
table.insert(chapters, curr_chapter)
curr_chapter = {
title = booknote[1].chapter,
entries = {}
}
end
table.insert(curr_chapter.entries, booknote[1])
end
if curr_chapter ~= nil then
table.insert(chapters, curr_chapter)
end
booknotes.chapters = chapters
booknotes.entries = nil
return booknotes
end
2022-06-03 17:56:10 +00:00
function HtmlExporter:getRenderedContent(t)
local title
if #t == 1 then
title = t[1].title
else
title = "All Books"
end
local template = slt2.loadfile(self.path .. "/template/note.tpl")
local clipplings = {}
for _, booknotes in ipairs(t) do
table.insert(clipplings, format(booknotes))
end
local content = slt2.render(template, {
clippings=clipplings,
document_title = title,
version = self:getVersion(),
timestamp = self:getTimeStamp(),
logger = logger
})
2022-06-03 17:56:10 +00:00
return content
end
function HtmlExporter:export(t)
local path = self:getFilePath(t)
local file = io.open(path, "w")
if not file then return false end
local content = self:getRenderedContent(t)
file:write(content)
file:close()
return true
end
2022-06-03 17:56:10 +00:00
function HtmlExporter:share(t)
local content = self:getRenderedContent({t})
self:shareText(content)
2022-06-03 17:56:10 +00:00
end
return HtmlExporter