Files

193 lines
12 KiB
Lua
Raw Permalink Blame History

This file contains invisible Unicode characters
This file contains invisible Unicode characters that are indistinguishable to humans but may be processed differently by a computer. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
--[[--
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 = [[<div class="nav-top"><a href="/files/chinese/" target="_parent">国内作家</a> |
<a href="/files/7.html" target="_parent">港台海外</a> | <a href="https://love.kanunu8.com/">传统言情</a> |
<a href="https://www.kanunu8.com/genres.html">更多图书分类</a></div>
<div class="nav-bottom"><a href="/wuxia/" target="_parent">武侠小说</a> |
<a href="/zt/" target="_parent">专题</a> | <a href="/tuili/" target="_parent">侦探推理</a></div>]]
-- 分类页:/wuxia/ 用 `2.html`,/files/chinese/ 用 `29-2.html`。
local WUXIA = NAV .. [[<div class="content book-content"><h1>武侠小说</h1>
<div class="neirong" id="neirong">
<table width='100%'><tr height='24'><td width='2%'><img src='/images/ar.gif'></td>
<td width='98%'><a href='/book2/11009/index.html' target='_blank'>沧月: 听雪楼(全集)</a></td></tr></table>
<table width='100%'><tr height='24'><td width='2%'><img src='/images/ar.gif'></td>
<td width='98%'><a href='/wuxia/201102/1727.html'>凤歌: 沧海1</a></td></tr></table>
<table width='100%'><tr height='24'><td width='2%'><img src='/images/ar.gif'></td>
<td width='98%'><a href='/zt/zt_10919.html'>江南: 龙族系列小说</a></td></tr></table>
</div></div>
<div class="book-nav"> <a href='#'>首页</a> <strong>1</strong><a href='2.html'>2</a>
<a href='2.html'>下一页</a> <a href='10.html'>末页</a> </div>]]
local CHINESE = NAV .. [[<div class="content book-content"><div class="neirong" id="neirong">
<table><tr><td><a href='/book5/changxiangsi/'>桐华:长相思(全集)</a></td></tr></table>
</div></div>
<div class="book-nav"><a href='29-2.html'>下一页</a></div>
<div class="footer">&copy; 努努书坊</div>]]
-- 频道页(最新更新):mulu-list 区块,没有分页。
local LATEST = NAV .. [[<div class="catalog"><h1>2026 最新更新</h1><div class="mulu-list"><ul>
<li><a href="/book8/songting02/" target="_blank">婚后失序</a></li>
<li><a href="/book8/maokou10/" target="_blank">修仙,写狗血文成神</a></li></ul></div></div>]]
-- 分类枢纽页:推荐作家区块里的作家页不算书。
local HUB = NAV .. [[<div class="catalog"><h1>港台、海外华人文学</h1><div class="mulu-title"><h2>推荐作家</h2></div>
<div class="mulu-list"><ul><li><a target="_blank" href="/files/writer/95.html"><strong>张爱玲</strong></a></li>
<li><a href="/book5/fanhua/">金宇澄:繁花</a></li></ul></div></div>]]
-- 真实第 2 页导航:上一页/下一页都是目录内相对路径。
local PAGE2 = [[
<div id="neirong"><ul><li><a href='/files/chinese/200805/519.html'>人生海海(完)</a></li></ul></div>
<div class="book-nav"><a href='29-1.html'>上一页</a> <a href='29-3.html'>下一页</a></div>
]]
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 .. [[<div class="neirong" id="neirong"><a href="/files/chinese/">国内作品</a></div>]],
"/files/chinese/"), nil, "认不出的链接说明不是列表页")
Assert.is_nil(Mapper.list("<div>维护中</div>", "/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("<div>没有导航</div>", "/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 = [[<div class="catalog"><h1>三体(全三册)</h1><div class="info"> 作者:刘慈欣 </div>
<div class="summary"><b>内容简介:</b><div class="intro"><p>三体系列。</p></div></div>
<div class="author"><a href="/files/writer/6620.html">刘慈欣作品集</a></div>
<div class="mulu-title"><h2>三体1:地球往事</h2></div>
<div class="mulu-list"><ul><li><a href="/book6/santi_1/11359.html">1.疯狂年代</a></li></ul></div>
<div class="mulu-title"><h2>三体2:黑暗森林</h2></div>
<div class="mulu-list"><ul><li><a href="/book6/santi_2/11400.html">1.末日之战</a></li></ul></div></div>
<div class="mulu-title"><h2 class="">看过此书的人还喜欢</h3></div>
<div class="common-list1"><a href="/book7/lingdian/"><b>1</b>《零点》</a></div>]]
local santi = Mapper.book(SANTI, "/book6/santi.html")
Assert.eq(santi.title, "三体(全三册)")
Assert.eq(santi.authors, "刘慈欣")
Assert.eq(santi.intro, "<p>三体系列。</p>")
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 = [[<div class="catalog"><h1>十日终焉</h1><div class="info">作者:杀虫队队员</div>
<div class="mulu-title"><h2>正文</h2></div>
<div class="mulu-list"><ul><li><a href="503931.html">第1章 空屋</a></li></ul></div></div>]]
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 = [[<h2><b>官场小说:驻京办主任</b></h2>
<TD height=30 class=l15> 来源: 作者:王晓方 发布时间:2008-05-29</TD>
<td><div align="center"><img height="160" width="116" src="/temp2/02/wxf-03.jpg" /></div></td>
<td class="p10-18"> 小说通过对东州市驻京办的刻画。<br /><a target="_blank" href="/files/writer/521.html">&rarr;王晓方作品集</a></td>
<table class="acss"><tbody>
<tr align="center" bgcolor="#ffffcc"><td colspan="3">第一章 迎来送往</td></tr>
<tr><td><a target="_blank" href="/files/chinese/200805/518/1339.html">1、恭王府</a></td>
<td><a target="_blank" href="/files/chinese/200805/518/1343.html">2、对弈</a></td></tr>
<tr align="center" bgcolor="#ffffcc"><td colspan="3">第二章 官场的怪圈</td></tr>
<tr><td><a target="_blank" href="/files/chinese/200805/518/1370.html">14、相约</a></td></tr>
</tbody></table>]]
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 = [[<h2><b>沧海1</b></h2>
<td><a href="/files/writer/375.html">&rarr;凤歌作品集</a></td>
<table bgcolor="#d4d0c8"><tbody>
<tr align="center" bgcolor="#ffffcc"><td align="center" colspan="3"><strong>正文</strong></td></tr>
<tr bgcolor="#ffffff"><td width="33%"><a href="1726/38518.html">第一章 祖孙</a></td></tr>
</tbody></table>]]
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 = [[<div class="content book-content"><h1>合欢</h1><div class="info">作者:金宇澄</div>
<div class="neirong" id="neirong"><p>大伯母在二楼房间里跪了四小时。<br /><br />蓓蒂以后再读《暴风骤雨》。</p></div>]]
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([[<div class="neirong" id="neirong"><p>&nbsp;&nbsp;&nbsp;&nbsp;一个老旧的钨丝灯<br />
<br /> &nbsp;&nbsp;&nbsp;&nbsp;静谧的气氛</p>
<script>var ad = 1;</script></div><div class="ad-bottom">广告</div>]]),
"<p>一个老旧的钨丝灯</p><p>静谧的气氛</p>")
Assert.eq(Mapper.contentHtml([[<div id='content' class="connie">丁能通驻足在福字碑前。<br> <br> 其实,他有私心。</div>]]),
"<p>丁能通驻足在福字碑前。</p><p>其实,他有私心。</p>")
Assert.is_nil(Mapper.contentHtml('<div class="neirong" id="neirong"><script>x</script></div>'))
Assert.is_nil(Mapper.contentHtml("<div>没有正文区</div>"))
-- /bookN/ 这类目录书页的章页在 `class="text"` 里,页尾翻页链接不算正文。
Assert.eq(Mapper.contentHtml([[<div class="col-left"><div id="Article"><div class="text" align="justify">
<p>&nbsp;&nbsp;&nbsp;&nbsp;血薇剑出鞘了。
<p>第二段落</p>
<table><td><script src="/advs/gg.js"></script></td></table>
<p align="center"><a href='index.html'>上一页</a> <a href="./">回目录</a> <a href='198282.html'>下一页</a></p>
</div></div></div><div class="side-r">侧栏推荐</div>]]),
"<p>血薇剑出鞘了。</p><p>第二段落</p>", "目录书页正文与翻页导航分离")
local dirToc = Mapper.toc([[<div class="mulu-title"><h2>第一部 血薇</h2></div>
<div class="mulu-list"><ul><li><a href="198281.html">第一节</a></li></ul></div>]], "/book2/11009/")
Assert.len(dirToc, 1, "目录书页的相对章链接能解析")
Assert.eq(dirToc[1].origin, "/book2/11009/198281.html", "章页落在书目录内")
Assert.eq(dirToc[1].volume, "第一部 血薇")