--[[-- 番茄正文:PUA 字体解码 + 目录归一化。章缓存走 source.chapter。 @module koplugin.book.moonsource_fanqie.content --]] local Text = require("utils.text") local Content = {} local PUA_CODE = { { 58344, 58715 }, { 58345, 58716 } } local PUA_CHARSET = { { "D","在","主","特","家","军","然","表","场","4","要","只","v","和","?","6","别","还","g","现","儿","岁","?","?","此","象","月","3","出","战","工","相","o","男","直","失","世","F","都","平","文","什","V","O","将","真","T","那","当","?","会","立","些","u","是","十","张","学","气","大","爱","两","命","全","后","东","性","通","被","1","它","乐","接","而","感","车","山","公","了","常","以","何","可","话","先","p","i","叫","轻","M","士","w","着","变","尔","快","l","个","说","少","色","里","安","花","远","7","难","师","放","t","报","认","面","道","S","?","克","地","度","I","好","机","U","民","写","把","万","同","水","新","没","书","电","吃","像","斯","5","为","y","白","几","日","教","看","但","第","加","候","作","上","拉","住","有","法","r","事","应","位","利","你","声","身","国","问","马","女","他","Y","比","父","x","A","H","N","s","X","边","美","对","所","金","活","回","意","到","z","从","j","知","又","内","因","点","Q","三","定","8","R","b","正","或","夫","向","德","听","更","?","得","告","并","本","q","过","记","L","让","打","f","人","就","者","去","原","满","体","做","经","K","走","如","孩","c","G","给","使","物","?","最","笑","部","?","员","等","受","k","行","一","条","果","动","光","门","头","见","往","自","解","成","处","天","能","于","名","其","发","总","母","的","死","手","入","路","进","心","来","h","时","力","多","开","已","许","d","至","由","很","界","n","小","与","Z","想","代","么","分","生","口","再","妈","望","次","西","风","种","带","J","?","实","情","才","这","?","E","我","神","格","长","觉","间","年","眼","无","不","亲","关","结","0","友","信","下","却","重","己","老","2","音","字","m","呢","明","之","前","高","P","B","目","太","e","9","起","稜","她","也","W","用","方","子","英","每","理","便","四","数","期","中","C","外","样","a","海","们","任" }, { "s","?","作","口","在","他","能","并","B","士","4","U","克","才","正","们","字","声","高","全","尔","活","者","动","其","主","报","多","望","放","h","w","次","年","?","中","3","特","于","十","入","要","男","同","G","面","分","方","K","什","再","教","本","己","结","1","等","世","N","?","说","g","u","期","Z","外","美","M","行","给","9","文","将","两","许","张","友","0","英","应","向","像","此","白","安","少","何","打","气","常","定","间","花","见","孩","它","直","风","数","使","道","第","水","已","女","山","解","d","P","的","通","关","性","叫","儿","L","妈","问","回","神","来","S","","四","望","前","国","些","O","v","l","A","心","平","自","无","军","光","代","是","好","却","c","得","种","就","意","先","立","z","子","过","Y","j","表","","么","所","接","了","名","金","受","J","满","眼","没","部","那","m","每","车","度","可","R","斯","经","现","门","明","V","如","走","命","y","6","E","战","很","上","f","月","西","7","长","夫","想","话","变","海","机","x","到","W","一","成","生","信","笑","但","父","开","内","东","马","日","小","而","后","带","以","三","几","为","认","X","死","员","目","位","之","学","远","人","音","呢","我","q","乐","象","重","对","个","被","别","F","也","书","稜","D","写","还","因","家","发","时","i","或","住","德","当","o","l","比","觉","然","吃","去","公","a","老","亲","情","体","太","b","万","C","电","理","?","失","力","更","拉","物","着","原","她","工","实","色","感","记","看","出","相","路","大","你","候","2","和","?","与","p","样","新","只","便","最","不","进","T","r","做","格","母","总","爱","身","师","轻","知","往","加","从","?","天","e","H","?","听","场","由","快","边","让","把","任","8","条","头","事","至","起","点","真","手","这","难","都","界","用","法","n","处","下","又","Q","告","地","5","k","t","岁","有","会","果","利","民" } } local function utf8_codepoint(str, i) local b1 = str:byte(i) if not b1 then return nil, i end if b1 < 0x80 then return b1, i + 1 elseif b1 >= 0xC2 and b1 <= 0xDF then local b2 = str:byte(i + 1) if not b2 then return nil, i end return (b1 - 0xC0) * 0x40 + (b2 - 0x80), i + 2 elseif b1 >= 0xE0 and b1 <= 0xEF then local b2 = str:byte(i + 1) local b3 = str:byte(i + 2) if not b2 or not b3 then return nil, i end return (b1 - 0xE0) * 0x1000 + (b2 - 0x80) * 0x40 + (b3 - 0x80), i + 3 elseif b1 >= 0xF0 and b1 <= 0xF4 then local b2 = str:byte(i + 1) local b3 = str:byte(i + 2) local b4 = str:byte(i + 3) if not b2 or not b3 or not b4 then return nil, i end return (b1 - 0xF0) * 0x40000 + (b2 - 0x80) * 0x1000 + (b3 - 0x80) * 0x40 + (b4 - 0x80), i + 4 end return nil, i end function Content.decode_pua_content(content) if not content then return "" end local result = {} local i = 1 while i <= #content do local code, next_i = utf8_codepoint(content, i) if not code then table.insert(result, content:sub(i, i)) i = i + 1 else local decoded = false for mode = 1, 2 do local range = PUA_CODE[mode] if code >= range[1] and code <= range[2] then local bias = code - range[1] local charset = PUA_CHARSET[mode] if bias + 1 <= #charset and charset[bias + 1] ~= "?" then table.insert(result, charset[bias + 1]) decoded = true end break end end if not decoded then table.insert(result, content:sub(i, next_i - 1)) end i = next_i end end return table.concat(result) end --- 官方目录 /api/reader/directory/detail 的 data → 平铺章节行。 --- ID 一律是字符串(19 位,超出 double 精度,数字形态无法还原)。 --- 优先 chapterListWithVolume(二维:卷 → 章),其次 chapterList,最后只有 allItemIds 时按序号起标题。 --- 有卷名的卷给章写 depth=2 和 volume(卷名本身不占 idx,避免打乱章节文件序号);无卷名时 depth=1。 --- 章上的官方子节点收成 anchors,不另开一行。 ---@param payload table|nil 完整回包 ---@return table[] function Content.normalize_chapters(payload) local records = type(payload) == "table" and payload.data if type(records) ~= "table" then return {} end local function trim(value) if type(value) ~= "string" then return nil end value = Text.trim(value) if value == "" then return nil end return value end local function volume_title(volume) if type(volume) ~= "table" then return nil end return trim(volume.volumeName or volume.volume_name or volume.volumeTitle or volume.volume_title or volume.name or volume.title) end local function volume_chapters(volume) if type(volume) ~= "table" then return nil end if volume[1] ~= nil then return volume end local list = volume.chapterList or volume.chapter_list or volume.itemList or volume.item_list return type(list) == "table" and list or nil end ---@param ch table ---@param depth integer ---@return table[] local function official_anchors(ch, depth) local anchors = {} for _idx, key in ipairs({ "children", "childList", "child_list", "catalogList", "catalog_list", "subItemList", "sub_item_list", "anchorList", "anchor_list", "tocList", "toc_list", "subChapterList", "sub_chapter_list", }) do local list = ch[key] if type(list) == "table" then for _row_idx, row in ipairs(list) do if type(row) == "table" then local title = trim(row.title or row.name or row.chapterName or row.chapter_name) local id = row.anchor or row.id or row.itemId or row.item_id if id ~= nil then id = tostring(id) end if title or (id and id ~= "") then anchors[#anchors + 1] = { title = title or ("#" .. #anchors + 1), depth = depth, id = id, } end end end end end return anchors end local function add_chapter(out, ch, depth, volume) if type(ch) ~= "table" or ch.itemId == nil then return end ch.depth = depth if volume then ch.volume = volume end local anchors = official_anchors(ch, depth + 1) if #anchors > 0 then ch.anchors = anchors end out[#out + 1] = ch end local out = {} for _idx, volume in ipairs(records.chapterListWithVolume or {}) do local chapters = volume_chapters(volume) local name = volume_title(volume) local depth = name and 2 or 1 for _ch_idx, ch in ipairs(type(chapters) == "table" and chapters or {}) do add_chapter(out, ch, depth, name) end end if #out > 0 then return out end if type(records.chapterList) == "table" then for _idx, ch in ipairs(records.chapterList) do add_chapter(out, ch, 1) end return out end for i, item_id in ipairs(records.allItemIds or {}) do out[#out + 1] = { itemId = tostring(item_id), title = "第" .. i .. "章", depth = 1 } end return out end function Content.readable_chapters(chapters) local out = {} for _idx, chapter in ipairs(chapters or {}) do if tostring(chapter.title or "") ~= "封面" then out[#out + 1] = chapter end end return out end --- 从章节 HTML 收集 id="toc_N" 章内锚点,按文档顺序。 --- 有可见标题用原标题;否则回退为 "#N"(与目录空标题章的 "#idx" 一致)。 ---@param html string|nil ---@param depth integer|nil ---@return BookChapterAnchor[] function Content.html_anchors(html, depth) if type(html) ~= "string" or html == "" then return {} end depth = math.max(1, math.floor(tonumber(depth) or 2)) local Text = require("utils.text") local out, seen = {}, {} local pos = 1 while true do local start_at, end_at, _quote, number = html:find('id=(["\'])toc_(%d+)%1', pos) if not start_at then break end pos = end_at + 1 local id = "toc_" .. number if not seen[id] then seen[id] = true local open = start_at while open > 1 and html:sub(open, open) ~= "<" do open = open - 1 end local title = "" if html:sub(open, open) == "<" then local gt = html:find(">", open, true) if gt and html:sub(gt - 1, gt) ~= "/>" then local tag = html:match("^<%s*([%w:-]+)", open) if tag then local close = html:find("", gt + 1, true) if close then title = Text.stripHtml(html:sub(gt + 1, close - 1)) or "" end end end end title = title:match("^%s*(.-)%s*$") or "" if title == "" then title = "#" .. number end out[#out + 1] = { title = title, depth = depth, id = id } end end return out end --- 把incoming锚点并入 chapter.anchors:按 id 去重,已有真实标题不被回退标题覆盖。 ---@param chapter table ---@param incoming BookChapterAnchor[]|nil ---@return boolean changed function Content.merge_anchors(chapter, incoming) if type(chapter) ~= "table" or type(incoming) ~= "table" or #incoming == 0 then return false end local existing = chapter.anchors or {} local by_id = {} for _idx, anchor in ipairs(existing) do if anchor.id then by_id[tostring(anchor.id)] = anchor end end local changed = false for _idx, anchor in ipairs(incoming) do local id = anchor.id and tostring(anchor.id) or nil local prev = id and by_id[id] if not prev then existing[#existing + 1] = anchor if id then by_id[id] = anchor end changed = true elseif (not prev.title or prev.title == "") and anchor.title and anchor.title ~= "" then prev.title = anchor.title changed = true end end if changed then chapter.anchors = existing end return changed end return Content