Moduł:Znajdź stronę z section

Z Wikiźródeł, wolnej biblioteki

Dokumentacja dla tego modułu może zostać utworzona pod nazwą Moduł:Znajdź stronę z section/opis

local p = {}

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

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

function p.FindSection_Begin(frame)

-- Wywołanie: {{#invoke: Sandbox/Draco flavus/Znajdź stronę | FindSection_Begin | Strona:PL Encyklopedyja powszechna 1860 T1.djvu|start|end|pattern}}
-- Wynik: jeśli w danym zakresie stron występuje początek danej sekcji -- zwraca pierwsze wystąpienie 
-- do wykorzystania w substytucji raczej

    -- local SubPageResult = frame.args[4]..string.char(10)..string.char(10)
    local n = tonumber(frame.args[2])
    local m = tonumber(frame.args[3])
    local Result = ""
    local SubPageName=""
	local SearchPattern = "%<section begin *=" .. string.char(34) .. string.char(63) .. escape_lua_pattern(frame.args[4]) .. string.char(34) .. string.char(63) .. " */%>"
	while n<=m do
		SubPageName=frame.args[1] .. "/" .. n
		if mw.title.new(SubPageName):getContent() == nil then
			n = n + 1
		else
			if (string.match(mw.title.new(SubPageName):getContent(), SearchPattern) ~= nil ) then
				Result = n
				n = m + 1
			else
				n = n + 1
			end
		end
	end

 	return Result 

end

function p.FindSection_End(frame)

-- Wywołanie: {{#invoke: Sandbox/Draco flavus/Znajdź stronę | FindSection_End | Strona:PL Encyklopedyja powszechna 1860 T1.djvu|start|end|pattern}}
-- Wynik: jeśli w danym zakresie stron występuje koniec danej sekcji -- zwraca pierwsze wystąpienie 
-- do wykorzystania w substytucji raczej

    -- local SubPageResult = frame.args[4]..string.char(10)..string.char(10)
    local n = tonumber(frame.args[2])
    local m = tonumber(frame.args[3])
    local Result = ""
    local SubPageName=""
	local SearchPattern = "%<section end *=" .. string.char(34) .. string.char(63) .. escape_lua_pattern(frame.args[4]) .. string.char(34) .. string.char(63) .. " */%>"
	while n<=m do
		SubPageName=frame.args[1] .. "/" .. m
		if mw.title.new(SubPageName):getContent() == nil then
			m = m - 1
		else
			if (string.match(mw.title.new(SubPageName):getContent(), SearchPattern) ~= nil ) then
				Result = m
				n = m + 1
			else
				m = m - 1
			end
		end
	end

 	return Result 

end

return p