Skip to content

Commit

Permalink
[feat] DocumentRegistry: add getProviders() and preferred by weight (#…
Browse files Browse the repository at this point in the history
…3651)

This is step one toward "open with".

References #3345

* Fix up some mimetypes
* Add XHTML to supported filetypes
* Add a few image files to MuPDF
	* ".bmp",
	* ".gif",
	* ".hdp",
	* ".j2k",
	* ".jp2",
	* ".jpeg",
	* ".jpg",
	* ".jpx",
	* ".jxr",
	* ".pam",
	* ".pbm",
	* ".pgm",
	* ".png",
	* ".pnm",
	* ".ppm",
	* ".tif",
	* ".tiff",
        * ".wdp",
  • Loading branch information
Frenzie committed Jan 31, 2018
1 parent 9005922 commit d714bd3
Show file tree
Hide file tree
Showing 6 changed files with 119 additions and 32 deletions.
37 changes: 21 additions & 16 deletions frontend/document/credocument.lua
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ local CreDocument = Document:new{
fallback_font = G_reader_settings:readSetting("fallback_font") or "Noto Sans CJK SC",
default_css = "./data/cr3.css",
options = CreOptions,
provider_name = "Cool Reader Engine",
}

-- NuPogodi, 20.05.12: inspect the zipfile content
Expand Down Expand Up @@ -524,27 +525,31 @@ function CreDocument:findText(pattern, origin, reverse, caseInsensitive)
end

function CreDocument:register(registry)
registry:addProvider("azw", "application/azw", self)
registry:addProvider("epub", "application/epub", self)
registry:addProvider("chm", "application/chm", self)
registry:addProvider("doc", "application/doc", self)
registry:addProvider("fb2", "application/fb2", self)
registry:addProvider("fb2.zip", "application/zip", self)
registry:addProvider("html", "application/html", self)
registry:addProvider("html.zip", "application/zip", self)
registry:addProvider("htm", "application/htm", self)
registry:addProvider("htm.zip", "application/zip", self)
registry:addProvider("azw", "application/vnd.amazon.mobi8-ebook", self, 90)
registry:addProvider("chm", "application/vnd.ms-htmlhelp", self, 90)
registry:addProvider("doc", "application/msword", self, 90)
registry:addProvider("epub", "application/epub+zip", self, 100)
registry:addProvider("fb2", "application/fb2", self, 90)
registry:addProvider("fb2.zip", "application/zip", self, 90)
registry:addProvider("htm", "text/html", self, 100)
registry:addProvider("html", "text/html", self, 100)
registry:addProvider("htm.zip", "application/zip", self, 100)
registry:addProvider("html.zip", "application/zip", self, 100)
registry:addProvider("log", "text/plain", self)
registry:addProvider("log.zip", "application/zip", self)
registry:addProvider("md", "text/plain", self)
registry:addProvider("md.zip", "application/zip", self)
registry:addProvider("mobi", "application/mobi", self)
registry:addProvider("pdb", "application/pdb", self)
registry:addProvider("prc", "application/prc", self)
registry:addProvider("mobi", "application/x-mobipocket-ebook", self, 90)
-- Palmpilot Document File
registry:addProvider("pdb", "application/vnd.palm", self, 90)
-- Palmpilot Resource File
registry:addProvider("prc", "application/vnd.palm", self)
registry:addProvider("tcr", "application/tcr", self)
registry:addProvider("txt", "text/plain", self)
registry:addProvider("txt.zip", "application/zip", self)
registry:addProvider("rtf", "application/rtf", self)
registry:addProvider("txt", "text/plain", self, 90)
registry:addProvider("txt.zip", "application/zip", self, 90)
registry:addProvider("rtf", "application/rtf", self, 90)
registry:addProvider("xhtml", "application/xhtml+xml", self, 90)
registry:addProvider("zip", "application/zip", self, 10)
end

return CreDocument
5 changes: 3 additions & 2 deletions frontend/document/djvudocument.lua
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ local DjvuDocument = Document:new{
options = KoptOptions,
koptinterface = nil,
color_bb_type = Blitbuffer.TYPE_BBRGB24,
provider_name = "DjVu Libre",
}

