Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

HTML dict: enable color rendering and per-dict user fix html func #3585

Merged
merged 1 commit into from Jan 9, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
33 changes: 32 additions & 1 deletion frontend/apps/reader/modules/readerdictionary.lua
Expand Up @@ -67,6 +67,8 @@ local ReaderDictionary = InputContainer:new{
lookup_msg = _("Searching dictionary for:\n%1"),
}

-- For a HTML dict, one can specify a specific stylesheet
-- in a file named as the .ifo with a .css extension
local function readDictionaryCss(path)
local f = io.open(path, "r")
if not f then
Expand All @@ -78,6 +80,26 @@ local function readDictionaryCss(path)
return content
end

-- For a HTML dict, one can specify a function called on
-- the raw returned definition to "fix" the HTML if needed
-- (as MuPDF, used for rendering, is quite sensitive to the
-- HTML quality) in a file named as the .ifo with a .lua
-- extension, containing for example:
-- return function(html)
-- html = html:gsub("<hr>", "<hr/>")
-- return html
-- end
local function getDictionaryFixHtmlFunc(path)
if lfs.attributes(path, "mode") == "file" then
local ok, func = pcall(dofile, path)
if ok and func then
return func
else
logger.warn("Dict's user provided file failed:", func)
end
end
end

function ReaderDictionary:init()
self.ui.menu:registerToMainMenu(self)
self.data_dir = os.getenv("STARDICT_DATA_DIR") or
Expand Down Expand Up @@ -108,7 +130,8 @@ function ReaderDictionary:init()
file = ifo_file,
name = dictname,
is_html = is_html,
css = readDictionaryCss(ifo_file:gsub("%.ifo$", ".css"))
css = readDictionaryCss(ifo_file:gsub("%.ifo$", ".css")),
fix_html_func = getDictionaryFixHtmlFunc(ifo_file:gsub("%.ifo$", ".lua")),
})
end
end
Expand Down Expand Up @@ -368,6 +391,14 @@ local function tidyMarkup(results)
if ifo and ifo.is_html then
result.is_html = ifo.is_html
result.css = ifo.css
if ifo.fix_html_func then
local ok, fixed_definition = pcall(ifo.fix_html_func, result.definition)
if ok then
result.definition = fixed_definition
else
logger.warn("Dict's user provided funcion failed:", fixed_definition)
end
end
else
local def = result.definition
-- preserve the <br> tag for line break
Expand Down
18 changes: 14 additions & 4 deletions frontend/ui/widget/htmlboxwidget.lua
Expand Up @@ -5,10 +5,11 @@ HTML widget (without scroll bars).
local DrawContext = require("ffi/drawcontext")
local Geom = require("ui/geometry")
local InputContainer = require("ui/widget/container/inputcontainer")
local logger = require("logger")
local Mupdf = require("ffi/mupdf")
local util = require("util")
local Screen = require("device").screen
local TimeVal = require("ui/timeval")
local logger = require("logger")
local util = require("util")

local HtmlBoxWidget = InputContainer:new{
bb = nil,
Expand Down Expand Up @@ -63,10 +64,17 @@ function HtmlBoxWidget:_render()
return
end

-- In pdfdocument.lua, color is activated only at the moment of
-- rendering and then immediately disabled, for safety with kopt.
-- We do the same here.
Mupdf.color = Screen:isColorEnabled()

local page = self.document:openPage(self.page_number)
local dc = DrawContext.new()
self.bb = page:draw_new(dc, self.dimen.w, self.dimen.h, 0, 0)
page:close()

Mupdf.color = false
end

function HtmlBoxWidget:getSize()
Expand Down Expand Up @@ -99,8 +107,10 @@ end
function HtmlBoxWidget:free()
self:freeBb()

self.document:close()
self.document = nil
if self.document then
self.document:close()
self.document = nil
end
end

function HtmlBoxWidget:onCloseWidget()
Expand Down