mirror of https://github.com/koreader/koreader
Markdown export (#9076)
Enables users to export markdown locally with some configuration options to allow users to format the output to a certain extent.pull/9139/head
parent
ae5b2d68ca
commit
6804b77251
@ -0,0 +1,135 @@
|
||||
local UIManager = require("ui/uimanager")
|
||||
local md = require("template/md")
|
||||
local _ = require("gettext")
|
||||
local T = require("ffi/util").template
|
||||
|
||||
-- markdown exporter
|
||||
local MarkdownExporter = require("base"):new {
|
||||
name = "markdown",
|
||||
extension = "md",
|
||||
init_callback = function(self, settings)
|
||||
local changed = false
|
||||
if not settings.formatting_options or settings.highlight_formatting == nil then
|
||||
settings.formatting_options = settings.formatting_options or {
|
||||
lighten = "italic",
|
||||
underscore = "underline_markdownit",
|
||||
strikeout = "strikethrough",
|
||||
invert = "bold",
|
||||
}
|
||||
settings.highlight_formatting = settings.highlight_formatting or true
|
||||
changed = true
|
||||
end
|
||||
return changed, settings
|
||||
end,
|
||||
}
|
||||
|
||||
local formatter_buttons = {
|
||||
{ _("None"), "none" },
|
||||
{ _("Bold"), "bold" },
|
||||
{ _("Bold italic"), "bold_italic" },
|
||||
{ _("Italic"), "italic" },
|
||||
{ _("Strikethrough"), "strikethrough" },
|
||||
{ _("Underline (Markdownit style, with ++)"), "underline_markdownit" },
|
||||
{ _("Underline (with <u></u> tags)"), "underline_u_tag" },
|
||||
}
|
||||
|
||||
function MarkdownExporter:editFormatStyle(drawer_style, label, touchmenu_instance)
|
||||
local radio_buttons = {}
|
||||
for _idx, v in ipairs(formatter_buttons) do
|
||||
table.insert(radio_buttons, {
|
||||
{
|
||||
text = v[1],
|
||||
checked = self.settings.formatting_options[drawer_style] == v[2],
|
||||
provider = v[2],
|
||||
},
|
||||
})
|
||||
end
|
||||
UIManager:show(require("ui/widget/radiobuttonwidget"):new {
|
||||
title_text = T(_("Formatting style for %1"), label),
|
||||
width_factor = 0.8,
|
||||
radio_buttons = radio_buttons,
|
||||
callback = function(radio)
|
||||
self.settings.formatting_options[drawer_style] = radio.provider
|
||||
touchmenu_instance:updateItems()
|
||||
end,
|
||||
})
|
||||
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"),
|
||||
checked_func = function() return self:isEnabled() end,
|
||||
sub_item_table = {
|
||||
{
|
||||
text = _("Export to Markdown"),
|
||||
checked_func = function() return self:isEnabled() end,
|
||||
callback = function() self:toggleEnabled() 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,
|
||||
},
|
||||
}
|
||||
}
|
||||
|
||||
for _idx, entry in ipairs(highlight_style) do
|
||||
table.insert(menu.sub_item_table, {
|
||||
text_func = function()
|
||||
return entry[1] .. ": " .. md.formatters[self.settings.formatting_options[entry[2]]].label
|
||||
end,
|
||||
enabled_func = function()
|
||||
return self.settings.highlight_formatting
|
||||
end,
|
||||
keep_menu_open = true,
|
||||
callback = function(touchmenu_instance)
|
||||
self:editFormatStyle(entry[2], entry[1], touchmenu_instance)
|
||||
end,
|
||||
})
|
||||
end
|
||||
return menu
|
||||
end
|
||||
|
||||
function MarkdownExporter:export(t)
|
||||
local path = self:getFilePath(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
|
||||
end
|
||||
file:write("\n\n_Generated at: " .. self:getTimeStamp() .. "_")
|
||||
file:close()
|
||||
return true
|
||||
end
|
||||
|
||||
return MarkdownExporter
|
@ -0,0 +1,62 @@
|
||||
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
|
||||
}
|
Loading…
Reference in New Issue