-- check DjVu magic string to validate
Expand Down Expand Up @@ -136,8 +137,8 @@ function DjvuDocument:drawPage(target, x, y, rect, pageno, zoom, rotation, gamma
end

function DjvuDocument:register(registry)
registry:addProvider("djvu", "application/djvu", self)
registry:addProvider("djv", "application/djvu", self)
registry:addProvider("djv", "image/vnd.djvu", self, 100)
registry:addProvider("djvu", "image/vnd.djvu", self, 100)
end

return DjvuDocument
35 changes: 31 additions & 4 deletions frontend/document/documentregistry.lua
Original file line number Diff line number Diff line change
Expand Up @@ -9,22 +9,49 @@ local DocumentRegistry = {
providers = {},
}

function DocumentRegistry:addProvider(extension, mimetype, provider)
table.insert(self.providers, { extension = extension, mimetype = mimetype, provider = provider })
function DocumentRegistry:addProvider(extension, mimetype, provider, weight)
table.insert(self.providers, {
extension = extension,
mimetype = mimetype,
provider = provider,
weight = weight or 100,
})
end

--- Returns the registered document handler.
--- Returns the preferred registered document handler.
-- @string file
-- @treturn string provider, or nil
function DocumentRegistry:getProvider(file)
local providers = self:getProviders(file)

if providers then
return providers[1].provider
end
end

--- Returns the registered document handlers.
-- @string file
-- @treturn table providers, or nil
function DocumentRegistry:getProviders(file)
local providers = {}

-- TODO: some implementation based on mime types?
for _, provider in ipairs(self.providers) do
local suffix = string.sub(file, -string.len(provider.extension) - 1)
if string.lower(suffix) == "."..provider.extension then
-- if extension == provider.extension then
return provider.provider
-- stick highest weighted provider at the front
if #providers >= 1 and provider.weight > providers[1].weight then
table.insert(providers, 1, provider)
else
table.insert(providers, provider)
end
end
end

if #providers >= 1 then
return providers
end
end

function DocumentRegistry:openDocument(file)
Expand Down
40 changes: 35 additions & 5 deletions frontend/document/pdfdocument.lua
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ local PdfDocument = Document:new{
dc_null = DrawContext.new(),
options = KoptOptions,
koptinterface = nil,
provider_name = "MuPDF",
}

function PdfDocument:init()
Expand Down Expand Up @@ -237,11 +238,40 @@ function PdfDocument:drawPage(target, x, y, rect, pageno, zoom, rotation, gamma,
end

function PdfDocument:register(registry)
registry:addProvider("pdf", "application/pdf", self)
registry:addProvider("cbz", "application/cbz", self)
registry:addProvider("cbt", "application/cbt", self)
registry:addProvider("zip", "application/zip", self)
registry:addProvider("xps", "application/xps", self)
--- Document types ---
registry:addProvider("cbt", "application/vnd.comicbook+tar", self, 100)
registry:addProvider("cbz", "application/vnd.comicbook+zip", self, 100)
registry:addProvider("epub", "application/epub+zip", self, 50)
registry:addProvider("fb2", "application/fb2", self, 80)
registry:addProvider("htm", "text/html", self, 90)
registry:addProvider("html", "text/html", self, 90)
registry:addProvider("pdf", "application/pdf", self, 100)
registry:addProvider("tar", "application/x-tar", self, 10)
registry:addProvider("xhtml", "application/xhtml+xml", self, 100)
registry:addProvider("xml", "application/xml", self, 10)
registry:addProvider("xps", "application/oxps", self, 100)
registry:addProvider("zip", "application/zip", self, 100)

--- Picture types ---
registry:addProvider("gif", "image/gif", self, 90)
-- MS HD Photo == JPEG XR
registry:addProvider("hdp", "image/vnd.ms-photo", self, 90)
registry:addProvider("j2k", "image/jp2", self, 90)
registry:addProvider("jp2", "image/jp2", self, 90)
registry:addProvider("jpeg", "image/jpeg", self, 90)
registry:addProvider("jpg", "image/jpeg", self, 90)
-- JPEG XR
registry:addProvider("jxr", "image/jxr", self, 90)
registry:addProvider("pam", "image/x-portable-arbitrarymap", self, 90)
registry:addProvider("pbm", "image/x‑portable‑bitmap", self, 90)
registry:addProvider("pgm", "image/x‑portable‑bitmap", self, 90)
registry:addProvider("png", "image/png", self, 90)
registry:addProvider("pnm", "image/x‑portable‑bitmap", self, 90)
registry:addProvider("ppm", "image/gif", self, 90)
registry:addProvider("tif", "image/tiff", self, 90)
registry:addProvider("tiff", "image/tiff", self, 90)
-- Windows Media Photo == JPEG XR
registry:addProvider("wdp", "image/vnd.ms-photo", self, 90)
end

return PdfDocument
11 changes: 6 additions & 5 deletions frontend/document/picdocument.lua
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,8 @@ local pic = nil
local PicDocument = Document:new{
_document = false,
is_pic = true,
dc_null = DrawContext.new()
dc_null = DrawContext.new(),
provider_name = "Picture Document",
}

function PicDocument:init()
Expand Down Expand Up @@ -53,10 +54,10 @@ function PicDocument:getCoverPageImage()
end

function PicDocument:register(registry)
registry:addProvider("jpeg", "image/jpeg", self)
registry:addProvider("jpg", "image/jpeg", self)
registry:addProvider("png", "image/png", self)
registry:addProvider("gif", "image/gif", self)
registry:addProvider("gif", "image/gif", self, 100)
registry:addProvider("jpg", "image/jpeg", self, 100)
registry:addProvider("jpeg", "image/jpeg", self, 100)
registry:addProvider("png", "image/png", self, 100)
end

return PicDocument
23 changes: 23 additions & 0 deletions spec/unit/document_registry_spec.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
describe("document registry module", function()
local DocumentRegistry

setup(function()
require("commonrequire")
DocumentRegistry = require("document/documentregistry")
end)

it("should get preferred rendering engine", function()
assert.is_equal("Cool Reader Engine",
DocumentRegistry:getProvider("bla.epub").provider_name)
assert.is_equal("MuPDF",
DocumentRegistry:getProvider("bla.pdf").provider_name)
end)

it("should return all supported rendering engines", function()
local providers = DocumentRegistry:getProviders("bla.epub")
assert.is_equal("Cool Reader Engine",
providers[1].provider.provider_name)
assert.is_equal("MuPDF",
providers[2].provider.provider_name)
end)
end)

0 comments on commit d714bd3

Please sign in to comment.