2017-04-12 18:42:28 +00:00
|
|
|
--[[--
|
|
|
|
This module is responsible for converting files.
|
|
|
|
]]
|
|
|
|
|
|
|
|
local ButtonDialogTitle = require("ui/widget/buttondialogtitle")
|
|
|
|
local ConfirmBox = require("ui/widget/confirmbox")
|
|
|
|
local UIManager = require("ui/uimanager")
|
|
|
|
local lfs = require("libs/libkoreader-lfs")
|
|
|
|
local logger = require("logger")
|
|
|
|
local util = require("util")
|
2018-08-15 19:56:35 +00:00
|
|
|
local _ = require("gettext")
|
2017-04-12 18:42:28 +00:00
|
|
|
local T = require("ffi/util").template
|
|
|
|
|
|
|
|
local FileConverter = {
|
|
|
|
formats_from = {
|
|
|
|
md = {
|
2020-01-07 11:26:49 +00:00
|
|
|
-- @translators See <https://en.wikipedia.org/wiki/Markdown>. In languages written in the Latin alphabet this is unlikely to change.
|
2018-08-15 19:56:35 +00:00
|
|
|
name = _("Markdown"),
|
2017-04-12 18:42:28 +00:00
|
|
|
from = "markdown",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
formats_to = {
|
|
|
|
epub = {
|
|
|
|
to = "epub",
|
|
|
|
},
|
|
|
|
html = {
|
|
|
|
to = "html",
|
|
|
|
},
|
|
|
|
pdf = {
|
|
|
|
to = "pdf",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
--curl --form input_files[]=@README.md --form from=markdown --form to=pdf http://c.docverter.com/convert
|
|
|
|
docverter_url = "http://c.docverter.com/convert",
|
|
|
|
}
|
|
|
|
|
|
|
|
--- Converts a markdown fragment to a full HTML document.
|
|
|
|
---- @string markdown the markdown fragment
|
|
|
|
---- @string title an optional title for the HTML document
|
|
|
|
---- @treturn string an HTML document
|
2021-07-20 15:22:45 +00:00
|
|
|
function FileConverter:mdToHtml(markdown, title, stylesheet)
|
2017-04-24 06:27:29 +00:00
|
|
|
local MD = require("apps/filemanager/lib/md")
|
2021-07-20 15:22:45 +00:00
|
|
|
stylesheet = stylesheet and string.format("<style>\n%s\n</style>\n", stylesheet) or ""
|
2017-04-12 18:42:28 +00:00
|
|
|
local md_options = {
|
|
|
|
prependHead = "<!DOCTYPE html>\n<html>\n<head>\n",
|
2021-07-20 15:22:45 +00:00
|
|
|
insertHead = string.format("<title>%s</title>\n%s</head>\n<body>\n", title, stylesheet),
|
2017-04-12 18:42:28 +00:00
|
|
|
appendTail = "\n</body>\n</html>",
|
|
|
|
}
|
|
|
|
local html, err = MD(markdown, md_options)
|
|
|
|
if err then
|
|
|
|
logger.warn("FileManagerConverter: could not generate HTML", err)
|
|
|
|
end
|
|
|
|
return html
|
|
|
|
end
|
|
|
|
|
|
|
|
function FileConverter:_mdFileToHtml(file, title)
|
|
|
|
local f = io.open(file, "rb")
|
2022-09-27 23:10:50 +00:00
|
|
|
if f == nil then
|
|
|
|
return
|
|
|
|
end
|
2017-04-12 18:42:28 +00:00
|
|
|
local content = f:read("*all")
|
|
|
|
f:close()
|
|
|
|
local html = self:mdToHtml(content, title)
|
|
|
|
return html
|
|
|
|
end
|
|
|
|
|
|
|
|
function FileConverter:writeStringToFile(content, file)
|
|
|
|
local f = io.open(file, "w")
|
2022-09-27 23:10:50 +00:00
|
|
|
if f == nil then
|
|
|
|
return
|
|
|
|
end
|
2017-04-12 18:42:28 +00:00
|
|
|
f:write(content)
|
|
|
|
f:close()
|
|
|
|
end
|
|
|
|
|
|
|
|
function FileConverter:isSupported(file)
|
|
|
|
return FileConverter.formats_from[util.getFileNameSuffix(file)] and true or false
|
|
|
|
end
|
|
|
|
|
|
|
|
function FileConverter:showConvertButtons(file, ui)
|
2018-08-15 19:56:35 +00:00
|
|
|
local __, filename_pure = util.splitFilePathName(file)
|
2017-04-12 18:42:28 +00:00
|
|
|
local filename_suffix = util.getFileNameSuffix(file)
|
|
|
|
local filetype_name = self.formats_from[filename_suffix].name
|
|
|
|
self.convert_dialog = ButtonDialogTitle:new{
|
2018-08-15 19:56:35 +00:00
|
|
|
title = T(_("Convert %1 to:"), filetype_name),
|
2017-04-12 18:42:28 +00:00
|
|
|
buttons = {
|
|
|
|
{
|
|
|
|
{
|
2018-08-15 19:56:35 +00:00
|
|
|
text = _("HTML"),
|
2017-04-12 18:42:28 +00:00
|
|
|
callback = function()
|
|
|
|
local html = FileConverter:_mdFileToHtml(file, filename_pure)
|
|
|
|
if not html then return end
|
|
|
|
local filename_html = file..".html"
|
|
|
|
if lfs.attributes(filename_html, "mode") == "file" then
|
|
|
|
UIManager:show(ConfirmBox:new{
|
2018-08-15 19:56:35 +00:00
|
|
|
text = _("Overwrite existing HTML file?"),
|
|
|
|
ok_text = _("Overwrite"),
|
2017-04-12 18:42:28 +00:00
|
|
|
ok_callback = function()
|
|
|
|
FileConverter:writeStringToFile(html, filename_html)
|
|
|
|
UIManager:close(self.convert_dialog)
|
|
|
|
end,
|
|
|
|
})
|
|
|
|
else
|
|
|
|
FileConverter:writeStringToFile(html, filename_html)
|
|
|
|
ui:refreshPath()
|
|
|
|
UIManager:close(self.convert_dialog)
|
|
|
|
end
|
|
|
|
end,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
2022-09-27 23:10:50 +00:00
|
|
|
self.convert_dialog.onCloseWidget = function(this)
|
|
|
|
local super = getmetatable(this)
|
|
|
|
if super.onCloseWidget then
|
|
|
|
-- Call our super's method, if any
|
|
|
|
super.onCloseWidget(this)
|
|
|
|
end
|
|
|
|
-- And then do our own cleanup
|
|
|
|
self:cleanup()
|
|
|
|
end
|
2017-04-12 18:42:28 +00:00
|
|
|
UIManager:show(self.convert_dialog)
|
|
|
|
end
|
|
|
|
|
2022-09-27 23:10:50 +00:00
|
|
|
function FileConverter:cleanup()
|
|
|
|
self.convert_dialog = nil
|
|
|
|
end
|
|
|
|
|
2017-04-12 18:42:28 +00:00
|
|
|
return FileConverter
|