2023年政策修订增补工作正在进行中,欢迎参与!
  • Moegirl.ICU:萌娘百科流亡社群 581077156(QQ),欢迎对萌娘百科运营感到失望的编辑者加入
  • Moegirl.ICU:账号认领正在试运行,有意者请参照账号认领流程

Module:Sandbox/サンムル/Luaq/test

萌娘百科,万物皆可萌的百科全书!转载请标注来源页面的网页链接,并声明引自萌娘百科。内容不可商用。
跳转到导航 跳转到搜索
Template-info.svg 模块文档  [创建] [刷新]
local module = {}

local getCode = require('Module:GetPageCode')
require("Module:Luaq")

local console = ""
local function printcls() console = "" end
local function println(...)
    local line = ""
    local count = select("#", ...)
    local flag = true
    for i = 1, count do
        if (flag) then flag = false
        else line = line .. "\t"
        end
        line = line .. tostring(select(i, ...))
    end
    line = line .. "\n"
    console = console .. line
end

local function getFuncCode(code, func)
    local funcCode = ""
    local declare = "function module." .. func .. "(" -- 函数定义开始语句
    local declare_len = mw.ustring.len(declare)
    local close = "end" -- 函数定义结束语句
    for line in mw.text.gsplit(code, "\n", true) do
        if declare then -- 尚未找到函数定义开始语句。
            if mw.ustring.len(line) >= declare_len + 1 and mw.ustring.sub(line, 1, declare_len) == declare then
                declare = nil -- 找到函数定义开始语句。
            end
        elseif close then -- 尚未找到函数定义结束语句。
            if line == close then
                close = nil -- 找到函数定义结束语句。
            else
                if funcCode ~= "" then funcCode = funcCode .. "\n" end
                funcCode = funcCode .. mw.ustring.sub(line, 5)
            end
        end
    end
    if declare == nil and close == nil then return funcCode
    else return nil
    end
end

function module.select()
    local t = { 5, 4, 3, str = "string" , 2, 1}
    local q = luaq.asQuery(t)
        :select(function(i, k, v)
            return type(v)
        end)
    
    println("数据源:")
    for k, v in pairs(t) do
        println("key = "..k, "value = "..v)
    end
     
    println()
    println("查询结果:")
    for i, v in enum(q) do
        println(v)
    end
end

function module.main(frame)
    local args = frame.args
    local func = args["func"]
    if not func or not module[func] then error("错误的函数名") end
    local output = args["output"] or "console"
    if output == "console" then
        module[func]()
        return console
    elseif output == "code" then
        local code = getCode(frame:getTitle())
        return getFuncCode(code, func)
    else return nil
    end
end

return module