HTML dictionary support (#3573)

* Adds a generic HTML widget modeled after the text widget, and HTML dictionary support. HTML dictionaries can have their own CSS (for X.ifo it must be X.css). The base CSS just resets the margin and sets the font.

Note that the widget doesn't handle links, that wasn't needed for the dictionary.

Closes <https://github.com/koreader/koreader/issues/1776>.

* Show tag stripped HTML if the dictionary entry isn't valid HTML

* Simulate the normal <br/> behavior

* Bump base
This commit is contained in:
TnS-hun
2018-01-07 20:24:15 +01:00
committed by Frans de Jonge
parent 5245f018a6
commit 06a8a33d39
6 changed files with 444 additions and 22 deletions

View File

@@ -67,6 +67,17 @@ local ReaderDictionary = InputContainer:new{
lookup_msg = _("Searching dictionary for:\n%1"),
}
local function readDictionaryCss(path)
local f = io.open(path, "r")
if not f then
return nil
end
local content = f:read("*all")
f:close()
return content
end
function ReaderDictionary:init()
self.ui.menu:registerToMainMenu(self)
self.data_dir = os.getenv("STARDICT_DATA_DIR") or
@@ -90,11 +101,14 @@ function ReaderDictionary:init()
local content = f:read("*all")
f:close()
local dictname = content:match("\nbookname=(.-)\n")
local is_html = content:find("sametypesequence=h", 1, true) ~= nil
-- sdcv won't use dict that don't have a bookname=
if dictname then
table.insert(available_ifos, {
file = ifo_file,
name = dictname,
is_html = is_html,
css = readDictionaryCss(ifo_file:gsub("%.ifo$", ".css"))
})
end
end
@@ -331,26 +345,42 @@ local function dictDirsEmpty(dict_dirs)
return true
end
local function getAvailableIfoByName(dictionary_name)
for _, ifo in ipairs(available_ifos) do
if ifo.name == dictionary_name then
return ifo
end
end
return nil
end
local function tidyMarkup(results)
local cdata_tag = "<!%[CDATA%[(.-)%]%]>"
local format_escape = "&[29Ib%+]{(.-)}"
for _, result in ipairs(results) do
local def = result.definition
-- preserve the <br> tag for line break
def = def:gsub("<[bB][rR] ?/?>", "\n")
-- parse CDATA text in XML
if def:find(cdata_tag) then
def = def:gsub(cdata_tag, "%1")
-- ignore format strings
while def:find(format_escape) do
def = def:gsub(format_escape, "%1")
local ifo = getAvailableIfoByName(result.dict)
if ifo and ifo.is_html then
result.is_html = ifo.is_html
result.css = ifo.css
else
local def = result.definition
-- preserve the <br> tag for line break
def = def:gsub("<[bB][rR] ?/?>", "\n")
-- parse CDATA text in XML
if def:find(cdata_tag) then
def = def:gsub(cdata_tag, "%1")
-- ignore format strings
while def:find(format_escape) do
def = def:gsub(format_escape, "%1")
end
end
-- ignore all markup tags
def = def:gsub("%b<>", "")
-- strip all leading empty lines/spaces
def = def:gsub("^%s+", "")
result.definition = def
end
-- ignore all markup tags
def = def:gsub("%b<>", "")
-- strip all leading empty lines/spaces
def = def:gsub("^%s+", "")
result.definition = def
end
return results
end