Przejdź do zawartości

Moduł:ContainsRef

Z Wikiźródeł, wolnej biblioteki

Dokumentacja dla tego modułu może zostać utworzona pod nazwą Moduł:ContainsRef/opis

local p = {}


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

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


function p.Przypisy(frame)

-- Wywołanie: {{#invoke: ContainsRef | Przypisy | Strona:Tadeusz Dołęga-Mostowicz - Ostatnia brygada.djvu|from|to|fromsection|tosection}}
-- Wynik: jeśli w danym zakresie stron występuje <ref> {{pw}} {{bwd}} to zwraca słowo Przypisy inaczej Void
-- Może być wykorzystany tak {{ {{#if:1|{{#invoke: ContainsRef | Przypisy | Strona:PL Encyklopedyja powszechna 1860 T1.djvu|702|702||trzecia}}}} }}
-- W efekcie wstawiana jest sekcja Przypisy tylko tam, gdzie jest potrzebna
-- Naśladuje nieco składnię <pages index=... from=... to=... fromsection=... tosection=... />
-- ale jest mniej odporna np. sekcja from section musi się znajdować na stronie from 
-- podobnie jest z tosection i to
-- żadne parametry step, include, exclude nie są przewidziane
-- działa tylko z podstronami plików wielostronicowych (.djvu i .pdf)

    local SubPageResult = "Void"
    local SubPageName = ""
    local n = tonumber(frame.args[2])
    local m = tonumber(frame.args[3])
    local from_section='<section begin=' .. '"?' .. escape_lua_pattern(frame.args[4]) .. '"? */>'
    local to_section='<section end=' .. '"?' .. escape_lua_pattern(frame.args[5]) .. '"? */>'
    local start=""
    local stop=""
    local substring=""
    SubPageName=frame.args[1] .. "/" .. n
    
    if n == m and frame.args[4] ~= '' and frame.args[4] ~= nil and frame.args[5] ~= '' and frame.args[5] ~= nil then
    	start, stop = string.find(mw.title.new(SubPageName):getContent(), from_section .. '.*' .. to_section)
		substring = string.sub(mw.title.new(SubPageName):getContent(),start,stop)
		    if ((string.match(substring, "<[Rr][Ee][Ff]>") ~= nil ) or
		    	(string.match(substring, "{{[Pp][Ww]|") ~= nil ) or
		    	(string.match(substring, "{{[Bb]wd|") ~= nil ) or
		    	(string.match(substring, "{{#tag:ref|") ~= nil))
		    then
		    	SubPageResult = "Przypisy"
		    	n = m + 2
		    end
	else
	    if frame.args[4] ~= '' and frame.args[4] ~= nil and (n <= m) then
	    	start, stop = string.find(mw.title.new(SubPageName):getContent(), from_section .. '.*')
	    	substring = string.sub(mw.title.new(SubPageName):getContent(),start,stop)
	    	if ((string.match(substring, "<[Rr][Ee][Ff]>") ~= nil ) or
	    		(string.match(substring, "{{[Pp][Ww]|") ~= nil ) or
	    		(string.match(substring, "{{[Bb]wd|") ~= nil ) or
	    		(string.match(substring, "{{#tag:ref|") ~= nil))
	    	then
	    		SubPageResult = "Przypisy"
	    		n = m + 2
	    	else
	    		n = n + 1
	    	end
	    end
	    
	    if frame.args[5] ~= '' and frame.args[5] ~= nil and (n <= m) then
	    	SubPageName=frame.args[1] .. "/" .. m
	    	start, stop = string.find(mw.title.new(SubPageName):getContent(), '.*' .. to_section)
	    	substring = string.sub(mw.title.new(SubPageName):getContent(),start,stop)
	    	if ((string.match(substring, "<[Rr][Ee][Ff]>") ~= nil ) or
	    		(string.match(substring, "{{[Pp][Ww]|") ~= nil ) or
	    		(string.match(substring, "{{[Bb]wd|") ~= nil ) or
	    		(string.match(substring, "{{#tag:ref|") ~= nil))
	   		then
	    		SubPageResult = "Przypisy"
	    		n = m + 2
	    	else
	    		m = m - 1
	    	end
	    end
	   
	    
	    while (n <= m) do
	    	SubPageName=frame.args[1] .. "/" .. n
	    	substring = mw.title.new(SubPageName):getContent()
	    	if ((string.match(substring, "<[Rr][Ee][Ff]>") ~= nil ) or
	    		(string.match(substring, "{{[Pp][Ww]|") ~= nil ) or
	    		(string.match(substring, "{{[Bb]wd|") ~= nil ) or
	    		(string.match(substring, "{{#tag:ref|") ~= nil))
	    	then
	    		SubPageResult = "Przypisy"
	    		n = m + 1
	    		
	    	end
			n=n+1
			
	    end
    end
	return SubPageResult
	
end

return p