Przejdź do zawartości

Moduł:Sandbox/Draco flavus/get index pages

Z Wikiźródeł, wolnej biblioteki

Dokumentacja dla tego modułu może zostać utworzona pod nazwą Moduł:Sandbox/Draco flavus/get index pages/opis

local p = {}

-- w tym module jest kilka funkcji związanych z emulacją tagów <pages> i <pagelist>
-- folio, foliohighroman, folioroman zaimplementowane inaczej niż w oryginale (nie do końca kompatybilne)
-- etykiety tekstowe stron (np. tytuł) działają również jeśli są podawane nie w kolejności (w oryginale mamy tu bardzo dziwne efekty)
-- definicja może być w kawałkach (kilka tagów <pagelist from=... to=.../> <pagelist from=... to=.../> <pagelist from=... to=.../>
-- zaimplementowane jest odd, even, XtoY, renumeracja (X=Y), roman, normal, highroman
-- empty jest po prostu pomijane
-- nie działają różne takie egzotyczne beng deva tamldec guru gujr telu knda mlym orya thai
-- --------------------
-- funkcja (prywatna) pages_from_index(arg) NAZWA_INDEKSU → tabela_ze_stronami, tabela_z_etykietami_stron, typ_indeksu
-- funkcja testowa drukuj jest wrapperem i służy do wywołania tabel zwracanych przez pages_from_index
-- wywołanie {{#invoke:Sandbox/Draco flavus/get index pages|drukuj|PL JI Kraszewski Przygody Stacha.djvu}}
-- ostatni argument to nazwa indeksu bez przestrzeni nazw
-- --------------------
-- funkcja (prywatna) pages_from_range(arg) string → tbl_lista_stron, tbl_etykiety_stron, argument_fromsection, argument_tosection, argument_onlysection
-- funkcja testowa drukuj2 jest wrapperem i służy do wywołania tabel zwracanych przez pages_from_range
-- wywołanie {{#invoke:Sandbox/Draco flavus/get index pages|drukuj2|index=PL JI Kraszewski Przygody Stacha.djvu from=24 to=27 fromsection=r05 }}
-- ostatni argument to praktycznie tag pages bez nawiasów ostrokątnych
-- --------------------
-- funkcja (prywatna) sklej(arg) string → tekst zakresu stron z dołączonymi znacznikami 
-- funkcja testowa drukuj3 jest wrapperem i służy do zwracania tekstu uzyskiwanego przez sklej
-- wywołanie {{#invoke:Sandbox/Draco flavus/get index pages|drukuj3|index=PL JI Kraszewski Przygody Stacha.djvu from=24 to=27 fromsection=r05 }}
-- ostatni argument to praktycznie tag pages bez nawiasów ostrokątnych
-- daje sobie radę nawet z kreskami na końcu strony
-- --------------------
-- funkcja (prywatna) zestawienie(arg) string → kod transkludujący dane strony (lub fragmenty) oraz znaczniki 
-- funkcja testowa drukuj4 jest wrapperem i służy do zwracania tekstu uzyskiwanego przez zestawienie
-- wywołanie {{#invoke:Sandbox/Draco flavus/get index pages|drukuj4|index=PL JI Kraszewski Przygody Stacha.djvu from=24 to=27 fromsection=r05 }}
-- ostatni argument to praktycznie tag pages bez nawiasów ostrokątnych
-- NIE daje sobie rady jednak z kreskami na końcu strony
-- --------------------
-- funkcja testowa drukuj5 jest wrapperem i służy do zwracania tekstu uzyskiwanego przez zestawienie jednak daje kod substytuujący
-- wywołanie {{#invoke:Sandbox/Draco flavus/get index pages|drukuj5|index=PL JI Kraszewski Przygody Stacha.djvu from=24 to=27 fromsection=r05 }}
-- ostatni argument to praktycznie tag pages bez nawiasów ostrokątnych
-- NIE daje sobie rady z kreskami na końcu strony
-- --------------------
-- funkcja (prywatna) ContainsRef(arg) string → TAK/NIE (czy dany zakres zawiera <ref> itp.?)
-- funkcja testowa drukuj6 jest wrapperem i służy do zwracania wyniku uzyskiwanego przez ContainsRef
-- wywołanie {{#invoke:Sandbox/Draco flavus/get index pages|drukuj6|index=PL JI Kraszewski Przygody Stacha.djvu from=24 to=27 fromsection=r05 }}
-- w przypadku gdy zawiera <ref> (lub tp.) drukuj6 daje string Przypisy w przeciwnym wypadku Void
-- ostatni argument to praktycznie tag pages bez nawiasów ostrokątnych
-- --------------------
-- funkcja (prywatna) informat(p, q) formatuje liczbę p zgodnie z formatem 
-- używa przy tym funkcji(prywatnej) ToRoman(num) i ToHighRoman(num)
-- ToRoman konwertuje do liczb rzymskich pisanych małymi literami
-- ToHighRoman konwertuje do liczb rzymskich pisanych wersalikami
-- funkcja escape_lua_pattern() jest niewykorzystywana obecnie (będzie do usunięcia)
-- --------------------
-- funkcja (prywatna) test_na_kreske(ptag, inv) zwraca zestawienie stron, na których jest dywiz na końcu (przenoszony wyraz)
-- dolna część zestawienia to gotowe komendy dla pywikibota
-- funkcja drukuj7(1=..., 2=...) to wrapper do wywołania test_na_kreske(ptag, inv)
-- przykładowe wywołanie:
-- {{#invoke: Sandbox/Draco flavus/get index pages 
--             | drukuj7 | 1=index="PL A Dumas Czterdziestu pięciu.djvu" from=70 to=73
--             |2=python3.8 pwb replace -user:"DracoBot" -site:wikisource:pl -pt:0 -always -summary:"Bot zmienia dywizy na pp/pk"}}
-- --------------------



local escape_lua_pattern
do
  local matches =
  {
    ["^"] = "%^";
    ["$"] = "%$";
    ["("] = "%(";
    [")"] = "%)";
    ["%"] = "%%";
    ["."] = "%.";
    ["["] = "%[";
    ["]"] = "%]";
    ["*"] = "%*";
    ["+"] = "%+";
    ["-"] = "%-";
    ["?"] = "%?";
  }

  escape_lua_pattern = function(s)
    return (s:gsub(".", matches))
  end
end

local HighRomans = {
    { 1000, 'M' },
    { 900, 'CM' }, { 500, 'D' }, { 400, 'CD' }, { 100, 'C' },
    {  90, 'XC' }, {  50, 'L' }, {  40, 'XL' }, {  10, 'X' },
    {   9, 'IX' }, {   5, 'V' }, {   4, 'IV' }, {   1, 'I' }
}
local Romans = {
    { 1000, 'm' },
    { 900, 'cm' }, { 500, 'd' }, { 400, 'cd' }, { 100, 'c' },
    {  90, 'xc' }, {  50, 'l' }, {  40, 'xl' }, {  10, 'x' },
    {   9, 'ix' }, {   5, 'v' }, {   4, 'iv' }, {   1, 'i' }
}


local function ToHighRoman(num)
    local ret = {}
    for _, v in ipairs(HighRomans) do
        local val, letter = unpack(v)
        while num >= val do
            num = num - val
            table.insert(ret, letter)
        end
    end
    return table.concat(ret)
end

local function ToRoman(num)
    local ret = {}
    for _, v in ipairs(Romans) do
        local val, letter = unpack(v)
        while num >= val do
            num = num - val
            table.insert(ret, letter)
        end
    end
    return table.concat(ret)
end
local function informat(p, q)
	local strona = tonumber(p)
	local aspekt = q
	local Y = ''
	if aspekt == "normal" then
		Y = tostring(strona)
	elseif aspekt == "roman" then
		Y = ToRoman(strona)
	elseif aspekt == "highroman" then
		Y = ToHighRoman(strona)
	elseif aspekt == "folio" then
		Y = tostring((strona + strona % 2) / 2) .. ((strona % 2 == 0) and 'ᵛ' or 'ʳ')
	elseif aspekt == "folioroman" then
		Y = ToRoman((strona + strona % 2) / 2) .. ((strona % 2 == 0) and 'ᵛ' or 'ʳ')
	elseif aspekt == "foliohighroman" then
		Y = ToHighRoman((strona + strona % 2) / 2) .. ((strona % 2 == 0) and 'ᵛ' or 'ʳ')
	else
		Y = aspekt
	end
	return Y
end


local function pages_from_index(arg)
	local index_typ = false
	local index_strony = {}
	local index_etykiety = {}
	local itxt = ''
	local test_abc = ''
	local maximal
	local t_arg = {}
	local i
	local j
	local numeracja = {}
	local etykiety = {}
	local a1
	local a2
	local a3
	local w
	
	itxt = mw.title.makeTitle('Indeks', arg):getContent()                       -- tekst strony indeksowej
	itxt = itxt:gsub('^.*\n|Strony=(.*)\n|Spis treści=.*$', '%1')		        -- tylko fragment z linkami do stron
	if itxt:find("<pagelist[/ \n]") then
		-- nowy typ
		index_typ = true
		local title = mw.title.new('File:' .. arg)
		maximal = #title.file.pages
		for i = 1, maximal do
			numeracja[i] = i
			etykiety[i] = "normal"
			w = arg:gsub("_", " ")
			index_strony[i] = w .. "/" .. i
		end
		
		itxt = itxt:gsub("\n", " ")
		itxt = itxt:gsub(".-(<pagelist[ \n].-/>)[^<]*", "%1")
		itxt = itxt:gsub("<pagelist", " from=1 to=" .. maximal .. " ")
		itxt = itxt:gsub("/>", " ")
		itxt = itxt:gsub(" ([fFrRoOmMtTdDeEvVnN0-9]+) *= *' *([^>']*) *'", " %1=>%2> ")
		itxt = itxt:gsub(' ([fFrRoOmMtTdDeEvVnN0-9]+) *= *" *([^>"]*) *"', " %1=>%2> ")
		itxt = itxt:gsub(' ([fFrRoOmMtTdDeEvVnN0-9]+) *= *([^>"\' ]+)', " %1=>%2> ")
		itxt = itxt:gsub(' *>', ">")
		for w in itxt:gmatch(" [fFrRoOmMtTdDeEvVnN0-9]+=>[^>]*>") do
			if w:match(" [fF][rR][oO][mM]=>%d+>") then
				-- first = tonumber(w:match("=>(%d*)>"))                        -- chyba zbędne, from, to jest nieużywane
			elseif w:match(" [tT][oO]=>%d+>") then
				-- last = tonumber(w:match("=>(%d*)>"))                         -- chyba zbędne, from, to jest nieużywane
			else
				table.insert(t_arg, w )                                         -- zawiera wszystkie parametry tagów <pagelist /> z wyjątkiem from i to
			end
		end

		for _, w in ipairs(t_arg) do
			if w:match(" %d+=>%d+>") then                                                                 -- 7=23 (renumeracja)
				a1 = tonumber(w:match(" (%d+)=>"))
				a2 = tonumber(w:match("=>(%d+)>"))
				j = 0
				for i = a1, maximal, 1 do
					numeracja[i] = a2 + j
					j = j + 1
				end
			elseif w:match(" %d+=>empty+>") then                                                          -- 9=empty
			elseif w:match(" %d+[tT][oO]%d+=>empty+>") then                                               -- 27to36=empty
			elseif w:match(" %d+[tT][oO]%d+[oO][dD][dD]=>empty+>") then                                   -- 27to36odd=empty
			elseif w:match(" %d+[tT][oO]%d+[eE][vV][eE][nN]=>empty+>") then                               -- 27to36even=empty
			elseif w:match(" %d+=>[^>]+>") then                                                           -- 9=roman (formatowanie, etykieta pojedynczej strony)
				a1 = tonumber(w:match(" (%d+)=>"))
				a2 = w:match("=>([^>]+)>")
				if a1 >= 1 and a1 <= maximal then
					etykiety[a1] = a2
				end
			elseif w:match(" %d+[tT][oO]%d+=>[^>]+>") then                                                -- 27to36=highroman (formatowanie, etykieta zakresu)
				a1 = tonumber(w:match("^ (%d+)[tT]"))
				a2 = tonumber(w:match("^ %d+[tT][oO](%d+)=>"))
				a3 = w:match("=>([^>]+)>")
				for i = ((a1 >= 1) and a1 or 1), ((a2 <= maximal) and a2 or maximal), 1 do
					etykiety[i] = a3
				end
			elseif w:match(" %d+[tT][oO]%d+[oO][dD][dD]=>[^>]+>") then                                    -- 34to51odd=roman (formatowanie, etykieta zakresu nieparzystych)
				a1 = tonumber(w:match("^ (%d+)[tT]"))
				a2 = tonumber(w:match("^ %d+[tT][oO](%d+)[oO][dD][dD]=>"))
				a3 = w:match("=>([^>]+)>")
				for i = ((a1 >= 1) and (a1 + 1 - a1 % 2) or 1), ((a2 <= maximal) and a2 or maximal), 2 do
					etykiety[i] = a3
				end
			elseif w:match(" %d+[tT][oO]%d+[eE][vV][eE][nN]=>[^>]+>") then                                 -- 34to51even=roman (formatowanie, etykieta zakresu parzystych)
				a1 = tonumber(w:match("^ (%d+)[tT]"))
				a2 = tonumber(w:match("^ %d+[tT][oO](%d+)[eE][vV][eE][nN]=>"))
				a3 = w:match("=>([^>]+)>")
				for i = ((a1 >= 1) and (a1 + a1 % 2) or 2), ((a2 <= maximal) and a2 or maximal), 2 do
					etykiety[i] = a3
				end
			end
		end
		for i = 1, maximal do
			index_etykiety[i] = informat(numeracja[i], etykiety[i])
		end

	else
		-- stary typ
		itxt = itxt:gsub("%[%[[Pp][Aa][Gg][Ee]:", "[[Strona:")				-- Page może być również używane na określenie przestrzeni
		itxt = itxt:gsub("%[%[[Ss][Tt][Rr][Oo][Nn][Aa]:", "[[Strona:")		-- tak samo Strona
		for i in itxt:gmatch("%[%[Strona:.-%]%]") do
			w = i:match("%[%[Strona:(.-)|"):gsub("_", " ")					-- bez Strona:  , spacje zamiast podkreśleń
			table.insert(index_strony, w)
			table.insert(index_etykiety, i:match("%[%[.-|(.-)%]%]"))
		end
	end
	return index_strony, index_etykiety, index_typ
end

local function pages_from_range(arg)
	-- funkcja przygotowuje listę stron do opracowania (transkluzji)
	-- argumentem jest string sformatowany tak jak tag pages
	
	local from_arg = ''
	local to_arg = ''
	local include_arg = ''
	local exclude_arg = ''
	local fromsection_arg = nil
	local tosection_arg = nil
	local onlysection_arg = nil
	local step_arg = ''
	local index_arg = ''
	local matching = ''
	local itxt = ''
	local lista_stron = {}
	local etykiety_stron = {}
	local typ_indeksu = false
	local strona_in_range = {}
	local i = 0
	local first_page
	local last_page
	local modulo_reminder
	local lista_stron_r = {}
	local etykiety_stron_r = {}
	
	itxt = tostring(arg)
	itxt = itxt:gsub('\n', ' ')
	itxt = " " .. itxt .. " "
	-- ta długa sekcja służy wyekstrahowaniu właściwych argumentów 
	-- (index_arg, from_arg, to_arg, include_arg, exclude_arg, from_section_arg, tosection_arg, onlysection_arg, step_arg)
	itxt = itxt:gsub(" [fF][rR][oO][mM] *= *' *([^>']*) *'", " from=>%1> ")
	itxt = itxt:gsub(' [fF][rR][oO][mM] *= *" *([^>"]*) *"', " from=>%1> ")
	itxt = itxt:gsub(' [fF][rR][oO][mM] *= *([^>"\' ]+)', " from=>%1> ")
	itxt = itxt:gsub(" [tT][oO] *= *' *([^>']*) *'", " to=>%1> ")
	itxt = itxt:gsub(' [tT][oO] *= *" *([^>"]*) *"', " to=>%1> ")
	itxt = itxt:gsub(' [tT][oO] *= *([^>"\' ]+)', " to=>%1> ")
	itxt = itxt:gsub(" [iI][nN][cC][lL][uU][dD][eE] *= *' *([^>']*) *'", " include=>%1> ")
	itxt = itxt:gsub(' [iI][nN][cC][lL][uU][dD][eE] *= *" *([^>"]*) *"', " include=>%1> ")
	itxt = itxt:gsub(' [iI][nN][cC][lL][uU][dD][eE] *= *([^>"\' ]+)', " include=>%1> ")
	itxt = itxt:gsub(" [eE][xX][cC][lL][uU][dD][eE] *= *' *([^>']*) *'", " exclude=>%1> ")
	itxt = itxt:gsub(' [eE][xX][cC][lL][uU][dD][eE] *= *" *([^>"]*) *"', " exclude=>%1> ")
	itxt = itxt:gsub(' [eE][xX][cC][lL][uU][dD][eE] *= *([^>"\' ]+)', " exclude=>%1> ")
	itxt = itxt:gsub(" [fF][rR][oO][mM][sS][eE][cC][tT][iI][oO][nN] *= *' *([^>']*) *'", " fromsection=>%1> ")
	itxt = itxt:gsub(' [fF][rR][oO][mM][sS][eE][cC][tT][iI][oO][nN] *= *" *([^>"]*) *"', " fromsection=>%1> ")
	itxt = itxt:gsub(' [fF][rR][oO][mM][sS][eE][cC][tT][iI][oO][nN] *= *([^>"\' ]+)', " fromsection=>%1> ")
	itxt = itxt:gsub(" [tT][oO][sS][eE][cC][tT][iI][oO][nN] *= *' *([^>']*) *'", " tosection=>%1> ")
	itxt = itxt:gsub(' [tT][oO][sS][eE][cC][tT][iI][oO][nN] *= *" *([^>"]*) *"', " tosection=>%1> ")
	itxt = itxt:gsub(' [tT][oO][sS][eE][cC][tT][iI][oO][nN] *= *([^>"\' ]+)', " tosection=>%1> ")
	itxt = itxt:gsub(" [oO][nN][lL][yY][sS][eE][cC][tT][iI][oO][nN] *= *' *([^>']*) *'", " onlysection=>%1> ")
	itxt = itxt:gsub(' [oO][nN][lL][yY][sS][eE][cC][tT][iI][oO][nN] *= *" *([^>"]*) *"', " onlysection=>%1> ")
	itxt = itxt:gsub(' [oO][nN][lL][yY][sS][eE][cC][tT][iI][oO][nN] *= *([^>"\' ]+)', " onlysection=>%1> ")
	itxt = itxt:gsub(" [sS][tT][eE][pP] *= *' *([^>']*) *'", " step=>%1> ")
	itxt = itxt:gsub(' [sS][tT][eE][pP] *= *" *([^>"]*) *"', " step=>%1> ")
	itxt = itxt:gsub(' [sS][tT][eE][pP] *= *([^>"\' ]+)', " step=>%1> ")
	itxt = itxt:gsub(" [iI][nN][dD][eE][xX] *= *' *([^>']*) *'", " index=>%1> ")
	itxt = itxt:gsub(' [iI][nN][dD][eE][xX] *= *" *([^>"]*) *"', " index=>%1> ")
	itxt = itxt:gsub(' [iI][nN][dD][eE][xX] *= *([^>"\' ]+)', " index=>%1> ")
	itxt = itxt:gsub(' *>', ">")
	for matching in itxt:gmatch("index=>([^>]*)>") do
		index_arg = matching
	end
	for matching in itxt:gmatch("from=>([^>]*)>") do
		from_arg = matching
	end
	for matching in itxt:gmatch("to=>([^>]*)>") do
		to_arg = matching
	end
	for matching in itxt:gmatch("include=>([^>]*)>") do
		include_arg = matching
	end
	for matching in itxt:gmatch("exclude=>([^>]*)>") do
		exclude_arg = matching
	end
	for matching in itxt:gmatch("fromsection=>([^>]*)>") do
		fromsection_arg = matching
	end
	for matching in itxt:gmatch("tosection=>([^>]*)>") do
		tosection_arg = matching
	end
	for matching in itxt:gmatch("onlysection=>([^>]*)>") do
		onlysection_arg = matching
	end
	for matching in itxt:gmatch("step=>([^>]*)>") do
		step_arg = matching
	end
	-- argumenty są już wydzielone
	
	-- pobieramy listę stron, etykiety stron i typ indeksu, dodatkowa lista określa czy strona jest w zakresie (jak funkcja charakterystyczna)
	lista_stron, etykiety_stron, typ_indeksu = pages_from_index(index_arg)
	for i = 1, #lista_stron, 1 do
		strona_in_range[i] = false
	end
	--
	
	if typ_indeksu then
		-- true oznacza indeks nowego typu, ta sekcja dotyczy opracowania argumentów dla indeksu nowego typu
		if include_arg == '' and exclude_arg == '' then
			-- tylko from-to
			if from_arg == '' and to_arg == '' then
				-- BŁĄD - co najmniej jeden argument musi być podany
			end
			if from_arg == '' and to_arg == '' then
				from_arg = 1
			end
			if from_arg ~= '' and to_arg == '' then
				to_arg = #strona_in_range
			end
			for i = from_arg, to_arg, 1 do
				strona_in_range[i] = true
			end
			first_page = from_arg
		else
			if from_arg == '' and to_arg ~= '' then
				from_arg = 1
			end
			if from_arg ~= '' and to_arg == '' then
				to = #strona_in_range
			end
			if from_arg == '' and to_arg == '' then
				if include_arg == '' then
					-- BŁĄD - co najmniej jeden z nich (from, to, include) musi być podany
				else
				end
			else
				for i = from_arg, to_arg, 1 do
					strona_in_range[i] = true
				end
			end
			if include_arg ~= '' then
				include_arg = include_arg:gsub('["\n ]', '')
				include_arg = include_arg:gsub(',', ',,')
				include_arg = ',' .. include_arg .. ','
				include_arg = include_arg:gsub(',(%d+)%-,', ',%1-' .. #strona_in_range .. ',')
				include_arg = include_arg:gsub(',%-(%d+),', ',1-%1,')
				include_arg = include_arg:gsub(',(%d+),', ',%1-%1,')
				for p, q in include_arg:gmatch('(%d+)-(%d+)') do
					if tonumber(q) > #strona_in_range then
						--  BŁĄD - liczba poza zakresem
					end
					for i = tonumber(p), tonumber(q), 1 do
						strona_in_range[i] = true
					end
				end
			end
			if exclude_arg ~= '' then
				exclude_arg = exclude_arg:gsub('["\n ]', '')
				exclude_arg = exclude_arg:gsub(',', ',,')
				exclude_arg = ',' .. exclude_arg .. ','
				exclude_arg = exclude_arg:gsub(',(%d+)%-,', ',%1-' .. #strona_in_range .. ',')
				exclude_arg = exclude_arg:gsub(',%-(%d+),', ',1-%1,')
				exclude_arg = exclude_arg:gsub(',(%d+),', ',%1-%1,')
				for p, q in exclude_arg:gmatch('(%d+)-(%d+)') do
					if tonumber(q) > #strona_in_range then
						--  BŁĄD - liczba poza zakresem
					end
					for i = tonumber(p), tonumber(q), 1 do
						strona_in_range[i] = false
					end
				end
			end
			i = 1
			while strona_in_range[i] == false do
				i = i + 1
			end
			first_page = i
		end
		if step_arg == '' then
			step_arg = 1
		end
		modulo_reminder = first_page % step_arg
		last_page = first_page
		for i = first_page, #strona_in_range, 1 do
			if i % step_arg == modulo_reminder then
				if strona_in_range[i] then
					last_page = i
				end
			else
				strona_in_range[i] = false
			end
		end
		-- koniec sekcji dot. opracowywania indeksu nowego typu
	else
		-- false oznacza indeks starego typu, ta sekcja dotyczy opracowania argumentów dla indeksu starego typu
		if from_arg == nil and to_arg == nil then
			-- BŁĄD - co najmniej jeden musi być podany
		elseif from_arg == nil and to_arg ~= nil then
			from_arg = lista_stron[1]
			to_arg = to_arg:gsub("_", " ")
		elseif from_arg ~= nil and to_arg == nil then
			from_arg = from_arg:gsub("_", " ")
			to_arg = lista_stron[#lista_stron]
		else
			from_arg = from_arg:gsub("_", " ")
			to_arg = to_arg:gsub("_", " ")
		end
		i=1
		while from_arg ~= lista_stron[i] and i <= #lista_stron do
			i = i + 1
		end
		if i > #lista_stron then
			-- BŁĄD - nie ma strony from w indeksie
		else
			first_page = i
		end
		while to_arg ~= lista_stron[i] and i <= #lista_stron do
			i = i + 1
		end
		if i > #lista_stron then
			-- BŁĄD - nie ma strony to w indeksie
		else
			last_page = i
		end
		if first_page > last_page then
			-- BŁĄD - strona to przed stroną from w indeksie
		else
			for i = first_page, last_page, 1 do
				strona_in_range[i] = true
			end
		end
		-- koniec sekcji dot. opracowywania indeksu nowego typu
	end
	for i = first_page, last_page, 1 do
		if strona_in_range[i] then
			table.insert(lista_stron_r, lista_stron[i])
			table.insert(etykiety_stron_r, etykiety_stron[i])
		end
	end
	return lista_stron_r, etykiety_stron_r, fromsection_arg, tosection_arg, onlysection_arg
	
end

local function get_firstpage(analizowana_strona, lp, fs, ts)
	local strona_r
	local temp_text = ''
	strona_r = mw.title.makeTitle('Page', analizowana_strona):getContent()
	strona_r = strona_r:gsub('<[nN][oO][iI][nN][cC][lL][uU][dD][eE]>.-</[nN][oO][iI][nN][cC][lL][uU][dD][eE]>', '')
	strona_r = strona_r:gsub('</?[iI][nN][cC][lL][uU][dD][eE][oO][nN][lL][yY]>', '')
	strona_r = strona_r:gsub('^.-<[oO][nN][lL][yY][iI][nN][cC][lL][uU][dD][eE]>', '')
	strona_r = strona_r:gsub('</[oO][nN][lL][yY][iI][nN][cC][lL][uU][dD][eE]>.-<[oO][nN][lL][yY][iI][nN][cC][lL][uU][dD][eE]>', '')
	strona_r = strona_r:gsub('</[oO][nN][lL][yY][iI][nN][cC][lL][uU][dD][eE]>.-$', '')
	strona_r = strona_r:gsub('<!%-%-.-%-%->', '')

	if analizowana_strona ~= lp then
		if fs == nil then
			-- nic (jak get_page)
		else
			strona_r = strona_r:gsub("<[sS][eE][cC][tT][iI][oO][nN]  *[bB][eE][gG][iI][nN] *= *'" .. fs .."' */>", '<section begin="' .. fs .. '"/>')
			if strona_r:find('<[sS][eE][cC][tT][iI][oO][nN]  *[bB][eE][gG][iI][nN] *= *"?' .. fs ..'"? */>') then
				strona_r = strona_r:gsub('(<[sS][eE][cC][tT][iI][oO][nN]  *[bB][eE][gG][iI][nN] *= *"?' .. fs ..'"? */>.*)', '%1')
			else
				-- BŁĄD brak sekcji fromsection na pierwszej stronie
				-- oryginał po prostu nie bierze nic
				strona_r = ''
			end
			strona_r = strona_r:gsub('<[sS][eE][cC][tT][iI][oO][nN] .-/>', '')                  -- w tym wypadku wszystkie znaczniki sekcji są wycinane przed łączeniem stron
		end
		if strona_r:sub(-1) == '-' then
			strona_r = strona_r:gsub('%-$', '')
		else
			strona_r = strona_r .. ' '
		end
	else
		strona_r = strona_r:gsub("<[sS][eE][cC][tT][iI][oO][nN] +[bB][eE][gG][iI][nN] *= *'" .. fs .."' */>", '<section begin="' .. fs .. '"/>')
		strona_r = strona_r:gsub("<[sS][eE][cC][tT][iI][oO][nN] +[eE][nN][dD] *= *'" .. ts .."' */>", '<section end="' .. ts .. '"/>')
		if  fs == nil and ts == nil then
			-- nic (jak get_page)
		elseif fs ~= ts then
			if strona_r:find('<[sS][eE][cC][tT][iI][oO][nN] +[eE][nN][dD] *= *"?' .. fs ..'"? */>') then
				strona_r = strona_r .. '<section end="' .. ts .. '"/>'
				for i in strona_r:gsub('<[sS][eE][cC][tT][iI][oO][nN] +[bB][eE][gG][iI][nN] *= *"?' .. fs .. '"? */>(.-)<[sS][eE][cC][tT][iI][oO][nN] +[eE][nN][dD] *= *"?' .. ts .. '"? */>' ) do
					temp_text = temp_text .. i
				end
				strona_r = temp_text
			else
				-- BŁĄD brak sekcji fromsection na jedynej stronie
				-- oryginał po prostu po prostu nie bierze nic
				strona_r = ''
			end
		else
			if strona_r:find('<[sS][eE][cC][tT][iI][oO][nN] +[eE][nN][dD] *= *"?' .. fs ..'"? */>') then
				strona_r = strona_r:gsub('^.-<[sS][eE][cC][tT][iI][oO][nN] +[bB][eE][gG][iI][nN] *= *"?' .. fs .. '"? */>', '<section begin="' .. fs .. '"/>')
				strona_r = strona_r:gsub('<[sS][eE][cC][tT][iI][oO][nN] +[eE][nN][dD] *= *"?' .. fs .. '"? */>.-<[sS][eE][cC][tT][iI][oO][nN] +[bB][eE][gG][iI][nN] *= *"?' .. fs .. '"? */>', '<section end="' .. fs .. '"/><section begin="' .. fs .. '"/>')
				strona_r = strona_r:gsub('<[sS][eE][cC][tT][iI][oO][nN] +[eE][nN][dD] *= *"?' .. fs .. '"? */>.-$', '<section end="' .. fs .. '"/>')
			else
				-- BŁĄD brak sekcji from/tosection na jedynej stronie
				-- oryginał po prostu po prostu nie bierze nic
				strona_r = ''
			end
		end
	end
	strona_r = strona_r:gsub('<[sS][eE][cC][tT][iI][oO][nN] .-/>', '')
	return strona_r	
end
local function get_page(analizowana_strona)
	local strona_r
	strona_r = mw.title.makeTitle('Page', analizowana_strona):getContent()
	strona_r = strona_r:gsub('<[nN][oO][iI][nN][cC][lL][uU][dD][eE]>.-</[nN][oO][iI][nN][cC][lL][uU][dD][eE]>', '')
	strona_r = strona_r:gsub('</?[iI][nN][cC][lL][uU][dD][eE][oO][nN][lL][yY]>', '')
	strona_r = strona_r:gsub('^.-<[oO][nN][lL][yY][iI][nN][cC][lL][uU][dD][eE]>', '')
	strona_r = strona_r:gsub('</[oO][nN][lL][yY][iI][nN][cC][lL][uU][dD][eE]>.-<[oO][nN][lL][yY][iI][nN][cC][lL][uU][dD][eE]>', '')
	strona_r = strona_r:gsub('</[oO][nN][lL][yY][iI][nN][cC][lL][uU][dD][eE]>.-$', '')
	strona_r = strona_r:gsub('<!%-%-.-%-%->', '')
	-- standardowe usunięcie zbędnych fragmentów
	if strona_r:sub(-1) == '-' then
		strona_r = strona_r:gsub('%-$', '')
	else
		strona_r = strona_r .. ' '
	end
	strona_r = strona_r:gsub('<[sS][eE][cC][tT][iI][oO][nN] .-/>', '')
	return strona_r
end
local function get_lastpage(analizowana_strona, ts)
	local strona_r
	strona_r = mw.title.makeTitle('Page', analizowana_strona):getContent()
	strona_r = strona_r:gsub('<[nN][oO][iI][nN][cC][lL][uU][dD][eE]>.-</[nN][oO][iI][nN][cC][lL][uU][dD][eE]>', '')
	strona_r = strona_r:gsub('</?[iI][nN][cC][lL][uU][dD][eE][oO][nN][lL][yY]>', '')
	strona_r = strona_r:gsub('^.-<[oO][nN][lL][yY][iI][nN][cC][lL][uU][dD][eE]>', '')
	strona_r = strona_r:gsub('</[oO][nN][lL][yY][iI][nN][cC][lL][uU][dD][eE]>.-<[oO][nN][lL][yY][iI][nN][cC][lL][uU][dD][eE]>', '')
	strona_r = strona_r:gsub('</[oO][nN][lL][yY][iI][nN][cC][lL][uU][dD][eE]>.-$', '')
	strona_r = strona_r:gsub('<!%-%-.-%-%->', '')
	if ts == nil then
		-- nic (jak get_page)
	else
		strona_r = strona_r:gsub("<[sS][eE][cC][tT][iI][oO][nN] +[eE][nN][dD] *= *'" .. ts .."' */>", '<section end="' .. ts .. '"/>')
		if strona_r:find('<[sS][eE][cC][tT][iI][oO][nN] +[eE][nN][dD] *= *"?' .. ts ..'"? */>') then
			strona_r = strona_r:gsub('<[sS][eE][cC][tT][iI][oO][nN] +[eE][nN][dD] *= *"?' .. ts ..'"? */>.*', '<section end="' .. ts .. '"/>')
		else
			-- BŁĄD brak sekcji tosection na ostatniej stronie
			-- oryginał po prostu bierze całą stronę
		end
	end	
	-- usuwanie łącznika i dodawanie spacji zbędne
	strona_r = strona_r:gsub('<[sS][eE][cC][tT][iI][oO][nN] .-/>', '')
	
	return strona_r
end
local function get_onlysectionpage(analizowana_strona, ons)
	local strona_r
	strona_r = mw.title.makeTitle('Page', analizowana_strona):getContent()
	strona_r = strona_r:gsub('<[nN][oO][iI][nN][cC][lL][uU][dD][eE]>.-</[nN][oO][iI][nN][cC][lL][uU][dD][eE]>', '')
	strona_r = strona_r:gsub('</?[iI][nN][cC][lL][uU][dD][eE][oO][nN][lL][yY]>', '')
	strona_r = strona_r:gsub('^.-<[oO][nN][lL][yY][iI][nN][cC][lL][uU][dD][eE]>', '')
	strona_r = strona_r:gsub('</[oO][nN][lL][yY][iI][nN][cC][lL][uU][dD][eE]>.-<[oO][nN][lL][yY][iI][nN][cC][lL][uU][dD][eE]>', '')
	strona_r = strona_r:gsub('</[oO][nN][lL][yY][iI][nN][cC][lL][uU][dD][eE]>.-$', '')
	strona_r = strona_r:gsub('<!%-%-.-%-%->', '')
	strona_r = strona_r:gsub("<[sS][eE][cC][tT][iI][oO][nN] +[bB][eE][gG][iI][nN] *= *'" .. ons .."' */>", '<section begin="' .. ons .. '"/>')
	strona_r = strona_r:gsub("<[sS][eE][cC][tT][iI][oO][nN] +[eE][nN][dD] *= *'" .. ons .."' */>", '<section end="' .. ons .. '"/>')
	if strona_r:find('<[sS][eE][cC][tT][iI][oO][nN] +[eE][nN][dD] *= *"?' .. ons ..'"? */>') then
		strona_r = strona_r:gsub('^.-<[sS][eE][cC][tT][iI][oO][nN] +[bB][eE][gG][iI][nN] *= *"?' .. ons .. '"? */>', '<section begin="' .. ons .. '"/>')
		strona_r = strona_r:gsub('<[sS][eE][cC][tT][iI][oO][nN] +[eE][nN][dD] *= *"?' .. ons .. '"? */>.-<[sS][eE][cC][tT][iI][oO][nN] +[bB][eE][gG][iI][nN] *= *"?' .. ons .. '"? */>', '<section end="' .. ons .. '"/><section begin="' .. ons .. '"/>')
		strona_r = strona_r:gsub('<[sS][eE][cC][tT][iI][oO][nN] +[eE][nN][dD] *= *"?' .. ons .. '"? */>.-$', '<section end="' .. ons .. '"/>')
	else
		-- brak sekcji onlysection na te stronie
		-- oryginał po prostu po prostu nie bierze nic
		strona_r = ''
	end
	strona_r = strona_r .. ' '
	strona_r = strona_r:gsub('<[sS][eE][cC][tT][iI][oO][nN] .-/>', '')
	
	return strona_r	
end
local function sklej(pages_arg)
	local strony={}
	local etykiety={}
	local fs = nil
	local ts = nil
	local ons = nil
	local text_on_pages = {}
	local i
	local znacznik_t = mw.title.makeTitle('Szablon', 'Proofreadpage pagenum template')
	strony, etykiety, fs, ts, ons = pages_from_range(pages_arg)
	if ons == nil then
		text_on_pages[1] = znacznik_t:getContent():gsub('{{{page|?}}}', 'Strona:' .. strony[1]):gsub('{{{num|?}}}', etykiety[1])
		text_on_pages[2] = get_firstpage(strony[1], strony[#strony], fs, ts)
		i = 2
		while i < #strony do
			text_on_pages[i * 2 - 1] = znacznik_t:getContent():gsub('{{{page|?}}}', 'Strona:' .. strony[i]):gsub('{{{num|?}}}', etykiety[i])
			text_on_pages[i * 2] = get_page(strony[i])
			i = i + 1
		end
		if #strony > 1 then
			text_on_pages[#strony * 2 - 1] = znacznik_t:getContent():gsub('{{{page|?}}}', 'Strona:' .. strony[#strony]):gsub('{{{num|?}}}', etykiety[#strony])
			text_on_pages[#strony * 2] = get_lastpage(strony[#strony], ts)
		end
	else
		i = 1
		while i <= #strony do
			text_on_pages[i * 2 - 1] = znacznik_t:getContent():gsub('{{{page|?}}}', 'Strona:' .. strony[i]):gsub('{{{num|?}}}', etykiety[i])
			text_on_pages[i * 2] = get_onlysectionpage(strony[i], ons)
			i = i + 1
		end
	end
	return table.concat(text_on_pages)
end
local function zest_firstpage(analizowana_strona, lp, fs, ts)
	local wynik
	
	if analizowana_strona ~= lp then
		if fs == nil then
			wynik = '{{Strona:' .. analizowana_strona ..'}}'
		else
			wynik = '{{#section:Strona:' .. analizowana_strona ..'|' .. fs .. '|}}'
		end
		wynik = wynik .. ' '
	else
		if fs == nil and ts == nil then
			wynik = '{{Strona:' .. analizowana_strona ..'}}'
		elseif fs ~= ts then
			wynik = '{{#section:Strona:' .. analizowana_strona ..'|' .. fs .. '|'.. ts .. '}}'
		else
			wynik = '{{#section:Strona:' .. analizowana_strona ..'|' .. fs .. '}}'
		end
	end
	return wynik
end
local function zest_lastpage(analizowana_strona, ts)
	local wynik
	
	if ts == nil then
		wynik = '{{Strona:' .. analizowana_strona ..'}}'
	else
		wynik = '{{#section:Strona:' .. analizowana_strona ..'||' .. ts .. '}}'
	end
	return wynik
end
local function zestawienie(pages_arg)
	local strony={}
	local etykiety={}
	local fs = nil
	local ts = nil
	local ons = nil
	-- local text_on_pages = {}
	local i
	local text_on_pages = {}
	strony, etykiety, fs, ts, ons = pages_from_range(pages_arg)
	if ons == nil then
		text_on_pages[1] = '{{Proofreadpage pagenum template|page=Strona:' .. strony[1] .. '|num=' .. etykiety[1] .. '}}'
		text_on_pages[2] = zest_firstpage(strony[1], strony[#strony], fs, ts)
		i = 2
		while i < #strony do
			text_on_pages[i * 2 - 1] = '{{Proofreadpage pagenum template|page=Strona:' .. strony[i] .. '|num=' .. etykiety[i] .. '}}'
			-- realizowane bezpośrednio w funkcji głównej
			text_on_pages[i * 2] = '{{Strona:' .. strony[i] ..'}} '
			i = i + 1
		end
		if #strony > 1 then
			text_on_pages[#strony * 2 - 1] = '{{Proofreadpage pagenum template|page=Strona:' .. strony[#strony] .. '|num=' .. etykiety[#strony] .. '}}'
			text_on_pages[#strony * 2] = zest_lastpage(strony[#strony], ts)
		end
	else
		i = 1
		while i <= #strony do
			text_on_pages[i * 2 - 1] = '{{Proofreadpage pagenum template|page=Strona:' .. strony[i] .. '|num=' .. etykiety[i] .. '}}'
			-- bezpośrednio w funkcji:
			text_on_pages[i * 2] = '{{#section:Strona:' .. strony[i] ..'|' .. ons .. '}} '
			i = i + 1
		end
	end
	return table.concat(text_on_pages)
end
local function ContainsRef(pages_arg)
	local strony={}
	local etykiety={}
	local fs = nil
	local ts = nil
	local ons = nil
	local i
	local wynik = nil
	local str = ''
	local PP1 = '<[Rr][Ee][Ff]>'
	local PP2 = '{{[Pp][Ww]|'
	local PP3 = '{{[Bb]wd|'
	local PP4 = '{{#tag:ref|'
	strony, etykiety, fs, ts, ons = pages_from_range(pages_arg)
	if ons == nil then
		str = get_firstpage(strony[1], strony[#strony], fs, ts)
		wynik = str:find(PP1) or str:find(PP2) or str:find(PP3) or str:find(PP4)
		i = 2
		while i < #strony and wynik == nil do
			str = get_page(strony[i])
			wynik = str:find(PP1) or str:find(PP2) or str:find(PP3) or str:find(PP4)
			i = i + 1
		end
		if #strony > 1 then
			str = get_page(strony[#strony])
			wynik = wynik or str:find(PP1) or str:find(PP2) or str:find(PP3) or str:find(PP4)
		end
	else
		i = 1
		while i <= #strony and wynik == nil do
			wynik = get_onlypage(strony[i], ons):find(poszukiwany_pattern)
			i = i + 1
		end
	end
	return wynik ~= nil
end
local function test_na_kreske(ptag, inv)

-- Wywołanie: {{#invoke: Sandbox/Draco flavus/get index pages | test_na_kreske | Index:PL Encyklopedyja powszechna 1860 T1.djvu|strona_p|strona_k|formatowanie}}
-- Wynik: jeśli w danym zakresie stron występuje na końcu kreska podaje numery stron i przenoszone słowo 
-- do wykorzystania w szablonie
	local strony={}
	local etykiety={}
	local fs = nil
	local ts = nil
	local ons = nil
	-- local text_on_pages = {}
	local i
	local text_on_pages = {}
	strony, etykiety, fs, ts, ons = pages_from_range(ptag)

	
    local SubPageResult = ''
    local SubPageName = ''
    local substring = ''
    local wynik = '<center><br>'
    local wynik1 = ''
    local PageA = ''
    local PageB = ''
    local p = ''
    local q = ''
    for i = 1, #strony - 1, 1 do
    	SubPageName = strony[i]
    	SP = mw.title.new('Page:' .. SubPageName):getContent()
    	if SP == nil then
    	else
	    	PageA = mw.ustring.match(SP, '(............. [^ ]*)<noinclude>.-</noinclude>$')
	    	if mw.ustring.sub(PageA, -1) == '-' then
	    		PageA = mw.ustring.sub(PageA, 1,-2)
	    		SubPageName=strony[i + 1]
	    		SP = mw.title.new('Page:' .. SubPageName):getContent()
    		    if SP == nil then
				else
					p = mw.ustring.match(PageA, ' ([^ ]*)$')
					if mw.ustring.find(p, "'") then
						wynik = wynik .. '!!!!!!!!!!!!!!!!!!!!!!' .. i .. '   ' .. PageA .. '!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!<br><br>'
					else
			    		PageB = mw.ustring.match(SP, '^<noinclude>.-</noinclude>(.-[ :.,!?;)”“…«»<].................)')
			    		q = mw.ustring.match(PageB,'(.-)[ :.,!?;)”“…«»<].*')
						if mw.ustring.sub(q, -1) == '’' then
							q = mw.ustring.match(frame.args[1],'(.-)’')
						end
			    		wynik = wynik .. '[[Strona:' .. strony[i] .. ']] / ' .. '[[Strona:' .. strony[i+1] .. ']]<br>' .. PageA .. '⭕' .. PageB .. '<br>' .. ' {{pp|' .. p .. '|' .. q .. '}}<br><br>'
			    		wynik1 = wynik1 .. inv .. ' -page:"' .. 'Strona:' .. strony[i] .. '" "' .. p .. '-<noinclude" "' .. '{{pp|' .. p .. '|' .. q .. '}}<noinclude"; <br>'
			    		wynik1 = wynik1 .. inv .. ' -page:"' .. 'Strona:' .. strony[i + 1] .. '" "noinclude>' .. q .. '" "noinclude>' .. '{{pk|' .. p .. '|' .. q .. '}}"; <br>'
					end
				end
			end
    	end
    end
    return wynik .. '</center><br><br><br>' .. wynik1 .. 'echo done'
end
local function test_pp_pk(ptag, inv)

-- Wywołanie: {{#invoke: Sandbox/Draco flavus/get index pages | test_na_kreske | Index:PL Encyklopedyja powszechna 1860 T1.djvu|strona_p|strona_k|formatowanie}}
-- Wynik: jeśli w danym zakresie stron występuje pp/pk podaje numery stron i przenoszone słowo 
-- do wykorzystania w szablonie
	local strony={}
	local etykiety={}
	local fs = nil
	local ts = nil
	local ons = nil
	-- local text_on_pages = {}
	local i
	local text_on_pages = {}
	strony, etykiety, fs, ts, ons = pages_from_range(ptag)

	
    local SP = ''
    local wynik = ' '
    local PageA = ''
    local PageB1 = ''
    local PageB2 = ''
    local PageB3 = ''
    local PageB4 = ''
    local PageC = ''
    local p = ''
    local q = ''
    for i = 1, #strony, 1 do
    	SP = mw.title.new('Page:' .. strony[i]):getContent()
    	if SP == nil then
    	else
    		PageB1 = mw.ustring.match(SP, '(%{%{pk%|[^%}]*%}%})')
    		PageB2 = mw.ustring.match(SP, '(%{%{pp%|[^%}]*%}%})')
    		PageB3 = mw.ustring.match(SP, '(%{%{pk%|.*%{%{pk%|.*)')
    		PageB4 = mw.ustring.match(SP, '(%{%{pp%|.*%{%{pp%|.*)')
    		if PageB1  then
    			SP = mw.title.new('Page:' .. strony[i-1]):getContent()
    			PageA = mw.ustring.match(SP, '(%{%{pp%|[^%}]*%}%})')
    			if PageA and string.sub(PageA, 5, -1) ~= string.sub(PageB1, 5, -1) then
    				wynik = wynik .. '[[Strona:' .. strony[i -1]  .. ']] ' .. PageA .. ' [[Strona:' ..  strony[i] ..  ']] ' .. PageB1 .. '<br>'
    			end
    			if PageA == nil then
    				wynik = wynik .. '[[Strona:' .. strony[i -1]  .. ']] BRAK' .. ' [[Strona:' ..  strony[i] .. ']] ' .. PageB1 .. '<br>'
    			end
    			if PageB3 then
    				wynik = wynik .. '[[Strona:' .. strony[i]  .. ']] DWA RAZY pk' .. '<br>'
    			end
    		end
    		if PageB2  then
    			SP = mw.title.new('Page:' .. strony[i+1]):getContent()
    			PageC = mw.ustring.match(SP, '(%{%{pk%|[^%}]*%}%})')
    			if PageB4 then
    				wynik = wynik .. '[[Strona:' .. strony[i]  .. ']] DWA RAZY pp' .. '<br>'
    			end
    			if PageC == nil then
    				wynik = wynik .. '[[Strona:' .. strony[i] .. ']] ' .. PageB2 .. ' [[Strona:' ..  strony[i + 1] .. ']] BRAK'.. '<br>'
    			end
    		end
    	end
    end
    return wynik .. '<br><br><br>'
end

function p.ArgsToString(frame)
	local wynik = ''
	for k, v in pairs(frame.args) do
		wynik = wynik .. k .. '="' .. v .. '" '
	end
	return wynik
end
function p.drukuj(frame)
	local strony={}
	local etykiety={}
	local typ
	strony, etykiety, typ = pages_from_index(frame.args[1])
	return table.concat(strony, ", ") .. '\n\n' .. table.concat(etykiety, ", ") 
end
function p.drukuj2(frame)
	local strony={}
	local etykiety={}
	local strony_inr={}
	local strony_inrX={}
	local fs = ''
	local ts = ''
	local ons = ''
	strony, etykiety, fs, ts, ons = pages_from_range(frame.args[1])
	for i=1, #strony_inr, 1 do
		if strony_inr[i] then
			strony_inrX[i] = "Y"
		else
			strony_inrX[i] = "N"
		end
	end
	local x
	if fs == nil then x = 'nil' else x = fs end if ts == nil then x= x .. 'nil' else x = x .. ts end if ons == nil then x = x .. 'nil' else x = x .. ons end 
	return table.concat(strony, ", ") .. '\n\n' .. table.concat(etykiety, ", ") .. '\n\n' .. x
end
function p.drukuj3(frame)
	return sklej(frame.args[1])
end
function p.drukuj4(frame)
	return zestawienie(frame.args[1])
end
function p.drukuj5(frame)
	wynik = zestawienie(frame.args[1]):gsub('{{Strona:', '{{subst:Strona:'):gsub('{{#section:Strona:', '{{subst:#section:Strona:')
	return wynik
end
function p.drukuj6(frame)
	return (ContainsRef(frame.args[1]) == true and 'Przypisy' or 'Void')
end
function p.drukuj7(frame)
	return test_na_kreske(frame.args[1], frame.args[2])
end
function p.drukuj8(frame)
	return test_pp_pk(frame.args[1], frame.args[2])
end
function p.pages1(frame)
    return frame:preprocess(sklej(frame.args[1]))
end
function p.pages2(frame)
    return frame:preprocess(zestawienie(frame.args[1]))
end
local function zestawieniesubst(x)
	y = tostring(x)
	y = x:gsub('^ *(.-) *$', '%1')
	wynik = zestawienie(y):gsub('{{Strona:', '{{subst:Strona:'):gsub('{{#section:Strona:', '{{subst:#section:Strona:')
	return wynik
end
function p.tekststrony3(frame)
	local ms = ''
	ms = mw.title.new(frame.args[1]):getContent()
    ms = ms:gsub(']]', '&#93;&#93;')
    ms = ms:gsub("''", '&#39;&#39;')
    ms = ms:gsub('}}', '&#125;&#125;')
	ms = ms:gsub('.-<pre><nowiki>(.-)</nowiki></pre>.*', '%1')
	ms = ms:gsub('⟅ *pages(.-)/⟆', zestawieniesubst )
	--ms = ms:gsub('&#125;', '}')
	--ms = ms:gsub('&#39;', "'")
	--ms = ms:gsub('&#93;', ']')
	-- mp = zestawienie(ms):gsub('{{Strona:', '{{subst:Strona:'):gsub('{{#section:Strona:', '{{subst:#section:Strona:')
	return ms
end
function p.pagessx(frame)
	--Testowa funkcja do znajdywania <section begin="X"/><section end="X"/>
	local nazwa_indeksu = frame.args[1]
	local strony={}
	local strony_znaczniki={}
	local strony_fragmenty={}
	local wyniki = {}
	local etykiety={}
	local typ
	strony, etykiety, typ = pages_from_index(nazwa_indeksu)
	for i = 1, #strony, 1 do
    	SubPageName = strony[i]
    	SP = mw.title.new("Page:" .. SubPageName):getContent()
    	if SP == nil then
    		strony_znaczniki[i] = 0
    		strony_fragmenty[i] = ''
    	else
    		PageA = mw.ustring.match(SP, '<section *begin="X" */><section *end="X" */>')
    		if PageA == nil then
    			strony_znaczniki[i] = 1
    			strony_fragmenty[i] = ''
    		else
    		    PageB = mw.ustring.match(SP, '^<noinclude>.-</noinclude><section *begin="X" */><section *end="X" */>')
    		    strony_fragmenty[i] = mw.ustring.match(SP, '<section *end="X" */>(.?.?.?.?.?.?.?.?.?.?.?.?.?.?.?.?.?.?.?.?.?.?.?.?.?.?.?.?.?.?.?.?.?.?.?.?.?.?.?.?.?.?.?.?.?)')
    		    if PageB == nil then
    			    strony_znaczniki[i] = 2
    			else
    				strony_znaczniki[i] = 3
    			end
    		end
        end
	end
	p = ''
	q = 0
	r = 1
	if typ then
	    for i = 1, #strony_znaczniki, 1 do
	    	if strony_znaczniki[i] > 1 then
	    		if q > 1 then
	    			if q == 3 then
	    				if strony_znaczniki[i] == 3 then
	    					wyniki[r] = '<pages index="' .. nazwa_indeksu .. '" from=' .. p .. ' to=' .. string.match(strony[i-1], '/(%d*)') .. ' />'
	    				else
	    					wyniki[r] = '<pages index="' .. nazwa_indeksu .. '" from=' .. p .. ' to=' .. string.match(strony[i], '/(%d*)') .. ' tosection="X" />'
	    				end
	    			else
	    				if strony_znaczniki[i] == 3 then
	    					wyniki[r] = '<pages index="' .. nazwa_indeksu .. '" from=' .. p .. ' to=' .. string.match(strony[i-1], '/(%d*)') .. ' fromsection="X" />'
	    				else
	    					wyniki[r] = '<pages index="' .. nazwa_indeksu .. '" from=' .. p .. ' to=' .. string.match(strony[i], '/(%d*)') .. ' fromsection="X" tosection="X" />'
	    				end
	    			end
	    			wyniki[r] = wyniki[r] .. '           ' .. strony_fragmenty[i]
	    			r = r + 1
	    			q = strony_znaczniki[i]
	    			p = string.match(strony[i], '/(%d*)')
	    		else
	     			q = strony_znaczniki[i]
	    			p = string.match(strony[i], '/(%d*)')
				end   			
	    	end
	    end
	else
	    for i = 1, #strony_znaczniki, 1 do
	    	if strony_znaczniki[i] > 1 then
	    		if q > 1 then
	    			if q == 3 then
	    				if strony_znaczniki[i] == 3 then
	    					wyniki[r] = '<pages index="' .. nazwa_indeksu .. '" from="' .. p .. '" to="' .. strony[i-1] .. ' />'
	    				else
	    					wyniki[r] = '<pages index="' .. nazwa_indeksu .. '" from="' .. p .. '" to="' .. strony[i] .. '" tosection="X" />'
	    				end
	    			else
	    				if strony_znaczniki[i] == 3 then
	    					wyniki[r] = '<pages index="' .. nazwa_indeksu .. '" from="' .. p .. '" to="' .. strony[i-1] .. ' fromsection="X" />'
	    				else
	    					wyniki[r] = '<pages index="' .. nazwa_indeksu .. '" from="' .. p .. '" to="' .. strony[i] .. '" fromsection="X" tosection="X" />'
	    				end
	    			end
	    			wyniki[r] = wyniki[r] .. '           ' .. strony_fragmenty[i]
	    			r = r + 1
	    			q = strony_znaczniki[i]
	    			p = strony[i]
	    		else
	     			q = strony_znaczniki[i]
	    			p = strony[i]
				end   			
	    	end
	    end
	end
	return table.concat(wyniki, "\n\n--------------------------\n\n")
end



return p