2014-09-05 13:02:13 +00:00
|
|
|
--[[
|
|
|
|
This code is derived from the LAPHLibs which can be found here:
|
|
|
|
|
|
|
|
https://github.com/Wiladams/LAPHLibs
|
|
|
|
--]]
|
2019-11-23 23:27:27 +00:00
|
|
|
local util = require("util")
|
2014-09-05 13:02:13 +00:00
|
|
|
local luxl = require("luxl")
|
|
|
|
local ffi = require("ffi")
|
|
|
|
|
|
|
|
local OPDSParser = {}
|
|
|
|
|
|
|
|
local unescape_map = {
|
|
|
|
["lt"] = "<",
|
|
|
|
["gt"] = ">",
|
|
|
|
["amp"] = "&",
|
|
|
|
["quot"] = '"',
|
|
|
|
["apos"] = "'"
|
|
|
|
}
|
|
|
|
|
2016-02-16 02:08:04 +00:00
|
|
|
local gsub = string.gsub
|
2014-09-05 13:02:13 +00:00
|
|
|
local function unescape(str)
|
|
|
|
return gsub(str, '(&(#?)([%d%a]+);)', function(orig, n, s)
|
2016-12-27 10:00:13 +00:00
|
|
|
if unescape_map[s] then
|
|
|
|
return unescape_map[s]
|
|
|
|
elseif n == "#" then -- unescape unicode
|
2019-11-23 23:27:27 +00:00
|
|
|
return util.unicodeCodepointToUtf8(tonumber(s))
|
2016-12-27 10:00:13 +00:00
|
|
|
else
|
|
|
|
return orig
|
|
|
|
end
|
2014-09-05 13:02:13 +00:00
|
|
|
end)
|
|
|
|
end
|
|
|
|
|
2016-12-27 10:00:13 +00:00
|
|
|
function OPDSParser:createFlatXTable(xlex, curr_element)
|
|
|
|
curr_element = curr_element or {}
|
2014-09-05 13:02:13 +00:00
|
|
|
|
2020-11-22 03:51:32 +00:00
|
|
|
local curr_attr_name
|
|
|
|
local attr_count = 0
|
2014-09-05 13:02:13 +00:00
|
|
|
|
|
|
|
-- start reading the thing
|
|
|
|
for event, offset, size in xlex:Lexemes() do
|
2020-11-22 03:51:32 +00:00
|
|
|
local txt = ffi.string(xlex.buf + offset, size)
|
2016-12-27 10:00:13 +00:00
|
|
|
if event == luxl.EVENT_START then
|
|
|
|
if txt ~= "xml" then
|
|
|
|
-- does current element already have something
|
|
|
|
-- with this name?
|
|
|
|
|
|
|
|
-- if it does, if it's a table, add to it
|
|
|
|
-- if it doesn't, then add a table
|
|
|
|
local tab = self:createFlatXTable(xlex)
|
|
|
|
if txt == "entry" or txt == "link" then
|
|
|
|
if curr_element[txt] == nil then
|
|
|
|
curr_element[txt] = {}
|
|
|
|
end
|
|
|
|
table.insert(curr_element[txt], tab)
|
|
|
|
elseif type(curr_element) == "table" then
|
|
|
|
curr_element[txt] = tab
|
2014-09-05 13:02:13 +00:00
|
|
|
end
|
|
|
|
end
|
2016-12-27 10:00:13 +00:00
|
|
|
elseif event == luxl.EVENT_ATTR_NAME then
|
|
|
|
curr_attr_name = unescape(txt)
|
|
|
|
elseif event == luxl.EVENT_ATTR_VAL then
|
|
|
|
curr_element[curr_attr_name] = unescape(txt)
|
2020-11-22 03:51:32 +00:00
|
|
|
attr_count = attr_count + 1
|
2016-12-27 10:00:13 +00:00
|
|
|
curr_attr_name = nil
|
|
|
|
elseif event == luxl.EVENT_TEXT then
|
|
|
|
curr_element = unescape(txt)
|
|
|
|
elseif event == luxl.EVENT_END then
|
|
|
|
return curr_element
|
2014-09-05 13:02:13 +00:00
|
|
|
end
|
|
|
|
end
|
2016-12-27 10:00:13 +00:00
|
|
|
return curr_element
|
2014-09-05 13:02:13 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
function OPDSParser:parse(text)
|
2020-11-22 03:51:32 +00:00
|
|
|
-- Murder Calibre's whole "content" block, because luxl doesn't really deal well with various XHTML quirks,
|
|
|
|
-- as the list of crappy replacements below attests to...
|
|
|
|
-- There's also a high probability of finding orphaned tags or badly nested ones in there, which will screw everything up.
|
|
|
|
text = text:gsub('<content type="xhtml">.-</content>', '')
|
|
|
|
-- luxl doesn't handle XML comments, so strip them
|
|
|
|
text = text:gsub("<!%-%-.-%-%->", "")
|
2020-12-14 17:43:23 +00:00
|
|
|
-- luxl is also particular about the syntax for self-closing, empty & orphaned tags...
|
|
|
|
text = text:gsub("<(%l+)/>", "<%1 />")
|
|
|
|
-- We also need to handle the slash-less variants for br & hr...
|
2020-12-14 17:47:11 +00:00
|
|
|
text = text:gsub("<([bh]r)>", "<%1 />")
|
2020-11-22 03:51:32 +00:00
|
|
|
-- Some OPDS catalogs wrap text in a CDATA section, remove it as it causes parsing problems
|
2018-10-22 20:57:04 +00:00
|
|
|
text = text:gsub("<!%[CDATA%[(.-)%]%]>", function (s)
|
|
|
|
return s:gsub( "%p", {["&"] = "&", ["<"] = "<", [">"] = ">" } )
|
|
|
|
end )
|
2014-09-05 13:02:13 +00:00
|
|
|
local xlex = luxl.new(text, #text)
|
2019-11-07 19:37:02 +00:00
|
|
|
return assert(self:createFlatXTable(xlex))
|
2014-09-05 13:02:13 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
return OPDSParser
|