Khác biệt giữa bản sửa đổi của “Mô đun:Ruby”
Không có tóm lược sửa đổi Thẻ: Đã bị lùi lại |
n Đã lùi lại sửa đổi của Admin (thảo luận) quay về phiên bản cuối của [[User:imported>Admin|imported>Admin]] Thẻ: Lùi tất cả |
||
| Dòng 1: | Dòng 1: | ||
local find = mw.ustring.find | |||
local | |||
local | local p = {} | ||
---Converts the given text to traditional tone marks. | ---Converts the given text to traditional tone marks. | ||
function | function p.toTraditionalTones(text) | ||
if type(text) == "table" then | if type(text) == "table" then | ||
text = text.args[1] | text = text.args[1] | ||
end | end | ||
return (gsub(text, "%a+", function (word) | return (mw.ustring.gsub(text, "%a+", function (word) | ||
if match(word, "^qu[yýỳỷỹỵ]$") then return word end | if mw.ustring.match(word, "^qu[yýỳỷỹỵ]$") then return word end | ||
return (gsub(word, "%a%a$", { | return (mw.ustring.gsub(word, "%a%a$", { | ||
["oá"] = "óa", ["oà"] = "òa", ["oả"] = "ỏa", ["oã"] = "õa", ["oạ"] = "ọa", | ["oá"] = "óa", ["oà"] = "òa", ["oả"] = "ỏa", ["oã"] = "õa", ["oạ"] = "ọa", | ||
["oé"] = "óe", ["oè"] = "òe", ["oẻ"] = "ỏe", ["oẽ"] = "õe", ["oẹ"] = "ọe", | ["oé"] = "óe", ["oè"] = "òe", ["oẻ"] = "ỏe", ["oẽ"] = "õe", ["oẹ"] = "ọe", | ||
| Dòng 34: | Dòng 19: | ||
---Converts the given text to reformed tone marks. | ---Converts the given text to reformed tone marks. | ||
function | function p.toReformedTones(text) | ||
if type(text) == "table" then | if type(text) == "table" then | ||
text = text.args[1] | text = text.args[1] | ||
end | end | ||
return (gsub(text, "%a+", function (word) | return (mw.ustring.gsub(text, "%a+", function (word) | ||
return (gsub(word, "%a%a$", { | return (mw.ustring.gsub(word, "%a%a$", { | ||
["óa"] = "oá", ["òa"] = "oà", ["ỏa"] = "oả", ["õa"] = "oã", ["ọa"] = "oạ", | ["óa"] = "oá", ["òa"] = "oà", ["ỏa"] = "oả", ["õa"] = "oã", ["ọa"] = "oạ", | ||
["óe"] = "oé", ["òe"] = "oè", ["ỏe"] = "oẻ", ["õe"] = "oẽ", ["ọe"] = "oẹ", | ["óe"] = "oé", ["òe"] = "oè", ["ỏe"] = "oẻ", ["õe"] = "oẽ", ["ọe"] = "oẹ", | ||
| Dòng 48: | Dòng 33: | ||
---Generate alternative orthographies. | ---Generate alternative orthographies. | ||
function | function p.allSpellings(main_spelling, makeLinks) | ||
local frame = nil | local frame = nil | ||
if type(main_spelling) == "table" then | if type(main_spelling) == "table" then | ||
| Dòng 56: | Dòng 41: | ||
local xformers = { | local xformers = { | ||
p.toTraditionalTones, p.toReformedTones, | |||
} | } | ||
| Dòng 78: | Dòng 63: | ||
---Unicode codepoints for combining Vietnamese tone marks. | ---Unicode codepoints for combining Vietnamese tone marks. | ||
p.combiningToneMarks = mw.ustring.char( | |||
0x300, -- à | 0x300, -- à | ||
0x301, -- á | 0x301, -- á | ||
| Dòng 87: | Dòng 72: | ||
---Unicode codepoints for combining Vietnamese accent marks. | ---Unicode codepoints for combining Vietnamese accent marks. | ||
p.combiningAccentMarks = mw.ustring.char( | |||
0x302, -- â | 0x302, -- â | ||
0x306, -- ă | 0x306, -- ă | ||
| Dòng 97: | Dòng 82: | ||
-- @param accents Set to “0” to leave accent marks intact. | -- @param accents Set to “0” to leave accent marks intact. | ||
-- @param đ Set to “0” to leave “Đ” and “đ” intact. | -- @param đ Set to “0” to leave “Đ” and “đ” intact. | ||
function | function p.removeDiacritics(text, toneMarks, accentMarks, stroke) | ||
if type(text) == "table" then | if type(text) == "table" then | ||
text, toneMarks, accentMarks, stroke = text.args[1], | text, toneMarks, accentMarks, stroke = text.args[1], | ||
| Dòng 104: | Dòng 89: | ||
not text.args["đ"] or tonumber(text.args["đ"]) == 1 | not text.args["đ"] or tonumber(text.args["đ"]) == 1 | ||
end | end | ||
text = toNFD(text) | text = mw.ustring.toNFD(text) | ||
if toneMarks then | if toneMarks then | ||
text = gsub(text, "[" .. | text = mw.ustring.gsub(text, "[" .. p.combiningToneMarks .. "]", "") | ||
end | end | ||
if accentMarks then | if accentMarks then | ||
text = gsub(text, "[" .. | text = mw.ustring.gsub(text, "[" .. p.combiningAccentMarks .. "]", "") | ||
end | end | ||
if stroke then | if stroke then | ||
text = gsub(text, "[Đđ]", {["Đ"] = "D", ["đ"] = "d"}) | text = mw.ustring.gsub(text, "[Đđ]", {["Đ"] = "D", ["đ"] = "d"}) | ||
end | end | ||
return toNFC(text) | return mw.ustring.toNFC(text) | ||
end | end | ||
---Vietnamese letters for use in comp(). | ---Vietnamese letters for use in comp(). | ||
p.letters = "aAàÀảẢãÃáÁạẠăĂằẰẳẲẵẴắẮặẶâÂầẦẩẨẫẪấẤậẬbBcCdDđĐeEèÈẻẺẽẼéÉẹẸêÊềỀểỂễỄếẾệỆfFgGhHiIìÌỉỈĩĨíÍịỊjJkKlLmMnNoOòÒỏỎõÕóÓọỌôÔồỒổỔỗỖốỐộỘơƠờỜởỞỡỠớỚợỢpPqQrRsStTuUùÙủỦũŨúÚụỤưƯừỪửỬữỮứỨựỰvVwWxXyYỳỲỷỶỹỸýÝỵỴzZ" | |||
---Compare two syllables according to Vietnamese dictionary sorting order. | ---Compare two syllables according to Vietnamese dictionary sorting order. | ||
function | function p.compWord(word1, word2) | ||
if find(word1, word2, 1, true) == 0 then return false end | if mw.ustring.find(word1, word2, 1, true) == 0 then return false end | ||
if find(word2, word1, 1, true) == 0 then return true end | if mw.ustring.find(word2, word1, 1, true) == 0 then return true end | ||
do | do | ||
local func1, static1, var1 = gmatch(word1, "[" .. | local func1, static1, var1 = mw.ustring.gmatch(word1, "[" .. p.letters .. "]") | ||
local func2, static2, var2 = gmatch(word2, "[" .. | local func2, static2, var2 = mw.ustring.gmatch(word2, "[" .. p.letters .. "]") | ||
while true do | while true do | ||
local c1 = func1(static1, var1) | local c1 = func1(static1, var1) | ||
| Dòng 133: | Dòng 118: | ||
if c1 == nil or c2 == nil then break end | if c1 == nil or c2 == nil then break end | ||
local idx1 = find( | local idx1 = mw.ustring.find(p.letters, c1, 1, true) | ||
local idx2 = find( | local idx2 = mw.ustring.find(p.letters, c2, 1, true) | ||
if idx1 and idx2 then | if idx1 and idx2 then | ||
if idx1 < idx2 then return true end | if idx1 < idx2 then return true end | ||
| Dòng 145: | Dòng 130: | ||
end | end | ||
---Abbreviations and text for Han tu references (used by p.createRefTag()) | |||
---Abbreviations and text for Han tu references (used by | |||
---[[Wiktionary:Beer parlour/2018/December#References for Vietnamese readings listed under Template:vi-readings]] | ---[[Wiktionary:Beer parlour/2018/December#References for Vietnamese readings listed under Template:vi-readings]] | ||
p.refAbbreviations = { | |||
tvctdhv = "Trần (1999)"; | tvctdhv = "Trần (1999)"; | ||
hvttd = "Nguyễn (1974)"; | hvttd = "Nguyễn (1974)"; | ||
| Dòng 205: | Dòng 148: | ||
---Creates a ref tag containing [[Template:vi-ref]]. | ---Creates a ref tag containing [[Template:vi-ref]]. | ||
---Expands abbreviations using | ---Expands abbreviations using p.refAbbreviations. | ||
function | function p.createRefTag(ref) | ||
local refFullName = | local refFullName = p.refAbbreviations[ref] or ref | ||
return mw.getCurrentFrame():extensionTag | return mw.getCurrentFrame():extensionTag{ | ||
name = "ref", | |||
args = { | |||
name = ref, | |||
}, | |||
content = mw.ustring.format("{{vi-ref|%s.}}", refFullName), | |||
} | |||
end | end | ||
---[[Template:vi-readings]] | ---[[Template:vi-readings]] | ||
function | function p.readings(hanviet, nom, rs, phienthiet, reading) | ||
local pagename = mw.title.getCurrentTitle().text | local pagename = mw.title.getCurrentTitle().text | ||
if type(hanviet) == "table" then | if type(hanviet) == "table" then | ||
| Dòng 225: | Dòng 174: | ||
{ | { | ||
link = "Hán Việt", | link = "Hán Việt", | ||
cat = "Vietnamese | cat = "Vietnamese Han tu", | ||
list = hanviet and mw.text.split(hanviet, "%s*,%s*"), | list = hanviet and mw.text.split(hanviet, "%s*,%s*"), | ||
phienthiet = phienthiet and mw.text.split(phienthiet, "%s*,%s*") | phienthiet = phienthiet and mw.text.split(phienthiet, "%s*,%s*") | ||
| Dòng 243: | Dòng 192: | ||
if style.list and #style.list > 0 and #style.list[1] > 0 then | if style.list and #style.list > 0 and #style.list[1] > 0 then | ||
local readings = style.list | local readings = style.list | ||
-- table.sort(readings, | -- table.sort(readings, p.comp) | ||
for j, reading in ipairs(readings) do | for j, reading in ipairs(readings) do | ||
local ref | local ref | ||
local a, b = match(reading, "(.-)%s*%-%s*(.+)") | local a, b = mw.ustring.match(reading, "(.-)%s*%-%s*(.+)") | ||
if a then | if a then | ||
reading, ref = a, b | reading, ref = a, b | ||
end | end | ||
local spellings = | local spellings = p.allSpellings(reading, true) | ||
readings[j] = table.concat(spellings, "/") | readings[j] = table.concat(spellings, "/") | ||
| Dòng 258: | Dòng 207: | ||
if style.phienthiet and style.phienthiet[j] then | if style.phienthiet and style.phienthiet[j] then | ||
pruby = "link" | pruby = "link" | ||
local ruby = | local ruby = p.ruby(mw.ustring.match(mw.text.trim(style.phienthiet[j]), | ||
"(%a+) +(.+)")) | "(%a+) +(.+)")) | ||
pruby = {} | pruby = {} | ||
if ruby then | if ruby then | ||
pruby = "nocolor" | pruby = "nocolor" | ||
local suffix = | local suffix = p.ruby("切", "thiết") | ||
pruby = {} | pruby = {} | ||
readings[j] = format("%s (%s[[fanqie#English|%s]])", | readings[j] = mw.ustring.format("%s (%s[[fanqie#English|%s]])", | ||
readings[j], ruby, suffix) | readings[j], ruby, suffix) | ||
end | end | ||
| Dòng 273: | Dòng 222: | ||
if ref then | if ref then | ||
for ref in mw.text.gsplit(ref, "%s*;%s*") do | for ref in mw.text.gsplit(ref, "%s*;%s*") do | ||
readings[j] = readings[j] .. | readings[j] = readings[j] .. p.createRefTag(ref) | ||
end | end | ||
end | end | ||
| Dòng 280: | Dòng 229: | ||
local sortkey = rs or mw.title.getCurrentTitle().text | local sortkey = rs or mw.title.getCurrentTitle().text | ||
readings = table.concat(readings, ", ") | readings = table.concat(readings, ", ") | ||
table.insert(lines, format("<span class='Hani' lang='vi' style='font-size: | table.insert(lines, mw.ustring.format("<span class='Hani' lang='vi' style='font-size: 120%%;font-style: normal!important;'>%s</span>: '''[[%s]]''' readings: %s[[Category:%s|%s]] [[Category:Vietnamese lemmas]] [[Category:Vietnamese Han characters]]</br>", | ||
pagename, style.link, readings, style.cat, sortkey)) | pagename, style.link, readings, style.cat, sortkey)) | ||
end | end | ||
| Dòng 290: | Dòng 239: | ||
---[[Template:vi-ruby]] | ---[[Template:vi-ruby]] | ||
function | function p.ruby(characters, readings, mark, alts) | ||
if type(characters) == "table" then | if type(characters) == "table" then | ||
local args = characters:getParent().args | local args = characters:getParent().args | ||
| Dòng 305: | Dòng 254: | ||
end | end | ||
readings = mw.text.split(readings, "[^" .. | readings = mw.text.split(readings, "[^" .. p.letters .. "]+") | ||
local result = {} | local result = {} | ||
local character_idx = 1 | local character_idx = 1 | ||
local alt_idx = 1 | local alt_idx = 1 | ||
for character in gmatch(characters, ".") do | for character in mw.ustring.gmatch(characters, ".") do | ||
local is_alt = false | local is_alt = false | ||
if character == "*" and alts[alt_idx] then | if character == "*" and alts[alt_idx] then | ||
| Dòng 317: | Dòng 266: | ||
alt_idx = alt_idx + 1 | alt_idx = alt_idx + 1 | ||
end | end | ||
if is_alt or (match(character, "^%a$") and not character:match("^%w$")) then | if is_alt or (mw.ustring.match(character, "^%a$") and not character:match("^%w$")) then | ||
local reading = readings[character_idx] | local reading = readings[character_idx] | ||
if mark and character == mark then | if mark and character == mark then | ||
character = format("<mark>%s</mark>", character) | character = mw.ustring.format("<mark>%s</mark>", character) | ||
reading = format("<mark>%s</mark>", reading) | reading = mw.ustring.format("<mark>%s</mark>", reading) | ||
end | end | ||
if pruby == 'link' then | if pruby == 'link' then | ||
character = format( | character = mw.ustring.format( | ||
"<ruby><rb><span class='Hani'; span style='font-size: 100%%'>[[%s#Vietnamese|%s]]</span></rb><rp>(</rp><rt><span style='padding: 0 0.25em; font-size: | "<ruby><rb><span class='Hani'; span style='font-size: 100%%'>[[%s#Vietnamese|%s]]</span></rb><rp>(</rp><rt><span style='padding: 0 0.25em; font-size: 120%%;'>[[%s#Vietnamese|%s]]</span></rt><rp>)</rp></ruby>", | ||
character, character, reading, reading) | character, character, reading, reading) | ||
end | end | ||
if pruby == 'nocolor' then | if pruby == 'nocolor' then | ||
character = format( | character = mw.ustring.format( | ||
"<ruby><rb><span class='Hani' style='color: | "<ruby><rb><span class='Hani' style='color:#000000;'>%s</span></rb><rp>(</rp><rt><span style='padding: 0 0.25em; font-size: 120%%;'>%s</span></rt><rp>)</rp></ruby>", | ||
character, reading) | character, reading) | ||
end | end | ||
if pruby ~= 'link' and pruby ~= 'nocolor' then | if pruby ~= 'link' and pruby ~= 'nocolor' then | ||
character = format( | character = mw.ustring.format( | ||
"<ruby><rb><span class='Hani'>%s</span></rb><rp>(</rp><rt><span style='padding: 0 0.25em;'>%s</span></rt><rp>)</rp></ruby>", | "<ruby><rb><span class='Hani'>%s</span></rb><rp>(</rp><rt><span style='padding: 0 0.25em;'>%s</span></rt><rp>)</rp></ruby>", | ||
character, reading) | character, reading) | ||
| Dòng 342: | Dòng 291: | ||
table.insert(result, character) | table.insert(result, character) | ||
end | end | ||
return format("<span lang='vi' style='font-size: | return mw.ustring.format("<span lang='vi' style='font-size: 120%%;font-style: normal!important;'>%s</span>", table.concat(result)) | ||
end | end | ||
function | function p.hantutab() | ||
local | local hantu = mw.ustring.gsub(mw.title.getCurrentTitle().text, '[^一-鿿㐀-䶿﨎﨏﨑﨓﨔﨟﨡﨣﨤﨧-﨩𠀀-𪛟𪜀-𰀀-]', '') | ||
local table_head = '<table class="floatright wikitable" style="text-align:center; font-size:small;"><tr><th colspan="' .. | local table_head = '<table class="floatright wikitable" style="text-align:center; font-size:small;"><tr><th colspan="' .. | ||
len(hantu) .. | mw.ustring.len(hantu) .. | ||
'" style="font-weight:normal;">[[Hán | '" style="font-weight:normal;">[[Hán tự]] in this term</th></tr><tr lang="vi" class="Hani" style="font-size:2em; background:white; line-height:1em;">' | ||
return table_head .. | return table_head .. | ||
gsub(hantu, '(.)', '<td style="padding:0.5em;">[[%1#Vietnamese|%1]]</td>') .. | mw.ustring.gsub(hantu, '(.)', '<td style="padding:0.5em;">[[%1#Vietnamese|%1]]</td>') .. | ||
'</tr></table>' | '</tr></table>' | ||
end | end | ||
---Returns the categories indicated by the given wikitext. | ---Returns the categories indicated by the given wikitext. | ||
function | function p.classifierCategories(frame) | ||
local src = frame.args[1] | local src = frame.args[1] | ||
local classifiers = {} | local classifiers = {} | ||
for classifier in gmatch(gsub(src, "<[^>]->", ""), "[" .. | for classifier in mw.ustring.gmatch(mw.ustring.gsub(src, "<[^>]->", ""), "[" .. p.letters .. "]+") do | ||
if classifier ~= "l" and classifier ~= "vi" and classifier ~= "vi-l" and | if classifier ~= "l" and classifier ~= "vi" and classifier ~= "vi-l" and | ||
classifier ~= "Vietnamese" then | classifier ~= "Vietnamese" then | ||
local cat = format("[[Category:Vietnamese nouns classified by %s]]", | local cat = mw.ustring.format("[[Category:Vietnamese nouns classified by %s]]", | ||
classifier) | classifier) | ||
table.insert(classifiers, cat) | table.insert(classifiers, cat) | ||
| Dòng 371: | Dòng 319: | ||
end | end | ||
function | function p.new(frame) | ||
local title = mw.title.getCurrentTitle().subpageText | local title = mw.title.getCurrentTitle().subpageText | ||
local args = frame:getParent().args | local args = frame:getParent().args | ||
| Dòng 390: | Dòng 338: | ||
local picc = args["picc"] or false | local picc = args["picc"] or false | ||
nom = nom and gsub(nom, "(.)", "[[%1]], ") or false | nom = nom and mw.ustring.gsub(nom, "(.)", "[[%1]], ") or false | ||
nom = nom and gsub(nom, ", $", "") or false | nom = nom and mw.ustring.gsub(nom, ", $", "") or false | ||
if args["h"] then | if args["h"] then | ||
etym = "{{vi-etym-sino|" .. args["h"] .. "}}." | etym = "{{vi-etym-sino|" .. args["h"] .. "}}." | ||
end | end | ||
if not etym and match(title, " ") then | if not etym and mw.ustring.match(title, " ") then | ||
etym = "{{com|vi" | etym = "{{com|vi" | ||
for word in mw.text.gsplit(title, " ") do | for word in mw.text.gsplit(title, " ") do | ||
| Dòng 403: | Dòng 351: | ||
end | end | ||
if etym == "-" then etym = false end | if etym == "-" then etym = false end | ||
if etym then etym = gsub(etym, "^%<", "From") end | if etym then etym = mw.ustring.gsub(etym, "^%<", "From") end | ||
local result = "" | local result = "" | ||
| Dòng 416: | Dòng 364: | ||
["cl"] = "Classifier", ["cls"] = "Classifier", ["num"] = "Numeral", ["abb"] = "Abbreviation", ["deter"] = "Determiner" | ["cl"] = "Classifier", ["cls"] = "Classifier", ["num"] = "Numeral", ["abb"] = "Abbreviation", ["deter"] = "Determiner" | ||
}; | }; | ||
return pos_title[text] or upper(sub(text, 1, 1)) .. sub(text, 2, -1) | return pos_title[text] or mw.ustring.upper(sub(text, 1, 1)) .. sub(text, 2, -1) | ||
end | end | ||
| Dòng 455: | Dòng 403: | ||
(args["wp"] == "y" and "" or "|" .. args["wp"]) .. "}}" end | (args["wp"] == "y" and "" or "|" .. args["wp"]) .. "}}" end | ||
if pic then result = result .. "\n[[File:" .. pic .. "|thumb|" .. | if pic then result = result .. "\n[[File:" .. pic .. "|thumb|" .. | ||
(picc or gsub(title, '^%l', upper) .. ".") .. "]]" end | (picc or mw.ustring.gsub(title, '^%l', mw.ustring.upper) .. ".") .. "]]" end | ||
result = result .. other("alt", "Alternative forms", args) | result = result .. other("alt", "Alternative forms", args) | ||
| Dòng 493: | Dòng 441: | ||
end | end | ||
function | function p.new_der(frame) | ||
local title = mw.title.getCurrentTitle().subpageText | local title = mw.title.getCurrentTitle().subpageText | ||
local data_module = require("Module:vi/vocab-list") | local data_module = require("Module:vi/vocab-list") | ||
| Dòng 517: | Dòng 465: | ||
local vi_sort_module = require("Module:vi-sortkey") | local vi_sort_module = require("Module:vi-sortkey") | ||
local makeSortKey = require("Module: | local makeSortKey = require("Module:fun").memoize(vi_sort_module.makeSortKey) | ||
table.sort(res, function(term1, term2) return makeSortKey(term1) < makeSortKey(term2) end) | table.sort(res, function(term1, term2) return makeSortKey(term1) < makeSortKey(term2) end) | ||
| Dòng 523: | Dòng 471: | ||
end | end | ||
function | function p.derived(frame) | ||
local tu_lay_note = "<span style=\"padding-left:4px; padding-right:4px\"> </span><span style=\"background:#ffffe0\">(''[[từ láy]]'')</span>" | local tu_lay_note = "<span style=\"padding-left:4px; padding-right:4px\"> </span><span style=\"background:#ffffe0\">(''[[từ láy]]'')</span>" | ||
local m_columns = require("Module:columns") | local m_columns = require("Module:columns") | ||
| Dòng 538: | Dòng 486: | ||
for i, word in ipairs(args) do | for i, word in ipairs(args) do | ||
word, is_tu_lay = gsub(word, "%:tl", "") | word, is_tu_lay = mw.ustring.gsub(word, "%:tl", "") | ||
tu_lay = is_tu_lay > 0 and tu_lay_note or "" | tu_lay = is_tu_lay > 0 and tu_lay_note or "" | ||
local word_parts = mw.text.split(gsub(word, "\n", "" ), ":") | local word_parts = mw.text.split(mw.ustring.gsub(word, "\n", "" ), ":") | ||
table.insert(result, m_links.full_link({ | table.insert(result, m_links.full_link({ | ||
lang = lang, | lang = lang, | ||
| Dòng 548: | Dòng 496: | ||
tu_lay) | tu_lay) | ||
length = math.max(len(word), length) | length = math.max(mw.ustring.len(word), length) | ||
end | end | ||
return | return | ||
m_columns. | m_columns.create_table( | ||
(length > 15 and 2 or 3), | |||
result, | |||
1, | |||
"#F5F5FF", | |||
((unfold or #result < 7) and false or true), | |||
"Derived terms", | |||
title_text, | |||
nil, | |||
nil, | |||
lang | |||
) | |||
end | end | ||
return | return p | ||