226 lines
7.3 KiB
Lua
226 lines
7.3 KiB
Lua
--[[--
|
|
中文维基文库的页面映射:纯函数,无网络。
|
|
|
|
分类是首页上的 Portal 链接。书是主命名空间页面,子页面归到第一个「/」前面的那一页。
|
|
目录按作品页 HTML 里的链接顺序,不用标题排序。
|
|
|
|
@module moonsource_wikisource.mapper
|
|
--]]
|
|
|
|
local _ = require("l10n").get("moonsource_wikisource")
|
|
|
|
local SOURCE_ID = "wikisource"
|
|
|
|
local Mapper = {}
|
|
|
|
Mapper.CONTENT_MARK = "moon:wikisource:1"
|
|
Mapper.SOURCE_ID = SOURCE_ID
|
|
|
|
---@param s string
|
|
---@return string
|
|
local function trim(s)
|
|
return (s:gsub("^%s+", ""):gsub("%s+$", ""))
|
|
end
|
|
|
|
---@param s string
|
|
---@return string
|
|
local function decode(s)
|
|
s = s:gsub("&", "&")
|
|
s = s:gsub("%%(%x%x)", function(h) return string.char(tonumber(h, 16)) end)
|
|
return (s:gsub("_", " "))
|
|
end
|
|
|
|
---@param html string
|
|
---@return string
|
|
local function plain(html)
|
|
local text = html:gsub("<[^>]+>", "")
|
|
text = text:gsub(""", '"'):gsub("'", "'"):gsub("&", "&")
|
|
text = text:gsub("<", "<"):gsub(">", ">")
|
|
return trim(text)
|
|
end
|
|
|
|
---@param title string
|
|
---@param category string
|
|
---@param intro string
|
|
---@return Book
|
|
local function book(title, category, intro)
|
|
return {
|
|
source_id = SOURCE_ID,
|
|
stable_id = title,
|
|
title = title,
|
|
authors = "",
|
|
category = category,
|
|
intro = intro,
|
|
cover = "",
|
|
}
|
|
end
|
|
|
|
--- 子页面归成书。主命名空间标题里的冒号是书名的一部分,不能丢掉。
|
|
---@param title string
|
|
---@return string
|
|
local function workTitle(title)
|
|
local parent = title:match("^(.-)/")
|
|
if parent and parent ~= "" then return parent end
|
|
return title
|
|
end
|
|
|
|
---@param wikitext string|nil
|
|
---@return { id: string, title: string }[]|nil
|
|
---@return string|nil
|
|
function Mapper.portals(wikitext)
|
|
if type(wikitext) ~= "string" or wikitext == "" then return nil, _("响应无效") end
|
|
local out, seen = {}, {}
|
|
for body in wikitext:gmatch("%[%[Portal:([^%[%]]-)%]%]") do
|
|
local name, label = body:match("^(.-)|(.*)$")
|
|
if not name then name, label = body, body end
|
|
name = trim(name):match("^([^#]+)") or ""
|
|
name = trim(name)
|
|
label = trim(label)
|
|
if label:find("{", 1, true) or label:find("[", 1, true) then label = name end
|
|
if name ~= "" and not seen[name] then
|
|
seen[name] = true
|
|
out[#out + 1] = { id = "Portal:" .. name, title = label ~= "" and label or name }
|
|
end
|
|
end
|
|
if not out[1] then return nil, _("响应无效") end
|
|
return out
|
|
end
|
|
|
|
---@param portals { id: string, title: string }[]
|
|
---@param node string
|
|
---@return StoreChipGroup[]
|
|
function Mapper.groups(portals, node)
|
|
local chips, active = {}, nil
|
|
for _i, portal in ipairs(portals) do
|
|
chips[#chips + 1] = { id = portal.id, title = portal.title }
|
|
if portal.id == node then active = node end
|
|
end
|
|
return { { title = _("分类"), chips = chips, active = active } }
|
|
end
|
|
|
|
---@param root table|nil
|
|
---@param category string
|
|
---@return Book[]|nil
|
|
---@return string|nil
|
|
---@return string|nil
|
|
function Mapper.portal(root, category)
|
|
if type(root) ~= "table" then return nil, nil, _("响应无效") end
|
|
local pages = root.query and root.query.pages or nil
|
|
local page = type(pages) == "table" and pages[1] or nil
|
|
if type(page) ~= "table" or page.missing then return nil, nil, _("响应无效") end
|
|
local links = page.links
|
|
if links == nil then links = {} end
|
|
if type(links) ~= "table" then return nil, nil, _("响应无效") end
|
|
local books, seen = {}, {}
|
|
for _i, link in ipairs(links) do
|
|
local title = type(link) == "table" and link.title or nil
|
|
if type(title) == "string" and title ~= "" and link.ns == 0 then
|
|
local id = workTitle(title)
|
|
if not seen[id] then
|
|
seen[id] = true
|
|
books[#books + 1] = book(id, category, "")
|
|
end
|
|
end
|
|
end
|
|
local cont = root["continue"]
|
|
local token = type(cont) == "table" and cont.plcontinue or nil
|
|
if type(token) ~= "string" or token == "" then token = nil end
|
|
return books, token
|
|
end
|
|
|
|
---@param root table|nil
|
|
---@return Book[]|nil
|
|
---@return string|nil
|
|
function Mapper.search(root)
|
|
local hits = type(root) == "table" and root.query and root.query.search or nil
|
|
if type(hits) ~= "table" then return nil, _("响应无效") end
|
|
local books, seen = {}, {}
|
|
for _i, hit in ipairs(hits) do
|
|
local title = type(hit) == "table" and hit.title or nil
|
|
if type(title) == "string" and title ~= "" then
|
|
local id = workTitle(title)
|
|
if not seen[id] then
|
|
seen[id] = true
|
|
local intro = type(hit.snippet) == "string" and plain(hit.snippet) or ""
|
|
books[#books + 1] = book(id, "", intro)
|
|
end
|
|
end
|
|
end
|
|
return books
|
|
end
|
|
|
|
---@param page table|nil
|
|
---@param stable_id string
|
|
---@param prev Book|nil
|
|
---@return Book|nil
|
|
function Mapper.detail(page, stable_id, prev)
|
|
if type(page) ~= "table" or type(page.title) ~= "string" or page.title == "" then return nil end
|
|
if type(stable_id) ~= "string" or stable_id == "" then return nil end
|
|
prev = type(prev) == "table" and prev or {}
|
|
return {
|
|
source_id = SOURCE_ID,
|
|
stable_id = stable_id,
|
|
title = page.title,
|
|
authors = type(prev.authors) == "string" and prev.authors or "",
|
|
category = type(prev.category) == "string" and prev.category or "",
|
|
intro = type(prev.intro) == "string" and prev.intro or "",
|
|
cover = "",
|
|
}
|
|
end
|
|
|
|
---@param href string
|
|
---@return string|nil
|
|
local function wikiTitle(href)
|
|
local path = href:match("/wiki/([^%?#]+)")
|
|
if not path or path == "" then return nil end
|
|
return decode(path)
|
|
end
|
|
|
|
--- 作品页上指向自己子页面的链接,按出现顺序。已有分章时略过「全覽」。
|
|
---@param page table|nil
|
|
---@return BookChapter[]|nil
|
|
---@return string|nil
|
|
function Mapper.toc(page)
|
|
if type(page) ~= "table" or type(page.title) ~= "string" or page.title == ""
|
|
or type(page.html) ~= "string" then
|
|
return nil, _("响应无效")
|
|
end
|
|
local parent = page.title
|
|
local prefix = parent .. "/"
|
|
local chapters, seen, overview = {}, {}, nil
|
|
for href in page.html:gmatch('href="([^"]+)"') do
|
|
local title = wikiTitle(href)
|
|
if title and title:sub(1, #prefix) == prefix and not seen[title] then
|
|
seen[title] = true
|
|
local rest = title:sub(#prefix + 1)
|
|
local chapter = {
|
|
uid = title,
|
|
title = rest ~= "" and rest or title,
|
|
}
|
|
if rest == "全覽" or rest == "全览" then
|
|
overview = chapter
|
|
else
|
|
chapters[#chapters + 1] = chapter
|
|
end
|
|
end
|
|
end
|
|
if not chapters[1] and overview then chapters[1] = overview end
|
|
if not chapters[1] then
|
|
chapters[1] = { uid = parent, title = parent }
|
|
end
|
|
for i, chapter in ipairs(chapters) do chapter.idx = i end
|
|
return chapters
|
|
end
|
|
|
|
---@param page table|nil
|
|
---@return string|nil
|
|
---@return string|nil
|
|
function Mapper.chapterHtml(page)
|
|
local html = type(page) == "table" and page.html or nil
|
|
if type(html) ~= "string" or html == "" then return nil, _("响应无效") end
|
|
if plain(html) == "" then return nil, _("这一章没有正文") end
|
|
return "<!--" .. Mapper.CONTENT_MARK .. "-->" .. html
|
|
end
|
|
|
|
return Mapper
|