Module:User:ZxxZxxZ/IPA2

This is a private module sandbox of ZxxZxxZ, for his own experimentation. Items in this module may be added and removed at ZxxZxxZ's discretion; do not rely on this module's stability.


local export = {}

-- [[Template:IPA]]
function export.template_IPA(frame)
    local args = frame:getParent().args
    local langcode = args["lang"] or 'en'
    local lang = require("Module:languages").getByCode(langcode)
    NAMESPACE = mw.title.getCurrentTitle().nsText

    ---- Initial link to pronunciation's guide page ----
    local ret = '[[Wiktionary:International Phonetic Alphabet|IPA]]'

    -- TODO

    ret = ret .. ': '

    ---- Format the given pronunciation(s) ----
    if args[1] == nil then
        error('No pronunciation given to the parameter #1')
    end

    local i = 1
    while args[i] do
        if args[i] == "" then
            error('No pronunciation given to the paramater #' .. i)
        end

        if i ~= 1 then
            ret = ret .. ','

            local note = args['n' .. (i - 1)]
            if note and note ~= '' then
            	ret = ret .. '<ref>' .. note .. '</ref>'
            end

            ret = ret .. ' '
        end

        ret = ret .. export.format_IPA(args[i], lang)

        i = i + 1
    end

    return ret
end

-- Takes an IPA pronunciation and formats it
function export.format_IPA(text, lang)
	if type(text) == "table" then
		local args = text:getParent().args
		text = args[1] or error("IPA string has not been specified")
		lang = args["lang"]
	end

    local categories = {}

    -- Fetch the representation type marks and remove them for "text2"
    local repr_mark = {}
    repr_mark.i, repr_mark.f, repr_mark.left, repr_mark.right = mw.ustring.find(text, '^(.).-(.)$')
    local text2 = mw.ustring.sub(text, 2, -2)

    -- Check for obsolete and nonstandard symbols
    local nonstandard = {
        "ɑ̢", "d̂", "t̂", "n̂", "l̂", "k̫", "ɔ̗", "ɔ̖", -- these symbols consist of more than one character, so we can't put them in the line below
        "[ʦʣʧʤʨʥ?ƍσƺƪƻƾƞᶀᶁᶂᶃᶄᶅᶆᶈᶇᶉᶊᶋƫᶌᶍᶎʓʆλƛłščžǰǧǯẋᵻᵿⱻʚɷωıȹȸ∅ØƥƭƈƙʠʇʗʖʞɩɼȣяɿʅʮʯᴀᴀᴇGRŒQȡȶȵȴKPT]",
    }

    for i, symbol in ipairs(nonstandard) do
        if mw.ustring.find(text2, symbol) then
            table.insert(categories, "IPA pronunciations with obsolete or nonstandard characters")
            break
        end
    end

    -- Check for invalid symbols
    local valid_symbols = '%(%)%%{%|%}.!abcdefhijklmnopqrstuvwxyz¡àáâãäæçèéêëìíîïðòóôõöøùúûüýÿāăēĕěħĩīĭŋōŏőœũūŭűŷǀǁǂǃǎǐǒǔǖǘǚǜǟǣǽǿȁȅȉȍȕȫȭȳɐɑɒɓɔɕɖɗɘəɚɛɜɝɞɟɠɡɢɣɤɥɦɧɨɪɫɬɭɮɯɰɱɲɳɴɵɶɸɹɺɻɽɾʀʁʂʃʄʈʉʊʋʌʍʎʏʐʑʒʔʕʘʙʛʜʝʟʡʢʬʭʰʱʲʳʴʵʶʷʸʼˀˁˈˌːˑ˞ˠˡˢˣ˥˦˧˨˩ˬ˭̘̙̜̝̞̟̠̣̤̥̩̪̬̯̰̹̺̻̼͇͈͉͍͎͔͕̀́̂̃̄̆̈̋̌̏̽͆͊͋͌̚͢͡βθχᴙᵊᵐᵑᵻᵿᶑᶣᶬᶮᶯᶰᶹ᷽᷄᷅᷆᷇᷈᷉ḁḛḭḯṍṏṳṵṹṻạẹẽịọụỳỵỹ‖․‥…‼‿ⁿ↑↓↗↘ⱱꜛꜜꟸꟹ𝆏𝆑'

    if mw.ustring.find(text2, '[^' .. valid_symbols .. ']') then
    	table.insert(categories, "IPA pronunciations with invalid IPA characters")
    end

    -- Check the representation type
    if not ((repr_mark.left == '/' and repr_mark.right == '/')
    or      (repr_mark.left == '[' and repr_mark.right == ']')) then
    	table.insert(categories, "IPA pronunciations with invalid representation marks")
    end

	-- link to rhymes page, if there is any
	--text = export.linkToRhyme(text, lang)

    -- Format the text
    text = '<span class="IPA" lang="">' .. text .. '</span>'

    -- Add the categories
    for key, cat in ipairs(categories) do
        text = text .. "[[Category:" .. cat .. "]]"
    end
 
    return text
end

-- Takes an IPA pronunciation and links to a rhyme, if there is any.
function export.linkToRhyme(text, lang)
	local rhyme
	local lang_name = lang:getCanonicalName()

	-- find possible rhymes and link to an existing one
	for i = 1, mw.ustring.len(text) do
		rhyme = mw.ustring.sub(text, i)
        rhyme = mw.ustring.gsub(rhyme, '[ˈˌ%.%/%[%]]', '')

		if mw.title.new(lang_name .. ':-' .. rhyme, 'Rhymes').exists then
			text = mw.ustring.sub(text, 1, i - 1)
			    .. '[[Rhymes:' .. lang_name .. ':-' .. rhyme .. '|'
			    .. mw.ustring.sub(text, i)
			    .. ']]'

			break
		end
	end

    return text
end

return export