Przejdź do zawartości

Moduł:Sandbox/Draco flavus/Moduł Dane tekstu4

Z Wikiźródeł, wolnej biblioteki

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

local p = {}
local list = { 'FULLPAGENAMEE?', 'PAGENAMEE?', 'BASEPAGENAMEE?', 'ROOTPAGENAMEE?', 'SUBPAGENAMEE?', 'ARTICLEPAGENAMEE?', 'SUBJECTPAGENAMEE?', 'TALKPAGENAMEE?', 'ARTICLESPACEE?', 'SUBJECTSPACEE?', 'TALKSPACEE?', 'NAMESPACEE?', 'NAMESPACENUMBER' }
local modul = 'Sandbox/Draco flavus/Moduł Dane tekstu3'

function FindFields4( frame )

-- Przeznaczenie znajduje na jakiejś stronie szablon {{:Dane tekstu}} kopiuje go, ale podmienia lub dodaje dodatkowe pola
-- tj. te wymienione w dalszych parametrach (pattern1, pattern2, pattern3 … )
-- Wynik: na nowo ewaluowany szablon {{Dane tekstu}}
-- pola szablonu źródłowego zazwyczaj {{Dane tekstu}} lub {{Dane tekstu3}} nie muszą być w osobnej linii (tak zazwyczaj robimy dla przejrzystości, ale nie jest to wymóg techniczny)
-- Na przykład chcąc zrezygnować z okładki napiszemy | okładka = | strona z okładką =
-- chcąc dodać pole pochodzenie napiszemy | pochodzenie = tutaj skąd pochodzi
-- parametr referencja określa referencyjną stronę, z niej ściągane są dane. Najczęściej będzie to 
--                     | referencja = {{ROOTPAGENAME}}
-- zródłowy szablon NIE jest pobierany, gdy w nim samym jest parametr referencja
-- w źródłowym szablonie nie są wykorzystywane parametry poprzedzone podkreśleniem, jednak są one dziedziczone i zastępują wtedy ewentualny argument bez podkreślenia

    local PageResult = ''
    local PageName = ''
    local PageContent= '{{}}'
    local referen = frame.args['referencja']
    if frame.args['referencja'] ~= nil then
    	PageContent= mw.title.new(frame:preprocess(frame.args['referencja'])):getContent()
    end
    local i = 0
    local j = 0
    local k = ''
    local l = ''
    local v = ''
    local t = ''
    local func = 'FindFields5'
    i, j =  PageContent:find('%b{}')
   	PageResult = ' ' .. PageContent:sub(i, j-2):gsub('[^|]*', '', 1) .. '|'
   	PageResult = PageResult:gsub('%b{}',
   		function(s1) return s1:gsub('|', '🐉') end)
   	PageResult = PageResult:gsub('%b[]',
   		function(s1) return s1:gsub('|', '🐉') end)
   	while PageResult:find('| *_') do
   		l = PageResult:match('| *_[^=]*')
   		l = l:gsub('^| *_', '', 1)
   		l = l:gsub(' *$', '')
   		PageResult = PageResult:gsub('| *' .. l .. '[^|]*|', '|', 1)
   		PageResult = PageResult:gsub('| *_' .. l .. ' *=', '| ' .. l .. ' =', 1)
   	end

   	for k,v in pairs(frame.args) do
   		if k ~= 'referencja' then
   			t = v:gsub('|', '🐉'):gsub('%%', '🐢'):gsub('^%s+', '')
			if (PageResult:find('| *' .. k .. ' *=[^|]*') ~= nil) then
				PageResult=PageResult:gsub('| *' .. k .. ' *=[^|]*', '| ' .. k .. ' = ' .. t )
			else
				PageResult = PageResult  .. k .. ' = ' .. t .. ' |' .. string.char(10)
			end
		end
   	end
   	l = PageResult:match('| *referencja *= [^|]*')
   	if l then
   		PageResult = PageResult:gsub(l, '')
   		for i = 1, #list do
			l = l:gsub('%{%{[ ]*(' .. list[i] .. ')%}%}', '{{%1:' .. referen .. '}}')
   		end
		l = l:gsub('%{%{[ ]*', '{{safesubst:'):gsub('%{%{safesubst:safesubst:', '{{safesubst:')
   		PageResult = PageResult:gsub('{', '🐊'):gsub('}', '🐋'):gsub('|$', '')
   		return frame:preprocess('{{safesubst:#invoke:' .. modul .. '|' .. func .. l .. PageResult .. '}}')
   		-- Chcemy dwukrotnie dziedziczyć dane.
   	else
	   	
	   	PageResult = PageResult:gsub('🐉', '|'):gsub('🐢', '%%')
	   	PageResult = PageResult:gsub('|\n?$', '\n')
	   	PageResult = '{{Dane tekstu\n' .. PageResult .. '}}'
	   	return frame:preprocess(PageResult)
	end
end

