Skip to content

Commit

Permalink
[feat] DocumentRegistry: add getProviders() and preferred by weight
Browse files Browse the repository at this point in the history
This is step one toward "open with".

References koreader#3345
  • Loading branch information
Frenzie committed Jan 31, 2018
1 parent a3e2a80 commit 676477e
Show file tree
Hide file tree
Showing 6 changed files with 63 additions and 10 deletions.
1 change: 1 addition & 0 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
1 change: 1 addition & 0 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
33 changes: 29 additions & 4 deletions frontend/document/documentregistry.lua
Original file line number Diff line number Diff line change
Expand Up @@ -9,22 +9,47 @@ 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 >= 1 then
return providers[1].provider
end
end

--- Returns the registered document handlers.
-- @string file
-- @treturn string provider, 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[#providers].weight then
table.insert(providers, #providers, provider)
else
table.insert(providers, provider)
end
end
end

return providers
end

function DocumentRegistry:openDocument(file)
Expand Down
12 changes: 7 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,12 @@ 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)
registry:addProvider("pdf", "application/pdf", self, 100)
registry:addProvider("epub", "application/epub", self, 50)
registry:addProvider("cbz", "application/cbz", self, 100)
registry:addProvider("cbt", "application/cbt", self, 100)
registry:addProvider("zip", "application/zip", self, 100)
registry:addProvider("xps", "application/xps", self, 100)
end

return PdfDocument
3 changes: 2 additions & 1 deletion 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 (MuPDF)",
}

function PicDocument:init()
Expand Down
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 676477e

Please sign in to comment.