"इस मॉड्यूल हेतु प्रलेख मॉड्यूल:Swadesh/doc पर बनाया जा सकता है"

local export = {}

local m_links = require("Module:links")
local m_scripts = require("Module:scripts")
local m_ipa = require("Module:IPA")

local gsub = mw.ustring.gsub

local English = {"[[I]] (<span style=\"font-variant:small-caps;\">1sg</span>)", "[[you]] (<span style=\"font-variant:small-caps;\">2sg</span>)", "[[he]], [[she]], [[it]] (<span style=\"font-variant:small-caps;\">3sg</span>)", "[[we]] (<span style=\"font-variant:small-caps;\">1pl</span>)", "[[you]] (<span style=\"font-variant:small-caps;\">2pl</span>)", "[[they]] (<span style=\"font-variant:small-caps;\">3pl</span>)", "[[this]]", "[[that]]", "[[here]]", "[[there]]", "[[who]]", "[[what]]", "[[where]]", "[[when]]", "[[how]]", "[[not]]", "[[all]]", "[[many]]", "[[some]]", "[[few]]", "[[other]]", "[[one]]", "[[two]]", "[[three]]", "[[four]]", "[[five]]", "[[big]]", "[[long]]", "[[wide]]", "[[thick]]", "[[heavy]]", "[[small]]", "[[short]]", "[[narrow]]", "[[thin]]", "[[woman]]", "[[man]] (adult male)", "[[man]] (human being)", "[[child]]", "[[wife]]", "[[husband]]", "[[mother]]", "[[father]]", "[[animal]]", "[[fish]]", "[[bird]]", "[[dog]]", "[[louse]]", "[[snake]]", "[[worm]]", "[[tree]]", "[[forest]]", "[[stick]]", "[[fruit]]", "[[seed]]", "[[leaf]]", "[[root]]", "[[bark]] (of a tree)", "[[flower]]", "[[grass]]", "[[rope]]", "[[skin]]", "[[meat]]", "[[blood]]", "[[bone]]", "[[fat]] (noun)", "[[egg]]", "[[horn]]", "[[tail]]", "[[feather]]", "[[hair]]", "[[head]]", "[[ear]]", "[[eye]]", "[[nose]]", "[[mouth]]", "[[tooth]]", "[[tongue]] (organ)", "[[fingernail]]", "[[foot]]", "[[leg]]", "[[knee]]", "[[hand]]", "[[wing]]", "[[belly]]", "[[guts]]", "[[neck]]", "[[back]]", "[[breast]]", "[[heart]]", "[[liver]]", "to [[drink]]", "to [[eat]]", "to [[bite]]", "to [[suck]]", "to [[spit]]", "to [[vomit]]", "to [[blow]]", "to [[breathe]]", "to [[laugh]]", "to [[see]]", "to [[hear]]", "to [[know]]", "to [[think]]", "to [[smell]]", "to [[fear]]", "to [[sleep]]", "to [[live]]", "to [[die]]", "to [[kill]]", "to [[fight]]", "to [[hunt]]", "to [[hit]]", "to [[cut]]", "to [[split]]", "to [[stab]]", "to [[scratch]]", "to [[dig]]", "to [[swim]]", "to [[fly]]", "to [[walk]]", "to [[come]]", "to [[lie]] (as in a bed)", "to [[sit]]", "to [[stand]]", "to [[turn]] (intransitive)", "to [[fall]]", "to [[give]]", "to [[hold]]", "to [[squeeze]]", "to [[rub]]", "to [[wash]]", "to [[wipe]]", "to [[pull]]", "to [[push]]", "to [[throw]]", "to [[tie]]", "to [[sew]]", "to [[count]]", "to [[say]]", "to [[sing]]", "to [[play]]", "to [[float]]", "to [[flow]]", "to [[freeze]]", "to [[swell]]", "[[sun]]", "[[moon]]", "[[star]]", "[[water]]", "[[rain]]", "[[river]]", "[[lake]]", "[[sea]]", "[[salt]]", "[[stone]]", "[[sand]]", "[[dust]]", "[[earth]]", "[[cloud]]", "[[fog]]", "[[sky]]", "[[wind]]", "[[snow]]", "[[ice]]", "[[smoke]]", "[[fire]]", "[[ash]]", "to [[burn]]", "[[road]]", "[[mountain]]", "[[red]]", "[[green]]", "[[yellow]]", "[[white]]", "[[black]]", "[[night]]", "[[day]]", "[[year]]", "[[warm]]", "[[cold]]", "[[full]]", "[[new]]", "[[old]]", "[[good]]", "[[bad]]", "[[rotten]]", "[[dirty]]", "[[straight]]", "[[round]]", "[[sharp]] (as a knife)", "[[dull]] (as a knife)", "[[smooth]]", "[[wet]]", "[[dry]]", "[[correct]]", "[[near]]", "[[far]]", "[[right]]", "[[left]]", "[[at]]", "[[in]]", "[[with]]", "[[and]]", "[[if]]", "[[because]]", "[[name]]"}
--array - list of 207 objects in this form {gloss=term}
local data = {}