function p.FindFields5(frame)
    local PageResult = ''
    local PageName = ''
    local PageContent= '{{}}'
    local referen = frame.args['referencja']
    if frame.args['referencja'] ~= nil then
    	PageContent= mw.title.new(frame.args['referencja']):getContent()
    end
    local i = 0
    local j = 0
    local k = ''
    local l = ''
    local v = ''
    local t = ''
	i, j =  PageContent:find('%b{}')
   	PageResult = ' ' .. PageContent:sub(i, j-2):gsub('[^|]*', '', 1) .. '|'
   	PageResult = PageResult:gsub('%b{}',
   		function(s1) return s1:gsub('|', '🐉') end)
   	PageResult = PageResult:gsub('%b[]',
   		function(s1) return s1:gsub('|', '🐉') end)
   	while PageResult:find('| *_') do
   		l = PageResult:match('| *_[^=]*')
   		l = l:gsub('^| *_', '', 1)
   		l = l:gsub(' *$', '')
   		PageResult = PageResult:gsub('| *' .. l .. '[^|]*|', '|', 1)
   		PageResult = PageResult:gsub('| *_' .. l .. ' *=', '| ' .. l .. ' =', 1)
   	end

   	for k,v in pairs(frame.args) do
   		if k ~= 'referencja' then
   			t = v:gsub('|', '🐉'):gsub('%%', '🐢'):gsub('^%s+', '')
			if (PageResult:find('| *' .. k .. ' *=[^|]*') ~= nil) then
				PageResult=PageResult:gsub('| *' .. k .. ' *=[^|]*', '| ' .. k .. ' = ' .. t )
			else
				PageResult = PageResult  .. k .. ' = ' .. t .. ' |' .. string.char(10)
			end
		end
   	end
   	l = PageResult:match('| *referencja *= [^|]*')
   	if l then
   		return 'Potrójne dziedziczenie'
   		-- możemy najwyżej dwukrotnie dziedziczyć dane.
   	else
	   	
	   	PageResult = PageResult:gsub('🐉', '|'):gsub('🐢', '%%'):gsub('🐊', '{'):gsub('🐋', '}')
	   	PageResult = PageResult:gsub('|\n?$', '\n')
	   	PageResult = '{{Dane tekstu\n' .. PageResult .. '}}'
	   	return frame:preprocess(PageResult)
	end
end

function p.FindFields( frame )
	local param = frame:getParent()
	if param.args['referencja'] ~= nil then
    	param.args['referencja'] = param.args['referencja']:gsub('%{%{ *', '{{safesubst:')
    end
    return  FindFields4( param )
end

function p.FindFields3( frame )

