local p = {}

-- ********************************************************** -- Andoza:Jadvalniraqamlash andozasi orqali -- Birinchi jadvaldagi sonni yoʻl raqamiga almashtiradi function p.AutoNumBox2(frame)

   local g = frame:getParent().args[1]
   local NumStr = tonumber (frame:getParent().args['Старт'] or "1");    
   if g == nil then return "Maʻlumot yoʻq" end
   g = g:gsub(".*{|(.*)|}.*", "%1")
   local Titul = g:sub(1, (g:find ("\n!") or 1) - 1)
   g = g:sub((g:find ("\n!") or 1))
   g = g:gsub("||", "\n|")
   local Pos0 = 0
   while g:find("|%-[^|]*(|[^\n]*\n)", Pos0 + 1) ~= nil do
       Pos1, Pos2 = g:find("|%-[^|]*(|[^\n]*\n)", Pos0 + 1)
       while g:find("|", Pos1 + 1) ~= nil and g:find("|", Pos1 + 1) < Pos2 do 
           Pos1 = g:find("|", Pos1 + 1) 
       end    
       if g:find("%d", Pos1) ~= nil and g:find("%d", Pos1) < Pos2 then
           Pos3, Pos4 = g:find("%d+", Pos1)    
           g = g:sub(1, Pos3 - 1)..NumStr..g:sub(Pos4 + 1)
           NumStr = NumStr + 1
       end
       Pos0 = Pos2
   end
   return "{|"..Titul..g.."|}" 

end

-- ********************************************************** -- Jonli chaqirish -- Birinchi jadvaldagi # simvolini yoʻl raqamiga almashtiradi function p.AutoNumBox ( frame )

   local NumStr = tonumber (frame.args['Старт'] or "1");
   local g = frame.args[1]
   g = g:gsub("^%s- -{{", "", 1)
   g = g:gsub("}}%s- -$", "", 1)
   pattern = "|%s*#%s*%s*|"
   while string.find (g, pattern) ~= nil do
       g = g:gsub(pattern, "|"..NumStr.."\n".."|", 1)
       NumStr = NumStr + 1
   end 
   return g

end

-- ********************************************************** function p.AutoNum ( frame )

   local args = frame.args;
   local Style = (frame.args['Koʻrinish'] or "standard");
   local Column = tonumber (frame.args['Ustunlar'] or "0");
   local NumStr = tonumber (frame.args['Старт'] or "1");
   local Title0 = (frame.args['Тақырып0'] or "№")
   local Titles = {}
   for i = 1, Column do
       Titles[i] =(frame.args["Mavzu"..i] or "Mavzu"..i)
   end
   local Width0 = (frame.args['Eni0']);
   local Widths = {}
   for i = 1, Column do
       Widths[i] =(frame.args["Eni"..i])
   end
   local Align0 = (frame.args['Toʻgʻirlash0'] or "center")
   local Aligns = {}
   for i = 1, Column do
       Aligns[i] =(frame.args["Toʻgʻirlash"..i] or "left")
   end

local g="

" -- Mavzular yoʻli g = g..""
Yoʻl soni bilan ustun mavzusi
   g = g.."<th"; if Width0 ~= nil then g = g.." width='"..Width0.."'" end; g = g..">"
g = g..Title0..""
Boshqa mavzular
   for i = 1, Column do
       g = g.."<th"; if Widths[i] ~= nil then g = g.." width='"..Widths[i].."'" end; g = g..">"
g = g..Titles[i].."" end g = g.."" -- Manbalarni olish yoʻllari J = 1 while args[(J-1)*Column+1] ~= nil do g = g..""
       for i = 1, Column do
            if args[(J-1)*Column+i] == nil then
g = g..""
           else
g = g..""
            end  
       end
g = g.."" NumStr = NumStr + 1 J = J + 1 end g = g.."
"..NumStr..""..args[(J-1)*Column+i].."

"

   return g

end

-- ********************************************************** function p.AutoNumSort(frame)

   local args = frame:getParent().args
   local Column = tonumber(args['Ustunlar'] or "0")
       if Column == 0 then return "Ustunlar sonini aniqlash mumkin emas" end 
   local TempSort = (args['Tartiblash'] or "0")
   local SortColumn = tonumber(string.match(TempSort, "%d*")) or 0
       if SortColumn > Column then SortColumn = 0 end
   local SortAsNumber = string.match(TempSort, "#") ~= nil
   local SortDescend 
       if SortAsNumber 
       then SortDescend = string.find(TempSort, "<") == nil
       else SortDescend = string.find(TempSort, ">") ~= nil end
   local Strings={}
   local NumCell = 0
   while args[NumCell + 1] ~= nil or NumCell%Column ~= 0 do 
       NumCell = NumCell + 1
   	if (args[NumCell] == nil) then args[NumCell]="" end
   	-- NumCell%Column==0 значит строка сформирована
   	if NumCell%Column == 0 then
   		local TempString = {NumCell-Column+1}
           if SortAsNumber then 
               KeyCell=args[TempString[1]+SortColumn-1]