function export.python_dictionary(frame)
	local args = frame:getParent().args
	local dataurl = args['lang']
	if args['var'] then dataurl = dataurl .. '/' .. args['var'] end
	local data = require("Module:Swadesh/data/" .. dataurl)
	local lang = require("Module:languages").getByCode(args['lang'])
	local res = "{'name': '" .. lang:getCanonicalName()
	if data['header'] ~= nil then res = res .. ' (' .. data['header'] .. ')' end
	res = res .. "',"
	
	for word = 1, #English do
		res = res .. "'" .. word .. "': ["
		if data[word] ~= nil then
			for _, termdata in ipairs(data[word]) do
				local best = m_scripts.findBestScript(termdata.alt or termdata.term, lang)
				local translit = lang:transliterate((termdata.alt or termdata.term), best)
				res = res .. '{'
				if termdata.term then res = res .. "'term': '" .. gsub(termdata.term, "'", "&#39;") .. "', " end
				if termdata.alt then res = res .. "'alt': '" .. gsub(termdata.alt, "'", "&#39;") .. "', " end
				if termdata.tr or translit then res = res .. "'translit': '" ..  gsub((termdata.tr or translit), "'", "&#39;") .. "', " end
				if termdata.ts then res = res .. "'transcript': '" ..  gsub(termdata.ts, "'", "&#39;") .. "', " end
				if termdata.ipa then res = res .. "'ipa': '" ..  gsub(termdata.ipa, "'", "&#39;") .. "', " end
				if termdata.nolink then res = res .. "'nolink': '" .. gsub(termdata.nolink, "'", "&#39;") .. "', " end
				if termdata.notes then res = res .. "'notes': '" ..  gsub(termdata.notes, "'", "&#39;") .. "', " end
				res = res .. '},'
			end
		end
		res = res .. "],"
	end
	res = res .. "}"
	
	return "<pre>" .. mw.text.nowiki(res) .. "</pre>"
end


function export.show(frame)
	local args = frame:getParent().args
	local data = {}
	local langs = {}
	
	local res = mw.html.create("table"):addClass("wikitable sortable")
	
	local headers = res:tag("tr")
	for _, text in ipairs { "№", "English" } do
		headers:tag("th"):node(text)
	end
	
	for i, arg in ipairs(args) do
		local lang = arg
		local header = arg
		langs[i] = require("Module:languages").getByCode(lang)
		if args["var" .. i] ~= nil then
			arg = arg .. '/' .. args["var" .. i]
		end
		data[i] = require("Module:Swadesh/data/" .. arg)
		local header = langs[i]:getCanonicalName()
		if data[i]['header'] ~= nil and args['translit'] == nil then
			header = header .. ' (' .. data[i]['header'] .. ')'
		end
		if data[i]['nativename'] ~= nil then
			header = header .. '<br><small>' .. m_links.full_link({lang = langs[i], alt = data[i]['nativename']}) .. "</small>"
		end
		local count = 0
		for k, v in pairs(data[i]) do
    		if (type(k) == 'number') then count = count + 1 end
    	end
		header = header .. "<br><small><sup>[[Module:Swadesh/data/" .. arg .. "|edit (" .. count .. ")]]</sup></small>"
		headers:tag("th"):node(header)
	end
	
	if args[2] == nil and args['translit'] == nil then
		headers:tag("th"):node("IPA")
	end
	
	for word = 1, #English do
		local row = mw.html.create("tr")
		row:tag("td"):node(word)
		row:tag("td"):node(English[word])
		
		for lang, arg in ipairs(args) do
			local res = ""
			local count = 0
			if data[lang][word] then
				for _, termdata in ipairs(data[lang][word]) do
					if count ~= 0 then res = res .. ", " end
					if args["translit"] then
						res = res .. '<span class="swadesh-translit">'
						local best = m_scripts.findBestScript(termdata.alt or termdata.term, langs[lang])
						local translit = langs[lang]:transliterate((termdata.alt or termdata.term), best)
						if termdata.nolink == nil and termdata.term ~= nil then
							res = res .. m_links.language_link({lang = langs[lang], term = termdata.term or '?',
								alt = termdata.ts or termdata.tr or translit or termdata.term or (termdata.ipa and '<span class="IPA">' .. termdata.ipa .. '</span>') or '?'})
						else
							res = res .. (termdata.ts or termdata.tr or translit or termdata.term or (termdata.ipa and '<span class="IPA">' .. termdata.ipa .. '</span>') or '?')
						end
					else
						res = res .. '<span class="swadesh-term">'
						if termdata.nolink == nil then
							res = res .. m_links.full_link({lang = langs[lang], term = termdata.term, alt = termdata.alt, tr = termdata.tr, ts = termdata.ts})
						else
							res = res .. termdata.term
						end
					end
					if termdata.notes then
						if args[2] == nil then res = res .. " (''<span class=\"swadesh-note\">" .. termdata.notes .. "</span>'')"
						else res = res .. '<abbr title = "' .. termdata.notes .. '>*</abbr>' end
					end
					res = res .. '</span>'
					count = count + 1
				end
			end
			row:tag("td"):node(res)
		end
		
		if args[2] == nil and args['translit'] == nil then
			local ipas = ""
			local count = 0
			if data[1][word] then
				for _, termdata in ipairs(data[1][word]) do
					if count ~= 0 then
						ipas = ipas .. ", "
					end
					if termdata.ipa then
						ipas = ipas .. '<span class="IPA">' .. termdata.ipa .. '</span>'
						count = count + 1
					end
				end
			end
			row:tag("td"):node(ipas)
		end
		res:node(row)
	end
	
	
	return res;
end

return export