-- Wywołanie: {{#invoke: ... | FindFields3 |Ostatnia brygada |pole}}
-- Przeznaczenie znajduje na jakiejś stronie w pierwszym szablonie ( pomyślane {{Dane tekstu}}/{{Dane tekstu2}}, ale może to być dowolny pierwszy indeks na stronie (na przykład szablon w indeksie)
-- wyszukuje argument pole
-- zwraca go
-- pomyślany jako sposób na wpisanie takiego samego argumentu jak na innej stronie

    local PageResult = ''
    local PageName = ''
    local PageContent= '{{}}'
    local referen = frame.args[1]
    if referen ~= nil then
    	PageContent= mw.title.new(referen):getContent()
    end
    local i = 0
    local j = 0
    local l = ''
    local m = ''
	i, j =  PageContent:find('%b{}')
   	PageResult = ' ' .. PageContent:sub(i, j-2):gsub('[^|]*', '', 1) .. '|'
   	PageResult = PageResult:gsub('%b{}',
   		function(s1) return s1:gsub('|', '🐉') end)
   	PageResult = PageResult:gsub('%b[]',
   		function(s1) return s1:gsub('|', '🐉') end)
   	m = PageResult:match('| *' .. frame.args[2] .. '[ ]*=[ ]*[^|]*')
   	if m then
		l = m:gsub('| *' .. frame.args[2] .. '[ ]*=[ ]*', ''):gsub('🐉', '|')
		return frame:preprocess(l)
	else
		return ''
	end
end

function p.test1(frame)
    local PageResult = ''
    local PageName = ''
    local PageContent= '{{}}'
    local i = 0
    local j = 0
    local l = ''
    local func = 'test2'
    local address = frame.args[1]
    PageContent= mw.title.new(address):getContent()
    i, j = PageContent:find('{{ *Dane tekstu')
	i, j = PageContent:find('%b{}' , i )
	PageResult = ' ' .. PageContent:sub(i, j-2):gsub('[^|]*', '', 1) .. '|'
   	PageResult = PageResult:gsub('%b{}',
   		function(s1) return s1:gsub('|', '🐉') end)
   	PageResult = PageResult:gsub('%b[]',
   		function(s1) return s1:gsub('|', '🐉') end)
   	l = PageResult:match('| *' .. 'licencja' .. '[ ]*=[ ]*[^|]*')
   	if l then
		l = l:gsub('| *' .. 'licencja' .. '[ ]*=[ ]*', ''):gsub('🐉', '|')
		return frame:preprocess(l)
	else
		l = PageResult:match('| *' .. 'referencja' .. '[ ]*=[ ]*[^|]*')
		if l then
			l = l:gsub('| *' .. 'referencja' .. '[ ]*=[ ]*', ''):gsub('{{', '{{safesubst:'):gsub('🐉', '|')
			return frame:preprocess('{{safesubst:#invoke:' .. modul .. '|' .. func .. "|" .. l .. "}}")
		else
			return "Brak odpowiedniego argumentu w szablonie!"
		end
   	end
end

function p.test2(frame)
    local PageResult = ''
    local PageName = ''
    local PageContent= '{{}}'
    local i = 0
    local j = 0
    local l = ''
    local func = 'test3'
	local address = string.gsub('' .. frame.args[1], '^%s*(.-)%s*$', '%1' .. '')
	PageContent= mw.title.new(address):getContent()
    i, j = PageContent:find('{{ *Dane tekstu')
	i, j = PageContent:find('%b{}' , i )
	PageResult = ' ' .. PageContent:sub(i, j-2):gsub('[^|]*','',1) .. '|'
   	PageResult = PageResult:gsub('%b{}',
   		function(s1) return s1:gsub('|', '🐉') end)
   	PageResult = PageResult:gsub('%b[]',
   		function(s1) return s1:gsub('|', '🐉') end)
   	l = PageResult:match('| *' .. '_licencja' .. '[ ]*=[ ]*[^|]*')
   	if l then
		l = l:gsub('| *' .. '_licencja' .. '[ ]*=[ ]*', ''):gsub('🐉', '|')
		return frame:preprocess(l)
	else
		l = PageResult:match('| *' .. 'licencja' .. '[ ]*=[ ]*[^|]*')
   		if l then
			l = l:gsub('| *' .. 'licencja' .. '[ ]*=[ ]*', ''):gsub('🐉', '|')
			return frame:preprocess(l)
		else
			l = PageResult:match('| *' .. 'referencja' .. '[ ]*=[^|]*')
			if l then
				for i = 1, #list do
					l = l:gsub('%{%{(' .. list[i] .. ')%}%}', '{{%1:' .. address .. '}}')
				end
   				l = l:gsub('| *' .. 'referencja' .. '[ ]*=[ ]*', ''):gsub('{{', '{{safesubst:'):gsub('🐉', '|')
				return frame:preprocess('{{safesubst:#invoke:' .. modul .. '|' .. func .. "|" .. l .. "}}")
   			else
				return 'Brak odpowiedniego argumentu w szablonie!'
			end
		end
   	end
end

function p.test3(frame)
    local PageResult = ''
    local PageName = ''
    local PageContent= '{{}}'
    local i = 0
    local j = 0
    local l = ''
	local address = string.gsub('' .. frame.args[1], '^%s*(.-)%s*$', '%1' .. '')
	PageContent= mw.title.new(address):getContent()
    i, j = PageContent:find('{{ *Dane tekstu'  )
	i, j = PageContent:find('%b{}' , i )
	PageResult = ' ' .. PageContent:sub(i, j-2):gsub('[^|]*','',1) .. '|'
   	PageResult = PageResult:gsub('%b{}',
   		function(s1) return s1:gsub('|', '🐉') end)
   	PageResult = PageResult:gsub('%b[]',
   		function(s1) return s1:gsub('|', '🐉') end)
   	l = PageResult:match('| *' .. 'referencja' .. '[ ]*=[ ]*[^|]*')
   	if l then
   		return 'Dopuszczamy co najwyżej podwójne dziedziczenie!'
   	else
   		l = PageResult:match('| *' .. '_licencja' .. '[ ]*=[ ]*[^|]*')
   		if l then
			l = l:gsub('| *' .. '_licencja' .. '[ ]*=[ ]*', ''):gsub('🐉', '|')
			return frame:preprocess(l)
		else
			l = PageResult:match('| *' .. 'licencja' .. '[ ]*=[ ]*[^|]*')
   			if l then
				l = PageResult:match('| *' .. 'licencja' .. '[ ]*=[ ]*[^|]*'):gsub('| *' .. 'licencja' .. '[ ]*=[ ]*', ''):gsub('🐉', '|')
				return frame:preprocess(l)
			else
				return 'Brak odpowiedniego argumentu w szablonie!'
   			end
		end
   	end
end

function p.GetTitleFromIndex(frame)

	-- Wywołanie: {{#invoke: Sandbox/Draco flavus/GetTitleFromIndex | TitleFromIndex |abcd}}
	-- Wynik: znajduje na danej stronie indeksowej tytuł tekstu

    local Result = ''
    local SubPageResult = mw.title.new('Index:' .. frame.args[1]):getContent()
	Result = SubPageResult:gsub('^{{:MediaWiki:Proofreadpage_index_template\n|Tytuł=[[]*(.-)[]|].*','%1')
	-- bardziej międzynarodowa wersja by się mogła odwoływać do pierwszego argumentu w szablonie (zamiast na sztywno Tytuł):
	-- Result = SubPageResult:gsub('^{{:MediaWiki:Proofreadpage_index_template\n|.-=[[]*(.-)[]|].*','%1')
	return Result
end

return p