Modul:Pivot

Aus GaretienWiki
Zur Navigation springen Zur Suche springen
The printable version is no longer supported and may have rendering errors. Please update your browser bookmarks and please use the default browser print function instead.
Dokumentation und Testfälle unter Modul:Pivot/Doku.
local p = {}

function countItems(thedata,tbl,spl,flt)
 local txt=mw.ustring.gsub(thedata,"%[%[SMW::on%]%]","")
 txt=mw.ustring.gsub(txt,"%[%[SMW::off%]%]","")
 local lines=mw.text.split(txt,spl)
 for key, value in pairs(lines) do
 	local v=mw.text.trim(value)
 	if (flt==nil) or (mw.ustring.sub(v,1,mw.ustring.len(flt))==flt) then
 		if v~="" then
 			local a=tbl[v];
 			if a==nil then a=0 end
 			tbl[v]=a+1
 		end
 	end
 end
 return tbl
end

function getCategory(link,cats)
 local catbp=mw.ustring.gsub(link,"(%[%[)([^%|]*)(%|.*)","%2")
 if catbp==nil then
 	return ""
 end
 local title=mw.title.new(catbp)
 if title==nil then
 	return ""
 end
 local titletext=title.text
 return "[[Kategorie:"..titletext.."|"..cats.."]]"
end

function pivotSub(data,data2,spl,srt,flt,bld,cat,out,dif)
 local result=""
 local cats=""
 local tbl={ }
 tbl=countItems(data,tbl,spl,flt)
 tbl=countItems(data2,tbl,spl,flt)
 
 local tbls={ }
 for key, value in pairs(tbl) do
 	table.insert(tbls,{key,value})
 end
 function compareC(a,b)
 	if a[2]~=b[2] then return a[2] > b[2] end
 	return a[1] < b[1]
 end
 function compareT(a,b)
 	if a[1]~=b[1] then return a[1] < b[1] end
 	return a[2] < b[2]
 end
 if srt=="C" then 
 	table.sort(tbls,compareC)
 else
 	table.sort(tbls,compareT)
 end
 
 if out=="L" then
	 for key, value in pairs(tbls) do
	 	local b=""
	 	if tonumber(value[2])>=bld then b="'''" end
	 	result=result..b..value[1].." ("..(value[2]-dif).."x)"..b..", "
	 	cats=cats..getCategory(value[1],cat)
	 end
	
	 local l=mw.ustring.len(result)
	 if l>=2 then
	 	result=mw.ustring.sub(result,1,l-2)
	 end
	
	 if cat~="" then 
	 	result=result..cats
	 end
else
  result="<table>"
  for key, value in pairs(tbls) do
   result=result.."<tr><td>"..value[1].."</td><td><div style=\"background-color:#ffcc00;width:"..((value[2]-dif)*5).."px;display:inline-block;\">&nbsp;</div>&nbsp;"..(value[2]-dif).."</td></tr>"
  end
  result=result.."</table>"
 end
 
 return result   
 
end

function p.countAndSort(frame)
 local data=frame.args[1] --data
 if data==nil then data="" end
 local spl=frame.args[2] --Split character
 if spl==nil then spl="\n" end
 local srt=frame.args[3] --Sort ("C"ount or "T"ype)
 if srt==nil then srt="C"
 	elseif srt~="C" then srt="T" end
 local flt=frame.args[4] --Filter by first character
 local bld=frame.args[5] --Number to apear bold
 if bld==nil then bld=5 else bld=tonumber(bld) end
 local data2=frame.args[6] --additional data (maximum string size)
 if data2==nil then data2="" end
 local cat=frame.args[7] --Categorize?
 if cat==nil then cat="" end
 local out=frame.args[8] --Output format ("L"ist or "D"iagram)
 if out==nil then out="L"
 	elseif out~="L" then out="D" end
 local dif=frame.args[9] --Difference
 if dif==nil then dif=0 else dif=tonumber(dif) end
 return pivotSub(data,data2,spl,srt,flt,bld,cat,out,dif)
end

function p.createEmpty(frame)
	local result=""
	local ya=2025
	for y=2006,ya,1 do
		--for m=1,12,1 do
			--local ms=""
			--if m<10 then
				--ms="0"
			--end
			result=result..y..", " --.."-"..ms..m..", "
		--end
	end
	return result
end


return p