此模块的文档可以在模块:Sort/doc创建
--Sort By Stapx Steve
--本模块包分类页面生成相关的功能
local p = {} --p代表一个包(package)
function p.create(frame)
-- 获取标题
local name = tostring(mw.title.getCurrentTitle())
name = "分类:玩家"
-- 获取完整链接
local main = "https://" .. mw.uri.fullUrl( "主页").host
-- 访问 API 获取分类列表
local api_url = main .. "/api.php?action=query&format=json&list=categorymembers&cmlimit=max&cmtitle=" .. name
local lists = mw.ext.externaldata.getWebData {
url = api_url
, data = "pid=$.query.categorymembers[*].pageid, pname=$.query.categorymembers[*].title, perror=$.error.code"
, format = "json"
, 'use jsonpath'
}
-- 报错弹出
if(lists.perror)
then
return "请不要在非分类页面使用此方法: " .. lists.perror
end
-- 解析 table
local str = ""
for key, value in pairs(lists) do
if(type(value) == "table")
then
-- lists[key]["pid"]
-- lists[key]["pname"]
-- 访问 API 获取页面概要文本
local api_url = main .. "/api.php?action=query&format=json&prop=extracts&exchars=20&explaintext=1&titles=" .. lists[key]["pname"]
local list_page = mw.ext.externaldata.getWebData {
url = api_url
, data = "text = $.query.pages." .. lists[key]["pid"] .. ".extract"
, format = "json"
, 'use jsonpath'
}
-- 将空格替换为下划线,将首字为数字的加上下划线,方便下一步处理
local uname = lists[key]["pname"]
uname = (string.gsub(uname, " ", "_"))
local n = tonumber(string.sub(uname, 1, 1));
if n then
uname = "_" .. uname
end
-- 特判部分带下划线的名字
local utitle = lists[key]["pname"]
utitle = (string.gsub(utitle, "L Ac", "L_Ac"))
-- 获取游戏名
uname = require( 'Module:Info' ).getGameID( uname )
if(uname == nil)
then
uname = "缺失"
end
-- 生成卡片
str = str .. "{{分类卡片|".. uname .."|[[" .. utitle .. "]]|" .. (string.gsub(list_page.text, "\n", " ")) .. "}}"
end
end
return frame:preprocess(str)
end
return p