본문으로 이동

모듈:usex/templates

위키낱말사전, 말과 글의 누리

모듈:usex/templates예문과 관련된 기능을 담당하는 모듈입니다.

  • usex: 예문 텍스트 (필수)[해설 1]
  • lang: 예문 언어의 ISO 언어코드 (필수)
  • termlang: 텍스트와 다른 용어의 언어 객체 (기본값은 lang).
  • sc: 텍스트의 스크립트 객체 (지정되지 않으면 자동 감지).
  • quote: 인용문일 경우 지정 (CSS 클래스가 다르게 적용).[해설 2]
  • inline: 인라인 형식으로 표시할 경우 지정.
  • translation: 외국어 예문 또는 인용문의 번역.
  • lit: 문자 그대로의 번역 (번역이 관용적일 경우).
  • normalization: 비표준 철자가 흔한 오래된 언어의 예문 또는 인용문의 표준화된 버전.
  • normsc: 표준화된 텍스트의 스크립트 객체.
  • transliteration: 예문의 음역 (지정되지 않으면 표준화된 텍스트를 음역).
  • transcription: 발음과 음역이 크게 다른 언어의 경우 예문의 전사.
  • subst: 음역 전에 예문/인용문과 표준화 텍스트에 수행할 대체 문자열.
  • q: 예문/인용문 텍스트 앞에 표시할 왼쪽 수식어 목록.
  • qq: 예문/인용문 텍스트 뒤에 표시할 오른쪽 수식어 목록.
  • translation: 한국어 외 단어에 대한 예문의 번역[해설 3]
  • ref: 출처 표시를 담당합니다.
  • orig: 원문 텍스트[해설 3]

해설[편집]

  1. 만약 없을 경우 예외처리 구문을 생성합니다.
  2. 예문의 화자가 다른 화자의 말을 인용하는 상황을 가정하고 있을 경우, 이 옵션을 적용할 수 있습니다.
  3. 3.0 3.1 "차라투스트라는 이렇게 말했다"는 한국어로 쓰여졌지만 독일어를 사용하는 니체의 독일어 어록을 한국어로 번역한 것입니다. 말하다의 예문을 작성하기 위해 "차라투스트라는 이렇게 말했다"를 예문으로 사용할 수 있지만, 그것에 대한 독일어 원문을 표시하고 싶다면 orig이 사용됩니다.

-- Module:usex/templates
local export = {}

local m_languages = require("Module:languages")
local usex_module = "Module:usex"
local usex_ja_module = "Module:usex/templates/ja"
local rsplit = mw.text.split
local rfind = mw.ustring.find

local function track(page)
    require("Module:debug").track("usex/templates/" .. page)
    return true
end

function export.fetch_categories(lang, param)
    local categories = {}
    table.insert(categories, "구식화된 예문 매개변수 [" .. param .. "] 사용/" .. lang)
    return categories
end

local function insert_deprecated_params(frame, params, deprecatedParams)
    for p, _ in pairs(deprecatedParams) do
        if frame:getParent().args[p] then
            deprecatedParams[p] = true
        end
    end
    
    if deprecatedParams["언어"] then
        params[1] = frame:getParent().args["언어"]
    end
end

local function handle_specific_language(lang_code, args)
    if lang_code == "ja" then
        local ja_args = {
            [1] = args[1],
            [2] = args[2],
            [3] = args[3],
            [4] = args[4],
            ["lit"] = args.lit,
            ["rom"] = args.rom,
            ["manyou"] = args.manyou,
            ["m"] = args.m,
            ["manyou_kana"] = args.manyou_kana,
            ["m_kana"] = args.m_kana,
            ["ref"] = args.ref,
            ["sort"] = args.sort,
            ["inline"] = args.inline,
        }
        local ja_module = require(usex_ja_module)
        return ja_module.show({ getParent = function() return { args = ja_args } end })
    end
end

local function fetch_script_data(args)
    local sc = args.sc and require("Module:scripts").getByCode(args.sc, "sc") or nil
    local normsc = args.normsc
    normsc = normsc == "auto" and normsc or normsc and require("Module:scripts").getByCode(normsc, "normsc") or nil
    
    if normsc and not args.norm then
        error("Cannot specify normsc= without norm=")
    end
    return sc, normsc
end

local function fetch_original_text_data(args)
    local origlang = m_languages.getByCode(args.origlang, "origlang", "allow etym")
    local origsc = args.origsc and require("Module:scripts").getByCode(args.origsc, "origsc") or nil
    local orignormsc = args.orignormsc
    orignormsc = orignormsc == "auto" and orignormsc or
        orignormsc and require("Module:scripts").getByCode(orignormsc, "normsc") or nil
    if orignormsc and not args.orignorm then
        error("Cannot specify orignormsc= without orignorm=")
    end
    return origlang, origsc, orignormsc
end

local function validate_orig_args(args)
    for _, noparam in ipairs { "origlang", "origtr", "origts", "origsc", "orignorm", "orignormsc", "origsubst", "origref" } do
        if args[noparam] then
            error(("Cannot specify %s= without orig="):format(noparam))
        end
    end
    if #args.origq > 0 then
        error("Cannot specify origq= without orig=")
    end
    if #args.origqq > 0 then
        error("Cannot specify origqq= without orig=")
    end
