샘플
漢 한나라 [한]
local p = {}
local mw = mw
local html = mw.html
local title_ = mw.title
local ustring = mw.ustring
---return 'U+ABCD'
---@param str string
---@return string
local function tounicode(str)
return 'U+' .. string.format('%x', ustring.codepoint(str)):upper()
end
---------------------------------------------------------------------------------------------------------
function p.main(frame) return p:main_(frame) end
function p:main_(frame)
local args = require('Module:Arguments').getArgs(frame)
local header = {}
header.mean_sound = {}
header.mean_sound.html = html.create('p'):addClass('hanja-info-header hanja-mean-sound')
function header.mean_sound:tostring()
return tostring(self.html)
end
function header.mean_sound:append(str)
self.html:wikitext(str)
end
header.projectlink = ""
local lang = args[1]
-- 1번 변수에 값이 있을 때
if lang == '한자만' then
return header.mean_sound.html:addClass('onlyhanja')
:tag('span'):addClass('hanja')
:wikitext('[[', args['한자'], ']]'):done()
elseif lang == '신자체 글자만' or lang == '일본어 글자만' then
return header.mean_sound.html:addClass('onlyhanja')
:tag('span'):addClass('hanja')
:wikitext(frame:preprocess { text = '{{일본어=|[[' .. (args['신자체'] or args['한자'] or '') ..
']]}}' }):
done()
end
---------------------------------------------------------------------------------------------------------
---------- 변수 처리
local hanja = {
char = args['한자'] or '',
-- codepoint 65535를 넘어가는지 체크할 변수, unicode의 예 U+10000
codepoint = ustring.codepoint(args['한자']),
unicode = tounicode(args['한자']),
mean = args['뜻'] or '',
sound = args['소리'] or '-',
mean_sound = args['뜻 소리'],
rating = args['급수'] or args['어문회급수']
}
if hanja.codepoint > 65535 then
header.mean_sound.html:wikitext('<span class="hanja">[[', hanja.unicode, '|', hanja.char, ']]</span> ')
else -- 아닐 때
header.mean_sound.html:wikitext('<span class="hanja">[[', hanja.char, ']]</span> ')
end
if hanja.mean_sound then
hanja.sound = hanja.mean_sound:sub(hanja.mean_sound:find('[[][^]]*[]]')):gsub('[%[%]]', '')
hanja.mean_sound = '<span class="mean">' .. hanja.mean_sound:gsub('%[', '<i>'):gsub('%]', '</i></span><span>')
else
hanja.mean_sound = hanja.mean .. ' <i>' .. hanja.sound .. '</i>'
end
if lang == "간체자만" or lang == "중국어만" then
header.mean_sound.html:wikitext('<span class="mean"> ', hanja.inChina.pinyin or '', '</span>')
else
header.mean_sound.html:node(hanja.mean_sound)
end
local wikitable = require('Module:표')
local strokeOrder = require('Module:한자/획순').strokeOrder_
local unicodeBox = require('Module:한자 정보/구현').unicodeBox
local new = function(char, strokes, strokeOrderFile, radical)
local self = {
char = nil,
strokes = nil,
radical = nil,
strokeOrderFile = nil,
content = '', title = ''
}
self.char = char;
self.strokes = strokes;
self.strokeOrderFile = strokeOrderFile;
self.radical = radical
---@param key string
---@param value any
function self:setKeyValue(key, value)
self[key] = value
end
return self
end
hanja.inKorea = new(args['한자'] or '', args['획수'] or '', args['획순파일'] or 'norm', args['부수'] or '')
hanja.inChina = new(
args['간체자'] or hanja.char,
args['간체자 획수'] or hanja.inKorea.strokes,
args['간체자획순파일'],
args['간체자 부수'] or hanja.inKorea.radical)
hanja.inChina:setKeyValue('pinyin', args['병음'])
function hanja.inChina:makeHTML()
wikitable:setClass("hanja-info-table")
wikitable:appendTd(unicodeBox(self.char, 'zh-cn'), 'unicodechar')
wikitable:appendTd(string.format('[[%s]]부 %s획', self.radical, self.strokes), 'unicodechar-sub')
wikitable:appendThTd('뜻과 소리', hanja.mean_sound, 'mean_sound')
wikitable:appendThTd('병음', self.pinyin)
wikitable:appendTd(strokeOrder("쓰는 순서", self.char, self.strokeOrderFile))
self.title = '중국어[中]'
self.content = wikitable:ret()
wikitable:clear()
end
hanja.inJapan = new(
args['신자체'] or hanja.char,
args['신자체 획수'] or hanja.inKorea.strokes,
args['신자체획순파일'],
args['신자체 부수'] or hanja.inKorea.radical)
function hanja.inJapan:makeHTML()
wikitable:setClass("hanja-info-table")
wikitable:appendTd(unicodeBox(self.char, 'ja'), 'unicodechar')
wikitable:appendTd(string.format('[[%s]]부 %s획', self.radical, self.strokes), 'unicodechar-sub')
wikitable:appendThTd('뜻과 소리', hanja.mean_sound, 'mean_sound')
wikitable:appendThTd('훈독(<span lang="ja">訓</span>読)', args['훈독'])
if args['오음'] or args['한음'] or args['당음'] or args['관용음'] then
wikitable:appendTh('음독(<span lang="ja">音読</span>)')
wikitable:appendThTd('오음(<span lang="ja">呉音</span>)', args['오음'])
wikitable:appendThTd('한음(<span lang="ja">漢音</span>)', args['한음'])
wikitable:appendThTd('당음(<span lang="ja">唐音)', args['당음'])
wikitable:appendThTd('관용음', args['관용음'])
else
wikitable:appendThTd('음독(<span lang="ja">音読</span>)', args['음독'])
end
wikitable:appendTd(strokeOrder("쓰는 순서", self.char, self.strokeOrderFile))
self.content = wikitable:ret()
self.title = '일본어[日]'
wikitable:clear()
end
hanja.inTaiwan = args['정체자'] or args['대만식']
function hanja.inKorea:makeHTML()
local hanja_variants = html.create('span'):addClass('variants')
local hasAlter = false
local function renderVariant(caption, hanja_, langcode)
if hanja_ then
return html.create('span'):addClass('variant')
:tag('span'):cssText('font-size: 11px; line-height: 1em;'):wikitext(caption):done()
:tag('span'):attr('lang', langcode):wikitext(hanja_):done()
else
return ''
end
end
if hanja.inTaiwan then
hanja_variants:node(renderVariant('대만', hanja.inTaiwan, 'zh-tw'))
hasAlter = true
end
local alterForm = {
{ args['약자'], '약자' }, { args['속자'], '속자' },
{ args['본자'], '본자' }, { args['고자'], '고자' },
{ args['이체자'], ' ' }
}
for _, elem in pairs(alterForm) do
if elem[1] then
hasAlter = true
hanja_variants:node(renderVariant(elem[2], elem[1], 'ko'))
end
end
if hasAlter == false then hanja_variants = nil end
wikitable:setClass("hanja-info-table")
wikitable:appendTd(unicodeBox(self.char, 'ko'), 'unicodechar')
wikitable:appendTd(string.format('[[%s]]부 %s획', self.radical, self.strokes), 'unicodechar-sub')
wikitable:appendThTd('뜻과 소리', hanja.mean_sound, 'mean_sound')
wikitable:appendThTd('[[중고한어]]', args['중고한어'])
wikitable:appendThTd('[[동국정운]]', args['동국정운'], 'jamocomposed_block')
wikitable:appendThTd('[[훈몽자회]]', args['훈몽자회'], 'jamocomposed_block')
if hanja.rating then
wikitable:appendThTd('[[어문회|어문회 급수]]',
string.format('[[%s 한자|%s]]', hanja.rating, hanja.rating))
end
wikitable:appendThTd('이체자', hanja_variants)
wikitable:appendTd(strokeOrder("쓰는 순서", self.char, self.strokeOrderFile))
self.title = '정자[韓/臺]'
self.content = wikitable:ret()
wikitable:clear()
end
if lang == "간체자만" or lang == "중국어만" then
hanja.codepoint = ustring.codepoint(args['간체자'])
hanja.unicode = tounicode(args['간체자'])
hanja.inChina:makeHTML()
elseif lang == "신자체만" and lang == "일본어만" then
hanja.codepoint = ustring.codepoint(args['신자체'])
hanja.unicode = tounicode(args['신자체'])
hanja.inJapan:makeHTML()
elseif lang == nil or lang == "간체자" or lang == "중국어" or lang == "신자체" or lang == "일본어" then
hanja.inJapan:makeHTML()
hanja.inChina:makeHTML()
hanja.inKorea:makeHTML()
end
---------------------------------------------------------------------------------------------------------
---------- 분류 정렬키 조작
local currentPage = mw.text.split(title_.getCurrentTitle().prefixedText, '/')
local pageName = {
root = currentPage[1],
sub = currentPage[2] or '',
}
if pageName.root == '시리즈:리브레 한자사전' and
(hanja.char == pageName.sub or hanja.unicode == pageName.sub) then
-- 제부수인지(if) 아닌지(else)
header.mean_sound.html:addClass('is-hanja-dict')
local radicalSortKey = '-' -- [[분류:(부수)|-]]
if args['부수'] == pageName.sub then
radicalSortKey = ' '
end -- [[분류:(부수)| ]]
local defaultsort -- 기본 정렬키
-- 소리가 없는지(if) 있는지(else)
if hanja.sound == '-' or args['미분류'] then
defaultsort = '-' -- 소리 없음
else
defaultsort = string.gsub(hanja.sound .. hanja.char, ' ', '')
end
local cat
-- 유니코드 U+10000 초과하는지(if) 아닌지(else) --한자 그 자체에 대한 분류
if hanja.codepoint > 65535 then
cat = hanja.unicode -- U+0000
else
cat = hanja.char -- 漢
end
local notFixedCat
local listsource = mw.title.new('사용자:하늘/메모장/카연갤'):getContent()
if listsource:find(args['한자']) ~= -1 then
notFixedCat = '[[분류:방금 추가됨]]'
end
header.mean_sound:append(ustring.format(
'[[분류:%s|%s]][[분류:한자/문자]]%s[[분류:%s| ]]%s',
args['부수'] or '',
radicalSortKey,
frame:preprocess { text = "{{DEFAULTSORT:" .. defaultsort .. "}}" },
cat,
notFixedCat))
if hanja.rating then
header.mean_sound:append('[[분류:' .. hanja.rating .. ' 한자]]')
end
if not args['상위문서x'] then
header.projectlink = require('Module:한자 정보/구현').projectlink:tostring()
end
end
---------------------------------------------------------------------------------------------------------
---------- 렌더링
local function render()
local outerbox = [[
<div class="libre hanja-info" style="
box-shadow: 0 0.5em 1em -0.125em rgb(10 10 10 / 10%%), 0 0 0 1px rgb(10 10 10 / 2%%);
border-radius: .25rem;
background:rgb(255, 255, 255, 17%%);">%s</div>]]
local innerboxes = [[
<div class="libre-tab">
<div class="libre-tab-btns">
<div class="libre-tab-btn libre-tab-btn-active">%s</div>
<div class="libre-tab-btn">%s</div>
<div class="libre-tab-btn">%s</div>
</div>
<div class="libre-tab-main">
<div class="libre-tab-main-content libre-tab-main-content-active">%s</div>
<div class="libre-tab-main-content">%s</div>
<div class="libre-tab-main-content">%s</div>
</div>
</div>]]
local hanja_info
if lang == '중국어' or lang == '간체자' then
hanja_info = ustring.format(outerbox, ustring.format(innerboxes,
hanja.inChina.title, hanja.inKorea.title, hanja.inJapan.title,
hanja.inChina.content, hanja.inKorea.content, hanja.inJapan.content))
elseif lang == "간체자만" or lang == "중국어만" then
hanja_info = ustring.format(outerbox, hanja.inChina.content)
elseif lang == '일본어' or lang == '신자체' then
hanja_info = ustring.format(outerbox, ustring.format(innerboxes,
hanja.inJapan.title, hanja.inKorea.title, hanja.inChina.title,
hanja.inJapan.content, hanja.inKorea.content, hanja.inChina.content))
elseif lang == '일본어만' or lang == '신자체만' then
hanja_info = ustring.format(outerbox, hanja.inJapan.content)
elseif lang == nil then
hanja_info = ustring.format(outerbox, ustring.format(innerboxes,
hanja.inKorea.title, hanja.inJapan.title, hanja.inChina.title,
hanja.inKorea.content, hanja.inJapan.content, hanja.inChina.content))
end
return table.concat({
header.projectlink,
hanja_info,
header.mean_sound:tostring(),
mw.getCurrentFrame():extensionTag('templatestyles', '', { src = '틀:한자 정보/styles.css' }),
mw.getCurrentFrame():extensionTag('templatestyles', '', { src = '틀:한자 정보/내부/styles.css' })
})
end
return render()
end
return p
