Module:interlinear

From Linguifex
Revision as of 19:50, 14 May 2026 by Sware (talk | contribs)
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Jump to navigation Jump to search


local p = {}
local data = mw.loadData( 'Module:interlinear/data' )
local gloss_override = {} -- for custom gloss abbreviations
local getArgs = require('Module:Arguments').getArgs
local yesno = require('Module:Yesno')
local languages = require('Module:languages') -- Switched from Module:Lang/data

--------------------------
-- Almost-global variables
--------------------------
local glossing_type, displaying_messages, free_translation, msg, buffer

-------------------
-- General settings
-------------------
local conf = { --settings
	WordSeparator = " \n\r\t", 
	GlossAbbrPattern = "^([Ø0-9A-Z]+)$",
	GlossAbbrBoundary = "-.,;:<>‹›⟨⟩/\\~+=%?%s%[%]()%_\127'",
	GlossExcludeTable = {I = true,},
	GlossExcludePattern = '^[0-9][0-9]+$', 
	GlossSmallCapsExclude = "^[AOPS]$", 
	GlossingType = "label", 
	ErrorCategory = "[[Category:Pages with errors in interlinear text]]",
	AmbiguousGlossCategory = "[[Category:Articles with ambiguous glossing abbreviations]]",
	MessageGlossingError = "Error(s) in interlinear glossing",
	combining_gender_numbers = "[0-9][0-9]?$", 
	combining_gender_prefixes = {G = "gender", CL = "class"},
	combining_person = {["1"] = "first person", ["2"] = "second person", ["3"] = "third person",},
	combining_number = {
		S = "singular", SG = "singular",
		P = "plural", PL = "plural",
		D = "dual", DU = "dual",
		TRI = "trial", PAU = "paucal", COL = "collective",
		IN = "inclusive", INC = "inclusive", INCL = "inclusive",
		EX = "exclusive", EXC = "exclusive", EXCL = "exclusive"
	},
	combining_gender = {F = "feminine", M = "masculine", N = "neuter"},
	LowerCaseGlosses = {
		["1sg"] = true, ["2sg"] = true, ["3sg"] = true,
		["1du"] = true, ["2du"] = true, ["3du"] = true,
		["1pl"] = true, ["2pl"] = true, ["3pl"] = true,
		["Fsg"] = true, ["Fpl"] = true, ["Msg"] = true, ["Mpl"] = true,
	}, 
	ErrorHelpLocation = "Template:Interlinear",
}

---------------------
-- CSS styles and classes
---------------------
conf.style = { 
	WordDiv = "float: left; margin-bottom: 0.3em;",
	WordMargin = "margin-right: 1em;",
	WordP = "margin: 0px;", 
	GlossAbbr = "font-variant: small-caps; font-variant-numeric: lining-nums; text-transform: lowercase; ",
	HiddenText = "display: none;",
	EndDiv = "clear: left; display: block;", 
	ErrorMessage = "font-size: inherit",
}
conf.class = { 
	Interlinear = "interlinear",
	GlossAbbr  = "gloss-abbr",
	GlossAbbrAmb = "gloss-abbr-ambiguous",
	GlossAbbrError = "gloss-abbr-error",
	ErrorMessage = "error",
}

---------------------
-- Section transclusion
---------------------
local page_content = nil 
local function get_section(frame, section_name)
	if page_content == nil then
		local current_title = mw.title.getCurrentTitle()
		page_content = current_title:getContent()
	end
	if page_content then
		if mw.ustring.find(page_content, section_name, 1, true) then
			return frame:preprocess('{{#section:{{FULLPAGENAME}}|' .. section_name .. '}}')
		end
	end
	return ''
end

---------------------
-- Sundry small functions
---------------------
local function normalise(str)
	return mw.ustring.gsub(str,"[" .. conf.WordSeparator .. "]+"," ")
end

local function tidyCss(str)
	str = mw.ustring.gsub(str, '^[\"\']*(.-)[\"\']*$', "%1") 
	if mw.ustring.sub(str, -1) ~= ";" then str = str .. ";" end 
	return str
end

local function highlight(text)
	if text then return '' .. text .. '' else return "" end
