--[[-- 识典古籍页面数据映射:纯函数,无网络。 书库、搜索、章节都是官网直接吐出的 JSON(Modern.js loader),不走要签名的 /api。 书库每个分类只有这一屏;章节页自带全书目录和这一章的段落。 @module moonsource_shidian.mapper --]] local JSON = require("json") local _ = require("l10n").get("moonsource_shidian") local SOURCE_ID = "shidian" local Mapper = {} Mapper.CONTENT_MARK = "moon:shidian:1" Mapper.SOURCE_ID = SOURCE_ID ---@param s string ---@return string local function esc(s) return (s:gsub("&", "&"):gsub("<", "<"):gsub(">", ">")) end ---@param raw string|table|nil ---@return table|nil local function object(raw) if type(raw) == "table" then return raw end if type(raw) ~= "string" or raw == "" then return nil end local ok, data = pcall(JSON.decode, raw) if ok and type(data) == "table" then return data end end ---@param name table|string|nil ---@return string local function chapterTitle(name) if type(name) == "string" then return name end if type(name) ~= "table" then return "" end local parts = {} for _i, item in ipairs(name) do local text = type(item) == "table" and item.content or item if type(text) == "string" and text ~= "" then parts[#parts + 1] = text end end return table.concat(parts) end ---@param authors table|nil ---@return string local function authorLine(authors) local names = {} for _i, author in ipairs(authors or {}) do local name = type(author) == "table" and author.persName or nil if type(name) == "string" and name ~= "" then local role = author.responsibleTypeStr names[#names + 1] = type(role) == "string" and role ~= "" and (name .. " " .. role) or name end end return table.concat(names, "、") end ---@param info table ---@return string local function coverOf(info) local pretty = info.beautifulCover if type(pretty) == "string" and pretty ~= "" then return pretty end if type(pretty) == "table" and type(pretty.pngUrl) == "string" then return pretty.pngUrl end local cover = info.coverUrl if type(cover) == "string" then return cover end if type(cover) == "table" and type(cover.pngUrl) == "string" then return cover.pngUrl end return "" end --- 书库卡片的封面埋在 content.fileDesc 里,和章节页不是同一层。 ---@param item table ---@return string local function libraryCover(item) local content = item.content local stmt = type(content) == "table" and content.fileDesc and content.fileDesc.titleStmt if type(stmt) ~= "table" then return "" end return coverOf({ beautifulCover = stmt.beautifulCover, coverUrl = stmt.cover }) end ---@param item table ---@return string local function libraryAuthors(item) local content = item.content local monogr = type(content) == "table" and content.fileDesc and content.fileDesc.sourceDesc and content.fileDesc.sourceDesc.biblStruct and content.fileDesc.sourceDesc.biblStruct.monogr return authorLine(type(monogr) == "table" and monogr.authorList or nil) end ---@param cats table|nil ---@return string local function cateLabel(cats) local names = {} for _i, cate in ipairs(cats or {}) do if type(cate) == "table" and type(cate.cateName) == "string" and cate.cateName ~= "" then names[#names + 1] = cate.cateName end end return table.concat(names, " · ") end ---@param cats table|nil ---@return string|nil local function leafCate(cats) local id for _i, cate in ipairs(cats or {}) do if type(cate) == "table" and type(cate.cateId) == "string" and cate.cateId ~= "" then id = cate.cateId end end return id end ---@param extra table|nil ---@return string|nil local function firstSeed(extra) local ids = type(extra) == "table" and extra.hasEntityRelationChapterIds local seed = type(ids) == "table" and ids[1] if type(seed) == "string" and seed ~= "" then return seed end end ---@param item table ---@param category string ---@return Book|nil local function bookFromCard(item, category) if type(item.bookId) ~= "string" or item.bookId == "" then return nil end if type(item.bookName) ~= "string" or item.bookName == "" then return nil end local extra = type(item.extra) == "table" and item.extra or {} local intro = type(extra.introduction) == "string" and extra.introduction or "" return { source_id = SOURCE_ID, stable_id = item.bookId, title = item.bookName, authors = libraryAuthors(item), category = category ~= "" and category or (item.dynastyCategoryName or ""), intro = intro, cover = libraryCover(item), meta = { source = { shidian = { seed = firstSeed(extra) } } }, } end ---@param nodes table ---@param id string ---@param trail table ---@return table|nil local function findPath(nodes, id, trail) for _i, node in ipairs(nodes) do trail[#trail + 1] = node local key = node.value or node.key if key == id then return trail end if type(node.children) == "table" and findPath(node.children, id, trail) then return trail end trail[#trail] = nil end end ---@param nodes table ---@return { id: string, title: string }[] local function chips(nodes) local out = {} for _i, node in ipairs(nodes) do local id = node.value or node.key if type(id) == "string" and id ~= "" and id ~= "all" and type(node.label) == "string" then out[#out + 1] = { id = id, title = node.label } end end return out end --- 两行筛选:部类,以及当前这一支的下一级。全部没有对应书库页,不放进芯片。 ---@param tree table ---@param node string ---@return StoreChipGroup[] local function groups(tree, node) local top = chips(tree) local path = {} findPath(tree, node, path) local top_active = path[1] and (path[1].value or path[1].key) or nil if top_active == "all" then top_active = nil end local here = path[#path] -- 部类自己的下一级仍叫「分类」。再往下钻时,这一行就是当前类,标题用它的名字, -- 否则类名从两行芯片里消失,只剩一排没有选中项的子类。 local branch = #path > 1 and type(here.children) == "table" and here.children[1] ~= nil local section = branch and here or (#path > 1 and path[#path - 1] or path[1]) local child_nodes = section and section.children or {} local child_chips = chips(child_nodes) local child_active for _i, chip in ipairs(child_chips) do if chip.id == node then child_active = node end end local child_title = branch and type(here.label) == "string" and here.label or _("分类") local out = { { title = _("部类"), chips = top, active = top_active } } if child_chips[1] then out[#out + 1] = { title = child_title, chips = child_chips, active = child_active } end return out end ---@param root table|nil ---@param node string ---@return Book[]|nil ---@return StoreChipGroup[]|nil ---@return string|nil function Mapper.library(root, node) local lib = type(root) == "table" and root.library or nil local data = type(lib) == "table" and lib.data or nil if type(lib) ~= "table" or lib.status == 404 or type(data) ~= "table" or type(data.bookList) ~= "table" or type(lib.treeData) ~= "table" then return nil, nil, _("响应无效") end local label = "" local path = {} findPath(lib.treeData, node, path) local here = path[#path] if here and type(here.label) == "string" then label = here.label end local books = {} for _i, item in ipairs(data.bookList) do local book = type(item) == "table" and bookFromCard(item, label) or nil if book then book.meta.source.shidian.cate = node if type(item.dynastyCategoryName) == "string" and item.dynastyCategoryName ~= "" and label ~= "" then book.category = label .. " · " .. item.dynastyCategoryName end books[#books + 1] = book end end return books, groups(lib.treeData, node) end ---@param item table ---@param seed string|nil ---@return Book|nil local function bookFromSearch(item, seed) if type(item.bookId) ~= "string" or item.bookId == "" then return nil end if type(item.bookName) ~= "string" or item.bookName == "" then return nil end return { source_id = SOURCE_ID, stable_id = item.bookId, title = item.bookName, authors = authorLine(item.authors), category = cateLabel(item.traditionalCategory), intro = "", cover = coverOf(item), meta = { source = { shidian = { seed = seed, cate = leafCate(item.traditionalCategory) } } }, } end ---@param root table|nil ---@return Book[]|nil ---@return string|nil function Mapper.search(root) local data = type(root) == "table" and root.data or nil if type(data) ~= "table" then return nil, _("响应无效") end local recommended = type(data.recommendBooks) == "table" and data.recommendBooks.books or nil local paragraphs = data.paragraphs if recommended == nil and paragraphs == nil then return nil, _("响应无效") end if recommended ~= nil and type(recommended) ~= "table" then return nil, _("响应无效") end if paragraphs ~= nil and type(paragraphs) ~= "table" then return nil, _("响应无效") end local seeds = {} for _i, para in ipairs(paragraphs or {}) do local info = type(para) == "table" and para.bookInfo or nil if type(info) == "table" and type(info.bookId) == "string" and type(para.chapterId) == "string" then if not seeds[info.bookId] then seeds[info.bookId] = { chapter = para.chapterId, info = info } end end end local books, seen = {}, {} for _i, item in ipairs(recommended or {}) do local hit = type(item) == "table" and seeds[item.bookId] or nil local book = type(item) == "table" and bookFromSearch(item, hit and hit.chapter or nil) or nil if book and not seen[book.stable_id] then seen[book.stable_id] = true books[#books + 1] = book end end for _i, para in ipairs(paragraphs or {}) do local info = type(para) == "table" and para.bookInfo or nil local book = type(info) == "table" and bookFromSearch(info, para.chapterId) or nil if book and not seen[book.stable_id] then seen[book.stable_id] = true books[#books + 1] = book end end return books end ---@param root table|nil ---@param book_id string ---@return string|nil function Mapper.librarySeed(root, book_id) local list = type(root) == "table" and root.library and root.library.data and root.library.data.bookList or {} for _i, item in ipairs(list) do if type(item) == "table" and item.bookId == book_id then return firstSeed(item.extra) end end end ---@param root table|nil ---@param book_id string ---@return string|nil function Mapper.searchSeed(root, book_id) local books = Mapper.search(root) if not books then return nil end for _i, book in ipairs(books) do local own = book.meta and book.meta.source and book.meta.source.shidian if book.stable_id == book_id and type(own) == "table" then return own.seed end end end ---@param page table ---@return Book|nil function Mapper.book(page) local info = type(page) == "table" and page.bookInfo or nil if type(info) ~= "table" or type(info.bookId) ~= "string" or type(info.bookName) ~= "string" then return nil end local extra = type(info.extra) == "table" and info.extra or {} local intro = type(extra.introduction) == "string" and extra.introduction or "" return { source_id = SOURCE_ID, stable_id = info.bookId, title = info.bookName, authors = authorLine(info.authors), category = cateLabel(info.traditionalCategory), intro = intro, cover = coverOf(info), meta = { source = { shidian = { seed = page.ssrChapterId or firstSeed(extra), cate = leafCate(info.traditionalCategory), } } }, } end ---@param nodes table ---@param out BookChapter[] local function flatten(nodes, out) for _i, node in ipairs(nodes) do if type(node) == "table" and type(node.chapterId) == "string" and node.chapterId ~= "" then local title = chapterTitle(node.chapterName) out[#out + 1] = { idx = #out + 1, uid = node.chapterId, title = title ~= "" and title or node.chapterId, } end if type(node) == "table" and type(node.subChapters) == "table" then flatten(node.subChapters, out) end end end ---@param page table|nil ---@return BookChapter[]|nil ---@return string|nil function Mapper.toc(page) local info = type(page) == "table" and page.bookInfo or nil local chapters = type(info) == "table" and type(info.catalog) == "table" and info.catalog.chapters or nil if type(chapters) ~= "table" then return nil, _("响应无效") end local toc = {} flatten(chapters, toc) if not toc[1] then return nil, _("这本书的公开页面没有给出可打开的章节") end return toc end ---@param lines table ---@param blocks string[] local function appendLines(lines, blocks) local para = {} local function flush() if para[1] then blocks[#blocks + 1] = "

" .. table.concat(para) .. "

" para = {} end end for _i, line in ipairs(lines) do local text = type(line) == "table" and line.content or nil if type(text) == "string" and text ~= "" then local kind = line.lineType if kind == 4 or kind == 8 or kind == 9 then flush() blocks[#blocks + 1] = "

" .. esc(text) .. "

" elseif kind == 3 then -- 页标,没有可读正文。 elseif kind == 2 then para[#para + 1] = "" .. esc(text) .. "" else para[#para + 1] = esc(text) end end end flush() end ---@param raw string|table|nil ---@return string local function translation(raw) local doc = object(raw) local sentences = type(doc) == "table" and doc.sentences or nil if type(sentences) ~= "table" then return "" end local parts = {} for _i, sentence in ipairs(sentences) do local text = type(sentence) == "table" and sentence.content or nil if type(text) == "string" and text ~= "" then parts[#parts + 1] = esc(text) end end if not parts[1] then return "" end return "

" .. _("译文") .. ":" .. table.concat(parts) .. "

" end ---@param page table|nil ---@return string|nil ---@return string|nil function Mapper.chapterHtml(page) local list = type(page) == "table" and page.paragraphList or nil if type(list) ~= "table" then return nil, _("响应无效") end local blocks = {} for _i, para in ipairs(list) do if type(para) == "table" then local content = object(para.content) appendLines(type(content) == "table" and content.lines or {}, blocks) local translated = translation(para.translateContent) if translated ~= "" then blocks[#blocks + 1] = translated end end end if not blocks[1] then return nil, _("这一章没有正文") end return table.concat(blocks) end return Mapper