Module:interlinear: Difference between revisions

No edit summary
No edit summary
 
(6 intermediate revisions by the same user not shown)
Line 1: Line 1:
local export = {}
local p = {}
local m_data = mw.loadData("Module:interlinear/data")
local data = mw.loadData( 'Module:interlinear/data' )
local m_languages = require("Module:languages")
local gloss_override = {} -- for custom gloss abbreviations
local m_script_utilities = require("Module:script utilities")
local getArgs = require('Module:Arguments').getArgs
local yesno = require('Module:Yesno')
local languages = require('Module:languages') -- Switched from Module:Lang/data


local function escape(x)
--------------------------
if mw.ustring.find(x, "&", 1, true) then x = mw.ustring.gsub(x, "&", "&") end
-- Almost-global variables
if mw.ustring.find(x, '"', 1, true) then x = mw.ustring.gsub(x, '"', """) end
--------------------------
if mw.ustring.find(x, "|", 1, true) then x = mw.ustring.gsub(x, "|", "|") end
local glossing_type, displaying_messages, free_translation, msg, buffer
if mw.ustring.find(x, "<", 1, true) then x = mw.ustring.gsub(x, "<", "&lt;") end
 
if mw.ustring.find(x, ">", 1, true) then x = mw.ustring.gsub(x, ">", "&gt;") end
-------------------
return x
-- General settings
end
-------------------
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",
}


local function render_gloss(gloss, categories)
---------------------
if type(gloss) == "string" then
-- Section transclusion
return escape(gloss)
---------------------
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
end
if page_content then
local gloss_id = gloss.gloss
if mw.ustring.find(page_content, section_name, 1, true) then
local gloss_data = m_data[gloss_id]
return frame:preprocess('{{#section:{{FULLPAGENAME}}|' .. section_name .. '}}')
end
local label = gloss_data and gloss_data.label or gloss_id
local tooltip = gloss_data and gloss_data.tooltip or ""
if gloss.negate then
label = "N" .. label
if #tooltip > 0 then tooltip = "non-" .. tooltip end
end
if not gloss_data then
return '<small>' .. escape(gloss_id) .. '</small>'
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


if gloss_data.ambiguous then
local function highlight(text)
table.insert(categories, "Interlinear glosses with ambiguous gloss abbreviations")
if text then return '' .. text .. '' else return "" end
end
end


return '<small><abbr title="' .. tooltip .. '">' .. escape(label) .. '</abbr></small>'
local function tone_sup(str)
return mw.ustring.gsub(str, "([^%p%s0-9])([0-9]+)", "%1<sup>%2</sup>")
end
end


local function get_tr(tr, word, lang, sc)
local function is_empty(str)  
if tr == true or tr == nil then
if not str then return true end
tr = lang:transliterate(word, sc) or ""
if mw.ustring.find(str, "[^" .. conf.WordSeparator .. "]")
elseif tr == false then
then return false
tr = ""
else return true end
end
return tr ~= "", tr
end
end


function export.interlinear_gloss(data)
local function help_link (anchor)
local result = '<table class="interlinear-gloss"><tr>'
if anchor then
local has_tr, tr_ok = false
return " ([[w:" .. conf.ErrorHelpLocation .. "#" .. anchor .. "|help]])"
local trs = {}
else return "" end
local categories = {}
end


if #data.words == 0 then
-- 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 ""
return ""
end
end
 
local lang = data.lang
-- Integration with Module:languages
local sc = data.sc
local lang_obj = languages.getByCode(lang_code)
if not sc then
if lang_obj and type(lang_obj.transliterate) == "function" then
local first = 1
return lang_obj:transliterate(str) or ""
while first < #data.words and data.words[first] == "" do first = first + 1 end
sc = lang:findBestScript(data.words[first])
end
end
local tr_ref = data.tr
-- Fallback for specific modules if getByCode doesn't provide it
if data.tr == nil then
local lookup = {grc = {module = 'Module:Ancient Greek', funct = "transliterate", } }
has_tr = sc:isTransliterated()
local t = lookup[lang_code]
tr_ref = {}
if t then
else
local module = require(t.module)
has_tr = #data.tr > 0
return module[t.funct](str)
else  
msg:add("error", "Can't find transliterator for language '" .. lang_code .. "'")
end
end
return ""
end


if data.top then
--------------------
local columns = math.max(#data.words, #data.glosses)
-- Glossing settings
result = result .. '<td colspan="' .. columns .. '"><span class="intetlinear-gloss-top">' .. data.top .. '</span></td></tr><tr>'
--------------------
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
end


if data.headers then
local function set_custom_glosses(list)
for _, header in ipairs(data.headers) do
local abbs = mw.text.split(list, '[;\n\t]')
result = result .. '<td><span class="intetlinear-gloss-header">' .. header .. "</span></td>"
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
result = result .. "</tr><tr>"
end
end
end


for i, word in ipairs(data.words) do
---------------------
result = result .. "<td>" .. m_script_utilities.tag_text(word, lang, sc) .. "</td>"
-- UserMessages object
if has_tr then
---------------------
local ok, tr = get_tr(tr_ref[i], word, lang, sc)
local UserMessages = {errors = {}, warnings = {}, gloss_messages = {}}
tr_ok = tr_ok or ok
function UserMessages:add(msgtype, text, gloss)
trs[i] = tr
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
end
out = out .. "\n\n" .. tostring(div)
end
end
return out
end


if tr_ok then
---------------------
result = result .. "</tr><tr>"
-- Gloss Lookup & Format
---------------------
for _, tr in ipairs(trs) do
local function gloss_lookup(a, label, wikilink)
result = result .. "<td>" .. m_script_utilities.tag_translit(tr, lang, "term") .. "</td>"
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
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


if data.morphology then
local function format_gloss(gloss, label, wikilink)
local m_links = require("Module:links")
if string.sub(gloss,1,3) == "000" then return gloss end
result = result .. "</tr><tr>"
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
for _, morph in ipairs(data.morphology) do
if source ~= "local" and displaying_messages then
if mw.ustring.find(morph, "[[", nil, true) then
local message = ""
result = result .. "<td>" .. m_links.full_link{ term = morph, lang = lang, sc = sc, tr = "-" } .. "</td>"
if label then message = "assuming " .. gloss2 .. " means \"" .. abbr_label .. "\";" end
else
if glossing_type == "wikilink" and wikilink then
result = result .. "<td>" .. m_links.full_link{ alt = morph, lang = lang, sc = sc, tr = "-" } .. "</td>"
message = message .. " linking to [[w:" .. wikilink .. "|" .. wikilink .. "]];"
end
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
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
end
return tostring(gloss_node)
result = result .. "</tr><tr>"
end
 
for _, glosses in ipairs(data.glosses) do
local function find_gloss(word)
result = result .. "<td>"
local function scan_gloss(boundary, gloss_abbr)
for _, gloss in ipairs(glosses) do
if (mw.ustring.match(gloss_abbr, conf.GlossAbbrPattern) or conf.LowerCaseGlosses[gloss_abbr])
result = result .. render_gloss(gloss, categories)
and not (conf.GlossExcludeTable[gloss_abbr] or mw.ustring.match(gloss_abbr, conf.GlossExcludePattern)) then
gloss_abbr = format_gloss(gloss_abbr)
end
end
result = result .. "</td>"
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
end
if i > cline.length then return i end
if data.word_translations then
local next_step, j, _, chunk
result = result .. "</tr><tr>"
local probe = mw.ustring.sub(cline.whole,i,i)
for _, wt in ipairs(data.word_translations) do
if mw.ustring.match(probe,"[" .. conf.WordSeparator .. "]") and tags_found == 0 then  
result = result .. "<td>" .. wt .. "</td>"
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
end
end
return next_step
if data.translation then
end
local columns = math.max(#data.words, #data.glosses)
 
result = result .. '</tr><tr><td colspan="' .. columns ..
function p.gcl(frame)
'"><span class="intetlinear-gloss-translation-double-quote">"</span><span class="intetlinear-gloss-translation">' ..
local args = getArgs(frame,{ trim = true, removeBlanks = false, parentOnly = true, wrappers = {'Template:Grammatical category label'}, })
data.translation .. '</span><span class="intetlinear-gloss-translation-double-quote">"</span></td>'
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
end
local _ablist_section = get_section(frame, 'list-of-glossing-abbreviations')
if #categories > 0 then
if _ablist_section and _ablist_section ~= "" then
categories = require("Module:utilities").format_categories(categories, lang)
local _a = mw.ustring.gsub(_ablist_section, '<div.->', '')  
else
set_custom_glosses(_a)
categories = ""
end
end
local _ablist = args.abbreviations
return result .. '</tr></table>' .. categories
if _ablist and _ablist ~= "" then set_custom_glosses(_ablist) end
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  


local function split_words(text)
line[i].whole = v
local pos = 1, mend, found
line[i].length = mw.ustring.len(v)
local len = mw.ustring.len(text)
local search = "([{%s}])"
local words = {}
local braces = 0
local word_start = 1
local last_brace = nil
local going = true


while going do
local _c = args["c" .. i]
pos, mend, found = mw.ustring.find(text, search, pos)
if _c and _c ~= "" then
if pos == nil then
line.hasComments = true
if braces > 0 then error("Unterminated { on the first or second line") end
line[i].c = _c
found = " "
pos, mend = len + 1, len + 1
going = false
end
end


if found == "{" then
line[i].class = ""
if braces == 0 and word_start == pos then
local _style = args["style" .. i]
word_start = word_start + 1
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
braces = braces + 1
end
elseif found == "}" and braces > 0 then
if template_name == 'Template:Interlinear' then
braces = braces - 1
if _lang == "nv" and i == 1 then
if braces == 0 then
line[1].attr.style = line[1].attr.style .. "font-family: Aboriginal Sans, DejaVu Sans, Calibri, Arial Unicode MS, sans-serif;"
last_brace = pos
end
end
elseif --[[ whitespace and ]] braces == 0 then
end
-- end word
 
local word_end = mend - 1
if yesno(args["italics" .. i]) then set_italics(i) end
if word_start <= word_end then
 
if word_end == last_brace then
local _transl = args["transl" .. i]
word_end = last_brace - 1
if _transl and #_transl > 1 then
end
_transl = mw.ustring.lower(_transl)
table.insert(words, mw.ustring.sub(text, word_start, word_end))
-- 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
word_start = mend + 1
last_brace = nil
end
end


pos = mend + 1
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
end


return words
local number_of_words, mismatch_found = 0, false
end
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


local function split_glosses(text)
if mismatch_found then
local glosses_all = {}
local error_text = "Mismatch in the number of words between lines: "
for i,v in ipairs(line) do
for _, word in ipairs(split_words(text)) do
local wc = #line[i].words
local glosses = {}
error_text = error_text .. wc .. " word(s) in line " .. i .. ", "
local pos = 1, mend, gloss
if wc ~= number_of_words then
local last = 1
for current_word = wc+1, number_of_words do line[i].words[current_word] = " " end
while true do
pos, mend, prefix, gloss = mw.ustring.find(word, "([!^]?)([0-9A-Z]+)", pos)
if pos == nil then break end
if last < pos then
table.insert(glosses, mw.ustring.sub(word, last, pos - 1))
end
end
if prefix == "^" then
table.insert(glosses, gloss)
else
table.insert(glosses, { gloss = gloss, negate = prefix == "!" })
end
last = mend + 1
pos = last
end
end
if string.sub(error_text, -2) == ", " then error_text = string.sub(error_text, 1, #error_text - 2) .. " " end
if last <= mw.ustring.len(word) then
error_text = error_text .. help_link("mismatch")
table.insert(glosses, mw.ustring.sub(word, last))
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
table.insert(glosses_all, glosses)
end
end
return glosses_all
end


local function split_trs(text)
if args.box and args.box ~= "" then
local trs = {}
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


if text == "-" then return {} end
for wi = 1, number_of_words do
local div2 = div:tag("div"):attr("style", conf.style.WordDiv)
for _, word in ipairs(split_words(text)) do
for i,_ in ipairs (line) do
if word == "+" then
if line[i].whole ~= "" then
table.insert(trs, true)
local p = div2:tag("p")
elseif word == "-" then
p:attr(line[i].attr)
table.insert(trs, false)
if line[i].class then p:addClass(line[i].class) end
else
local _text = line[i].words[wi]
table.insert(trs, word)
if _text == "" or _text == " " then _text = " " end
p:wikitext(_text)
end
end
end
end
end
return trs
end


function export.interlinear_t(frame)
if line.hasComments then
local params = {
local divc = div:tag("div"):attr("style", conf.style.WordDiv)
[1] = {required = true},
for i,_ in ipairs (line) do
[2] = {required = true},
local p = divc:tag("p"):attr("style", conf.style.WordP)
[3] = {required = true},
if line[i].c then p:wikitext(line[i].c) else p:wikitext(" ") end
[4] = {},
end
end


["tr"] = {},
for i,v in ipairs(line) do
["wt"] = {},
local hidden_line = div:tag("p")
["morph"] = {},
hidden_line:attr("style", conf.style.HiddenText):wikitext(v.whole)
["top"] = {},
end
["header"] = {},
["sc"] = {},
}
local args = require("Module:parameters").process(frame:getParent().args, params)
local lang = m_languages.getByCode(args[1]) or m_languages.err(args[1], 1)
local sc = args["sc"] and (require("Module:scripts").getByCode(args["sc"]) or error("The script code \"" .. args["sc"] .. "\" is not valid.")) or nil


return export.interlinear_gloss{
local ft_line = div:tag("p")
words = split_words(args[2]),
if free_translation and free_translation ~= "" then
glosses = split_glosses(args[3]),
ft_line:attr("style", "clear: left;")
tr = args.tr and split_trs(args.tr) or nil,
ft_line:wikitext(free_translation)
morphology = args.morph and split_words(args.morph) or nil,
end
word_translations = args.wt and split_trs(args.wt) or nil,
if args.bottom and args.bottom ~= "" then
headers = args.header and split_words(args.header) or nil,
local bottom = div:tag('p')
translation = args[4],
bottom:css('margin-top', '0')
top = args.top,
bottom:wikitext(args.bottom)
end
ft_line:node(msg:print_errors())


lang = lang,
local end_div = div:tag("div")
sc = sc
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
end


return export
return p