模組:引據-temp

文出維基大典

可在模組:引據-temp/doc建立此模組的說明文件

----------------------------------------
-- 挖个坑,现在还不能用
--
-- <s>梦想夏乡</s>引据模板完坑日,家祭无忘告乃翁
----------------------------------------
local p = {}

----------------------------------------
-- 工具函数
----------------------------------------
local function is_empty(str)
    return str==nil or mw.text.trim(str)==''
end

local function is_not_empty(str)
    return not is_empty(str)
end

local function error_text(str)
    local span = mw.html.create('span')
    span
        :css('color', 'red')
        :wikitext(str)
    return tostring(span)
end

local function get_param(args, names)
    for i, name in ipairs(names) do
        if is_not_empty(args[name]) then
            return args[name]
        end
    end
    return nil
end

local function escape(arg)
    return mw.text.trim(mw.text.nowiki(arg or ''))
end

local function num2chinese(num)
    return nil
end

local function date2chinese(date)
    -- 注意:這裡並沒有進行非常精確地判斷。
    if string.match(date, '.-[〇零一二三四五六七八九十百千元]年閏?[正一二三四五六七八九十臘]?[一二]?月?[一二三四五六七八九十廿卅]*日?') then
        -- 漢字日期
        return date
    elseif string.match(date, '[西農公]?[曆元]?前?[0-9]*年[0-9]*月?[0-9]*日?') then
        -- 阿拉伯數字和「年月日」
        return num2chinese(num)
    elseif string.match(date, '[0-9]-%-[0-9]-%-[0-9]-') then
        -- yyyy-mm-dd
        return nil
    else
        return nil
    end
end

----------------------------------------
-- 根据参数生成引据中的元素
----------------------------------------
local function make_title(params)
    if is_empty(params.title) then
        return error_text('錯誤:無題')
    else
        if is_empty(params.url) then
            return escape(params.title)
        else
            if is_not_empty(params.url) and is_not_empty(params.wikisourcelink) then
                return error_text('錯誤:不可同置網址與文庫連結')
            elseif is_not_empty(params.wikisourcelink) then
                return '[[s:zh:'..escape(params.wikisourcelink)..'|'..escape(params.title)..']]'
            else
                return '['..escape(params.url)..' '..escape(params.title)..']'
            end
        end
    end
end

local function make_author(params)

end

local function make_date(params)
end

----------------------------------------
-- 生成引据
----------------------------------------
local function _cite(args)
    local output = {}

    --------------------
    -- 原始參數
    --------------------
    -- 類別,取值:web/book/article
    local type = get_param(args, {'type'})
    -- 標題相關
    local title = get_param(args, {'title', '題'})
    local url = get_param(args, {'url', '網址', '址'})
    local wikisourcelink = get_param(args, {'wikisourcelink', '文庫連結', '文庫鏈接'})
    -- 作者相關
    local author = get_param(args, {'author', '著'})
    local firstname = get_param(args, {'first', '名'})
    local lastname = get_param(args, {'last', '姓'})
    local authorlink = get_param(args, {'author-link', '著者址'})
    -- 引文
    local quote = get_param(args, {'quote', '引文'})
    -- 日期相關
    -- 出處相關
    -- 存檔相關

    --------------------
    -- 生成的內容
    --------------------
    -- 標題
    local r_title = make_title({ title = title, url = url, wikisourcelink = wikisourcelink })
    if type == 'book' then
        r_title = '《'..r_title..'》'
    else
        r_title = '〈'..r_title..'〉'
    end
    -- 引文
    local r_quote = is_not_empty(quote) and ('曰「'..quote..'」') or ''

    table.insert(output, r_title)

    return table.concat(output)
end

function p.cite(frame)
    local mArguments = require('Module:Arguments')
    local origArgs = mArguments.getArgs(frame)
    local args = {}
    for k, v in pairs(origArgs) do
        args[k] = v
    end
    return _cite(args)
end

return p