Module:rhymes

Revision as of 20:10, 29 July 2024 by TheNightAvl (talk | contribs)

Underlies {{rhymes}}.


local export = {}
local getArgs = require("Module:Arguments").getArgs

local m_links = require("Module:links")
local m_languages = require("Module:languages")

local V = "aeiouyɑæɐəɵɛɜɪɨɔœɒøʊʉʌɯʏɤ"

local from_secondary = {
	["lfv"] = true
} -- list of language codes which get rhyme from secondary stress as well as primary

function de_onset(ipa)
	ipa = mw.ustring.gsub(ipa, "[ˈˌ]", "")
	ipa = mw.ustring.gsub(ipa, "^[^" .. V .. "]+", "")
	ipa = mw.ustring.gsub(ipa, "^[" .. V .. "][̥̊]", "")
	return ipa
end

function export.generate(lang, rhymes, nopre)
    local language = m_languages.get_by_code(lang)
    local rhymes_show = ""
	
	if not nopre then
		rhymes_show = "\n* Rhyme"
		if #rhymes > 1 then rhymes_show = rhymes_show .. "s" end 
		rhymes_show = rhymes_show .. ": "
	end

    for num, rhyme in ipairs(rhymes) do
    	local rhyme_cat = "Rhymes:" .. language.name .. "/" .. rhyme
        local rhyme_count = mw.site.stats.pagesInCategory(rhyme_cat, "pages")
        if num > 1 then
            rhymes_show = rhymes_show .. ", "
        end
        if rhyme_count < 2 then
        	rhymes_show = rhymes_show ..
        		"<span class=\"IPA\">-" .. rhyme .. "</span>[[Category:" .. rhyme_cat .. "]]"
    	else
            rhymes_show = rhymes_show ..
                "<span class=\"IPA\">[[:Category:" .. rhyme_cat .. "|-" .. rhyme .. "]]</span>[[Category:" .. rhyme_cat .. "]]" ..
                "<sup> (" .. rhyme_count .. ")</sup>"
        end
    end
    
    return rhymes_show
end

function export.show(frame)
	local args = getArgs(frame)
	local language = m_languages.get_by_code(args[1])
	local rhymes = {}
	if args[2] == nil then error("At least one rhyme is required") end
	
	for i, rhyme in ipairs(args) do
		if i > 1 then
			rhyme = mw.ustring.gsub(rhyme, "[%/%[%]]", "")
			local rhyme2
			if from_secondary[language.code] then
				rhyme2 = {}
				for rhyme_match in mw.ustring.gmatch(rhyme, "ˌ(.*)$") do
					table.insert(rhyme2, rhyme_match)
				end
			end
			rhyme = mw.ustring.match(rhyme, "ˈ(.*)$") or rhyme
			table.insert(rhymes, de_onset(rhyme))
			if rhyme2 then
				for _, rhyme2_temp in ipairs(rhyme2) do
					table.insert(rhymes, de_onset(rhyme2_temp))
				end
			end
		end
	end
	
	mw.logObject(rhymes)

	return export.generate(language.code, rhymes, args["nopre"] ~= nil)
end

return export
--[[
=p.show(mw.getCurrentFrame():newChild{title="parent", args={"lfv", "/ˈvydɛ/"}}:newChild{title="child"})
]]--