end

function export.usex_t(frame)
    local iparams = {
        ["quote"] = {},
        ["compat"] = {type = "boolean"},
        ["inline"] = {type = "boolean"},
        ["nocat"] = {type = "boolean"},
        ["class"] = {},
    }

    local iargs = require("Module:parameters").process(frame.args, iparams)
    local compat = iargs.compat

    local params = {
        [1] = {required = true},
        [2] = {},
        [3] = {},
        ["termlang"] = {},
        ["tr"] = {},
        ["transliteration"] = {alias_of = "tr"},
        ["ts"] = {},
        ["transcription"] = {alias_of = "ts"},
        ["sc"] = {},
        ["norm"] = {},
        ["normalization"] = {alias_of = "norm"},
        ["normsc"] = {},
        ["subst"] = {},
        ["q"] = {list = true},
        ["qualifier"] = {alias_of = "q"},
        ["qq"] = {list = true},
        ["ref"] = {},
        [4] = {},
        ["t"] = {alias_of = 4},
        ["translation"] = {alias_of = 4},
        ["lit"] = {},
        ["orig"] = {},
        ["origlang"] = {},
        ["origtr"] = {},
        ["origts"] = {},
        ["origsc"] = {},
        ["orignorm"] = {},
        ["orignormsc"] = {},
        ["origsubst"] = {},
        ["origq"] = {list = true},
        ["origqq"] = {list = true},
        ["origref"] = {},
        ["source"] = {},
        ["footer"] = {},
        ["inline"] = {type = "boolean"},
        ["brackets"] = {type = "boolean"},
        ["nocat"] = {type = "boolean"},
        ["sort"] = {},
        ["m"] = {},
        ["manyou"] = {alias_of = "m"},
        ["manyou_kana"] = {},
        ["m_kana"] = {alias_of = "manyou_kana"},
    }

    local deprecatedParams = {
        ["언어"] = false,
        ["원문"] = false,
        ["발음"] = false,
        ["외래어표기법"] = false,
        ["외국말적기법"] = false
    }

    if compat then
        params["lang"] = {required = true}
        params["t"].alias_of = 2
        params["translation"].alias_of = 2
        table.remove(params, 1)
    end
    
    insert_deprecated_params(frame, params, deprecatedParams)

    local args = require("Module:parameters").process(frame:getParent().args, params)
    local langparam = compat and "lang" or 1
    local lang = m_languages.getByCode(args[langparam] or "und", langparam, "allow etym")

    local result = handle_specific_language(lang:getCode(), args)
    if result then
        return result
    end

    local sc, normsc = fetch_script_data(args)

    if #args.qq > 0 then track("qq") end
    if #args.q > 0 then track("q") end

    local termlang
    if args.termlang then
        termlang = m_languages.getByCode(args.termlang, "termlang", "allow etym")
        table.insert(args.qq, 1, "in " .. lang:getCanonicalName())
    end

    local origlang, origsc, orignormsc
    if args.orig then
        origlang, origsc, orignormsc = fetch_original_text_data(args)
        table.insert(args.origqq, 1, "in " .. origlang:getCanonicalName())
    else
        validate_orig_args(args)
    end

    local usex = args[compat and 1 or 2]
    local translation = args[compat and 2 or 3]
    local tr = args.tr

    if usex then
        usex = '<span style="font-size: 120%">' .. usex .. '</span>'
    end

    local data = {
        lang = lang,
        sc = sc,
        normsc = normsc,
        usex = usex,
        translation = translation,
        transliteration = tr,
        transcription = args.ts,
        normalization = args.norm,
        inline = args.inline or iargs.inline,
        ref = args.ref,
        quote = iargs.quote,
        lit = args.lit,
        subst = args.subst,
        qq = #args.qq > 0 and args.qq or args.q,
        source = args.source,
        footer = args.footer,
        nocat = args.nocat or iargs.nocat,
        brackets = args.brackets,
        sortkey = args.sort,
        class = iargs.class,
        orig = args.orig,
        origlang = origlang,
        origtr = args.origtr,
        origts = args.origts,
        origsc = origsc,
        orignorm = args.orignorm,
        orignormsc = orignormsc,
        origsubst = args.origsubst,
        origq = args.origq,
        origqq = args.origqq,
        origref = args.origref
    }

    local result = require(usex_module).format_usex(data)

    local categories = data.categories or {}
    if not args.nocat then
        table.insert(categories, ("%s 용례가 포함된 낱말|%s"):format(lang:getCanonicalName(), mw.title.getCurrentTitle().text))
    end

    return result .. require("Module:utilities").format_categories(categories, lang, args.sort)
end

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

function export.page_should_be_ignored(page)
    for _, ip in ipairs(ignore_prefixes) do
        if rfind(page, "^" .. ip) then
            return true
        end
    end
    if rfind(page, " talk:") then
        return true
    end
    return false
end

function export.page_should_be_ignored_t(frame)
    return export.page_should_be_ignored(frame.args[1]) and "true" or ""
end

return export