--[[-- Legado 解析结果 → 月读书籍 / 目录行。 书籍身份 stable_id = `<书源key>|<书籍地址>`;目录行 origin = `<章节地址>` 或 `<章节地址>\t<下一章地址>` (下一章地址用来在正文分页时不越界到下一章)。 @module moonsource_legadorule.mapper --]] local Mapper = {} local SEP = "|" ---@param key string ---@param bookUrl string function Mapper.stableId(key, bookUrl) return key .. SEP .. bookUrl end ---@return string|nil key ---@return string|nil bookUrl function Mapper.splitStableId(stable_id) local key, url = tostring(stable_id or ""):match("^([^|]+)|(.+)$") return key, url end --- 解析器的书 → 月读书籍行。 function Mapper.book(source_id, key, book, source_name) local intro = book.intro or "" local meta = {} if book.kind ~= "" then meta[#meta + 1] = book.kind end if book.wordCount ~= "" then meta[#meta + 1] = book.wordCount end if book.latest ~= "" then meta[#meta + 1] = book.latest end if #meta > 0 then intro = table.concat(meta, " · ") .. (intro ~= "" and "\n" .. intro or "") end return { source_id = source_id, stable_id = Mapper.stableId(key, book.bookUrl), title = book.name, authors = book.author or "", intro = intro, cover = book.cover ~= "" and book.cover or nil, source_name = source_name, } end --- 章节数组 → 目录行;分卷行只用来标注后面章节的 volume。 function Mapper.toc(chapters) local rows, volume = {}, nil local real = {} for _idx, ch in ipairs(chapters) do if not ch.isVolume then real[#real + 1] = ch end end local n = 0 for _idx, ch in ipairs(chapters) do if ch.isVolume then volume = ch.title else n = n + 1 local nextUrl = real[n + 1] and real[n + 1].url rows[#rows + 1] = { idx = n, uid = ch.url, title = ch.title, origin = nextUrl and (ch.url .. "\t" .. nextUrl) or ch.url, volume = volume, } end end return rows end ---@return string url ---@return string|nil nextUrl function Mapper.splitOrigin(origin) local url, nextUrl = tostring(origin):match("^(.-)\t(.*)$") if url then return url, nextUrl end return origin, nil end return Mapper