Module:Lang

local p = {} local param = {} local langCode = require('Module:Lang/code')

function trim(s) return s:match '^%s*(.-)%s*$' end

function p.main(frame) local args = require('Module:Arguments').getArgs(frame, {		parentFirst = true,		wrappers = {			'Template:Lang'		}	}) return p._main(args) end

function eachLang(altLangs, args, tBlock, key, value) local part = mw.html.create('span') local vertical = args.vertical or nil local append = {} if langCode[key] then if not(args.mini) then part:wikitext(' ' .. langCode[key] .. ' ' .. ': ') end if not(args.selective) then part:tag('span') :attr('lang', key) :wikitext(value) else part:node(p.SelectiveSpan(value,key)) end if tBlock[key .. '_rm'] then -- Romanization table.insert(append, ' .. args[key .. '_rm'] .. ') end if tBlock[key .. '_tl'] then -- Translation if vertical then table.insert(append, args[key .. '_tl']) else table.insert(append, '"' .. args[key .. '_tl'] .. '"') end end if tBlock[key .. '_lit'] then -- Literal table.insert(append, 'lit. "' .. args[key .. '_lit'] .. '"') end if #append>0 and vertical then part:wikitext(' ' .. table.concat(append, ' '))		elseif #append>0 then part:wikitext(' ' .. table.concat(append, ', '):gsub('", ','," ')) end else part:wikitext(key .. ': ' .. value) end table.insert(altLangs, tostring(part)) end

function p._main(args) local out = mw.html.create('')

local tBlock = {sort = true, mini = true, selective = true, vertical = true}

--Obtain all _tn keys for k, v in pairs(args) do		if mw.ustring.find(k, '_rm$') or mw.ustring.find(k, '_tl$') or mw.ustring.find(k, '_lit$') then tBlock[k] = true end end --Obtain all numerical keys for k,v in ipairs(args) do		tBlock[k] = true end

--Output default Value out:wikitext(args[1])

local altLangs = {} --Process Sort Paremeter (Split by Delimiter: comma) for sKey in string.gmatch(args.sort or '', '([^,]+)') do		if not(tBlock[sKey]) and args[sKey] then eachLang(altLangs, args, tBlock, sKey, args[sKey]) tBlock[sKey]=true end end

--Process Rest for key, value in pairs(args) do		if not(tBlock[key]) then eachLang(altLangs, args, tBlock, key, value) end end if #altLangs>0 then if not(args.mini) then out:wikitext(' (')		end		out:wikitext(table.concat(altLangs,'; '))		if not(args.mini) then			out:wikitext(')') end end return tostring(out) end

function p.SelectiveSpan(s, lang, if1all) --initial check, return blank if nil if s then else return '' end --mw.logObject(s,'Input') --debug --initialize tables local span = {} local nospan = {} local split = {} --separate the string through unicode codepoints as default lua split cannot handle non-UTF-8 characters for c in mw.ustring.gcodepoint(s) do		--mw.logObject(c,'c')      --debug table.insert(split, mw.ustring.char(c)) end --mw.logObject(split,'split')  --debug --separate the split string into ASCII and non-ASCII, include anything inside '<>' as ASCII to not mess with html tags rendering local loopnum = 1 local html, link = false, false while split[loopnum] do		--check for start of html tag if split[loopnum] == '<' then html = true end --check for start of link if (split[loopnum] == '[' and split[loopnum+1] == '[') then link = true end if (split[loopnum]:find('[\201-\255ÊêÔôƠơƯưẮắẤấẾếỐốỚớỨứẰằẦầỀềỒồỜờỪừẢảẲẳẨẩẺẻỂểỈỉỎỏỔổỞởỦủỬửỶỷẴẵẪẫẼẽỄễỖỗỠỡỮữỸỹẠạẶặẬậẸẹỆệỊịỌọỘộỢợỤụỰựỴỵ]') and html == false and link == false) then span[loopnum] = split[loopnum] else nospan[loopnum] = split[loopnum] end --check for end of html tag if split[loopnum] == '>' then html = false end --check for end of link if (split[loopnum] == '|' or (split[loopnum] == ']' and split[loopnum+1] == ']')) then link = false end loopnum = loopnum + 1 end --mw.logObject(span,'span')    --debug --mw.logObject(nospan,'nospan') --debug if (if1all and next(span, nil) ~= nil) then return mw.html.create('span'):attr('lang', lang):wikitext(s) end --build the string while applying the lang span to the non-ASCII characters only local i = 1 local s = mw.html.create while (span[i] or nospan[i]) do		if span[i] then local text = mw.html.create local try = true text:wikitext(span[i]) while try do				if span[i+1] then text:wikitext(span[i+1]) i = i + 1 else try = false end end s:tag('span'):attr('lang', lang):node(text):done elseif nospan[i] then s:wikitext(nospan[i]) end i = i + 1 end --mw.logObject(s,'Output') --debug return s end

return p