KeyCell=mw.ustring.gsub(KeyCell, "<span[^<]+","") KeyCell=mw.ustring.gsub(KeyCell, ",",".") KeyCell=mw.ustring.gsub(KeyCell, " ","") KeyCell=mw.ustring.gsub(KeyCell, " ","") KeyCell=mw.ustring.gsub(KeyCell, "%s","") KeyCell=mw.ustring.match(KeyCell, "^%D*(%d+%.?%d*)") or "" TempString.Key=tonumber(KeyCell)

           elseif SortColumn > 1 then 
               TempString.Key = (args[TempString[1]+SortColumn-1] or "") 
               -- Возможность сортировки по скрытому ключу
               TempString.Key = mw.ustring.gsub(TempString.Key, '', "")
               -- Сортировка ё как е
               TempString.Key = mw.ustring.gsub(TempString.Key, "ё", "е")
               TempString.Key = mw.ustring.gsub(TempString.Key, "Ё", "Е")
               -- trim
               TempString.Key = mw.ustring.gsub(TempString.Key, "^%s*(.-)%s*$", "%1")
           else TempString.Key = tonumber(TempString[1]) end
           TempString.Group = mw.ustring.gsub(args[TempString[1]], "[^a-zA-Zа-яА-Я0-9]", "") 
           table.insert(Strings, TempString)
       end  
   end 
   local function SortString(a, b)  
       if a.Group ~= b.Group then return a.Group < b.Group end
       if SortDescend then a, b = b, a end
       if SortAsNumber or (SortColumn < 2) then
           return a.Key < b.Key 
       else
           local LenA = mw.ustring.len(a.Key)
           local LenB = mw.ustring.len(b.Key)
           for i = 1, (LenA < LenB) and LenA or LenB do
               if mw.ustring.codepoint(a.Key, i, i) ~= mw.ustring.codepoint(b.Key, i, i) then
                   return mw.ustring.codepoint(a.Key, i, i) < mw.ustring.codepoint(b.Key, i, i) 
               end
           end
           return LenA < LenB
       end            
   end  

-- Guruhlar va kalt boʻyicha ajratish

   table.sort(Strings, SortString)

-- HTML-jadvalini tuzish

   local HTML = mw.html.create('table')
   HTML:attr('class', (args['Koʻrinish'] or "standard"))

-- Nomlanishi

   if args['Nomi'] then

TempCaption=HTML:tag('th') :wikitext(args['Nomi']) :attr('colspan', Column)

   end

-- Mavzular yoʻli

   local TempRow
   TempRow = HTML:tag('tr')
   TempRow:tag('th')
       :css('width', (args['Eni1'] or ""))
       :wikitext(args['Mavzu1'] or "№")
   for i = 2, Column do
       TempColumn=TempRow:tag('th')
       TempColumn:css('width', (args["Eni"..i] or ""))
       TempColumn:wikitext(args["Mavzu"..i] or " Mavzu"..i)

if args["Сортировка"..i]=="sortlashsiz" then TempColumn:addClass('unsortable') elseif args["Сортировка"..i]=="hafta kuni" then TempColumn:attr('data-sort-type', 'number') elseif args["Сортировка"..i]=="kuni" then TempColumn:attr('data-sort-type', 'date') end end

-- Manbalarni berish yoʻllari

   local HideNum = 0

local NumStr = tonumber (args['Старт'] or "1") - 1

   local Backlight = (args['Подсветка'] or "class='bright'"):gsub("\"", "'")

for Index, TempString in pairs(Strings) do

       TempRow = HTML:tag('tr')
       local first = args[TempString[1]]

-- Yoʻllar stili

       if string.find(first, "%$") ~= nil then TempRow:attr('class', 'shadow')
       elseif string.find(first, "%%") ~= nil then TempRow:attr('class', 'bright')
       elseif string.find(first, "@") ~= nil then TempRow:attr('class', 'dark')
       elseif string.find(first, "%+") ~= nil then 
           for Part in string.gmatch(Backlight, "[%w]*=%s*'[^']*'") do
               TempRow:attr(string.match(Part, "([^=]*)="), string.match(Part, "'([^']*)'")) 
           end
       end
       if string.find(first, "!") ~= nil then TempRow:attr('style', 'font-weight:bold') end

-- Yoʻl raqamini chiqarish

       if string.find(first, "-") ~= nil then 
           HideNum = HideNum + 0.000001
           TempRow:tag('td')
               :tag('span')
               :attr('style', 'display: none; speak: none;')
               :wikitext( tonumber(NumStr + HideNum))
       else
           NumStr = NumStr + 1
           TempRow:tag('td')
               :attr('align', (args['Toʻgʻirlash1'] or "center") )
               :wikitext(tonumber(NumStr))
       end

-- Вывод ячеек строки for i = 2, Column do

           TempTd=TempRow:tag('td')
           KeyCell=""

if args["Сортировка"..i]=="hafta kuni" then KeyCell=mw.ustring.gsub(args[TempString[1]+i-1], ",",".") KeyCell=mw.ustring.gsub(KeyCell, " ","") KeyCell=mw.ustring.gsub(KeyCell, "<span[^<]+","") KeyCell=mw.ustring.gsub(KeyCell, "%s","") KeyCell=mw.ustring.match(KeyCell, "^%D*(%d+%.?%d*)") or "" TempTd:attr('data-sort-value', KeyCell) end

           TempTd:wikitext(args[TempString[1]+i-1])
           TempTd:attr('align', args["Toʻgʻirlash"..i] or "left")

end

   end 
   return tostring(HTML)

end

return p