end

local function tone_sup(str)
	return mw.ustring.gsub(str, "([^%p%s0-9])([0-9]+)", "%1<sup>%2</sup>")
end

local function is_empty(str) 
	if not str then return true end
	if mw.ustring.find(str, "[^" .. conf.WordSeparator .. "]")
		then return false
	else return true end
end

local function help_link (anchor)
	if anchor then
		return " ([[w:" .. conf.ErrorHelpLocation .. "#" .. anchor .. "|help]])"
	else return "" end
end

-- Trial implementation of automatic transliteration using Module:languages:
local function transliterate (str, lang_code)
	if not lang_code then
		msg:add("error", "Source language for transliteration is not set")
		return ""
	end
	
	-- Integration with Module:languages
	local lang_obj = languages.getByCode(lang_code)
	if lang_obj and type(lang_obj.transliterate) == "function" then
		return lang_obj:transliterate(str) or ""
	end
	
	-- Fallback for specific modules if getByCode doesn't provide it
	local lookup = {grc = {module = 'Module:Ancient Greek', funct = "transliterate", } }
	local t = lookup[lang_code]
	if t then
		local module = require(t.module)
		return module[t.funct](str)
	else 
		msg:add("error", "Can't find transliterator for language '" .. lang_code .. "'")
	end
	return ""
end 

--------------------
-- Glossing settings
--------------------
local function set_global_glossing_settings(a)
	local style = ""
	if a.style then style = tidyCss(a.style) end
	if a.underline == "no" then style = style .. "text-decoration: none;" end
	if a.small_caps == "no" then style = style .. "font-variant:normal; text-transform: none;" end
	if style ~= "" then conf.style.GlossAbbr = conf.style.GlossAbbr .. style end
end

local function set_glossing_type(glossing)
	if glossing then
		local GlossingType
		glossing = mw.ustring.lower(mw.text.trim(glossing))
		if mw.ustring.find(glossing, 'link') then
			GlossingType = "wikilink"
		elseif mw.ustring.find(glossing, 'label') or mw.ustring.find(glossing, 'no link') then
			GlossingType = 'label'
		elseif mw.ustring.find(glossing, 'no abbr') then
			GlossingType = "no abbr"
		elseif yesno(glossing) == false then
			GlossingType = nil
		elseif yesno(glossing) then
			GlossingType = conf.GlossingType
		else
			msg:add('error', 'Glossing type "' .. glossing .. '" not recognised') end
		return GlossingType
	else error("set_glossing_type: 'glossing' is nil or false", 2)
	end
end

local function set_custom_glosses(list)
	local abbs = mw.text.split(list, '[;\n\t]')
	for _,v in pairs(abbs) do
		local gloss = mw.text.split(v, ':')
		local a = mw.text.trim(gloss[1])
		if a and a ~= "" then
			gloss_override[a] = {}
			gloss_override[a].expansion = gloss[2]
			gloss_override[a].wikipage = gloss[3]
		end
	end
end

---------------------
-- UserMessages object
---------------------
local UserMessages = {errors = {}, warnings = {}, gloss_messages = {}}
function UserMessages:add(msgtype, text, gloss)
	if msgtype == "gloss_message" then self.gloss_messages[gloss] = text
	elseif msgtype == "warning" then table.insert(self.warnings, text)
	elseif msgtype == "non-repeating error" then self.errors.nre = text
	elseif msgtype == "ambiguous gloss" then self.if_ambiguous_glosses = true
	elseif msgtype == "error" then table.insert(self.errors, text)
	else return error("UserMessages:add(): unknown message type", 2) end
end
function UserMessages:print_errors()
	local out = ""
	local namespace = mw.title.getCurrentTitle().namespace
	if next(self.errors) or self.warnings[1] then
		local err_span = mw.html.create("span")
		err_span:attr("style", conf.style.ErrorMessage)
		err_span:addClass(conf.class.ErrorMessage)
		for _,v in pairs(self.errors) do err_span:wikitext(" " .. v .. ";") end
		if namespace % 2 == 0 and namespace ~= 2 then err_span:wikitext(conf.ErrorCategory) end
		out = tostring(err_span)
		mw.addWarning(conf.MessageGlossingError)
	end
	if self.if_ambiguous_glosses then
		if namespace == 0 then out = out .. conf.AmbiguousGlossCategory end
	end
	return out
