--[[-- moonsource_kanunu.mapper 离线用例:三套页面模板(新版书页、旧版书页、单页作品)、 分类与频道 chip、两种分页形式、GBK 正文切段。 @module tests.moonsource_kanunu.mapper_spec --]] ---@diagnostic disable: undefined-field, missing-fields, redundant-parameter, missing-parameter local Assert = require("support.assert") local Mapper = require("moonsource_kanunu.mapper") local NAV = [[ ]] -- 分类页:/wuxia/ 用 `2.html`,/files/chinese/ 用 `29-2.html`。 local WUXIA = NAV .. [[

武侠小说

沧月: 听雪楼(全集)
凤歌: 沧海1
江南: 龙族系列小说
首页 12 下一页 末页
]] local CHINESE = NAV .. [[
桐华:长相思(全集)
下一页
]] -- 频道页(最新更新):mulu-list 区块,没有分页。 local LATEST = NAV .. [[

2026 最新更新

]] -- 分类枢纽页:推荐作家区块里的作家页不算书。 local HUB = NAV .. [[

港台、海外华人文学

推荐作家

]] -- 真实第 2 页导航:上一页/下一页都是目录内相对路径。 local PAGE2 = [[
上一页 下一页
]] Assert.len(Mapper.list(WUXIA, "/wuxia/"), 2, "专题合集不进书城") Assert.eq(Mapper.list(WUXIA, "/wuxia/")[1].stable_id, "book2/11009/", "index.html 归并成带斜杠的目录身份") Assert.eq(Mapper.list(WUXIA, "/wuxia/")[1].authors, "沧月") Assert.eq(Mapper.list(WUXIA, "/wuxia/")[2].stable_id, "wuxia/201102/1727.html", "旧版书页身份就是页面路径") Assert.eq(Mapper.list(CHINESE, "/files/chinese/")[1].title, "长相思(全集)", "作者:书名 拆开") Assert.eq(Mapper.list(CHINESE, "/files/chinese/")[1].authors, "桐华") Assert.eq(Mapper.list(HUB, "/files/7.html")[1].stable_id, "book5/fanhua/", "作家页链接跳过") Assert.eq(Mapper.bookPath("/book2/11009/index.html", "/wuxia/"), "/book2/11009/", "目录书页统一成带斜杠形式") Assert.eq(Mapper.bookPath("/book2/11009", "/wuxia/"), "/book2/11009/", "缺斜杠与上一行同身份,站点不会回 301") Assert.eq(Mapper.bookPath("/book6/santi.html", "/book8/"), "/book6/santi.html", "文件形式书页保持原样") Assert.eq(Mapper.list(LATEST, "/book8/")[2].authors, "", "没有作者就不编") Assert.eq(Mapper.list(NAV .. [[
国内作品
]], "/files/chinese/"), nil, "认不出的链接说明不是列表页") Assert.is_nil(Mapper.list("
维护中
", "/wuxia/")) Assert.eq(Mapper.nextPath(WUXIA, "/wuxia/"), "/wuxia/2.html", "分页形式跟着链接走") Assert.eq(Mapper.nextPath(CHINESE, "/files/chinese/"), "/files/chinese/29-2.html") Assert.eq(Mapper.nextPath(PAGE2, "/files/chinese/29-2.html"), "/files/chinese/29-3.html", "第 2 页接着往第 3 页") Assert.is_nil(Mapper.nextPath(LATEST, "/book8/"), "最新更新没有下一页") local groups = Mapper.groups(WUXIA, "/wuxia/") Assert.len(groups, 2) Assert.eq(groups[1].title, "发现") Assert.eq(groups[2].active, "/wuxia/", "当前分类标出来") Assert.eq(groups[1].active, nil, "分类节点时频道不高亮") local channel = Mapper.groups(CHINESE, "/book8/") Assert.eq(channel[1].active, "/book8/", "频道节点时频道高亮") Assert.is_nil(channel[2].active) for _idx, chip in ipairs(groups[2].chips) do Assert.eq(Mapper.node(chip.id), chip.id, "每个分类节点都能当浏览节点:" .. tostring(chip.id)) Assert.is_true(chip.title:len() > 0, "分类有名字") end Assert.is_nil(Mapper.groups("
没有导航
", "/wuxia/")) Assert.is_nil(Mapper.node("/../../etc")) Assert.is_nil(Mapper.node("//evil.test/wuxia/")) Assert.is_nil(Mapper.node("/files/chinese/200805/518.html"), "书页不是书城节点") Assert.eq(Mapper.node("/files/chinese/"), "/files/chinese/") -- 新版书页:目录分卷,章 href 跨目录。 local SANTI = [[

三体(全三册)

作者:刘慈欣
内容简介:

三体系列。

刘慈欣作品集

三体1:地球往事

三体2:黑暗森林

看过此书的人还喜欢

1《零点》
]] local santi = Mapper.book(SANTI, "/book6/santi.html") Assert.eq(santi.title, "三体(全三册)") Assert.eq(santi.authors, "刘慈欣") Assert.eq(santi.intro, "

三体系列。

") Assert.is_nil(santi.cover, "新版书页没有封面") local toc = Mapper.toc(SANTI, "/book6/santi.html") Assert.len(toc, 2, "推荐书不进目录") Assert.eq(toc[1].uid, "/book6/santi_1/11359.html") Assert.eq(toc[1].volume, "三体1:地球往事") Assert.eq(toc[2].volume, "三体2:黑暗森林") -- 新版单卷书页:相对 href,卷名「正文」不占展示。 local MODERN = [[

十日终焉

作者:杀虫队队员

正文

]] Assert.eq(Mapper.book(MODERN, "/book7/10rizhongyan/").authors, "杀虫队队员") local modern = Mapper.toc(MODERN, "/book7/10rizhongyan/") Assert.eq(modern[1].origin, "/book7/10rizhongyan/503931.html") Assert.eq(modern[1].title, "第1章 空屋") Assert.is_nil(modern[1].volume) -- 旧版书页:作者带发布时间,封面在内容区,目录表格带卷名。 local OLD = [[

官场小说:驻京办主任

来源: 作者:王晓方 发布时间:2008-05-29
小说通过对东州市驻京办的刻画。
→王晓方作品集
第一章 迎来送往
1、恭王府 2、对弈
第二章 官场的怪圈
14、相约
]] local old = Mapper.book(OLD, "/files/chinese/200805/518.html") Assert.eq(old.title, "官场小说:驻京办主任", "书名里的冒号不切") Assert.eq(old.authors, "王晓方", "作者 cut 掉发布时间") Assert.eq(old.cover, "https://www.kanunu8.com/temp2/02/wxf-03.jpg") local oldToc = Mapper.toc(OLD, "/files/chinese/200805/518.html") Assert.len(oldToc, 3) Assert.eq(oldToc[1].origin, "/files/chinese/200805/518/1339.html") Assert.eq(oldToc[1].volume, "第一章 迎来送往") Assert.eq(oldToc[3].volume, "第二章 官场的怪圈") -- 旧版武侠书页:目录里还有作品集与相对 href。 local WUXIA_BOOK = [[

沧海1

→凤歌作品集
正文
第一章 祖孙
]] local wuxiaToc = Mapper.toc(WUXIA_BOOK, "/wuxia/201102/1727.html") Assert.len(wuxiaToc, 1, "作品集不算章") Assert.eq(wuxiaToc[1].origin, "/wuxia/201102/1726/38518.html") Assert.is_nil(wuxiaToc[1].volume, "「正文」不当卷名") -- 单页作品:书页就是正文,目录只有一章。 local STORY = [[

合欢

作者:金宇澄

大伯母在二楼房间里跪了四小时。

蓓蒂以后再读《暴风骤雨》。

]] local storyInfo = Mapper.book(STORY, "/files/little/2024/02-08-11162.html") Assert.eq(storyInfo.title, "合欢", "单页作品也能出详情") Assert.eq(storyInfo.authors, "金宇澄") Assert.eq(storyInfo.intro, "", "官网没有简介就不编") Assert.is_nil(storyInfo.cover, "广告图不算封面") local story = Mapper.toc(STORY, "/files/little/2024/02-08-11162.html") Assert.len(story, 1) Assert.eq(story[1].title, "合欢") Assert.eq(story[1].origin, "/files/little/2024/02-08-11162.html") -- 正文:新版 neirong、旧版 content、广告与脚本剔除、段首缩进交回宿主 CSS。 Assert.eq(Mapper.contentHtml([[

    一个老旧的钨丝灯

    静谧的气氛

广告
]]), "

一个老旧的钨丝灯

静谧的气氛

") Assert.eq(Mapper.contentHtml([[
丁能通驻足在福字碑前。

其实,他有私心。
]]), "

丁能通驻足在福字碑前。

其实,他有私心。

") Assert.is_nil(Mapper.contentHtml('
')) Assert.is_nil(Mapper.contentHtml("
没有正文区
")) -- /bookN/ 这类目录书页的章页在 `class="text"` 里,页尾翻页链接不算正文。 Assert.eq(Mapper.contentHtml([[

    血薇剑出鞘了。

第二段落

上一页 回目录 下一页

侧栏推荐
]]), "

血薇剑出鞘了。

第二段落

", "目录书页正文与翻页导航分离") local dirToc = Mapper.toc([[

第一部 血薇

]], "/book2/11009/") Assert.len(dirToc, 1, "目录书页的相对章链接能解析") Assert.eq(dirToc[1].origin, "/book2/11009/198281.html", "章页落在书目录内") Assert.eq(dirToc[1].volume, "第一部 血薇")