diff --git a/moonsource-legadorule.koplugin/_meta.lua b/moonsource-legadorule.koplugin/_meta.lua new file mode 100644 index 0000000..d4160d9 --- /dev/null +++ b/moonsource-legadorule.koplugin/_meta.lua @@ -0,0 +1,5 @@ +local _ = require("gettext") +return { + fullname = _("月读书源:开源阅读书源"), + description = _("给月读增加「开源阅读书源」:导入 Legado 书源 JSON 后在本机直接运行。需要同时安装月读,与平台官方无关。"), +} diff --git a/moonsource-legadorule.koplugin/main.lua b/moonsource-legadorule.koplugin/main.lua new file mode 100644 index 0000000..f7b1e52 --- /dev/null +++ b/moonsource-legadorule.koplugin/main.lua @@ -0,0 +1,9 @@ +--[[-- +KOReader 只认带 main.lua 的插件目录;书源本身不需要任何插件钩子,月读读取旁边的 moon_source.lua。 +--]] +local WidgetContainer = require("ui/widget/container/widgetcontainer") + +return WidgetContainer:extend{ + name = "moonsource_legadorule", + is_doc_only = false, +} diff --git a/moonsource-legadorule.koplugin/moon_source.lua b/moonsource-legadorule.koplugin/moon_source.lua new file mode 100644 index 0000000..2cbf6de --- /dev/null +++ b/moonsource-legadorule.koplugin/moon_source.lua @@ -0,0 +1,20 @@ +--[[-- +月读第三方书源描述:开源阅读。契约见月读 examples/moonsource-demo.koplugin/moon_source.lua。 +--]] + +-- 接入远程管理里的书源管理网页 +require("moonsource_legadorule.extras") + +local function lazy(module) + return setmetatable({}, { __index = function(_, key) return require(module)[key] end }) +end + +return { + api = 1, + provides = { + source = true, + }, + meta = function() return require("moonsource_legadorule.source").meta() end, + new = function() return require("moonsource_legadorule.source").new() end, + setting = lazy("moonsource_legadorule.setting"), +} diff --git a/moonsource-legadorule.koplugin/moonsource_legadorule/_meta.lua b/moonsource-legadorule.koplugin/moonsource_legadorule/_meta.lua new file mode 100644 index 0000000..ca11adc --- /dev/null +++ b/moonsource-legadorule.koplugin/moonsource_legadorule/_meta.lua @@ -0,0 +1,15 @@ +--[[-- +书源元信息:id / 名称 / 类型,加上启用页展示用的说明与能力清单。`init.lua` 的 `meta()` 直接返回本表。 + +@module moonsource_legadorule._meta +--]] + +local _ = require("l10n").get("moonsource_legadorule") + +return { + id = "legadorule", + name = _("开源阅读书源"), + type = "chapter", + description = _("导入 Legado 书源 JSON,在本机直接运行书源规则"), + abilities = { _("多书源搜索"), _("书源发现页浏览"), _("目录及整本缓存"), _("按章在线阅读") }, +} diff --git a/moonsource-legadorule.koplugin/moonsource_legadorule/l10n/en.lua b/moonsource-legadorule.koplugin/moonsource_legadorule/l10n/en.lua new file mode 100644 index 0000000..e2b7f51 --- /dev/null +++ b/moonsource-legadorule.koplugin/moonsource_legadorule/l10n/en.lua @@ -0,0 +1,39 @@ +return { + ["开源阅读书源"] = "Legado Sources", + ["导入 Legado 书源 JSON,在本机直接运行书源规则"] = "Import Legado book-source JSON and run the rules on this device", + ["多书源搜索"] = "Search across sources", + ["书源发现页浏览"] = "Browse source explore pages", + ["目录及整本缓存"] = "Chapter list and whole-book cache", + ["按章在线阅读"] = "Read online chapter by chapter", + ["还没有导入书源"] = "No book source imported yet", + ["没有可浏览的书源"] = "No source to browse", + ["书源已被删除"] = "This book source was removed", + ["书源"] = "Sources", + ["分类"] = "Category", + ["章节列表为空"] = "The chapter list is empty", + ["导入书源"] = "Import sources", + ["书源地址、本地文件路径或 JSON"] = "URL, file path or JSON", + ["导入"] = "Import", + ["管理书源"] = "Manage sources", + ["正在导入…"] = "Importing…", + ["没有找到有效的书源"] = "No valid book source found", + ["无法读取书源"] = "Unable to read the sources", + ["已导入 %1 个书源,跳过 %2 个无效项"] = "Imported %1 sources, skipped %2 invalid", + ["%1 个,启用 %2 个"] = "%1 total, %2 enabled", + ["未导入"] = "None", + ["启用"] = "Enable", + ["停用"] = "Disable", + ["删除"] = "Delete", + ["已停用"] = "Disabled", + ["已启用"] = "Enabled", + ["已删除"] = "Deleted", + ["书架换源"] = "Switch source for a book", + ["已换到 %1"] = "Switched to %1", + ["其他书源里没有找到这本书"] = "Not found in other sources", + ["换源:%1"] = "Switch source: %1", + ["换源"] = "Switch source", + ["正在其他书源里搜索《%1》…"] = "Searching other sources for “%1”…", + ["书源不可用"] = "Source unavailable", + ["搜索失败"] = "Search failed", + ["书架里还没有这个书源的书"] = "No books from this source on the shelf", +} diff --git a/moonsource-legadorule.koplugin/moonsource_legadorule/l10n/zh_TW.lua b/moonsource-legadorule.koplugin/moonsource_legadorule/l10n/zh_TW.lua new file mode 100644 index 0000000..3deb6cf --- /dev/null +++ b/moonsource-legadorule.koplugin/moonsource_legadorule/l10n/zh_TW.lua @@ -0,0 +1,39 @@ +return { + ["开源阅读书源"] = "開源閱讀書源", + ["导入 Legado 书源 JSON,在本机直接运行书源规则"] = "匯入 Legado 書源 JSON,在本機直接執行書源規則", + ["多书源搜索"] = "多書源搜尋", + ["书源发现页浏览"] = "書源發現頁瀏覽", + ["目录及整本缓存"] = "目錄及整本快取", + ["按章在线阅读"] = "按章線上閱讀", + ["还没有导入书源"] = "尚未匯入書源", + ["没有可浏览的书源"] = "沒有可瀏覽的書源", + ["书源已被删除"] = "此書源已被刪除", + ["书源"] = "書源", + ["分类"] = "分類", + ["章节列表为空"] = "章節列表為空", + ["导入书源"] = "匯入書源", + ["书源地址、本地文件路径或 JSON"] = "書源網址、本機檔案路徑或 JSON", + ["导入"] = "匯入", + ["管理书源"] = "管理書源", + ["正在导入…"] = "正在匯入…", + ["没有找到有效的书源"] = "找不到有效的書源", + ["无法读取书源"] = "無法讀取書源", + ["已导入 %1 个书源,跳过 %2 个无效项"] = "已匯入 %1 個書源,略過 %2 個無效項", + ["%1 个,启用 %2 个"] = "%1 個,啟用 %2 個", + ["未导入"] = "未匯入", + ["启用"] = "啟用", + ["停用"] = "停用", + ["删除"] = "刪除", + ["已停用"] = "已停用", + ["已启用"] = "已啟用", + ["已删除"] = "已刪除", + ["书架换源"] = "書架換源", + ["已换到 %1"] = "已換到 %1", + ["其他书源里没有找到这本书"] = "其他書源裡沒有找到這本書", + ["换源:%1"] = "換源:%1", + ["换源"] = "換源", + ["正在其他书源里搜索《%1》…"] = "正在其他書源裡搜索《%1》…", + ["书源不可用"] = "書源不可用", + ["搜索失败"] = "搜索失敗", + ["书架里还没有这个书源的书"] = "書架裡還沒有這個書源的書", +} diff --git a/moonsource-legadorule.koplugin/moonsource_legadorule/loadmeta.lua b/moonsource-legadorule.koplugin/moonsource_legadorule/loadmeta.lua new file mode 100644 index 0000000..d719023 --- /dev/null +++ b/moonsource-legadorule.koplugin/moonsource_legadorule/loadmeta.lua @@ -0,0 +1,5 @@ +return function() + local copy = {} + for k, v in pairs(require("moonsource_legadorule._meta")) do copy[k] = v end + return copy +end diff --git a/moonsource-legadorule.koplugin/moonsource_legadorule/mapper.lua b/moonsource-legadorule.koplugin/moonsource_legadorule/mapper.lua new file mode 100644 index 0000000..392ee1f --- /dev/null +++ b/moonsource-legadorule.koplugin/moonsource_legadorule/mapper.lua @@ -0,0 +1,80 @@ +--[[-- +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 diff --git a/moonsource-legadorule.koplugin/moonsource_legadorule/source.lua b/moonsource-legadorule.koplugin/moonsource_legadorule/source.lua new file mode 100644 index 0000000..746ef01 --- /dev/null +++ b/moonsource-legadorule.koplugin/moonsource_legadorule/source.lua @@ -0,0 +1,287 @@ +--[[-- +开源阅读书源:导入 Legado 书源 JSON,在本地直接执行书源规则(搜索 / 发现 / 详情 / 目录 / 正文)。 + +不依赖阅读 App。含 JS / WebView 的规则不支持,该书源对应步骤会报错。没有书架、进度、评论同步。 + +@module moonsource_legadorule.source +--]] + +local Browse = require("source.browse") +local Mapper = require("moonsource_legadorule.mapper") +local Sources = require("moonsource_legadorule.sources") +local SourceBase = require("source.base") +local Toc = require("source.toc") +local _ = require("l10n").get("moonsource_legadorule") + +local LegadoRule = {} + +local SEARCH_PARALLEL = 4 +local FETCH_INTERVAL = 1 + +---@return BookSourceMeta +function LegadoRule.meta() + return require("moonsource_legadorule.loadmeta")() +end + +---@class LegadoRuleSource : SourceBase +local Source = setmetatable({}, { __index = SourceBase }) +Source.__index = Source + +function LegadoRule.new() + local meta = LegadoRule.meta() + return setmetatable({ id = meta.id, name = meta.name, type = meta.type, parsers = {} }, Source) +end + +function Source:configured() + return Sources.enabledCount() > 0 +end + +function Source:capabilities() + return { scrape = false, edit = false, stats_pull = false, shelf_cache = false, shelf_push = false } +end + +--- 书源对应的解析器(同一书源复用,Cookie 罐与页面缓存随之保留)。 +function Source:parser(entry) + local cached = self.parsers[entry.key] + if cached and cached.version == Sources.version then return cached.parser end + local Parser = require("moonsource_legadorule.parser") + local parser = Parser.new(Sources.rule(entry)) + self.parsers[entry.key] = { version = Sources.version, parser = parser } + return parser +end + +local function inert() + return { cancel = function() end } +end + +--- 多个并发任务的合并取消句柄。 +local function group() + local g = { cancelled = false, handles = {} } + function g.cancel() + g.cancelled = true + for _idx, h in ipairs(g.handles) do h.cancel() end + end + return g +end + +function Source:searchAsync(keyword, cb) + local entries = Sources.enabled() + if #entries == 0 then return SourceBase.defer(cb, nil, _("还没有导入书源")) end + local g = group() + local results, errors, nextIndex, running, finished = {}, {}, 1, 0, 0 + local function maybeDone() + if g.cancelled or finished < #entries then return end + local books = {} + for i = 1, #entries do + for _idx, b in ipairs(results[i] or {}) do books[#books + 1] = b end + end + if #books == 0 and errors[1] then + cb(nil, errors[1]) + else + cb({ data = books }) + end + end + local function launch() + while running < SEARCH_PARALLEL and nextIndex <= #entries do + local i = nextIndex + nextIndex = nextIndex + 1 + running = running + 1 + local entry = entries[i] + local settled = false + local handle = self:parser(entry):search(keyword, 1, function(books, err) + if settled or g.cancelled then return end + settled = true + running, finished = running - 1, finished + 1 + if books then + local rows = {} + for _idx, b in ipairs(books) do + rows[#rows + 1] = Mapper.book(self.id, entry.key, b, entry.name) + end + results[i] = rows + else + errors[#errors + 1] = entry.name .. ": " .. tostring(err) + end + launch() + maybeDone() + end) + g.handles[#g.handles + 1] = handle + end + end + launch() + return g +end + +local function defaultNode() + for _idx, entry in ipairs(Sources.enabled()) do + local items = Sources.explore(Sources.rule(entry)) + if #items > 0 then return entry.key .. "|" .. items[1].url end + end +end + +---@return string|nil key, string|nil url +local function splitNode(node) + if type(node) ~= "string" then return nil end + return node:match("^([^|]+)|(.+)$") +end + +local function groupsFor(key, url) + local sourceChips, categoryChips = {}, {} + for _idx, entry in ipairs(Sources.enabled()) do + local items = Sources.explore(Sources.rule(entry)) + if #items > 0 then + sourceChips[#sourceChips + 1] = { id = entry.key .. "|" .. items[1].url, title = entry.name } + if entry.key == key then + for _i, item in ipairs(items) do + categoryChips[#categoryChips + 1] = { id = key .. "|" .. item.url, title = item.title } + end + end + end + end + local groups = { { title = _("书源"), chips = sourceChips, active = nil } } + for _idx, chip in ipairs(sourceChips) do + if chip.id:match("^([^|]+)|") == key then groups[1].active = chip.id end + end + if #categoryChips > 1 then + groups[#groups + 1] = { title = _("分类"), chips = categoryChips, active = key .. "|" .. url } + end + return groups +end + +function Source:browseAsync(opts, cb) + opts = opts or {} + local node = opts.node + local key, url = splitNode(node) + if not key then + node = defaultNode() + key, url = splitNode(node) + end + local entry = key and Sources.find(key) + if not entry or not entry.enabled then return SourceBase.defer(cb, nil, _("没有可浏览的书源")) end + local parser = self:parser(entry) + local paged = url:find("{{", 1, true) and url:find("page", 1, true) or url:find("<[^>]*,[^>]*>") + local lastFirst + local g = group() + local handle = Browse.pages(function(page, page_cb) + if not paged and page > 1 then page_cb({}, nil, false) return inert() end + local h = parser:explore(url, page, function(books, err) + if not books then page_cb(nil, err) return end + local rows = {} + for _idx, b in ipairs(books) do rows[#rows + 1] = Mapper.book(self.id, key, b, entry.name) end + local first = rows[1] and rows[1].stable_id + local more = paged and #rows > 0 and first ~= lastFirst + lastFirst = first + page_cb(rows, nil, more) + end) + return h + end, opts.limit or 60, groupsFor(key, url), function(result, err) + if result then result.groups = groupsFor(key, url) end + cb(result, err) + end) + g.handles[1] = handle + return g +end + +--- identity.stable_id → 书源条目、解析器、书地址(换过源的书读新书源)。 +function Source:resolve(identity) + local key, bookUrl = Sources.alias(identity.stable_id) + if not key then key, bookUrl = Mapper.splitStableId(identity.stable_id) end + local entry = key and Sources.find(key) + if not entry then return nil, _("书源已被删除") end + return entry, self:parser(entry), bookUrl +end + +function Source:getDetailAsync(identity, cb) + local entry, parser, bookUrl = self:resolve(identity) + if not entry then return SourceBase.defer(cb, nil, parser) end + return parser:detail(bookUrl, nil, function(book, err) + if not book then cb(nil, err) return end + local row = Mapper.book(self.id, entry.key, book, entry.name) + cb(require("book.store").rememberDetail({ + source_id = self.id, + stable_id = identity.stable_id, + title = row.title, + authors = row.authors, + intro = row.intro, + cover = row.cover, + })) + end) +end + +function Source:loadTocAsync(identity, cb, force) + if force then return self:refreshTocAsync(identity, cb) end + local cancelled, active = false, nil + active = Toc.readAsync(self.id, identity.stable_id, function(cached, err) + if cancelled then return end + if cached and #cached > 0 then cb(cached) return end + if err then cb(nil, err) return end + active = self:refreshTocAsync(identity, cb) + end) + return { cancel = function() + cancelled = true + if active then active.cancel() end + end } +end + +function Source:refreshTocAsync(identity, cb) + local entry, parser, bookUrl = self:resolve(identity) + if not entry then return SourceBase.defer(cb, nil, parser) end + local cancelled, active, pending = false, nil, true + local request = parser:detail(bookUrl, nil, function(book, err) + if cancelled then return end + if not book then pending = false cb(nil, err) return end + local inner = parser:chapters(book, function(chapters, cerr) + pending = false + if cancelled then return end + if not chapters then cb(nil, cerr) return end + active = Toc.parseAsync(self.id, identity.stable_id, function() + local toc = Mapper.toc(chapters) + if #toc == 0 then return nil, _("章节列表为空") end + return toc + end, cb) + end) + if pending then active = inner end + end) + if pending and not active then active = request end + return { cancel = function() + cancelled = true + if active then active.cancel() end + end } +end + +---@return ChapterFetchContent +local function contentFetcher(self) + return function(ref, ch, done) + local entry, parser, bookUrl = self:resolve(ref) + if not entry then return SourceBase.defer(done, nil, parser) end + local url, nextUrl = Mapper.splitOrigin(ch.origin) + local chapter = { title = ch.title, url = url, baseUrl = url, isVolume = false } + local book = { bookUrl = bookUrl, tocUrl = bookUrl, name = "" } + return parser:content(chapter, book, nextUrl, function(res, err) + if not res then done(nil, err) return end + done({ title = res.title ~= "" and res.title or ch.title, text = res.text }) + end) + end +end + +function Source:openBookAsync(identity, opts, cb) + local Chapter = require("source.chapter") + local open = opts and opts.chapter_idx and Chapter.openAsync or Chapter.openWithUi + return open(self, identity, identity.book, opts, { + loadToc = function(ref, done) return self:loadTocAsync(ref, done) end, + fetchContent = contentFetcher(self), + }, cb) +end + +function Source:prefetchChaptersAsync(identity, toc, from_idx, count, cb) + return require("source.chapter").prefetchAsync(identity, identity.book, toc, from_idx, count, { + fetchContent = contentFetcher(self), + interval_seconds = FETCH_INTERVAL, + }, cb) +end + +function Source:cacheAllChaptersAsync(identity, on_progress, cb, range) + return require("source.chapter").cacheAllAsync(self, identity, contentFetcher(self), on_progress, cb, + FETCH_INTERVAL, range) +end + +return LegadoRule diff --git a/moonsource-legadorule.koplugin/moonsource_legadorule/version.lua b/moonsource-legadorule.koplugin/moonsource_legadorule/version.lua new file mode 100644 index 0000000..25b37f6 --- /dev/null +++ b/moonsource-legadorule.koplugin/moonsource_legadorule/version.lua @@ -0,0 +1,4 @@ +-- 版本号由 tools/package.sh 打包时把占位符换成发布版本;源码运行显示 0.0.0-dev。 +local VERSION = "@VERSION@" +if VERSION:find("^@") then return "0.0.0-dev" end +return VERSION