Module:uz-inflection-docs-utils

-- Module:uz-inflection-docs-utils v0.6 -- 2015-07-11

local export = {}

local u = require("Module:uz-utils") local wu = require("Module:uz-wiki-utils")

export.args_values = {} export.affixes_changes = {} export.condition_vars = {}

-- WIKIFY

function export.var(var_name) return wu.link('var '..var_name, wu.code_blue(var_name)) end

function export.arg(arg_name) return wu.link('arg '..arg_name, wu.code_purple(arg_name)) end

function export.form(form_name) return wu.link('form '..form_name, wu.code_maroon(form_name)) end

function export.join_values(values, func) if type(values) == 'string' then values = {values} end local mapped_values = {} for j, value in pairs(values) do table.insert(mapped_values, '"' .. func(value) .. '"') end return table.concat(mapped_values, ", ") end

function export.print_value(value) if value:find('%<') == nil then return '"' .. wu.span_blue(value) .. '"' end value = value:gsub("%>([^<]+)%<", '> + "' .. wu.span_blue('%1') .. '" + <') value = value:gsub("%>([^<]+)$", '> + "' .. wu.span_blue('%1') .. '"') value = value:gsub("%>%<", '> + <') value = value:gsub("%", wu.code_green('base')) for affix_key, v in pairs(export.affixes) do value = value:gsub("%<" .. affix_key .. "%>", export.var(affix_key)) end for var_name, v in pairs(export.condition_vars) do value = value:gsub("%<" .. var_name .. "%>", export.var(var_name)) end for i, key in pairs(export.form_keys) do value = value:gsub("%<" .. key .. "%>", export.form(key)) end -- TODO: if doesn't exist then mark it as red! value = value:gsub("%<([^<> /=]+)%>", wu.code_red('%1')) return value end

function export.print_changes(t, affix) if not t[affix] then return '' end local values = {} for var_value, conditions in pairs(t[affix]) do		local _conditions = {} for i, condition in pairs(conditions) do			table.insert(_conditions, wu.link('condition '..condition, wu.span_green('#'..condition))) end local conditions_text = table.concat(_conditions, ', ') table.insert(values, var_value .. ' (' .. conditions_text .. ')')	end return table.concat(values, ' ') end

function export.print_action_value(action_value) if type(action_value) == 'string' then return export.print_value(action_value) elseif type(action_value) == 'table' then local action = action_value if action[1] == 'replace' or action[1] == 'var_replace' or action[1] == 'replace_var' then local var_name = action[2] local pattern = action[3] local replace = action[4] return string.format('%s.%s(/%s/, "%s")', export.var(var_name), wu.span_maroon('replace'), wu.span_blue(pattern), wu.span_blue(replace)) elseif action[1] == 'arg_replace' or action[1] == 'replace_arg' then local var_name = action[2] local pattern = action[3] local replace = action[4] return string.format('%s.%s(/%s/, "%s")', export.arg(var_name), wu.span_maroon('replace'), wu.span_blue(pattern), wu.span_blue(replace)) elseif action[1] == 'arg_match' or action[1] == 'match_arg' then local arg_name = action[2] local pattern = action[3] pattern = pattern:gsub('([])', wu.span_red('%1')) return string.format('%s.%s(/%s/)', export.arg(arg_name), wu.span_maroon('match'), wu.span_blue(pattern)) elseif action[1] == 'match' or action[1] == 'var_match' or action[1] == 'match_var' then local var_name = action[2] local pattern = action[3] pattern = pattern:gsub('([])', wu.span_red('%1')) return string.format('%s.%s(/%s/)', export.var(var_name), wu.span_maroon('match'), wu.span_blue(pattern)) elseif action[1] == 'substring' then return '{substring}' else return " {not documented} " end else return action_value end end

-- GENERATE DATA

function export.gen_args_values(conditions, prefix) if conditions == nil then conditions = export.conditions end if prefix == nil then prefix = '' end for i, condition in pairs(conditions) do		for param_name, param_value in pairs(condition) do			if param_name:match("^arg") ~= nil then local arg_name if param_name:match("_NOT$") ~= nil then param_name = param_name:sub(1, -5) end if param_name == 'arg' then -- arg = {' ', } arg_name = param_value[1] param_value = param_value[2] else arg_name = param_name:sub(5) -- arg_ = param_name = param_name:sub(1, 3) end if type(param_value) == 'string' then param_value = {param_value} end for j, arg_value in pairs(param_value) do arg_value = '"' .. wu.span_purple(arg_value) .. '"' u.put_value(export.args_values, arg_name, arg_value, prefix .. i)				end elseif param_name == 'sub_conditions' then export.gen_args_values(param_value, prefix .. i .. '.') end end end end

function export.gen_vars_changes(conditions, prefix) if conditions == nil then conditions = export.conditions end if prefix == nil then prefix = '' end for i, condition in pairs(conditions) do		if condition['actions'] then for j, action in pairs(condition['actions']) do				if action[1] == 'set' then var_name = action[2] var_value = export.print_action_value(action[3]) if export.affixes[var_name] and export.affixes[var_name] ~= var_value then u.put_value(export.affixes_changes, var_name, var_value, prefix .. i)					else u.put_value(export.condition_vars, var_name, var_value, prefix .. i)					end end end end if condition['sub_conditions'] then export.gen_vars_changes(condition['sub_conditions'], prefix .. i .. '.') end end end

function export.gen_form_keys export.form_keys = {} for class_name, forms in pairs(export.classes) do		for form_key, form_value in pairs(forms) do			if not export.form_keys[form_key] then table.insert(export.form_keys, form_key) end end end end

-- ANALYZE CONDITIONS

function export.get_conditions_depth(conditions, depth) if depth == nil then depth = 0 end local max_depth = depth for i, condition in pairs(conditions) do		if condition['sub_conditions'] then current_depth = export.get_conditions_depth(condition['sub_conditions'], depth + 1) if current_depth > max_depth then max_depth = current_depth end end end return max_depth end

-- ORDERING

function export.case_order(t, key_1, key_2) if key_1 == nil then return true elseif key_2 == nil then return false end local order_words = { 'nom', 'nominative', 'gen', 'genitive', 'dat', 'dative', 'acc', 'accusative', 'ccusative', -- little hack because of common first 'a'		'ins', 'prp', 'locative', 'ablative', 'blative', -- little hack because of common first 'a'		'sg', 'pl', 'm_', 'n_', 'f_', }	local common_pos = u.find_common(key_1, key_2) local diff_key_1 = key_1:sub(common_pos) local diff_key_2 = key_2:sub(common_pos) local key_1_index = 999 local key_2_index = 999 for i, order_word in pairs(order_words) do if diff_key_1:match('^' .. order_word) then key_1_index = i		end if diff_key_2:match('^' .. order_word) then key_2_index = i		end end if key_1_index ~= key_2_index then return key_1_index < key_2_index end return key_1 < key_2 end

function export.get_ordered_classes(conditions) local class_names = {'common'} for i, condition in pairs(conditions) do		for param_name, param_value in pairs(condition) do			if condition['actions'] then for j, action in pairs(condition['actions']) do					if action[1] == 'add_class' then class_name = action[2] if not class_names[class_name] then table.insert(class_names, class_name) end end end end end end return class_names end

return export