Module:RQ:mxi:Kharja


local export = {};

local data = require("Module:RQ:mxi:Kharja/data");

local function quote_section(title, kharja_index, section_index, lang)

	local kharja = data[kharja_index];
	local section = kharja.sections[section_index];

	local bold = {};

	local function rm_brackets(str)
		return str:gsub('^%[', ''):gsub('%]$', '');
	end
	local bracket_regex = '%[.-%]';

	local function strip_diacritics(str)
		-- TODO: for now just Arabic
		return str
			:gsub('[\u{064B}-\u{0655}]', '')
			:gsub('ے', 'ی')
			:gsub('ن', 'ࢽ');
	end

	local i = 1;
	local text = mw.ustring.gsub(section.text, bracket_regex, function (str)
		str = rm_brackets(str);
		if (str:find(':')) then
			local new_str = str:gsub(':.+', '');
			if (strip_diacritics(str:gsub('.+:', '')) == title) then
				str =  "'''"..new_str.."'''";
				bold[i] = true;
			else
				str = new_str;
			end
		elseif (strip_diacritics(str) == title) then
			str = "'''"..str.."'''";
			bold[i] = true;
		end
		i = i + 1;
		return str;
	end);

	local function bold_with_colon(str)
		str = rm_brackets(str);
		if (str:find(':')) then
			local new_str = str:gsub(':.+', '');
			if (bold[tonumber((str:gsub('.+:', '')))]) then
				str =  "'''"..new_str.."'''";
			else
				str = new_str;
			end
		elseif (bold[i]) then
			str = "'''"..str.."'''";
		end
		i = i + 1;
		return str;
	end

	local function bold_no_colon(str)
		str = rm_brackets(str);
		if (bold[i]) then
			str = "'''"..str.."'''";
		end
		i = i + 1;
		return str;
	end

	local norm;
	if (section.norm) then
		i = 1;
		norm = mw.ustring.gsub(section.norm, bracket_regex, bold_with_colon);
	end

	local tr = require('Module:mxi-translit').tr(norm or text, 'mxi', ({ A = 'Arab', H = 'Hebr' })[string.sub(kharja_index, 1, 1)]);
	tr = tr:gsub('{', [[<span style="font-style:normal">]]):gsub('}', [[</span>]]);

	text = mw.ustring.gsub(text, '[{}]', '');
	if (norm) then norm = mw.ustring.gsub(norm, '[{}]', ''); end

	local ts;
	if (section.ts) then
		i = 1;
		ts = mw.ustring.gsub(section.ts, bracket_regex, bold_no_colon);
	end

	local t;
	if (section.t) then
		i = 1;
		t = mw.ustring.gsub(section.t, bracket_regex, bold_with_colon);
	end

	local lit;
	if (section.lit) then
		i = 1;
		lit = mw.ustring.gsub(section.lit, bracket_regex, bold_with_colon);
	end

	return {
		title = "quote-book",
		args = {
			'mxi',
			termlang = lang,
			year = kharja.year or 'c. 1100',
			author = kharja.author,
			title = '[[AP:Mozarabic kharjas#'..kharja_index..'|Kharja '..kharja_index..']]',
			section = section_index,
			norm = norm, text = text, tr = tr, ts = ts, t = t, lit = lit,
		}
	};

end

function export.main(frame)

	local args = frame:getParent().args;
	local title = args.title or mw.title.getCurrentTitle().text;

	local ret = '';

	local first = true;
	for i, v in ipairs(args) do
		ret = ret .. frame:expandTemplate(quote_section(title, v:gsub('%..+', ''), tonumber((v:gsub('.+%.', ''))), args.lang));
		if (first) then first = false;
		else ret = '\n#*'..ret; end
	end

	return ret;

end

return export;