local mw = mw
local ustring = mw.ustring
local p = {}
------------------------- code to char ------------------------------------------
---@param code string string: U+ABCD
function p.tochar(code)
return ustring.char(tonumber(code:gsub(" ", ""):gsub("[Uu][+]", "0x"), 16))
end
------------------------- codepoint (frame) ------------------------------------------
---return 'U+ABCD'
---@param frame table
---@return string
function p.tounicode(frame)
return 'U+' .. string.format('%x', ustring.codepoint(frame.args[1])):upper()
end
------------------------- codepoint ------------------------------------------
---문자 코드를 반환합니다.
---@param char string
---@param base string example) 10 : U+1234, r10 : 1234, 16(default) : U+ABCD
---@param offset integer 0(default)
---@return string
function p._code(char, base, offset)
if char == nil then
return 'char is nil'
end
if type(char) ~= 'string' then
return 'char is not a string (' .. type(char) ..')'
end
if offset == nil then
elseif type(offset) ~= 'number' then
return 'offset is not a number (' .. type(offset) ..')'
end
base = base or '16'
local codepoint = ustring.codepoint(char) + (offset or 0)
if base == 'r10' then
return codepoint
end
local head = 'U+'
local baseformat = {
['10'] = "%d",
['16'] = "%x"
}
return head .. ustring.format(baseformat[base], codepoint):upper()
end
function p.code(data, base, offset)
local getArgs = require('Module:Arguments').getArgs --- frame 인수 처리
local char = getArgs(data)[1] or nil
return p._code(char, base, offset)
end
---------------------- next codepoint ---------------------------------------
---
---입력된 문자의 이후 문자 코드 반환
---@param data table | string table : frame, string : char
---@return string
function p.nextcode(data)
local getArgs = require('Module:Arguments').getArgs --- frame 인수 처리
local char
if type(data) == 'table' then
char = getArgs(data)[1] or nil
else
char = data or nil
end
if char then
return p._code(char, '16', 1)
else
return '입력값이 없습니다.'
end
end
---------------------- prev codepoint ---------------------------------------
---
---입력된 문자의 이전 문자 코드 반환
---@param data table | string table : frame, string : char
---@return string
function p.prevcode(data)
local getArgs = require('Module:Arguments').getArgs --- frame 인수 처리
local char
if type(data) == 'table' then
char = getArgs(data)[1] or nil
else
char = data or nil
end
if char then
return p._code(char, '16', -1)
else
return '입력값이 없습니다.'
end
end
---------------------- next char ---------------------------------------
---
---입력된 문자의 이후 문자 반환
---@param data table | string table : frame, string : char
---@return string nextchar
function p.nextchar(data)
local getArgs = require('Module:Arguments').getArgs --- frame 인수 처리
local char
if type(data) == 'table' then
char = getArgs(data)[1] or nil
else
char = data or nil
end
if char then
return ustring.char(p._code(char, 'r10', 1))
else
return '입력값이 없습니다.'
end
end
---------------------- prev char ---------------------------------------
---
---입력된 문자의 이전 문자 반환
---@param data table | string table : frame, string : char
---@return string prevchar
function p.prevchar(data)
local getArgs = require('Module:Arguments').getArgs --- frame 인수 처리
local char
if type(data) == 'table' then
char = getArgs(data)[1] or nil
else
char = data or nil
end
if char then
return ustring.char(p._code(char, 'r10', -1))
else
return '입력값이 없습니다.'
end
end
---------------------- {{유니코드문자}} --------------------------
---@param frame table
---@return string
function p.uni_char(frame)
local unicodeBlock = require('Module:UnicodeBlock').unicodeBlock
local _buildlink = require('Module:linkwithtemplate').makelink
local getArgs = require('Module:Arguments').getArgs --- frame 인수 처리
---@type table
local args = getArgs(frame)
local class = (args['class'] == 'x') and '' or (args['class'] or 'infobox')
local char_current = args[1] or args['기호'] or args['문자'] or nil
local char_name = args['이름'] or mw.title.getCurrentTitle().prefixedText
local eng_name = args['영어이름'] or unicodeBlock(char_current)
local locale = args['lang'] or 'ko'
local prefix = args['경로'] or ''
local prev_arrow_icon = '<i class="fas fa-chevron-left uni_char-arrow"></i></span>'
local prev_char = args['이전'] or args['전문자'];
local prev_linkname = args['전문자이름'] or args['이전이름'] or prev_char
local next_arrow_icon = '<i class="fas fa-chevron-right uni_char-arrow"></i></span>'
local next_char = args['이후'] or args['후문자'];
local next_linkname = args['후문자이름'] or args['이후이름'] or next_char
local template = args['템플릿']
local editintro = args['editintro']
local prev_link, next_link
local htmlroot = mw.html.create('table')
:addClass(class)
:addClass('uni_char')
:cssText('margin: 0 auto; float: right; text-align: center; clear:right')
local header
if char_name == '없음' then
header = ustring.format('<tr><th colspan="3"><small>%s</small></th></tr>', eng_name)
else
header = ustring.format('<tr><th colspan="3"><div>%s</div><small>%s</small></th></tr>', char_name, eng_name)
end
local function buildlink(article, linkname, arrow, preload, editintro)
if preload then
return _buildlink(article, string.format('<div>%s<br>%s</div>', linkname, arrow), preload, editintro)
else
return ustring.format('[[%s|<div>%s<br>%s</div>]]', article, linkname, arrow or '')
end
end
if char_current == nil then
return '<span class="warning">기호 인수가 없습니다.</span>'
else
if char_current:find("[Uu]") then
char_current = p.tochar(char_current)
end
--------------------- 이전 문자
if prev_char then
prev_link = buildlink(prev_char, prev_linkname, prev_arrow_icon, template, editintro)
--- 이전 문자 없으면 자동 계산
else
local char = ustring.char(p._code(char_current, 'r10', -1))
local article = prefix .. (ustring.codepoint(char) > 65535 and p._code(char_current, '16', -1) or char)
prev_link = buildlink(article, prev_linkname or char, prev_arrow_icon, template, editintro)
end
------------------- 다음 문자
if next_char then
next_link = buildlink(next_char, next_linkname, next_arrow_icon, template, editintro)
--- 이전 문자 없으면 자동 계산
else
local char = ustring.char(p._code(char_current, 'r10', 1))
local article = prefix .. (ustring.codepoint(char) > 65535 and p._code(char_current, '16', 1) or char)
next_link = buildlink(article, next_linkname or char, next_arrow_icon, template, editintro)
end
end
local t = {}
local format =
[[<td class='uni_char-prev' style="width: 7em;font-size:1.2em;" lang="%s">%s</td>
<td class='uni_char-char' style="width: 6em"><div style="font-size: 1.8em;line-height: 1.4em;"><b lang="%s">%s</b></div>
<div style="font-size:80%%; color: gray; ">%s</div></td>
<td class='uni_char-next' style="width: 7em;font-size:1.2em;" lang="%s">%s</td>]]
if args['순서반전'] then
header = ustring.format(format .. "%s", locale, prev_link, locale, char_current, p._code(char_current), locale,
next_link, header)
else
header = ustring.format("%s" .. format, header, locale, prev_link, locale, char_current, p._code(char_current),
locale, next_link)
end
return tostring(htmlroot:node(header))
end
return p