end
function UserMessages:print_warnings()
	local out = ""
	if displaying_messages and (next(self.gloss_messages) or next(self.warnings)) then
		local div = mw.html.create("div")
		div:addClass("interlinear-preview-warning")
			:cssText('border: 1px solid #a2a9b1; background-color: #f8f9fa; width: 80%; padding: 0.2em;')
			:wikitext("<i>This message box is shown only in preview:</i>")
			:newline()
		for _,v in ipairs(self.warnings) do
			local p = div:tag("p")
			p:addClass(conf.class.ErrorMessage)
			p:attr("style", conf.style.ErrorMessage)
			p:wikitext(v)
		end
		if self.gloss_messages then
			div:wikitext("<p>  To change any of the following default expansions, see [[Template:Interlinear/doc#Custom abbreviations|the template's documentation]]:</p>")
		end
		for _,v in pairs(self.gloss_messages) do
			div:wikitext("<p>" .. v .. "</p>")
		end
		out = out .. "\n\n" .. tostring(div)
	end
	return out
end

---------------------
-- Gloss Lookup & Format
---------------------
local function gloss_lookup(a, label, wikilink)
	local _label, _wikilink, _lookup, source = nil, nil, nil, nil
	if gloss_override[a] then
		_lookup = gloss_override[a]
		source = "local"
	elseif data.abbreviations[a] then _lookup = data.abbreviations[a] end
	if _lookup and _lookup.expansion ~= "" then
		_label, _wikilink = _lookup.expansion, _lookup.wikipage
	else
		local prefix = mw.ustring.sub(a,1,1)
		local suffix = mw.ustring.sub(a,2)
		if conf.combining_person[prefix] then 
			_label = conf.combining_person[prefix]
		local _suffix = conf.combining_number[suffix] or conf.combining_gender[suffix]
			if _suffix then
				_label = _label .. ", " .. _suffix
			else
				local suffix1 = mw.ustring.sub(suffix,1,1)
				local suffix2 = mw.ustring.sub(suffix,2)
					if conf.combining_gender[suffix1] and conf.combining_number[suffix2] then
						_label = _label .. ", " .. conf.combining_gender[suffix1] .. ", " .. conf.combining_number[suffix2]
					else _label = nil end
			end
	elseif mw.ustring.match(suffix,conf.combining_gender_numbers) then 
		local _i,_j = mw.ustring.find(a, conf.combining_gender_numbers)
		local _pre = mw.ustring.sub(a, 1, _i - 1)
		local _suff = mw.ustring.sub(a, _i)
		if conf.combining_gender_prefixes[_pre] then
			_label = conf.combining_gender_prefixes[_pre] .. " " .. _suff
		end
	elseif prefix == "N" then 
		local s = gloss_override[suffix] or data.abbreviations[suffix]
			if s ~= nil and not s.ExcludeNegation then
				_label = "non-" .. s.expansion
				_wikilink = s.wikipage
			end
			s = nil
		end
	end
	if _label == "" then _label = nil end
	if _wikilink == "" then _wikilink = nil end
	if not label then label = _label end
	if not wikilink then wikilink = _wikilink end
	return label, wikilink, source
end

