A modult a Modul:R:M&A/doc lapon tudod dokumentálni

--[[
This module provides the front-end to an index of Meissner and Auden's Latin 
phrasebook.  It includes three data modules.  The exported function `phrasebook' 
retrieves the calling template's argument or, if none exists, the calling page's
name, and passes this into the table found in 
"Module:R:M&A/lemmas_no_collision_to_ix_phrase", which yields a set of phrase 
indices unambiguously containing forms of the given word.  These indices are 
retrieved from the table in "Module:R:M&A/ix_to_phrase", formatted into a 
collapsed phrase list, and returned to the caller.  When no match is found, an
empty string is returned.

Approximately 78% of the words in Meissner and Auden's phrase book can be 
de-stemmed unambigously.  Those which cannot are indexed separately in the 
table exported by this module: "Module:R:M&A/lemmas_collision_to_ix_phrase".
The corresponding additional lines are added to the bottom and prefaced with the
caption: (ambiguous).  If this is too obtrusive they can be perhaps further
collapsed.
]]--

local export = {}

local special_cases = {["a"]={false}, ["de"]={true}}

function print_collapsible_list_html(ls, x)
	local lst = {}
	local count = 0
	for k,x in pairs(ls) do
		table.insert(lst, "<li>"..x.."</li>")
		count = count + 1
	end
	local expandtext = count.." "..x..(count==1 and "" or "s")
	table.insert(lst, 1, "<div class='mw-collapsible mw-collapsed' style='display: inline' data-expandtext='"..expandtext.."><ul>")
    table.insert(lst, "</ul></div>")
	return table.concat(lst)	
end

function map(f, xs)
	local s = {}
	if not (xs == nil) then for i,x in pairs(xs) do s[#s+1] = f(x) end end 
	return s 
end

function is_member(xs, i)
	if (not (xs==nil)) then for k,v in pairs(xs) do if v==i then return true end end end
	return false
end
	
function load_lemma_nc_to_ix(w)
    --return lemmas_no_collision_to_ix_phrase = mw.loadData("Module:R:M&A/lemmas_no_collision_to_ix_phrase")[w]
	return require("Module:data tables").index_table("la_RMA_lemmas_no_collision_to_ix_phrase", w)
end

function print_html(w)
	local lemmas_collision_to_ix_phrase = mw.loadData("Module:R:M&A/lemmas_collision_to_ix_phrase")	--small table, hence loaded unsharded 
	local lsNC, lsC = load_lemma_nc_to_ix(w), lemmas_collision_to_ix_phrase[w]
	local ls = {}
	if lsNC then for k,v in pairs(lsNC) do ls[#ls+1] = v end end
	if lsC then for k,v in pairs(lsC) do ls[#ls+1] = v end end
	--local ix_to_phrase = mw.loadData("Module:R:M&A/ix_to_phrase")		--needs a data_table.select_array function: table_name -> [key] -> (key -> val); TBD
	local ix_to_phrase = (w=="a" or w=="de") and special_cases[w][1] and require("Module:R:M&A/memory special cases")[w] or require("Module:data tables").index_table_all("la_RMA_index_to_phrases", ls)
	function print_phrase(i) 
		local pL, pE = ix_to_phrase[i][1], ix_to_phrase[i][2]
		local prevSubst = 1
		local n = 1
		while prevSubst > 0 do 
		  pL, prevSubst = string.gsub(pL, "_", (n%2==1 and "<i><b>" or "</b></i>"), 1)
		  pE, pS0 = string.gsub(pE, "_", (n%2==1 and "<i>" or "</i>"), 1)
		  prevSubst = prevSubst + pS0
		  n = n + 1
		end
		pL = string.gsub(pL, "%[%d%]", "")
		return (is_member(lsC, i) and "<b><i>(ambiguous)</i></b> " or "").. string.gsub(pE..": "..pL.."<br/>", ".:", ":")
	end
	local ls_html = print_collapsible_list_html(map(print_phrase, ls), "phrase")
	--return true and "True" or "False"
	return ((ls == nil and "") or ls_html)
end

function export.phrasebook(frame)
	local args = frame:getParent().args
	local w = args[1] or mw.title.getCurrentTitle().text
	if (not args[1] or args[1] == "") and mw.title.getCurrentTitle().nsText == "Template" then
		return ""
	else
		return print_html(w)
	end
end


return export