Modul:Leksemi2

Izvor: Wječnik

Documentation for this module may be created at Modul:Leksemi2/doc

local p = {}

local function err(e)
	return '<b><span class="error">' .. e .. '</span></b>'
end

local function delinkReversePipeTrick(s)
	if s:match("^%[%[|.*[|\n]") then -- Check for newlines or multiple pipes.
		return s
	end
	return s:match("%[%[|(.*)%]%]")

end

local function delinkPipeTrick(s)
	-- We need to deal with colons, brackets, and commas, per [[Help:Pipe trick]].
	-- First, remove the text before the first colon, if any.
	if s:match(":") then
		s = s:match("%[%[.-:(.*)|%]%]")
	-- If there are no colons, grab all of the text apart from the square brackets and the pipe.
	else
		s = s:match("%[%[(.*)|%]%]")
	end
	
	-- Next up, brackets and commas.
	if s:match("%(.-%)$") then -- Brackets trump commas.
		s = s:match("(.-) ?%(.-%)$")
	elseif s:match(",") then -- If there are no brackets, display only the text before the first comma.
		s = s:match(":(.-),.*$")
	end
	return s
end

local function getDelinkedLabel(s)
	local result = s
	-- Deal with the reverse pipe trick.
	if result:match("%[%[|") then
		return delinkReversePipeTrick(result)
	end

	result = mw.uri.decode(result, "PATH") -- decode percent-encoded entities. Leave underscores and plus signs.
	result = mw.text.decode(result, true) -- decode HTML entities.

	-- Check for bad titles. To do this we need to find the
	-- title area of the link, i.e. the part before any pipes.
	local target_area
	if result:match("|") then -- Find if we're dealing with a piped link.
		target_area = result:match("^%[%[(.-)|.*%]%]")
	else
		target_area = result:match("^%[%[(.-)%]%]")
	end

	-- Check for bad characters.
	if mw.ustring.match(target_area, "[%[%]<>{}%%%c\n]") and mw.ustring.match(target_area, "[%[%]<>{}%%%c\n]") ~= "?" then
		return s
	end

	-- Check for categories, interwikis, and files.
	local colon_prefix = result:match("%[%[(.-):.*%]%]") or "" -- Get the text before the first colon.
	local ns = mw.site.namespaces[colon_prefix] -- see if this is a known namespace
	if mw.language.isKnownLanguageTag(colon_prefix) or (ns and (ns.canonicalName == "File" or ns.canonicalName == "Category")) then
		return ""
	end

	-- Remove the colon if the link is using the [[Help:Colon trick]].
	if result:match("%[%[:") then
		result = "[[" .. result:match("%[%[:(.*%]%])")
	end

	-- Deal with links using the [[Help:Pipe trick]].
	if mw.ustring.match(result, "^%[%[[^|]*|%]%]") then
		return delinkPipeTrick(result)
	end
	
	-- Find the display area of the wikilink
	if result:match("|") then -- Find if we're dealing with a piped link.
		result = result:match("^%[%[.-|(.+)%]%]")
		-- Remove new lines from the display of multiline piped links,
		-- where the pipe is before the first new line.
		result = result:gsub("\n", "")
	else
		result = result:match("^%[%[(.-)%]%]")
	end

	return result
end

local function delinkLinkClass(text, pattern, delinkFunction)
	if type(text) ~= "string" then
		error("Attempt to de-link non-string input.", 2)
	end
	if type(pattern) ~= "string" or mw.ustring.sub(pattern, 1, 1) ~= "^" then
		error('Invalid pattern detected. Patterns must begin with "^".', 2)
	end
	-- Iterate over the text string, and replace any matched text. using the 
	-- delink function. We need to iterate character by character rather 
	-- than just use gsub, otherwise nested links aren't detected properly.
	local result = ""
	while text ~= "" do
		-- Replace text using one iteration of gsub.
		text = mw.ustring.gsub(text, pattern, delinkFunction, 1)
		-- Append the left-most character to the result string.
		result = result .. mw.ustring.sub(text, 1, 1)
		text = mw.ustring.sub(text, 2, -1)
	end
	return result
end

-- ========================


local function allcases(s)
	return (s or ''):gsub('([%^%$%(%)%%%.%[%]%*%+%-])', '%%%1')
		:gsub('%a', function(letter) return '['..letter:upper()..letter:lower()..']' end)
end

function trimspaces(s)
	return s:gsub('^%s*(.-)%s*$', '%1')
end

function escape( pattern_str )
	return mw.ustring.gsub( pattern_str, "([%(%)%.%%%+%-%*%?%[%^%$%]])", "%%%1" )
end

function finalize(str)
	return str:match('^([^|}]*{{[^}]+}}[^|}]*)|') or str:match('([^|}]+)')
end

function trazi_pp(content, predlozak, parametar)
	local predlozakfind = string.match(content, '{{' .. allcases(predlozak) .. '(.+)') or ''
	local paramfind = string.match(predlozakfind, '|%s*' .. (allcases(parametar) .. '%s*=%s*') .. '([^|].*)') or ''
	if predlozakfind == '' or paramfind == '' then
		return 'Greška, ne postoji predložak ' .. predlozak .. ' ili parametar ' .. parametar
		else
	-- mw.log('\npredlozak\n', predlozak)
	-- mw.log('\nparam\n', param)
			return finalize(paramfind)
	end
		
end



function p.leksem(frame)
	mw.log('\n\n==============\n')
	
	local pipemarker = '~_~TPVPIPEMARKER~_~'
	
	local content = string.gsub(mw.title.getCurrentTitle():getContent() or '', '[\r\n]', '\n')
	
	-- definicija
	for v in content:gmatch(':(%([1-9]%.[1-9%)].-)\n') do
		mw.log(v)
		mw.log('de-linked:', delinkLinkClass(v, "^%[%[.-%]%]", getDelinkedLabel))
	end
	
	if (trazi_pp(content, 'imenica', 'rod'):find('Greška')) then
		return trazi_pp(content, 'imenica', 'rod')
	else
		return imenica(trazi_pp(content, 'imenica', 'rod'), content)
	end
end


function imenica(rod, content)
	mw.log(rod)
	local lexcat = 'Q1084' -- imenice
	local rodProp = 'P5185'
	local rodQ=''
	local padezi={
	    N='', Nmn='',
	    G='', Gmn='',
	    D='', Dmn='',
	    A='', Amn='',
	    V='', Vmn='',
	    L='', Lmn='',
	    I='', Imn='',
	}

	-- ROD
	if rod == 'm' or rod:find('muš') then
		rodQ = 'Q499327'
		else if rod == 'ž' or rod:find('žens') then
			rodQ = 'Q1775415'
			else if rod == 's' or rod:find('sred') then
				rodQ = 'Q1775461'
				else return 'error - rod nije prepoznat'
			end
		end
	end
	-- padezi
	for k, v in pairs(padezi) do
		if trazi_pp(content, 'deklinacija', k):gsub('\n', '') == '' then
			return err('error: nedostaje padež ' .. k)
		else
			padezi[k] = trazi_pp(content, 'deklinacija', k):gsub('\n', '')
		end
	end
	mw.logObject(padezi)
end


return p