-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathIPA.lua
More file actions
263 lines (242 loc) · 6.54 KB
/
Copy pathIPA.lua
File metadata and controls
263 lines (242 loc) · 6.54 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
require('strict')
local p = {}
local function multiFind(s, patterns, init)
local i, j = mw.ustring.find(s, patterns[1], init)
for n = 2, #patterns do
local i2, j2 = mw.ustring.find(s, patterns[n], init)
if i2 and (not i or i2 < i) then
i, j = i2, j2
end
end
return i, j
end
local function wrapAtSpaces(s)
return mw.ustring.gsub(s, '(%s+)', '<span class="wrap">%1</span>')
end
local function wrapAtSpacesSafely(s)
local patterns = {
'%[%[[^%]|]-%s[^%]|]-|', -- Piped links
'</?[A-Za-z][^>]-%s[^>]->' -- HTML tags
}
s = mw.ustring.gsub(s, '%[%[([^%]|]-%s[^%]|]-)%]%]', '[[%1|%1]]') -- Pipe all links
local t = {}
local init
while true do
local i, j = multiFind(s, patterns, init)
if not i then
break
end
local pre = wrapAtSpaces(mw.ustring.sub(s, init, i - 1)) -- What precedes the match
table.insert(t, pre)
table.insert(t, mw.ustring.sub(s, i, j)) -- The match
init = j + 1
end
local post = wrapAtSpaces(mw.ustring.sub(s, init)) -- What follows the last match
table.insert(t, post)
return table.concat(t)
end
local function checkNamespace(isDebug)
return isDebug or require('Mô đun:Category handler').main({ true })
end
local function renderCats(cats, isDebug)
if not cats[1] or not checkNamespace(isDebug) then
return ''
end
local t = {}
for _, v in ipairs(cats) do
table.insert(t, string.format(
'[[%sThể loại:%s]]',
isDebug and ':' or '',
v
))
end
return table.concat(t)
end
local function resolveSynonym(s)
return mw.loadData('Mô đun:Lang/ISO 639 synonyms')[s] or s
end
local function getLangName(code, link)
return require('Mô đun:Lang')._name_from_tag({
code,
link = link,
-- Without linking, "{{IPA}}" gets expanded in some contexts
template = '[[Bản mẫu:IPA|IPA]]'
})
end
local function linkLang(name, target, link)
return link == 'yes' and string.format(
'[[%s|%s]]',
target or name .. ' language',
name
) or name
end
function p._main(args)
local ret, cats = {}, {}
local isDebug = args.debug == 'yes'
local s, langCode, isPrivate, fullLangCode
-- Guide-linking mode
if args[2] and args[2] ~= '' then
local data = mw.loadData('Mô đun:IPA/data')
local isGeneric = args.generic == 'yes'
s = args[2]
-- Split tag into language and region codes
langCode = args[1]:gsub('%-.*', ''):lower()
langCode = resolveSynonym(langCode)
local regionCode = args[1]:match('%-(.+)')
local langData = data.langs[langCode] or {}
if regionCode then
isPrivate = regionCode:sub(1, 2) == 'x-'
if not isPrivate then
regionCode = regionCode:upper()
end
if langData.dialects and langData.dialects[regionCode] then
-- Overwrite language data with the dialect's
local newLangData = {}
for k, v in pairs(langData) do
if k ~= 'dialects' then
newLangData[k] = v
end
end
local dialectData = langData.dialects[regionCode]
if dialectData.aliasOf then
-- Use the canonical region code
regionCode = dialectData.aliasOf
dialectData = langData.dialects[regionCode]
end
-- Lowercase IANA variant
if dialectData.isVariant then
regionCode = regionCode:lower()
end
for k, v in pairs(dialectData) do
newLangData[k] = v
end
langData = newLangData
else
isGeneric = true
end
fullLangCode = langCode .. '-' .. regionCode
else
fullLangCode = langCode
end
local langName = langData.name
and linkLang(langData.name, langData.link, args.link)
or getLangName(fullLangCode, args.link)
if langName:sub(1, 5) == '<span' then
-- Module:Lang has returned an error
return langName .. renderCats({ 'Lỗi bản mẫu IPA' }, isDebug)
end
if args.cat ~= 'no' then
local catLangName = args.link == 'yes'
and mw.ustring.match(langName, '([^%[|%]]+)%]%]$')
or langName
table.insert(cats, string.format('Trang có IPA %s', catLangName))
end
-- Label
local label = args.label
if not label then
local labelCode = args[3] and args[3]:lower()
or langData.defaultLabelCode
if labelCode == '' then
label = ''
else
local langText
if langData.text then
langText = linkLang(
langData.text,
mw.ustring.match(langName, '^%[%[([^|%]]+)'),
args.link
)
else
langText = mw.ustring.gsub(
langName,
'^%[%[(([^|]+) languages)%]%]$',
'[[%1|%2]]'
)
langText = mw.ustring.gsub(
langText,
' languages(%]?%]?)$',
'%1'
)
end
if labelCode and data.labels[labelCode] then
label = data.labels[labelCode]:format(langText)
else
label = data.defaultLabel:format(langText)
end
end
end
if label and label ~= '' then
local span = mw.html.create('span')
:addClass('IPA-label')
:wikitext(label)
if args.small ~= 'no' then
span:addClass('IPA-label-small')
table.insert(ret, mw.getCurrentFrame():extensionTag({
name = 'templatestyles',
args = { src = 'Mô đun:IPA/styles.css' }
}))
end
table.insert(ret, tostring(span) .. ' ')
end
-- Brackets
s = (not isGeneric and langData.format or '[%s]'):format(s)
-- Link to key
local key = not isGeneric and langData.key or data.defaultKey
s = string.format('[[%s|%s]]', key, s)
else
-- Basic mode
s = args[1]
if args.cat ~= 'no' then
table.insert(cats, 'Trang có IPA thường')
end
end
-- Transcription
do
local lang = isPrivate and langCode or fullLangCode or
args.lang ~= '' and args.lang or 'und'
local span = mw.html.create('span')
:addClass('IPA')
:addClass(args.class)
:attr('lang', lang .. '-Latn-fonipa')
-- wrap=all: Do nothing
-- wrap=none: Never break
-- Otherwise: Break at spaces only
if args.wrap ~= 'all' then
span:addClass('nowrap')
if args.wrap ~= 'none' then
s = wrapAtSpacesSafely(s)
end
end
if (not args[2] or args[2] == '') and args.tooltip ~= '' then
local tooltip = args.tooltip or
'Ký tự trong Bảng Phiên âm Quốc tế (IPA)'
span:attr('title', tooltip)
end
s = tostring(span:wikitext(s))
table.insert(ret, s)
end
-- Audio
local audio = args.audio ~= '' and args.audio or args[4] ~= '' and args[4]
if audio then
local button = mw.getCurrentFrame():expandTemplate({
title = 'Audio',
args = { audio, '' }
})
table.insert(ret, ' ' .. button)
table.insert(cats, 'Trang có phát âm được ghi âm')
end
-- Categories
table.insert(ret, renderCats(cats, isDebug))
return table.concat(ret)
end
function p.main(frame)
local args = frame:getParent().args
if not args[1] then
return ''
end
for i, v in ipairs(args) do
args[i] = mw.text.trim(v)
end
return p._main(args)
end
return p