✨ 初始提交:月读第三方书源插件集合
每个书源一个独立插件目录,含微信读书公众号订阅、书架导入等完整功能。 Co-authored-by: Copilot <[email protected]>
This commit is contained in:
commit
ed44254f1c
54 files changed
+15615
No files matched your search
@@ -0,0 +1,146 @@
|
||||
--[[--
|
||||
微信进度原文缓存与章节壳层的坐标回归。
|
||||
|
||||
@module tests.moonsource_weread.progress_spec
|
||||
--]]
|
||||
|
||||
local Assert = require("support.assert")
|
||||
local JSON = require("support.json_stub")
|
||||
package.preload["json"] = function() return JSON end
|
||||
|
||||
local fetched = 0
|
||||
local source = "<html><body><p>原始正文与章节末尾</p></body></html>"
|
||||
package.preload["moonsource_weread.chapter"] = function()
|
||||
return {
|
||||
fetchHtmlAsync = function(_id, _chapter, cb)
|
||||
fetched = fetched + 1
|
||||
cb(source, nil, source, "html")
|
||||
end,
|
||||
}
|
||||
end
|
||||
local Position = require("moonsource_weread.progress")
|
||||
local Paths = require("utils.paths")
|
||||
local identity = { source_id = "ext.weread", stable_id = "progress-coordinate-spec" }
|
||||
local chapter = { idx = 1, uid = "old", title = "章名" }
|
||||
local path = Paths.bookWorkDir(identity.stable_id, identity.source_id) .. "/1.position.json"
|
||||
local local_path = Paths.chapterPath(identity.stable_id, 1, identity.source_id)
|
||||
os.remove(path)
|
||||
os.remove(local_path)
|
||||
|
||||
local ok, err = Position.remember(identity.stable_id, chapter, source, "html")
|
||||
Assert.is_true(ok)
|
||||
Assert.is_nil(err)
|
||||
local raw, format
|
||||
Position.loadAsync(identity, chapter, function(s, f) raw, format = s, f end)
|
||||
Assert.eq(raw, source)
|
||||
Assert.eq(format, "html")
|
||||
Assert.eq(fetched, 0, "相同章节原文命中缓存,不联网")
|
||||
|
||||
chapter.uid = "new"
|
||||
Position.loadAsync(identity, chapter, function(s, f) raw, format = s, f end)
|
||||
Assert.eq(fetched, 1, "章节 uid 变化不能复用旧原文")
|
||||
Assert.eq(raw, source)
|
||||
Position.loadAsync(identity, chapter, function(s) raw = s end)
|
||||
Assert.eq(fetched, 1)
|
||||
|
||||
local pos = { fraction = 0.5, extra = { chapter_offset = 16 } }
|
||||
Assert.is_true(Position.localize(identity, chapter, pos, source, "html"))
|
||||
Assert.eq(pos.locator, "/html/body/p/text().1")
|
||||
Assert.is_nil(pos.chapter_fraction, "字符比不能进章内页比例字段")
|
||||
Assert.not_nil(pos.extra.chapter_char_fraction)
|
||||
Assert.eq(Position.encode(identity, chapter, pos, source, "html"), 16)
|
||||
|
||||
pos = { fraction = 0.5, extra = { chapter_offset = 1 } }
|
||||
Assert.is_true(Position.localize(identity, chapter, pos, "一行正文\n第二行", "txt"))
|
||||
Assert.eq(pos.locator, "/html/body/p[1]/text().1", "TXT 的标题与章节壳层不能改变原文字符坐标")
|
||||
Assert.eq(Position.encode(identity, chapter, pos, "一行正文\n第二行", "txt"), 1)
|
||||
Assert.eq(Position.encode(identity, chapter, { fraction = 0, chapter_fraction = 0 }, source, "html"), 0)
|
||||
Assert.not_nil(Position.encode(identity, chapter, { fraction = 1, chapter_fraction = 1 }, source, "html"))
|
||||
local mid_offset = Position.encode(identity, chapter, { fraction = 0.5, chapter_fraction = 0.25 }, source, "html")
|
||||
local Annotations = require("book.annotations")
|
||||
local wire_end = Annotations.rangeMapping(source, "html").ends
|
||||
wire_end = wire_end[#wire_end] or 0
|
||||
Assert.eq(mid_offset, math.floor(0.25 * wire_end + 0.5), "没有 locator 时按章内比例换算原文偏移")
|
||||
local offset, summary, fraction, reason = Position.encode(identity, chapter, { fraction = 0.5 }, source, "html")
|
||||
Assert.eq(offset, 0, "没有定位和章内比例时按章首上报")
|
||||
Assert.eq(summary, "")
|
||||
Assert.eq(fraction, 0)
|
||||
Assert.is_nil(reason)
|
||||
Assert.eq(Position.encode(identity, chapter, {
|
||||
fraction = 0.5, locator = "/html/body/div/text().0", extra = { chapter_idx = 1, chapter_offset = 16 },
|
||||
}, source, "html"), 16, "对不上时回退同章旧偏移")
|
||||
Assert.eq(Position.encode(identity, chapter, {
|
||||
fraction = 0.5, locator = "/html/body/div/text().0", extra = { chapter_idx = 9, chapter_offset = 16 },
|
||||
}, source, "html"), 0, "旧偏移属于别的章时不能复用")
|
||||
Assert.eq(Position.encode(identity, chapter, {
|
||||
fraction = 0.5, chapter_fraction = 0.25, locator = "/html/body/div/text().0",
|
||||
extra = { chapter_idx = 1, chapter_offset = 16 },
|
||||
}, source, "html"), mid_offset, "对不上时优先章内比例,不用陈旧 extra 偏移")
|
||||
|
||||
local open = io.open
|
||||
io.open = function(filename, mode)
|
||||
if filename == path .. ".part" then return nil, "read-only cache" end
|
||||
return open(filename, mode)
|
||||
end
|
||||
ok, err = Position.remember(identity.stable_id, chapter, source, "html")
|
||||
io.open = open
|
||||
Assert.is_nil(ok)
|
||||
Assert.eq(err, "read-only cache")
|
||||
os.remove(path)
|
||||
|
||||
do
|
||||
local comic = '<html><body>'
|
||||
.. '<div><img class="qqreader-fullimg" src="a.jpg"/></div>'
|
||||
.. '<div><img class="qqreader-fullimg" src="b.jpg"/></div>'
|
||||
.. '</body></html>'
|
||||
local comic_id = { source_id = "ext.weread", stable_id = "progress-comic-spec" }
|
||||
local comic_chapter = { idx = 4, uid = "29", title = "向日葵花园" }
|
||||
local comic_local = Paths.chapterPath(comic_id.stable_id, 4, comic_id.source_id)
|
||||
Paths.ensureDir(comic_local:match("(.+)/[^/]+$"))
|
||||
local out = assert(io.open(comic_local, "wb"))
|
||||
out:write(comic)
|
||||
out:close()
|
||||
local pos = {
|
||||
locator = "/html/body/div[2]/img.0",
|
||||
chapter_fraction = 0.5,
|
||||
extra = {},
|
||||
}
|
||||
local offset, summary, fraction, err = Position.encode(comic_id, comic_chapter, pos, comic, "html")
|
||||
Assert.is_nil(err)
|
||||
Assert.not_nil(offset)
|
||||
Assert.is_true(offset > 0, "第二张图不应落在文档开头")
|
||||
Assert.eq(summary, "")
|
||||
Assert.eq(fraction, 0.5, "纯图章沿用阅读器章内页比例")
|
||||
local frag_offset, frag_summary, frag_fraction, frag_err = Position.encode(comic_id, comic_chapter, {
|
||||
locator = "/body/DocFragment[1]/body/div[2]/img.0",
|
||||
chapter_fraction = 0.5,
|
||||
extra = {},
|
||||
}, comic, "html")
|
||||
Assert.is_nil(frag_err)
|
||||
Assert.eq(frag_offset, offset, "纯图章 DocFragment 路径应映到同一偏移")
|
||||
Assert.eq(frag_summary, "")
|
||||
Assert.eq(frag_fraction, 0.5)
|
||||
local raw_len = 0
|
||||
do
|
||||
local i, len = 1, #comic
|
||||
while i <= len do
|
||||
local byte = comic:byte(i)
|
||||
i = i + (byte < 0x80 and 1 or byte < 0xe0 and 2 or byte < 0xf0 and 3 or 4)
|
||||
raw_len = raw_len + 1
|
||||
end
|
||||
end
|
||||
local fb_offset, fb_summary, fb_fraction, fb_err = Position.encode(comic_id, comic_chapter, {
|
||||
locator = "/html/body/div[9]/img.0",
|
||||
chapter_fraction = 0.25,
|
||||
extra = {},
|
||||
}, comic, "html")
|
||||
Assert.is_nil(fb_err)
|
||||
Assert.eq(fb_offset, math.floor(0.25 * raw_len + 0.5), "纯图章对不上时按原文 HTML 长度比例回退")
|
||||
Assert.eq(fb_summary, "")
|
||||
Assert.eq(fb_fraction, 0.25)
|
||||
-- 正文回归:原有文本章 encode 不被破坏
|
||||
Assert.eq(Position.encode(identity, chapter, {
|
||||
locator = "/html/body/p/text().1", fraction = 0.5, extra = { chapter_offset = 16 },
|
||||
}, source, "html"), 16)
|
||||
os.remove(comic_local)
|
||||
end
|
||||
Reference in new issue
Block a user