local function format_gloss(gloss, label, wikilink)
	if string.sub(gloss,1,3) == "000" then return gloss end
	local gloss2 = mw.ustring.gsub(gloss,"<.->","") 
	gloss2 = mw.ustring.gsub(gloss2, "%'%'+", "") 
	gloss2 = mw.text.trim(mw.ustring.upper(gloss2))
	if not (label or wikilink) or (not label and glossing_type == "label") or (not wikilink  and glossing_type == "wikilink") then
		if glossing_type ~= "no abbr" then label, wikilink, source = gloss_lookup(gloss2, label, wikilink) end
	end
	local gloss_node
	if glossing_type == "no abbr" then gloss_node = mw.html.create("span")
	else gloss_node = mw.html.create("abbr") end
	gloss_node:addClass(conf.class.GlossAbbr)
	
	if label or wikilink then
		if not mw.ustring.match(gloss, "%l") and not mw.ustring.match(gloss,conf.GlossSmallCapsExclude) then
			gloss_node:attr("style", conf.style.GlossAbbr)
		end
		local abbr_label = label and label or wikilink
		gloss_node:attr("title", abbr_label)
		if source ~= "local" and data.abbreviations[gloss2] then
			if data.abbreviations[gloss2].ambiguous then
				gloss_node:addClass(conf.class.GlossAbbrAmb)
				msg:add("ambiguous gloss")
			end
		end
		if glossing_type == "wikilink" and wikilink then gloss_node:wikitext("[[w:", wikilink, "|" , gloss, "]]")
		else gloss_node:wikitext(gloss) end
		
		if source ~= "local" and displaying_messages then 
			local message = ""
			if label then message = "assuming " .. gloss2 .. " means \"" .. abbr_label .. "\";" end
			if glossing_type == "wikilink" and wikilink then
				message = message .. " linking to [[w:" .. wikilink .. "|" .. wikilink .. "]];"
			end
			msg:add("gloss_message", message, gloss)
		end
	elseif glossing_type == "no abbr" then 
		gloss_node:attr("style", conf.style.GlossAbbr):wikitext(gloss)
	else
		if displaying_messages then
			msg:add("warning", "Gloss abbreviation " .. highlight(gloss2) .. "  not recognised" .. help_link("gloss abbr"))
		end
		msg:add("non-repeating error", "Unknown glossing abbreviation(s)" .. help_link("gloss abbr"))
		gloss_node:addClass(conf.class.GlossAbbrError):addClass("error"):css("font-size", "100%")
			:attr("title", gloss2 .. ": glossing abbreviation not found")
			:attr("style", conf.style.ErrorMessage)
			:wikitext(gloss)
	end
	return tostring(gloss_node)
end

local function find_gloss(word)
	local function scan_gloss(boundary, gloss_abbr)
		if (mw.ustring.match(gloss_abbr, conf.GlossAbbrPattern) or conf.LowerCaseGlosses[gloss_abbr])
			and not (conf.GlossExcludeTable[gloss_abbr] or mw.ustring.match(gloss_abbr, conf.GlossExcludePattern)) then 
			gloss_abbr = format_gloss(gloss_abbr)
		end
		return boundary .. gloss_abbr
	end
	local word = mw.text.decode(word, true)
	if word == "I" then return word end
	local pattern = "([" .. conf.GlossAbbrBoundary .. "]?)([^" .. conf.GlossAbbrBoundary .. "]+)"
	word = mw.ustring.gsub(word, pattern, scan_gloss)
	return word
end

