Przejdź do zawartości

Moduł:Sandbox/Draco flavus/PrevNextSection

Z Wikiźródeł, wolnej biblioteki

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

require('mw.ustring')
local p = {}

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

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

local function PrepareListOfSections(page,number_start,number_end)
	local SubPageResult = ""
    local n = tonumber(number_start) - 1
    local m = tonumber(number_end) + 1
    local p = "%<section  *[be][en][gd]i?n? *%=[^>]*/>"
    local SubPageName=""
    local FirstPage={}
    local LastPage={}
    local ListOfItems="◇"
    while (n <= m) do
    	
    	--
    
    	SubPageName=page .. "/" .. n
    
    
    	
    	if (mw.ustring.match(mw.title.new(SubPageName):getContent(), p) ~= nil ) then
    		SubPageResult = SubPageResult .. string.char(10) .. string.char(10) .. "𝕬".. n .. "◇"
    		for w in  mw.ustring.gmatch(mw.title.new(SubPageName):getContent(), p) do
    			w = mw.ustring.gsub(w, "%<section  *begin=" .. string.char(34) .. string.char(63), "𝕭")
    			w = mw.ustring.gsub(w, "%<section  *end=" .. string.char(34) .. string.char(63), "𝕰")
    			w = mw.ustring.gsub(w, string.char(34) .. string.char(63) .. string.char(32)  .. string.char(63) .. "/%>", "𝖅")
    			w = w .. n .. "◇"
        		SubPageResult = SubPageResult .. w
    		end
    	end
	
    	--
		n=n+1
		SubPageName=page .. "/" .. n
    end
    for k, v in mw.ustring.gmatch(SubPageResult, "𝕭([^𝖅]*)𝖅(%d*)◇") do
    	-- Items=Items .. k .. v
    	if FirstPage[k]==nil then
    		FirstPage[k]=v
    		ListOfItems=ListOfItems .. k .. "◇"
    	end
    end

	-- return SubPageResult
	-- return frame.args[5]
	return ListOfItems

end

-- these functions work fine for the text whith
--    * non overlappig sections with no or just negligible gaps, the previous section ends at the same page as the previous one or one before, the next on the same page or on the next
-- Usually this is the case in encyclopedias or lexicons

function p.previous(frame)

-- Wywołanie: {{#invoke: Sandbox/Draco flavus/PrevNextSection | sections_in_range | Strona:PL Encyklopedyja powszechna 1860 T1.djvu|start|end|item|pattern}}
-- Wynik: jeśli w danym zakresie stron występuje dany string zwraca go 
-- do wykorzystania w szablonie
	local s = escape_lua_pattern(frame.args[4])
	return mw.ustring.match(PrepareListOfSections(frame.args[1],frame.args[2],frame.args[3]), "◇([^◇]*)◇" .. s .. "◇")

end

function p.next(frame)
	local s = escape_lua_pattern(frame.args[4])
	return mw.ustring.match(PrepareListOfSections(frame.args[1],frame.args[2],frame.args[3]), "◇" .. s .. "◇([^◇]*)◇")
end

function p.zestawienie(frame)
	local s = escape_lua_pattern(frame.args[4])
	return PrepareListOfSections(frame.args[1],frame.args[2],frame.args[3])
	-- return s .. "◇([^◇]*)◇"
end

function raw(page, number_start, number_end)
	local SubPageResult = ""
    local n = tonumber(number_start) - 1
    local m = tonumber(number_end) + 1
    local p = "%<section  *[be][en][gd]i?n? *%=[^>]*/>"
    local SubPageName=""
    local FirstPage={}
    local LastPage={}
    local ListOfItems="◇"
    while (n <= m) do
    	
    	--
    
    	SubPageName=page .. "/" .. n
    
    
    	
    	if (string.match(mw.title.new(SubPageName):getContent(), p) ~= nil ) then
    		SubPageResult = SubPageResult .. string.char(10) .. string.char(10) .. "𝕬".. n .. "◇"
    		for w in  string.gmatch(mw.title.new(SubPageName):getContent(), p) do
    			w = w:gsub("%<section  *begin=" .. string.char(34) .. string.char(63), "𝕭")
    			w = w:gsub("%<section  *end=" .. string.char(34) .. string.char(63), "𝕰")
    			w = w:gsub(string.char(34) .. string.char(63) .. string.char(32)  .. string.char(63) .. "/%>", "𝖅")
    			w = w .. n .. "◇"
        		SubPageResult = SubPageResult .. w
    		end
    	end
	
    	--
		n=n+1
		SubPageName=page .. "/" .. n
    end
    for k in mw.ustring.gmatch(SubPageResult, "𝕭([^𝖅]*)𝖅%d*◇") do
    	-- Items=Items .. k .. v
    	
    		
    		ListOfItems=ListOfItems .. k .. "◇"
    	
    end
	return ListOfItems
end
function p.all(frame)
	local s=raw(frame.args[1],frame.args[2],frame.args[3])
	local b=""
    for k, v in string.gmatch(s, "𝕭([^𝖅]*)𝖅(%d*)◇") do
		b = b..k
    end
return s
end
return p