Dokumentation för denna modul finns på /dok (redigera), /test


Syfte redigera

Används av Mall:uttal.

Automatiserade test redigera

Modul:uttal/test

-- Depends on:
-- * Modul:para  Modul:lang  Modul:lang/data
-- * Extension:Phonos  MediaWiki:Phonos-attribution-icon

-- BEWARE:
-- * "label" (NOT an attribute) overrides "ipa" (indeed an attribute)
-- * empty attributes MUST NOT be fed into Phonos

local export = {}

local import_lang  = require("Modul:lang")
local import_param = require("Modul:param")

local name_short = 'uttal'
local name_long  = '"Mall:' .. name_short .. '"'

	local function removeLeadingSpace (sx)
		return mw.ustring.gsub(sx, "^ ", "")
	end

	local function cmpInsensitiveShorter (string_aa, string_bb)

		local length_aa = string.len(string_aa)
		local length_bb = string.len(string_bb)
		local ascii_aa = 0
		local ascii_bb = 0
		local index_nope = 1 -- ONE-based
		local is_match = true

		while true do
			if (index_nope>length_aa) or (index_nope>length_bb) then
				break
			end--if
			ascii_aa = string.byte(string_aa,index_nope,index_nope)
			ascii_bb = string.byte(string_bb,index_nope,index_nope)
			if (ascii_aa>=97) and (ascii_aa<=122) then
				ascii_aa = ascii_aa - 32
			end--if
			if (ascii_bb>=97) and (ascii_bb<=122) then
				ascii_bb = ascii_bb - 32
			end--if
			is_match = (ascii_aa==ascii_bb)
			if not is_match then
				break
			end--if
			index_nope = index_nope + 1
		end--while

		return is_match

	end--function cmpInsensitiveShorter

	local function launchPhonos (main_fra, x_simple, x_ipa, x_sound)

		local elements = {} -- 1 or 2 or 3 elements possible
		local my_index = 1
		local my_phonos = ''

		if x_simple then
			elements[#elements+1] = x_simple -- "simple" at the beginning
		end
		if x_sound and x_ipa then
			elements[#elements+1] = main_fra:extensionTag('phonos','',{['file']=x_sound,['ipa']=x_ipa})
		end--if
		if x_sound and (not x_ipa) then
			elements[#elements+1] = main_fra:extensionTag('phonos','&nbsp; &nbsp; &nbsp; &nbsp;',{['file']=x_sound})
		end--if
		if (not x_sound) and x_ipa then
			elements[#elements+1] = '<span class="ipa">' .. x_ipa .. '</span>'
			-- my_phonos = main_fra:extensionTag('phonos','',{['ipa']=x_ipa}) BROKEN [[mw:Help_talk:Extension:Phonos#One more absurd bug -- IPA only]]
		end--if
		if x_sound then
			elements[#elements+1] = '[[c:File:' .. x_sound .. '|ⓘ]]'
		end--if

		while true do
			if (my_index>#elements) then
				break
			end--if
			my_phonos = my_phonos .. '<span style="border:1px solid #808080;padding-top:3px;padding-bottom:3px;padding-left:4px;padding-right:4px;">' .. elements[my_index] .. '</span>'
			my_index = my_index + 1
		end--while

		return my_phonos

	end

	local function expandTwo (control_string, insert_m_string, insert_v_string)

	  local temp_string = ''
	  local expanded_string = ''
	  local x_length = 0
	  local x_index = 0
	  local chark = 0
	  local charl = 0

	  x_length = string.len (control_string)
	  while true do
		if (x_index==x_length) then
		  break
		end--if
		chark = string.byte (control_string,(x_index+1),(x_index+1))
		x_index = x_index + 1
		charl = 0
		if (x_index<x_length) then
		  charl = string.byte (control_string,(x_index+1),(x_index+1))
		end--if
		if ((chark==64) and (charl==77)) then -- @M cannot be empty
		  temp_string = insert_m_string
		  x_index = x_index + 1
		else
		  if ((chark==64) and (charl==86)) then -- @V can be empty
			temp_string = insert_v_string
			x_index = x_index + 1
		  else
			temp_string = string.char (chark)
		  end--if
		end--if
		expanded_string = expanded_string .. temp_string
	  end--while

	  return expanded_string

	end--function expandTwo

function export.getWikitext(main_frame)

	local args          = main_frame:getParent().args
	local language_code = args[1] or ''
	local region        = args["region"]
	local tagg          = args["tagg"]
	local par_text      = args["text"]
	local simple        = args["enkel"]
	local ipa           = args["ipa"]
	local sound         = args["ljud"]
	local meanings      = args["betydelser"]
	local pron_link     = args["uttalslänk"]
	local str           = ""

	local legal_params = {}
	local many_complaints = {}

	local list_bad_params = ''
	local violating_string = ''
	local problem_1_main = ''
	local problem_2_desc = ''
	local problem_3_hint = ''
	local p_2_desc_expanded = ''
	local error_visible = ''
	local error_cat = ''

	local final_result = ''

	local error_code = 0

	local boo_show_link  = false
	local boo_no_cat_true = false
	local boo_no_cat_false = false

	local legal_params = {[0]=1, "region", "tagg", "text", "enkel", "ipa", "ljud", "betydelser", "uttalslänk", "nocat"}
	list_bad_params = import_param.go (args,legal_params)

	boo_no_cat_true  = (args["nocat"]=="true")
	boo_no_cat_false = (args["nocat"]=="false")
	boo_show_link  = (pron_link~="-")

	many_complaints[1] = {'Ogiltiga parametrar','@M använder inte @V',''}
	many_complaints[2] = {'Mallsyntax','Obligatorisk parameter saknas i @M','Språkkod krävs alltid.'}
	many_complaints[3] = {'Uppenbarligen ogiltig språkkod','Kod @V skickades in i @M',''}
	many_complaints[4] = {'Okänd språkkod','Kod @V skickades in i @M',''}
	many_complaints[5] = {'Mallsyntax','Tomt anrop till @M','En av parametrarna "enkel=" "ipa=" "ljud=" bör anges.'}
	many_complaints[6] = {'Mallsyntax','Värde @V skickades in i parametern "uttalslänk=" för @M','Det enda tillåtna värdet är "-".'}
	many_complaints[8] = {'Mallsyntax','Tom sträng i parameter till @M',''}
	many_complaints[9] = {'Mallsyntax','Parameter "ljud=" till @M','Bör anges utan prefix och utan hakparenteser.'}

	while true do
		if (list_bad_params~='') then
		  error_code = 1
		  violating_string = list_bad_params
		  break
		end--if
		if (language_code=='') then
		  error_code = 2 -- no violating string here
		  break
		end--if
		if import_lang.isObviouslyInvalid(language_code) then
		  error_code = 3
		  violating_string = '"' .. language_code .. '"'
		  break
		end--if
		if not import_lang.hasLanguage(language_code) then
		  error_code = 4
		  violating_string = '"' .. language_code .. '"'
		  break
		end--if
		if (not simple) and (not ipa) and (not sound) then
		  error_code = 5 -- no violating string here
		  break
		end--if
		if pron_link and boo_show_link then
		  error_code = 6
		  violating_string = '"' .. pron_link .. '"'
		  break
		end--if
		if (simple=='') or (ipa=='') or (sound=='') then
		  error_code = 8 -- no violating string here
		  break -- must be tested BEFORE #9
		end--if
		if sound then
			if string.find(sound,'[',1,true) or string.find(sound,']',1,true) then
			  error_code = 9
			  break
			end--if
			if cmpInsensitiveShorter(sound,'fil:') or cmpInsensitiveShorter(sound,'file:') or cmpInsensitiveShorter(sound,'media:') then
			  error_code = 9
			  break
			end--if
		end--if
		break
	end--while

	if (error_code~=0) then

		problem_1_main = many_complaints[error_code][1]
		problem_2_desc = many_complaints[error_code][2] -- @M cannot be empty, @V can be empty
		problem_3_hint = many_complaints[error_code][3] -- optional, can be empty
		p_2_desc_expanded = expandTwo (problem_2_desc, name_long, violating_string)
		error_cat = '[[Kategori:' .. problem_1_main .. ']][[Kategori:' .. problem_1_main .. ' (' .. name_short .. ')]]'
		error_visible = '<br><span class="error"><b>' .. problem_1_main .. ' -- ' .. p_2_desc_expanded .. '.</b></span><br>'
		if (problem_3_hint~='') then
		  error_visible = error_visible .. problem_3_hint
		end--if

	else

		local language_name = import_lang.getLanguageUCFirst(language_code)

	if boo_show_link then
		if language_code == 'sv' then
			str = str .. '[[Wiktionary:Stilguide/Uttal|uttal]]:'
		elseif language_code ~= '--' then
			str = str .. '[[Wiktionary:Stilguide/Uttal/' .. language_name .. '|uttal]]:'
		else
			str = 'uttal:'
		end
		str = '<b>' .. str .. '</b>'
	end

	if region then
		str = str .. '&nbsp;<i>(' .. region
		if tagg then
			str = str .. ',&nbsp;'
		else
			str = str .. ')</i>'
		end
	end

	if tagg then
		if (not region) then
			str = str .. '&nbsp;<i>('
		end
		str = str .. '<span style="background-color:#E0E0E0;">' .. tagg .. '</span>)</i>'
	end

	if par_text then
		str = str .. '&nbsp;"' .. par_text .. '"'
	end

		str = str .. '&nbsp;' .. launchPhonos (main_frame, simple, ipa, sound)

		if meanings then
			str = str .. '&nbsp;<i>(betydelse: ' .. meanings .. ')</i>'
		end

		str = removeLeadingSpace(str)

	end--if

	local tracking_cats = "[[Kategori:Wiktionary:uttal/Använder mallen]]"

--  inaktiverad p g a många röda länkar.
--	if sound then
--		tracking_cats = tracking_cats .. "[[Kategori:Wiktionary:uttal/Använder ljudparametern]]"
--		tracking_cats = tracking_cats .. "[[Kategori:Wiktionary:uttal/Använder ljudparametern/" .. language_name .. "]]"
--	end

	if (error_code==0) then
		final_result = str
	else
		if ((main_frame:preprocess ('{{REVISIONID}}') == '') or boo_no_cat_true or boo_no_cat_false) then
		  mw.addWarning (error_visible)
		  final_result = error_visible
		end--if
		final_result = name_long .. final_result
	end--if
	if boo_no_cat_false or (mw.title.getCurrentTitle().namespace==0) and (not boo_no_cat_true) then
		if (error_code==0) then
		  final_result = final_result .. tracking_cats
		else
		  final_result = final_result .. error_cat .. tracking_cats
		end--if
	end--if

	return final_result

end

return export