새 문서: -- 예: Module:Dictionary local p = {} local json = mw.text.jsonEncode local data = mw.loadData('Module:Dictionary/data') -- 위키 문법 표 렌더링 function p.render(frame) local out = {} out[#out+1] = '{| class="wikitable mw-dictionary"' out[#out+1] = '! 단어 !! 뜻' for term, def in pairs(data) do out[#out+1] = '|-' out[#out+1] = '| ' .. term .. ' || ' .. def end out[#out+1] = '|}' -- JS가 가져갈 원본 JSON을 숨겨둔다 out[#out+1] = '<script...
 
편집 요약 없음
 
(같은 사용자의 중간 판 5개는 보이지 않습니다)
1번째 줄: 1번째 줄:
-- : Module:Dictionary
-- Module:Dictionary – v2 (데이터를 인수로 선택 가능)
local p     = {}
--------------------------------------------------
local json  = mw.text.jsonEncode
-- 이 모듈은 {{#invoke:Dictionary|render|dataset=xxx}} 형태로 호출할 때
local data  = mw.loadData('Module:Dictionary/data')
-- |dataset= 인수(또는 첫 번째 무명 인수)에 지정된 하위 데이터 페이지를
-- 읽어 사전을 교체합니다. 지정이 없거나, 하위 페이지가 없으면 기본
-- Module:Dictionary/data 를 그대로 사용합니다.
--------------------------------------------------
local p     = {}
local encode = mw.text.encode


-- 위키 문법 표 렌더링
-- 기본 데이터 테이블
local defaultData = mw.loadData('Module:Dictionary/data')
 
-- 주어진 이름의 하위 데이터 페이지를 안전하게 불러옵니다.
local function loadDataset(name)
    if not name or name == '' then
        return defaultData
    end
    local ok, dataset = pcall(mw.loadData, 'Module:Dictionary/data/' .. name)
    if ok and type(dataset) == 'table' then
        return dataset
    end
    return defaultData
end
 
--- Renders the dictionary search container.
-- @param frame mw.frame
function p.render(frame)
function p.render(frame)
local out = {}
    -- dataset 명은 ① 첫 번째 무명 인수, ② named 인수 dataset 에서 찾습니다.
out[#out+1] = '{| class="wikitable mw-dictionary"'
    local datasetName = frame.args[1] or frame.args.dataset
out[#out+1] = '! 단어 !! 뜻'
    local data = loadDataset(datasetName)
for term, def in pairs(data) do
out[#out+1] = '|-'
out[#out+1] = '| ' .. term .. ' || ' .. def
end
out[#out+1] = '|}'
-- JS가 가져갈 원본 JSON을 숨겨둔다
out[#out+1] = '<script type="application/json" id="dictionary-json">'
              .. json(data) .. '</script>'
return table.concat(out, '\n')
end


-- API 나 다른 모듈에서 JSON으로만 쓰고 싶을 때 호출
    -- ① 검색 UI가 들어갈 빈 컨테이너
function p.export()
    local out = {
return json(data)
        '<div class="dictionary-container dict-card"></div>',
        -- ② 숨겨 둔 JSON (XSS 방지 HTML‑escape)
        '<div id="dictionary-json" style="display:none">',
        encode(mw.text.jsonEncode(data)),
        '</div>'
    }
    return table.concat(out, '\n')
end
end


return p
return p

2025년 7월 2일 (수) 16:21 기준 최신판

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

-- Module:Dictionary – v2 (데이터를 인수로 선택 가능)
--------------------------------------------------
-- 이 모듈은 {{#invoke:Dictionary|render|dataset=xxx}} 형태로 호출할 때
-- |dataset= 인수(또는 첫 번째 무명 인수)에 지정된 하위 데이터 페이지를
-- 읽어 사전을 교체합니다. 지정이 없거나, 하위 페이지가 없으면 기본
-- Module:Dictionary/data 를 그대로 사용합니다.
--------------------------------------------------
local p      = {}
local encode = mw.text.encode

-- 기본 데이터 테이블
local defaultData = mw.loadData('Module:Dictionary/data')

-- 주어진 이름의 하위 데이터 페이지를 안전하게 불러옵니다.
local function loadDataset(name)
    if not name or name == '' then
        return defaultData
    end
    local ok, dataset = pcall(mw.loadData, 'Module:Dictionary/data/' .. name)
    if ok and type(dataset) == 'table' then
        return dataset
    end
    return defaultData
end

--- Renders the dictionary search container.
-- @param frame mw.frame
function p.render(frame)
    -- dataset 명은 ① 첫 번째 무명 인수, ② named 인수 dataset 에서 찾습니다.
    local datasetName = frame.args[1] or frame.args.dataset
    local data = loadDataset(datasetName)

    -- ① 검색 UI가 들어갈 빈 컨테이너
    local out = {
        '<div class="dictionary-container dict-card"></div>',
        -- ② 숨겨 둔 JSON (XSS 방지 HTML‑escape)
        '<div id="dictionary-json" style="display:none">',
        encode(mw.text.jsonEncode(data)),
        '</div>'
    }
    return table.concat(out, '\n')
end

return p