Modul:Pivot: Unterschied zwischen den Versionen

Aus TobrienWiki
Zur Navigation springen Zur Suche springen
K (Schützte „Modul:Pivot“: Automatisch gesperrt. ([Verschieben=Nur Administratoren erlauben] (unbeschränkt) [Bearbeiten=Nur Administratoren erlauben] (unbeschränkt)))
K (Automatisch synchronisiert.)
Zeile 2: Zeile 2:


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


function getCategory(link,cats)
function getCategory(link,cats)
  local catbp=string.gsub(link,"(%[%[)([^%|]*)(%|.*)","%2")
  local catbp=mw.ustring.gsub(link,"(%[%[)([^%|]*)(%|.*)","%2")
  if catbp==nil then
  if catbp==nil then
  return ""
  return ""
Zeile 64: Zeile 64:
end
end
local l=string.len(result)
local l=mw.ustring.len(result)
if l>=2 then
if l>=2 then
result=string.sub(result,1,l-2)
result=mw.ustring.sub(result,1,l-2)
end
end

Version vom 17. Mai 2021, 15:08 Uhr

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=2019
	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