local function parse(cline, i, tags_found,ifglossing)
	local function issue_error(message, culprit)
		UserMessages:add("error",  message .. ": ''" .. mw.ustring.sub(cline.whole, 1, i-1) .. "'''" .. culprit  .. "'''''")
	end
	if i > cline.length then return i end
	local next_step, j, _, chunk
	local probe = mw.ustring.sub(cline.whole,i,i)
	if mw.ustring.match(probe,"[" .. conf.WordSeparator .. "]") and tags_found == 0 then 
		next_step =  i-1
	elseif probe == "[" then 
		if mw.ustring.sub(cline.whole,i+1,i+1) == "[" then
			_,j,chunk = mw.ustring.find(cline.whole,"(%[%[.-%]%])", i)
		else chunk = "["; j = i end 
		buffer = buffer .. chunk
		next_step =  parse(cline, j+1,tags_found,ifglossing)
	elseif probe == "{"  and tags_found == 0 then 
		_,j,chunk = mw.ustring.find(cline.whole,"(.-)(})", i+1)
		if not chunk then
			issue_error("Unclosed curly bracket", "{")
			chunk = highlight("{"); j = i
		elseif ifglossing==true then
			chunk = find_gloss(chunk)
		else
			if cline.tone_sup then chunk = tone_sup(chunk) end
		end
		buffer = buffer .. chunk
		next_step =  parse(cline, j+1,tags_found,ifglossing)
	elseif probe == "<" then 
		local _,j,chunk = mw.ustring.find(cline.whole,"(<.->)",i)
		if not chunk then
			issue_error("Unclosed angle bracket", "<")
			chunk = highlight("<"); j = i
		elseif mw.ustring.sub(cline.whole,i,i+1) == "</" then 
			if mw.ustring.find(chunk, "</abbr>", 1, true) then ifglossing=true end
			tags_found = tags_found - 1
		elseif not mw.ustring.match(chunk, "/>$") then 
			if ifglossing == true and mw.ustring.find(chunk, conf.class.GlossAbbr, 1, true) then ifglossing = false end
			tags_found = tags_found + 1
		end
		buffer = buffer .. chunk
		next_step = parse(cline, j+1,tags_found,ifglossing)
	else 
		local _,k,chunk = mw.ustring.find(cline.whole,"(..-)([ <[])",i)
		if k then 
			if ifglossing==true then buffer = buffer .. find_gloss(chunk)
			else
				if cline.tone_sup then chunk = tone_sup(chunk) end
				buffer = buffer .. chunk
			end
			next_step = parse(cline, k, tags_found, ifglossing)
		else 
			if ifglossing == true then chunk = find_gloss(mw.ustring.sub(cline.whole,i))
			else
				chunk = mw.ustring.sub(cline.whole,i)
				if cline.tone_sup then chunk = tone_sup(chunk) end
			end
			buffer = buffer .. chunk
			next_step = cline.length
		end
	end
	return next_step
end

function p.gcl(frame)
	local args = getArgs(frame,{ trim = true, removeBlanks = false, parentOnly = true, wrappers = {'Template:Grammatical category label'}, })
	msg = UserMessages
	set_global_glossing_settings{style = args.style, underline = args.underline, small_caps = args['small-caps']}
	if not args.glossing then glossing_type = conf.GlossingType else glossing_type = set_glossing_type(args.glossing) end
	local gloss, label, wikilink = args[1], args[2], args[3]
	if not gloss then UserMessages:add("error", "No gloss supplied") return UserMessages:print() end
	if wikilink and not args.glossing then glossing_type = 'wikilink' end 
	if label == "" then label = nil end
	if wikilink == "" then wikilink = nil end
	local result = format_gloss(gloss, label, wikilink)
	return result
end

