Module:siwa-headword

Revision as of 22:37, 17 January 2021 by Sware (talk | contribs)


local export = {}
	function full_headword(data)
		local tracking_categories = {}
	
		-- Script-tags the topmost header.
		local pagename = mw.title.getCurrentTitle().text
		local fullPagename = mw.title.getCurrentTitle().fullText
		local namespace = mw.title.getCurrentTitle().nsText
		
		
		
		local displayTitle
		if (namespace == "" and data.sc and toBeTagged[data.sc:getCode()]
				and not pagename:find "^[%z\1-\127]+$")
				or (data.sc:getCode() == "Jpan" and (test_script(pagename, "Hira") or test_script(pagename, "Kana"))) then
			displayTitle = '<span class="' .. data.sc:getCode() .. '">' .. pagename .. '</span>'
		elseif namespace == "Reconstruction" then
			displayTitle, matched = mw.ustring.gsub(
				fullPagename,
				"^(Reconstruction:[^/]+/)(.+)$",
				function(before, term)
					return before ..
						require("Module:script utilities").tag_text(
							term,
							data.lang,
							data.sc
						)
				end
			)
			
			if matched == 0 then
				displayTitle = nil
			end
		end
		
		if displayTitle then
			local frame = mw.getCurrentFrame()
			frame:callParserFunction(
				"DISPLAYTITLE",
				displayTitle
			)
		end
		
		if data.force_cat_output then
			--[=[
			[[Special:WhatLinksHere/Template:tracking/headword/force cat output]]
			]=]
			require("Module:debug").track("headword/force cat output")
		end
		
		if data.getCanonicalName then
			error('The "data" variable supplied to "full_headword" should not be a language object.')
		end
			
		-- Were any categories specified?
		if data.categories and #data.categories > 0 then
			local lang_name = require("Module:string").pattern_escape(data.lang:getCanonicalName())
			for _, cat in ipairs(data.categories) do
				-- Does the category begin with the language name? If not, tag it with a tracking category.
				if not mw.ustring.find(cat, "^" .. lang_name) then
					mw.log(cat, data.lang:getCanonicalName())
					table.insert(tracking_categories, "head tracking/no lang category")
					
					--[=[
					[[Special:WhatLinksHere/Template:tracking/head tracking/no lang category]]
					]=]
					require("Module:debug").track{
						"headword/no lang category",
						"headword/no lang category/lang/" .. data.lang:getCode()
					}
				end
			end
			
			if not data.pos_category
				and mw.ustring.find(data.categories[1], "^" .. data.lang:getCanonicalName())
					then
				data.pos_category = mw.ustring.gsub(data.categories[1], "^" .. data.lang:getCanonicalName() .. " ", "")
				table.remove(data.categories, 1)
			end
		end
		
		if not data.pos_category then
			error(
				'No valid part-of-speech categories were found in the list '
				.. 'of categories passed to the function "full_headword". '
				.. 'The part-of-speech category should consist of a language\'s '
				.. 'canonical name plus a part of speech.'
				)
		end
		
		-- Categorise for unusual characters
		local standard = data.lang:getStandardCharacters()
		
		if standard then
			if mw.ustring.len(title.subpageText) ~= 1 and not non_categorizable() then
				for character in mw.ustring.gmatch(title.subpageText, "([^" .. standard .. "])") do
					local upper = mw.ustring.upper(character)
					if not mw.ustring.find(upper, "[" .. standard .. "]") then
						character = upper
					end
					table.insert(
						data.categories,
						data.lang:getCanonicalName() .. " terms spelled with " .. character
					)
				end
			end
		end
		
		-- Categorise for palindromes
		if title.nsText ~= "Reconstruction" and mw.ustring.len(title.subpageText)>1
			and require('Module:palindromes').is_palindrome(
				title.subpageText, data.lang, data.sc
				) then
			table.insert(data.categories, data.lang:getCanonicalName() .. " palindromes")
		end
	
		-- This may add more categories (e.g. gender categories), so make sure it gets
		-- evaluated first.
		local text = show_headword_line(data)
		return
			text ..
			require("Module:utilities").format_categories(
				data.categories, data.lang, data.sort_key, nil,
				data.force_cat_output or test_force_categories, data.sc
				) ..
			require("Module:utilities").format_categories(
				tracking_categories, data.lang, data.sort_key, nil,
				data.force_cat_output or test_force_categories, data.sc
				)
	end

return export