Module:IPA: Difference between revisions
Jump to navigation
Jump to search
(Created page with "local export = {} local params = { [1] = {}, [2] = {list = true}, nolarge = {type = "boolean"}, linked = {type = "boolean"}, } function format_inline_ipa(language, ipa) end function export.show(frame) local args = require("Module:parameters").process(frame:getParent().args, params) local language = require("Module:languages").get_by_code(args["l"]) if language == nil then error("No such language: " .. args["l"]) end local language_pron = "Appendix:" .. langu...") |
TheNightAvl (talk | contribs) No edit summary |
||
(26 intermediate revisions by 2 users not shown) | |||
Line 5: | Line 5: | ||
nolarge = {type = "boolean"}, | nolarge = {type = "boolean"}, | ||
linked = {type = "boolean"}, | linked = {type = "boolean"}, | ||
nopre = {type = "boolean"}, | |||
} | |||
local params_inline = { | |||
[1] = {}, | |||
[2] = {list = true}, | |||
} | |||
local params_inline_linkless = { | |||
[1] = {list = true}, | |||
} | } | ||
function | function export.get_SAMPAd(ipa) | ||
local correspondences = mw.loadData("Module:IPA/SAMPA") | |||
ipa = mw.ustring.gsub(ipa, "'''", "ʹʹ") | |||
ipa = mw.ustring.gsub(ipa, "''", "ʹ") | |||
for from, to in pairs(correspondences) do | |||
ipa = mw.ustring.gsub(ipa, "([^%#])" .. from, "%1" .. to) | |||
ipa = mw.ustring.gsub(ipa, "^" .. from, to) | |||
end | |||
ipa = mw.ustring.gsub(ipa, "ʹʹ", "'''") | |||
ipa = mw.ustring.gsub(ipa, "ʹ", "''") | |||
ipa = mw.ustring.gsub(ipa, "%#", "") | |||
return ipa | |||
end | end | ||
function | function format_ipa(language, ipa, nolarge, nopre, linked) | ||
local | local language_pron = "Appendix:" .. language.name .. "_pronunciation" | ||
local | local out = "" | ||
if | if not nopre then | ||
out = out .. "[[w:International_Phonetic_Alphabet|IPA]]" | |||
out = out .. "<sup>([[" .. language_pron .. "|key]])</sup>: " | |||
end | end | ||
local class = "IPA" | local class = "IPA" | ||
if | if nolarge then class = class .. " nolarge" end | ||
for i, i_ipa in ipairs( | for i, i_ipa in ipairs(ipa) do | ||
if i > 1 then out = out .. ", " end | if i > 1 then out = out .. ", " end | ||
out = out .. '<span class="' .. class .. '">' | out = out .. '<span class="' .. class .. '">' | ||
if | if mw.ustring.match(i_ipa, "%*[^%*]+%*") then | ||
local pieces = {} | |||
for pre, escaped, post in mw.ustring.gmatch(i_ipa, "([^%*]*)(%*[^%*]+%*)([^%*]*)") do | |||
local piece = export.get_SAMPAd(pre) .. '</span>' .. mw.ustring.match(escaped, "^%*([^%*]+)%*$") .. '<span class="' .. class .. '">' .. export.get_SAMPAd(post) | |||
table.insert(pieces, piece) | |||
end | |||
i_ipa = table.concat(pieces) | |||
i_ipa = mw.ustring.gsub(i_ipa, "(%s+)(%<%/span%>)(%S)", "%2%1%3") | |||
i_ipa = mw.ustring.gsub(i_ipa, '(%S+)(%<span class%=%"' .. class .. '%"%>)(%s)', "%1%3%2") | |||
if linked then | |||
-- there's a reason this isn't merged with the linked condition below but I can't be bothered to explain why | |||
i_ipa = mw.ustring.gsub(i_ipa, "(%<%/span%>)", "]]%1") | |||
i_ipa = mw.ustring.gsub(i_ipa, '(.)(%<span class%=%"' .. class .. '%"%>)', "%1%2[[" .. language_pron .. "|") | |||
end | |||
else | |||
i_ipa = export.get_SAMPAd(i_ipa) | |||
end | |||
if linked then | |||
out = out .. "[[" .. language_pron .. "|" .. i_ipa .. "]]" | out = out .. "[[" .. language_pron .. "|" .. i_ipa .. "]]" | ||
else | else | ||
Line 30: | Line 65: | ||
end | end | ||
out = out .. "</span>" | out = out .. "</span>" | ||
out = mw.ustring.gsub(out, '%<span[^%>]+%>%[%[[^%|]+%|%]%]%<%/span%>', "") | |||
out = mw.ustring.gsub(out, '%<span[^%>]+%><%/span%>', "") | |||
end | end | ||
return out | return out | ||
end | |||
function export.show(frame) | |||
local args = require("Module:parameters").process(frame:getParent().args, params) | |||
local language = require("Module:languages").get_by_code(args[1]) | |||
return format_ipa(language, args[2], args["nolarge"], args["nopre"], args["linked"]) | |||
end | |||
function export.show_inline(frame) | |||
local args = require("Module:parameters").process(frame:getParent().args, params_inline) | |||
local language = require("Module:languages").get_by_code(args[1]) | |||
return format_ipa(language, args[2], true, true, true) | |||
end | |||
function export.show_inline_linkless(frame) | |||
local args = require("Module:parameters").process(frame:getParent().args, params_inline_linkless) | |||
return format_ipa({name = ""}, args[1], true, true, false) | |||
end | end | ||
return export | return export | ||
--[[ | |||
=p.show(mw.getCurrentFrame():newChild{title="parent", args={"rad", "/ˈvydɛ/ *(''officially'')*", ["nopre"] = "true"}}:newChild{title="child"}) | |||
]]-- |
Latest revision as of 20:49, 29 July 2024
{{IPA}}
local export = {}
local params = {
[1] = {},
[2] = {list = true},
nolarge = {type = "boolean"},
linked = {type = "boolean"},
nopre = {type = "boolean"},
}
local params_inline = {
[1] = {},
[2] = {list = true},
}
local params_inline_linkless = {
[1] = {list = true},
}
function export.get_SAMPAd(ipa)
local correspondences = mw.loadData("Module:IPA/SAMPA")
ipa = mw.ustring.gsub(ipa, "'''", "ʹʹ")
ipa = mw.ustring.gsub(ipa, "''", "ʹ")
for from, to in pairs(correspondences) do
ipa = mw.ustring.gsub(ipa, "([^%#])" .. from, "%1" .. to)
ipa = mw.ustring.gsub(ipa, "^" .. from, to)
end
ipa = mw.ustring.gsub(ipa, "ʹʹ", "'''")
ipa = mw.ustring.gsub(ipa, "ʹ", "''")
ipa = mw.ustring.gsub(ipa, "%#", "")
return ipa
end
function format_ipa(language, ipa, nolarge, nopre, linked)
local language_pron = "Appendix:" .. language.name .. "_pronunciation"
local out = ""
if not nopre then
out = out .. "[[w:International_Phonetic_Alphabet|IPA]]"
out = out .. "<sup>([[" .. language_pron .. "|key]])</sup>: "
end
local class = "IPA"
if nolarge then class = class .. " nolarge" end
for i, i_ipa in ipairs(ipa) do
if i > 1 then out = out .. ", " end
out = out .. '<span class="' .. class .. '">'
if mw.ustring.match(i_ipa, "%*[^%*]+%*") then
local pieces = {}
for pre, escaped, post in mw.ustring.gmatch(i_ipa, "([^%*]*)(%*[^%*]+%*)([^%*]*)") do
local piece = export.get_SAMPAd(pre) .. '</span>' .. mw.ustring.match(escaped, "^%*([^%*]+)%*$") .. '<span class="' .. class .. '">' .. export.get_SAMPAd(post)
table.insert(pieces, piece)
end
i_ipa = table.concat(pieces)
i_ipa = mw.ustring.gsub(i_ipa, "(%s+)(%<%/span%>)(%S)", "%2%1%3")
i_ipa = mw.ustring.gsub(i_ipa, '(%S+)(%<span class%=%"' .. class .. '%"%>)(%s)', "%1%3%2")
if linked then
-- there's a reason this isn't merged with the linked condition below but I can't be bothered to explain why
i_ipa = mw.ustring.gsub(i_ipa, "(%<%/span%>)", "]]%1")
i_ipa = mw.ustring.gsub(i_ipa, '(.)(%<span class%=%"' .. class .. '%"%>)', "%1%2[[" .. language_pron .. "|")
end
else
i_ipa = export.get_SAMPAd(i_ipa)
end
if linked then
out = out .. "[[" .. language_pron .. "|" .. i_ipa .. "]]"
else
out = out .. i_ipa
end
out = out .. "</span>"
out = mw.ustring.gsub(out, '%<span[^%>]+%>%[%[[^%|]+%|%]%]%<%/span%>', "")
out = mw.ustring.gsub(out, '%<span[^%>]+%><%/span%>', "")
end
return out
end
function export.show(frame)
local args = require("Module:parameters").process(frame:getParent().args, params)
local language = require("Module:languages").get_by_code(args[1])
return format_ipa(language, args[2], args["nolarge"], args["nopre"], args["linked"])
end
function export.show_inline(frame)
local args = require("Module:parameters").process(frame:getParent().args, params_inline)
local language = require("Module:languages").get_by_code(args[1])
return format_ipa(language, args[2], true, true, true)
end
function export.show_inline_linkless(frame)
local args = require("Module:parameters").process(frame:getParent().args, params_inline_linkless)
return format_ipa({name = ""}, args[1], true, true, false)
end
return export
--[[
=p.show(mw.getCurrentFrame():newChild{title="parent", args={"rad", "/ˈvydɛ/ *(''officially'')*", ["nopre"] = "true"}}:newChild{title="child"})
]]--