Przejdź do zawartości

Moduł:Sandbox/Draco flavus/Test5

Z Wikiźródeł, wolnej biblioteki

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

local p = {}
require('mw.ustring')
local strona_r  -- holds the page content
local strona_raw  -- holds the page 
local escape_lua_pattern
do
  local matches =
  {
    ["^"] = "%^";
    ["$"] = "%$";
    ["("] = "%(";
    [")"] = "%)";
    ["%"] = "%%";
    ["."] = "%.";
    ["["] = "%[";
    ["]"] = "%]";
    ["*"] = "%*";
    ["+"] = "%+";
    ["-"] = "%-";
    ["?"] = "%?";
  }

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


local function new(args)

	local strony = {}
	local numery_stron = {}
	local index = args.index
	local from = tonumber(args.from)
	local to = tonumber(args.to)
	local step = tonumber(args.step)
	local include = args.include
	local exclude = args.exclude
	local title = mw.title.new('File:' .. index)
	local pages_temp = {}
	local max_page = #title.file.pages
	local i
	local first_page
	local n = 1
	local wynik = ''

	if include==nil and exclude==nil then
		-- tylko from-to
		if from==nil and to~=nil then
			from = 1
		end
		if from~=nil and to==nil then
			to = max_page
		end
		for i = from, to, 1 do
			pages_temp[i] = true
		end
		first_page = from
	else
		for i = 1, max_page, 1 do
			pages_temp[i] = false
		end
		if from==nil and to~=nil then
			from = 1
		end
		if from~=nil and to==nil then
			to = max_page
		end
		if from==nil and to==nil then
			-- puste from/to
			n='tutaj'
		else
			for i = from, to, 1 do
				pages_temp[i] = true
			end
			n= 'tam' 
		end
		if include ~= '' and include ~= nil then
			include = ',' .. include .. ','
			include = string.gsub(include, ',(%d*)%-,', ',%1-' .. max_page .. ',')
			include = string.gsub(include, ',%-(%d*),', ',1-%1,')
			include = string.gsub(include, ',(%d*),', ',%1-%1,')
			for p, q in string.gmatch(include, '(%d*)-(%d*)') do
				for i = tonumber(p), tonumber(q), 1 do
					pages_temp[i] = true
				end
			end
		end
		if exclude ~= '' and exclude ~= nil then
			exclude = ',' .. exclude .. ','
			exclude = string.gsub(exclude, ',(%d*)%-,', ',%1-' .. max_page .. ',')
			exclude = string.gsub(exclude, ',%-(%d*),', ',1-%1,')
			exclude = string.gsub(exclude, ',(%d*),', ',%1-%1,')
			for p, q in string.gmatch(exclude, '(%d*)-(%d*)') do
				for i = tonumber(p), tonumber(q), 1 do
					pages_temp[i] = false
				end
			end
		end
		i = 1
			while pages_temp[i] == false do
				i = i + 1
			end
		first_page = i
	end
	if step ==nil then
		step = 1
	end
	
	for i = first_page, max_page, step do
		if pages_temp[i] then
			table.insert(strony, "Strona:" .. index .. "/" .. i)
			table.insert(numery_stron, i)
		end
	end

	
	return table.concat(numery_stron, "ø") .. "\n\n" .. table.concat(strony, "ø")
	-- return strony, numery_stron
end

