2
0
mirror of https://github.com/koreader/koreader synced 2024-11-08 07:10:27 +00:00
koreader/plugins/exporter.koplugin/template/md.lua
Utsob Roy 6804b77251
Markdown export (#9076)
Enables users to export markdown locally with some configuration options to allow users to format the output to a certain extent.
2022-05-28 10:32:36 +02:00

63 lines
1.8 KiB
Lua

local _ = require("gettext")
local formatters = {
none = {
formatter = "%s",
label = _("None")
},
bold = {
formatter = "**%s**",
label = _("Bold")
},
italic = {
formatter = "*%s*",
label = _("Italic")
},
bold_italic = {
formatter = "**_%s_**",
label = _("Bold italic")
},
underline_markdownit = {
formatter = "++%s++",
label = _("Underline (Markdownit style, with ++)")
},
underline_u_tag = {
formatter = "<u>%s</u>",
label = _("Underline (with <u></u> tags)")
},
strikethrough = {
formatter = "~~%s~~",
label = _("Strikethrough")
},
}
local function prepareBookContent(book, formatting_options, highlight_formatting)
local content = ""
local current_chapter = nil
content = content .. "# " .. book.title .. "\n"
content = content .. "##### " .. book.author:gsub("\n", ", ") .. "\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"
end
content = content .. "### Page " .. entry.page .. " @ " .. os.date("%d %B %Y %I:%M %p", entry.time) .. "\n"
if highlight_formatting then
content = content .. string.format(formatters[formatting_options[entry.drawer]].formatter, entry.text) .."\n"
else
content = content .. entry.text .. "\n"
end
if entry.note then
content = content .. "\n---\n" .. entry.note .. "\n"
end
content = content .. "\n"
end
return content
end
return {
prepareBookContent = prepareBookContent,
formatters = formatters
}