Przejdź do zawartości

Moduł:Pages

Z Wikiźródeł, wolnej biblioteki

Moduł zwraca niesparsowany wikitekst będący wynikiem użycia tagu <pages />. Parametry:

  • index (wymagany)
  • from (wymagany)
  • to (wymagany)
  • step (opcjonalny)
  • exclude (opcjonalny)
  • fromsection (opcjonalny)
  • tosection (opcjonalny)
  • onlysection (aktualnie niewspierany)

local p = { }

local function strony_z_indeksu_old(index, from, to)
	-- tworzenie listy stron w oparciu o zawartość indeksu starego typu
    local lista = {}
    local itxt = mw.title.makeTitle('Indeks', index):getContent()
    local itxt = string.gsub(itxt, '^.*\n|Strony=', '')
    local itxt = string.gsub(itxt, '\n|Spis treści=.*', '')
    local itxt = string.gsub(itxt, ' ', '_')
    local n = 1
    local strona
    local pos = string.find(itxt, '%[%[[sS]trona:' .. string.gsub(from, ' ', '_') .. '|')
    if pos == nil then
    	error("Nie znaleziono strony [[Strona:" .. from .. "]] w indeksie [[Indeks:" .. index .. "]].", 1)
    else
    	itxt = string.sub(itxt, pos+string.len(from)+10)
    	pos = string.find(itxt, '%]%]')
		lista[1] = {from, string.sub(itxt, 1, pos-1)}
	end
	if string.gsub(from, ' ', '_') == string.gsub(to, ' ', '_') then
		return lista
	end
	repeat
		pos = string.find(itxt, '%[%[[sS]trona:')
		if pos == nil then
			error("Nie znaleziono strony [[Strona:" .. to .. "]] w indeksie [[Indeks:" .. index .. "]].", 1)
		end
		n = n+1
		itxt = string.sub(itxt, pos+9)
		pos = string.find(itxt, '|')
		strona = string.gsub(string.sub(itxt, 1, pos-1), '_', ' ')
		itxt = string.sub(itxt, pos)
		pos = string.find(itxt, '%]%]')
		lista[n] = {strona, string.sub(itxt, 1, pos-1)}
    until strona == string.gsub(to, '_', ' ')
    return lista
end    

local function strony_z_indeksu_new(args)
	-- tworzenie listy stron dla indeksu starego typu
	local lista = {}
	local index = args.index
	local from = tonumber(args.from)
	local to = tonumber(args.to)
	local i, step, exclude
	local n = 1
	if args.step ~= nil and tonumber(args.step) ~= nil then
		step = tonumber(args.step)
	else
		step = 1
	end
	if args.exclude ~= nil and tonumber(args.exclude) ~= nil then
		exclude = tonumber(args.exclude)
	else
		exclude = nil
	end
	for i = from, to, step do
		if i ~= exclude then
			-- ideałem by było zastąpienie "i" przez oznaczenie numeru strony zdefiniowane w indeksie
			lista[n] = {index .. "/" .. tostring(i), tostring(i)}
			n = n + 1
		end
	end
	return lista
end

local function eval_pages(args)
	local kod = nil
	local index = args.index
	local from = args.from
	local to = args.to
	local fromsection = args.fromsection
	local tosection = args.tosection
	local lista
--	local onlysection = args.onlysection
	local znacznik_t = mw.title.makeTitle('Szablon', 'Proofreadpage pagenum template'):getContent()
	if index == nil or from == nil or to == nil or type(tonumber(from)) ~= type(tonumber(to)) then
		error("Niewłaściwe użycie wywołania pages()", 1)
	end

	-- generowanie listy stron, z których należy pobrać treść
	if tonumber(from) ~= nil then
		lista = strony_z_indeksu_new(args)
	else
		lista = strony_z_indeksu_old(index, from, to)
	end

	-- pobiersnie treści stron i ekstrakcja wybranych sekcji
	for i = 1, table.maxn(lista) do
		local strona_raw = mw.title.makeTitle('Strona', lista[i][1])
		if not strona_raw.exists then
			error("Brak strony [[Strona:" .. lista[i][1] .. "]]", 1)
		else
			local pocz_s, kon_s
			local 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>
			-- obsługa onlysection
			-- TODO
			-- obsługa fromsection
			if i == 1 and fromsection ~= nil then
				pocz_s = string.find(strona_r, '<[sS][eE][cC][tT][iI][oO][nN] +begin *= *"?' .. fromsection .. '"? */>')
				if pocz_s == nil then
					error("Brak znacznika początku sekcji " .. fromsection, 1)
				end
				strona_r = string.sub(strona_r, pocz_s)
			end
			-- obsługa tosection
			if i == table.maxn(lista) and tosection ~= nil then
				kon_s = string.find(strona_r, '<[sS][eE][cC][tT][iI][oO][nN] +end *= *"?' .. tosection .. '"? */>')
				if kon_s == nil then
					error("Brak znacznika końca sekcji " .. tosection, 1)
				end
				strona_r = string.sub(strona_r, 1, kon_s-1)
			end
			-- usonięcie znaczników kodu do transkluzji i zbędnych po transkluzji
			-- (<includeonlu>, <onlyinclude>, <section>)
			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/>
			-- znacznik z numerem strony dla bieżącej strony
			znacznik = string.gsub(znacznik_t, '{{{page|?}}}', 'Strona:' .. lista[i][1])
			znacznik = string.gsub(znacznik, '{{{num|?}}}', lista[i][2])
			-- scalanie
			if kod == nil then
				kod = znacznik .. strona_r
			elseif string.sub(kod, -1) == '-' then
				-- usunięcie łącznika na końcu stony jako znacznika przeniesienia
				kod = string.sub(kod, string.len(kod)-1) .. znacznik .. strona_r
			else
				kod = kod .. ' ' .. znacznik .. strona_r
			end
		end
	end
	return kod
end

function p.pages(frame)
    return eval_pages(frame.args)
end

return p