function p.interlinearise(frame)
	local if_auto_translit = false
	local args = getArgs(frame, { trim = true, removeBlanks = false, parentFirst = true, wrappers = {'Template:Interlinear', 'Template:Fs interlinear'}, })
	local template_name = frame:getParent():getTitle()
	if template_name == 'Template:Fs interlinear' then
		args.italics1 = args.italics1 or "no"
		args.italics2 = args.italics2 or "yes"
		args.glossing3 = args.glossing3 or "yes"
		if args.lang and not args.lang2 then args.lang2 = args.lang .."-Latn" end
		if args.transl and not args.transl2 then args.transl2 = args.transl end
		if_auto_translit = true
	end
	local revid = frame:preprocess( "{{REVISIONID}}" )
	if revid == "" then
		if not args['display-messages'] or yesno(args['display-messages']) then displaying_messages = true end
	end
	msg = UserMessages
	local line = {}

	local function set_italics(n)
		line[n].attr.style = line[n].attr.style .. "font-style: italic;"
		line[n].tone_sup = true 
		if args['tone-superscripting'] and not yesno(args['tone-superscripting']) then line[n].tone_sup = false end
	end

	if args.glossing then 
		local _gl = set_glossing_type(args.glossing)
		if _gl then conf.GlossingType = _gl end
	end
	local _ablist_section = get_section(frame, 'list-of-glossing-abbreviations')
	if _ablist_section and _ablist_section ~= "" then
		local _a = mw.ustring.gsub(_ablist_section, '<div.->', '') 
		set_custom_glosses(_a)
	end
	local _ablist = args.abbreviations
	if _ablist and _ablist ~= "" then set_custom_glosses(_ablist) end
	local _ablist = args.ablist
	if _ablist and _ablist ~= "" then set_custom_glosses(_ablist) end

	local _spacing = tonumber(args.spacing)
	if _spacing and _spacing <= 20 then conf.style.WordDiv = conf.style.WordDiv .. 'margin-right: ' .. _spacing .. 'em;'
	else conf.style.WordDiv = conf.style.WordDiv .. conf.style.WordMargin end

	local offset, last_line = 0, 0
	for j,v in ipairs(args) do 
		last_line = last_line + 1
		if is_empty(v) then offset = offset + 1
		else
		local i = j - offset
		line[i] = {}
		v = normalise(v)

		if if_auto_translit and v == "auto" and i > 1 then
			local source_line = line[i-1]
			local src_lang = source_line.lang or args.lang
			if src_lang then
				v = transliterate(source_line.whole, src_lang)
			else v = ""; msg:add("error", "No language specified for automatic transliteration") end
		end 

		line[i].whole = v
		line[i].length = mw.ustring.len(v)

		local _c = args["c" .. i]
		if _c and _c ~= "" then
			line.hasComments = true
			line[i].c = _c
		end

		line[i].class = ""
		local _style = args["style" .. i]
		if not _style then _style = "" else _style = tidyCss(_style) end
		line[i].attr = {style = conf.style.WordP .. _style}

		local _lang = args["lang" .. i]
		if _lang and #_lang > 1 then line[i].lang = _lang
		else _lang = args.lang
			if _lang and #_lang > 1 and i == 1 then line[i].lang = _lang end
		end
		line[i].attr.lang = line[i].lang
		if template_name == 'Template:Fs interlinear' then
			if _lang == "bo" and i == 1 then
				line[1].class = line[1].class .. " uchen"
				line[1].attr.style = line[1].attr.style .. "font-size:1.25em; word-wrap:break-word;"
			end
		end
		if template_name == 'Template:Interlinear' then
			if _lang == "nv" and i == 1 then
				line[1].attr.style = line[1].attr.style .. "font-family: Aboriginal Sans, DejaVu Sans, Calibri, Arial Unicode MS, sans-serif;"
			end
		end

		if yesno(args["italics" .. i]) then set_italics(i) end

		local _transl = args["transl" .. i]
		if _transl and #_transl > 1 then
			_transl = mw.ustring.lower(_transl)
			
			-- Look up transliteration schema titles dynamically from Module:languages
			local langObj = languages.getByCode(_lang or "en")
			local t_title = _transl .. " transliteration" -- generic fallback 
			if langObj and type(langObj.getTranslitTitle) == "function" then
			    t_title = langObj:getTranslitTitle(_transl) or t_title
			end
			line[i].attr.title = t_title
		end

		local _glossing = args["glossing" .. i]
		if _glossing then
			line[i].glossing = set_glossing_type(_glossing)
			if not ((i == 1 and not yesno(_glossing)) or (i == 2 and yesno(_glossing))) then
				line.HasCustomGlossing = true
			end
		end

		local _ipa = args['ipa' .. i]
		if yesno(_ipa) then line[i].class = "IPA" end
		local _class = args['class' .. i]
		if _class then line[i].class = line[i].class .. " " .. _class end
		if line[i].class == "" then line[i].class = nil end
		end 
	end

	local line_count = #line
	if line_count == 0 then
		msg:add("error", template_name .. ": no lines supplied.")
		return msg:print_errors()
	end

	if line_count > 1 then
		local _italics = args.italics
		local n = tonumber(_italics)
		if n and n > 0 then
			set_italics(n)
		elseif not (_italics and not yesno(_italics)) and not (args["italics1"] and not yesno(args["italics1"])) then
			set_italics(1) 
		end
		free_translation = args[last_line]
		if not is_empty(free_translation) then line [line_count] = nil end
	end

	if yesno(args.glossing) == false then line.HasCustomGlossing = true end
	if not line.HasCustomGlossing then
		if line_count == 1 then line[1].glossing = conf.GlossingType
		elseif line[2] then line[2].glossing = conf.GlossingType end
	end
	set_global_glossing_settings{style = args['glossing-style'], underline = args.underline, small_caps = args['small-caps']}

	for i,v in ipairs(line) do
		local ifglossing = false
		if line[i].glossing then
			ifglossing = true 
			glossing_type = line[i].glossing 
		end
		local wc, n = 1, 1
		line[i].words = {}
		while n <= line[i].length do
			buffer = ""
			n = parse(line[i], n, 0, ifglossing)+2
			line[i].words[wc] = buffer
			wc = wc + 1
		end
	end

	local number_of_words, mismatch_found = 0, false
	for i,v in ipairs(line) do 
		local wc = #line[i].words
		if wc ~= number_of_words then
			if i ~= 1 and wc ~= 0 then mismatch_found = true end
			if wc > number_of_words then number_of_words = wc end
		end
	end

	if mismatch_found then
		local error_text = "Mismatch in the number of words between lines: "
		for i,v in ipairs(line) do
			local wc = #line[i].words
			error_text = error_text .. wc .. " word(s) in line " .. i .. ", "
			if wc ~= number_of_words then
				for current_word = wc+1, number_of_words do line[i].words[current_word] = " " end
			end
		end
		if string.sub(error_text, -2) == ", " then error_text = string.sub(error_text, 1, #error_text - 2) .. " " end
		error_text = error_text .. help_link("mismatch")
		UserMessages:add("error", error_text)
	end

	if line_count == 1 then
		local span = mw.html.create('span')
		span:attr(line[1].attr)
		for wi = 1, number_of_words do
			local space = (wi < number_of_words) and " " or ""
			span:wikitext(line[1].words[wi] .. space)
		end
		return tostring(span)
	end

	local div = mw.html.create("div")
	div:addClass(conf.class.Interlinear)

	local number, indent = nil, nil
	if args.number and args.number ~= "" then number = args.number end
	if args.indent and args.indent ~="" then indent = args.indent end
	if indent or number then
		if not indent then indent = "4" end 
		div:css("margin-left", indent .. 'em')
		if number then
			div:tag("div"):css("position", "absolute"):css("left", "1em"):wikitext(args.number)
		end
	end

	if args.box and args.box ~= "" then
		div:css("background-color", "#f8f9fa"):css("border", "1px solid #eaecf0"):css("padding", "1em") end
	if args.top and args.top ~= "" then div:tag("div"):wikitext(args.top) end

	for wi = 1, number_of_words do
		local div2 = div:tag("div"):attr("style", conf.style.WordDiv)
		for i,_ in ipairs (line) do
			if line[i].whole ~= "" then 
				local p = div2:tag("p")
				p:attr(line[i].attr)
				if line[i].class then p:addClass(line[i].class) end
				local _text = line[i].words[wi]
				if _text == "" or _text == " " then _text = " " end 
				p:wikitext(_text)
			end
		end
	end

	if line.hasComments then
		local divc = div:tag("div"):attr("style", conf.style.WordDiv)
		for i,_ in ipairs (line) do
			local p = divc:tag("p"):attr("style", conf.style.WordP)
			if line[i].c then p:wikitext(line[i].c) else p:wikitext(" ") end
		end
	end

	for i,v in ipairs(line) do
		local hidden_line = div:tag("p")
		hidden_line:attr("style", conf.style.HiddenText):wikitext(v.whole)
	end

	local ft_line = div:tag("p")
	if free_translation and free_translation ~= "" then
		ft_line:attr("style", "clear: left;")
		ft_line:wikitext(free_translation)
	end
	if args.bottom and args.bottom ~= "" then 
		local bottom = div:tag('p')
		bottom:css('margin-top', '0')
		bottom:wikitext(args.bottom)
	end
	ft_line:node(msg:print_errors()) 

	local end_div = div:tag("div")
	end_div:attr("style", conf.style.EndDiv)
	div:newline()
	
	local temp_track = ""
	if last_line == 2 then temp_track = "[[Category:Pages with interlinear glosses using two unnamed parameters]]" end
	if last_line > 3 and template_name ~= 'Template:Fs interlinear' then  temp_track = "[[Category:Pages with interlinear glosses using more than three unnamed parameters]]" end
	return tostring(div) .. temp_track .. msg:print_warnings()
end

return p