Module:User:Φρύδια/table

local this = { }

local TAGS = { { MASK = '<,>', A = , B =  } }

local function split(str, sep) if not sep then sep = '%s' end local parts = { } for s in str:gmatch('([^'..sep..']+)') do		table.insert(parts, s)	end return parts end

local function interpret(str) local function _intp(s, i, tgs) local pattern = '([^<,>]*)<(.-),(.-)>([^<,>]*)' local opn, sep, cls = TAGS[i].MASK:match '(.)(.)(.)' pattern = pattern:gsub('<', opn):gsub(',', sep):gsub('>', cls) local a, b = ,  for _1, _2a, _2b, _3 in s:gmatch(pattern) do	       a = a.._1.._2a.._3 b = b.._1.._2b.._3 end local tgs_sep = (tgs ==  and  or ', ') if i < #TAGS then if a..b == '' then return _intp(s, i+1, tgs) else return _intp(a, i+1, tgs..tgs_sep..TAGS[i].A), _intp(b, i+1, tgs..tgs_sep..TAGS[i].B)			end else if a..b == '' then return { txt = s, tags = tgs } else return { txt = a, tags = tgs..tgs_sep..TAGS[i].A }, { txt = b, tags = tgs..tgs_sep..TAGS[i].B } end end end return { _intp(str, 1, '') } end

function this.create(cnjg, metadata) if metadata.TAGS then for _, tag in ipairs(metadata.TAGS) do table.insert(TAGS, tag) end end local mg_col_widths = { } for _, mg in ipairs(metadata.MOOD_GROUPS) do		local c_max = 1 for _, m in ipairs(mg) do			local c = #metadata.DIMENSIONS[tonumber(m.DIMS:sub(1, 1))][1] if c > c_max then c_max = c end end table.insert(mg_col_widths, c_max) end local header = cnjg[metadata.LEMMA_FORMS[1]] local co_lemmas = '' for i = 2, #metadata.LEMMA_FORMS do		co_lemmas = co_lemmas..', '..cnjg[metadata.LEMMA_FORMS[i]] end if co_lemmas ~= '' then header = header..' '..co_lemmas..' ' end local code = ' '..			' '..header..' '.. ' '	local function begin_mood_group(i) local text = metadata.LABELS['MOOD_GROUP_'..i]		local cols_i = mg_col_widths[i] + 5 code = code..''.. ' '..text..' ' end local function begin_table(i_group, mood_name)--, n_cols)		local infx = i_group == 2 and ' id="'..mood_name..'"' or 		code = code..'\n{| class="mood-tb" |\n'	end	local function begin_row		code = code..'|-\n'	end	local function append_cell(style, text, span)		span = span and 'colspan="'..span..'"' or 		code = code..'| class="'..style..'" '..span..' |'..(text or )..'\n'	end	local function parse(query_result)		if not query_result then return '–' end		tagged_forms = {}		for i, v in ipairs(interpret(query_result)) do			local link_txt = v.txt:gsub('ā', 'a'):gsub('ē', 'e'):gsub('ī', 'i')								 :gsub('ō', 'o'):gsub('ū', 'u'):gsub('ȳ', 'y')			local form = ..v.txt..			if v.tags ~=  then form = v.tags..' '..form end			tagged_forms[i] = form		end		return table.concat(tagged_forms, ' ')	end	local function end_row code = code..'\n' end local function end_table code = code..'|}\n ' end local function end_mood_group code = code..' ' end for k, group in ipairs(metadata.MOOD_GROUPS) do		begin_mood_group(k) for _, mood in ipairs(group) do			local dims_ref, info_block = mood.DIMS:match '(%d)|(.+)' local dims_all = metadata.DIMENSIONS[tonumber(dims_ref)] local dims_info_all = split(info_block, ':') local dims, dims_info = { }, { } for i, v in ipairs(dims_info_all) do				if v:gsub('n', ) ~=  then table.insert(dims_info, v)					table.insert(dims, dims_all[i]) end end local cols = { } for n, tense in ipairs(dims[1]) do				if dims_info[1]:sub(n, n) == 'y' then table.insert(cols, tense) end end local mood_name = metadata.LABELS[mood.ID] begin_table(k, mood_name)--, #cols)			begin_row			append_cell('h1 rsz bf', mood_name, #cols+1)			end_row			local function make_lvl(lvl, i_upval, id)				for i, val in ipairs(dims[lvl]) do					local token = dims_info[lvl]:sub(i, i)					if token == 'n' then do end					elseif token:find '[y!]' or tonumber(token) == i_upval then						local function extend(id_str, seg, start)							if seg and seg ~=  then								if start then return seg..(id_str ==  and  or '_'..id_str)								else return (id_str ==  and '' or id_str..'_')..seg end							else return id_str end						end						local i_id = extend(id, val)						local function get_label(prefix, start)							local label_id = token == '!' and extend(val, mood.ID, 1) or val							return metadata.LABELS[extend(label_id, prefix, start)]						end						begin_row						if lvl == #dims then							append_cell((i%2 > 0 and 'h3' or 'h2')..' fxd', get_label('ab', 1) or '–') for t, tense in ipairs(cols) do								local final_id = extend(extend(i_id, tense, 1), mood.ID, 1) local text = parse(cnjg[final_id]) append_cell((i%2 > 0 and 'wh' or 'gr')..' rsz', text) end elseif lvl == 2 then append_cell('h1 fxd', '➤') for t, tense in ipairs(cols) do								append_cell('h2 rsz bf', get_label(tense, 1)) end elseif lvl == #dims - 1 then append_cell 'h2 fxd' for m = 1, #cols do								append_cell('h3 rsz', get_label) end end end_row if lvl < #dims then make_lvl(lvl+1, i, i_id) end end end end make_lvl(2, 0, '') end_table end end_mood_group end return code..' ' end

return this