본문으로 이동

모듈:also

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

같이 보기에서 데이터 처리를 맡는 모듈입니다.

관련 모듈

[편집]

local m_debug = require("Module:debug")
local m_params = require("Module:parameters")
local m_scripts = require("Module:scripts")
local m_str_utils = require("Module:string utilities")
local m_util = require("Module:utilities")
local m_links = require("Module:also/link")

local codepoint = m_str_utils.codepoint
local ulen = m_str_utils.len
local yesno = require("Module:yesno")

local und = require("Module:languages").getByCode("und")

local export = {}

function export.main(frame)
    local params = {
        [1] = {required = true, list = true},
        ["sc"] = {list = true, allow_holes = true},
        ["uni"] = {list = true, allow_holes = true, separate_no_index = true},
        ["detectsc"] = {type = "boolean"}
    }
    
    local args = m_params.process(frame:getParent().args or frame.args, params)
    
    if args["sc"].maxindex > 0 then
        m_debug.track("also/sc param")
    end
    
    local uni_default = yesno((args["uni"]["default"] == "auto") or args["uni"]["default"]) and "auto" or nil
    
    local title = mw.title.getCurrentTitle()
    local full_pagename = title.fullText
    
    local detect_sc = title.nsText == "" or args["detectsc"]
    
    local items = {}
    local use_semicolon = false
    local arg_plaintext
    
    for i, arg in ipairs(args[1]) do
        local uni = args["uni"][i] or uni_default
        local sc = args["sc"][i] and m_scripts.getByCode(args["sc"][i]) or und:findBestScript(arg)
        
        if arg:find(",", 1, true) then
            use_semicolon = true
        end
        
        if not yesno(uni, uni) then
            uni = nil
        end
        
        -- Create the link.
        arg = m_links.plain_link{term = arg, lang = und, sc = sc}
        
        if not arg:match("<strong class=\"selflink\">") then
            arg = '<b class="' .. sc:getCode() .. '">' .. arg .. "</b>"
            
            local cp
            if uni then
                m_debug.track("also/uni")
                
                if uni == 'auto' then
                    arg_plaintext = m_util.get_plaintext(arg)
                    cp = (ulen(arg_plaintext) == 1) and codepoint(arg_plaintext, 1, 1)
                else
                    cp = tonumber(uni)
                    
                    if ulen(arg) ~= 1 or cp ~= codepoint(arg, 1, 1) then
                        m_debug.track("also/uni/noauto")
                    else
                        m_debug.track("also/uni/auto")
                    end
                end
            end
            
            if cp then
                local m_unidata = require('Module:Unicode data')
                
                arg = arg .. (" <small>[U+%04X %s]</small>"):format(
                    cp,
                    m_unidata.lookup_name(cp):gsub("<", "&lt;")
                )
            end
            
            table.insert(items, arg)
        end
    end
    
    if #items == 0 then
        table.insert(items, "{{{1}}}")
    end
    
    local function serial_comma_join(seq, conjunction)
        conjunction = conjunction or ","
        if #seq == 0 then
            return ""
        else
            return table.concat(seq, conjunction .. " ")
        end
    end
    
    return ("<div class=\"disambig-see-also%s\">같이 보기: <span style=\"font-size: 120%%;\">%s</span></div>"):format(
        (#items == 2) and "-2" or "",
        serial_comma_join(items, use_semicolon and ";" or ",")
    )
end

return export