local function old(args)

	local strony = {}
	local numery_stron = {}
	local index = args.index
	local itxt = '' 
	local from = ''
	local to = ''
	local chunk = ''

	itxt = mw.title.makeTitle('Indeks', index):getContent()                     -- tekst strony indeksowej
	itxt = string.gsub(itxt, '^.*\n|Strony=(.*)\n|Spis treści=.*$', '%1')		-- tylko fragment z linkami do stron
	itxt = string.gsub(itxt, "%[%[[Pp][Aa][Gg][Ee]:", "🚀")					 -- Page może być również używane na określeśnie przestrzeni
	itxt = string.gsub(itxt, "%[%[[Ss][Tt][Rr][Oo][Nn][Aa]:", "🚀")			 -- tak samo strona
	itxt = string.gsub(itxt, "(🚀[^]]*)]]", "%1🚁")							  -- Usuwamy wszystko, co nie jest linkiem do strony
	if args.from==nil and args.to~=nil then
		from = escape_lua_pattern(string.gsub(string.match(itxt, '^[^🚀]*🚀([^|🚁]*)[|🚁]'), ' ', '_'))
	else
		from = escape_lua_pattern(string.gsub(args.from, " ", "_"))
	end
	if args.from~=nil and args.to==nil then
		to = escape_lua_pattern(string.gsub(string.gsub(string.match(itxt, '🚀([^🚀]*)🚁[^🚁]*$'), '|.*', ''), ' ', '_'))
	else
		to = escape_lua_pattern(string.gsub(args.to, " ", "_"))
	end
	itxt = string.gsub(itxt, ' ', '_')											-- wszędzie podkreślenia zamiast odstępów
	-- discard everything but from-to
	if from == to then
		itxt = string.match(itxt, '🚀' .. from .. '[^🚁]*🚁')
		if itxt == nil then
			error("Nie znaleziono strony [[Strona:" .. from .. "]] w indeksie [[Indeks:" .. index .. "]].", 1)
		end
	else
		itxt = string.match(itxt, '🚀' .. from .. '[|🚁].*' .. '🚀' .. to .. '[^🚁]*🚁')
		if itxt == nil then
			error("Nie znaleziono strony [[Strona:" .. from .. "]] lub [[Strona:" .. to .. "]] w indeksie [[Indeks:" .. index .. "]].", 1)
		end
	end
	for chunk in string.gmatch(itxt, '🚀([^🚁]*)🚁') do
		if string.match(chunk, '|') ~= nil then
			table.insert(strony, 'Strona:' .. string.match(chunk, '^([^|]*)|'))
			table.insert(numery_stron, string.match(chunk, '|(.*)$'))
		else
			table.insert(strony, chunk)
			table.insert(numery_stron, chunk)
		end
	end

	
	return table.concat(numery_stron, "ø") .. "\n\n" .. table.concat(strony, "ø")
	-- return numery_stron, strony
	-- return itxt
end

local function old_or_new(args)
	-- rozpoznawanie old-new opiera się na argumentach from i tó z tagu pages
	-- być może bardziej uniwersalne (jeśli chodzi o wykorzystanie kodu) by było
	-- sprawdzanie czy w indeksie jest tag <pagelist ... />
	if args.from == nil and type(tonumber(args.to)) == 'number' or 
			type(tonumber(args.from)) == 'number' and args.to == nil or 
			type(tonumber(args.from)) == 'number' and type(tonumber(args.to)) == 'number' then
		return true
	else
		return false
	end
end
local function get_page(args)
	title=args[1]
	strona_raw = mw.title.makeTitle('Strona', title)
	
	if not strona_raw.exists then
		error("Brak strony [[Strona:" .. args[1] "]]", 1)
	else
		strona_r = strona_raw:getContent()
		strona_r = string.gsub(strona_r, '<[nN][oO][iI][nN][cC][lL][uU][dD][eE]>.-</[nN][oO][iI][nN][cC][lL][uU][dD][eE]>', '') --usunięcie z zawartości "unparsed" sekcje <noinclude>
		-- czy tutaj usunąć ewentualny łącznik (jak w oryginale)
		strona_r = string.gsub(strona_r, '</?[iI][nN][cC][lL][uU][dD][eE][oO][nN][lL][yY]>', '')
		strona_r = string.gsub(strona_r, '</?[oO][nN][lL][yY][iI][nN][cC][lL][uU][dD][eE]>', '')
		strona_r = string.gsub(strona_r, '<[sS][eE][cC][tT][iI][oO][nN] .-/>', '') -- usunięcie zbędnych znaczników <section/>
		if string.sub(strona_r, -1) == '-' then
			-- usunięcie łącznika na końcu stony jako znacznika przeniesienia
			strona_r = string.sub(strona_r, 1, -2)
		end
	end
end
local function sklej(args)
	local strony = {}
	local numery_stron = {}
	numery_stron, strony = sciagnij(args)
	local args_getpage = {}
	local wynik = {}
	local i
	local j

	-- prepare the list of args_getpage  -- START
	args_getpage.from = strony[1]
	args_getpage.to = strony[#strony]
	args_getpage.fromsection = args.fromsection
	args_getpage.tosection = args.tosection
	args_getpage.onlysection = args.onlysection
	args_getpage.currentpage = ''
	-- prepare the list of args -- END
	
	if old_or_new(args) and args_getpage.onlysection ~= nil then                
		i = 1
		j = 2
		while i <= pages_count do
			args_getpage.currentpage = strony[i]
			wynik[j] = get_onlypage(args_getpage)
			i = i + 1
			j = j + 2
		end
	else
		-- inne
		args_getpage.currentpage = strony[1]
		wynik[2] = get_firstpage(args_getpage)
		i = 2
		j = 4
		while i <= pages_count - 1 do
			args_getpage.currentpage = strony[i]
			wynik[j] = get_page(args_getpage)
			i = i + 1
			j = j + 2
		end
		args_getpage.currentpage = strony[pages_count]
		wynik[2 * pages_count] = get_lastpage(args_getpage)
	end

end
function p.zestawienie(frame)
	if old_or_new(frame.args) then
		return new(frame.args)
	else
		return old(frame.args)
    end 
end


return p