본문으로 이동

모듈:usex/templates/ja

위키낱말사전, 말과 글의 누리
(모듈:usex/ja에서 넘어옴)

이 모듈에 대한 설명문서는 모듈:usex/templates/ja/설명문서에서 만들 수 있습니다

local export = {}

local remove_ruby_markup = require("Module:ja").remove_ruby_markup
local kana_to_romaji = require("Module:Hrkt-translit").tr
local lang = require("Module:languages").getByCode("ja")
local pagename = mw.loadData("Module:headword/data").pagename
local scripts = require("Module:scripts")

local function test_script(text, scriptCode, entirely)
    if type(text) ~= "string" or type(scriptCode) ~= "string" then
        mw.log("Parameters to test_script were incorrect.")
        return
    end

    local characters
    if scriptCode == "HaniHiraKana" then
        if not characters then
            characters = ""
            for code in scriptCode:gmatch("%u%l%l%l") do
                characters = characters .. scripts.getByCode(code):getCharacters()
            end
        end
    else
        characters = scripts.getByCode(scriptCode):getCharacters()
    end

    if entirely then
        return mw.ustring.match(text, "^[" .. characters .. "]+$")
    else
        text = mw.ustring.gsub(text, "%W", "")
        return mw.ustring.find(text, "[" .. characters .. "]")
    end
end

local function process_ruby_text(example, kana)
    local ruby_text
    if example and kana and example ~= kana then
        ruby_text = require('Module:ja-ruby').ruby_auto{ term = example, kana = kana }
    else
        ruby_text = remove_ruby_markup(kana)
        kana = require("Module:also/link").remove_links(kana)
    end

    if ruby_text and string.find(ruby_text, "[[", 1, true) then
        ruby_text = require("Module:also/link").language_link{ term = ruby_text, lang = lang }
    end
    ruby_text = ruby_text and ('<span lang="ja" class="Jpan">%s</span>'):format(ruby_text)
    return ruby_text, kana
end

local function should_add_category(title)
    local ignore_prefixes = {
        "사용자:", "토론:", "위키낱말사전:자유게시판",
        "Wiktionary:Translation requests", "Wiktionary:Grease pit",
        "Wiktionary:Etymology scriptorium", "Wiktionary:Information desk",
        "Wiktionary:Tea room", "Wiktionary:Requests for", "Wiktionary:Votes"
    }

    for _, prefix in ipairs(ignore_prefixes) do
        if mw.ustring.find(title, "^" .. prefix) or mw.ustring.find(title, " talk:") then
            return false
        end
    end
    return true
end

local function get_sort_key(pagename)
    if test_script(pagename, "Hira", true) or test_script(pagename, "Kana", true) then
        return lang:makeSortKey(pagename)
    else
        require("Module:debug").track("ja-usex/no sortkey")
        return nil
    end
end

local function insert_manyou_text(text, manyou, old_kana)
    local tag_start = " <span style=\"color:darkgreen; font-size:x-small;\">&#91;"
    local tag_end = "&#93;</span>"
    table.insert(text, ('<span lang="ja" class="Jpan">%s</span>'):format(old_kana ~= "" and require('Module:ja-ruby').ruby_auto{
        term = manyou,
        kana = old_kana,
    } or manyou))
    table.insert(text, tag_start)
    table.insert(text, "[[w:만요가나|만요가나]]")
    table.insert(text, tag_end)
    table.insert(text, "<dd>")
end

local function insert_kana_or_romaji(text, kana, romaji)
    if kana or romaji then
        table.insert(text, '<dd><i><span class="tr">')
        if romaji then
            table.insert(text, romaji)
        elseif kana then
            if mw.ustring.match(kana, "[。?!]") and not mw.ustring.match(kana, "%^") then
                kana = mw.ustring.gsub(kana, "([^「」『』。?!]+)", "^%1")
                kana = mw.ustring.gsub(kana, "([)」』])%^", "%1")
                kana = mw.ustring.gsub(kana, "([^)」』])([(「『])%^", "%1%2")
            end
            table.insert(text, kana_to_romaji(kana, "ja"))
        end
        table.insert(text, "</span></i></dd>")
    end
end

function export.show(frame)
    local params = {
        [1] = {},  -- 언어 코드
        [2] = {},  -- 예문
        [3] = {},  -- 가나
        [4] = {},  -- 한국어 해석
        ["lit"] = {},
        ["rom"] = {},
        ["manyou"] = {},
        ["m"] = { alias_of = "manyou" },
        ["manyou_kana"] = {},
        ["m_kana"] = { alias_of = "manyou_kana" },
        ["ref"] = {},
        ["sort"] = {},
        ["inline"] = {type = "boolean"},
    }
    local args = require("Module:parameters").process(frame:getParent().args, params)

    local text = {}
    local example, kana, translation

    if not args[2] then
        error("예문이 입력되지 않았습니다.")
    elseif test_script(args[2], "Hani") then
        example = args[2]
        if args[3] and test_script(args[3], "HaniHiraKana") then
            kana = args[3]
            translation = args[4]
        else
            error("예문의 가나 표기가 입력되지 않았습니다.")
        end
    elseif test_script(args[2], "HaniHiraKana") then
        example = args[2]
        if args[3] and test_script(args[3], "HaniHiraKana") then
            kana = args[3]
            translation = args[4]
        else
            kana = args[2]
            translation = args[3]
        end
    end

    translation = translation or '<small>(예문의 한국어 번역을 추가해주세요)</small>'
    local sortkey = args.sort or get_sort_key(pagename)

    if args["manyou"] then
        insert_manyou_text(text, args["manyou"], args["manyou_kana"])
    end

    local ruby_text
    ruby_text, kana = process_ruby_text(example, kana)
    
    if args.inline then
        table.insert(text, '<span style="font-size: 120%">' .. (ruby_text or example) .. '</span>')
    else
        table.insert(text, '<div style="font-size: 120%">' .. (ruby_text or example) .. '</div>')
    end

    if kana or args["rom"] or translation then
        if args.inline then
            table.insert(text, "<span>")
        else
            table.insert(text, "<dl>")
        end
    end
    insert_kana_or_romaji(text, kana, args["rom"])
    if translation then
        if args.inline then
            table.insert(text, "<span>" .. translation .. "</span>")
        else
            table.insert(text, "<dd>" .. translation .. "</dd>")
        end
    end
    if args["lit"] then
        if args.inline then
            table.insert(text, "<span>(literally, “" ..  args["lit"] .. "”)</span>")
        else
            table.insert(text, "<dd>(literally, “" ..  args["lit"] .. "”)</dd>")
        end
    end
    if args.inline then
        table.insert(text, "</span>")
    else
        table.insert(text, "</dl>")
    end

    local result = table.concat(text)
    local title = mw.title.getCurrentTitle()
    if title.namespace == 0 and should_add_category(title.text) then
        result = result .. "[[분류:일본어 용례가 포함된 낱말|" .. pagename .. "]]"
    end

    return result
end

return export