I gn a nén co di dzo-pådje /doc pol notule d’ esplikêyes. Clitchîz cial po l’ ahiver.
-- get informations about a reference
-- for infobox take a look at Module:Sourdant/infoboesse
 
local p = {}
local builder = require("Module:Builder")

-- regex pattern for ref ID 
-- E89, E212b, PiB2, R10:1842, LBer, ...
-- eto eployî divins l' module Ortografeyes
local refPattern = "([A-Z]+[A-Za-z0-9-]+)(%:?[^%s]*)"

-- return the table of a reference
-- or false if reference doesn't exist
-- @fromWD bool try to load data from wikidata if true
function p.get_reference(refID, fromWD)
	
	-- default fromWD is true
	if fromWD ~= false then fromWD = true end 
	
	-- if refID is not a correct ID
	if not string.match(refID, refPattern) then return nil end

	-- try to load local data
	local ref = builder.load_data("Sourdant/" .. refID)
	if not ref then	return false end
	
	-- follow alias : FR13 -> R13
	-- redirecting is not yet available for module
	if ref.alias ~= nil then
		ref = p.get_reference(ref.alias)
	end
	
	if mw.title.getCurrentTitle().namespace == 102 and ref.title == nil and ref.author ~= nil then
		builder.add_category("Sourdants po Wikidata")
	end
	
	-- get data from Wikidata if possible
	if fromWD and ref.wikidata ~= nil and ref.wikidata ~="" then
		local wd = require("Module:Sourdant/wikidata")
		data = wd.get_reference(ref.wikidata)
		if data ~= nil then
			data.id = refID
			data.wikidata = ref.wikidata
			data.group = ref.group
			data.name = ref.name
			data.cover = data.cover or ref.cover
			data.aliases = ref.aliases
			return 	data
		end
	end
	
	-- A DISFACER
	-- if sourdant author is an ID, get informations about the author
	-- LM -> Lucien Mahin
	if ref.id_author ~= nil then
		-- because ref is a read only table
		-- we need to make an editable copy
		local data = setmetatable( {}, { __index = ref });
		local a = p.get_reference(ref.id_author)
		if a then
			if a.authors ~= nil then
				data.authors = a.authors
			elseif a.author ~= nil then
				data.author = a.author
				if a.author ~= nil then
					data.author = a.author
				end
			end
		end
		return data
	end
	
	return ref
end

-- return a reference as wikicode (author, title)
function p.get_text(frame)	
	local args = frame.args
	local refID = args[1]
	
	local ref = p.get_reference(refID)
	local text = ""
	local aid = nil
	local aname = nil
	if ref then
		-- 'name' param. overwrite all : author, title, year,...
		-- eg. Motî Haust (not Jean Haust, Dictionnaire liégeois, 1933,...)
		if ref.name and ref.name ~= "" then
			text = text .. "[[Sourdant:" .. refID .."|" .. ref.name .. "]]"
		else
			-- template {{s|oteur=}}
			if args.author and args.author ~= "" then
				local ref_author = p.get_reference(args.author)
				if aname then
					text = text .. p.get_authors_list(ref_author.author, true)
				else
					text = text .. args.author
				end
			elseif ref.author ~= nil then
				text = text .. p.get_authors_list(ref.author, true)
			end
		
			-- title (and year)
			if ref.title and ref.title ~= "" then
				if text ~= "" then text = text .. ', ' end
				text = text .. "''[[Sourdant:" .. refID .."|" .. ref.title .. "]]''"
				--[[ NI ROTE NÉN AVOU LES GAZETES
				if ref.year and ref.year ~= "" then
					text = text .. ", " .. ref.year
				end--]]
			end
		end
	else
		text = refID
	end
	
	text = frame:newParserValue(text).expand()
	if args[2] ~= nil and args[2] ~= "" then
		text = text .. ", " .. args[2]
	end
		
	if(args.ritch ~= nil and args.ritch ~= "0") then
		if args.ritch ~= "" then
			text = text .. " (fråze "
			if args.ritch == "rarindjeye" then
				text = text .. "rarindjeye"
			elseif args.ritch == "rif" then
				text = text .. "rifondowe"
			elseif args.ritch == "rif+" then
				text = text .. "rifondowe et recråxheye"
			elseif args.ritch == "rif~" then
				text = text .. "rifondowe et rarindjeye"
			end
			text = text .. ")"
		end
		text = '<span class="sourdant">— '..text..'.</span>'
	end
	
	return text
end

-- return the list of authors
-- @link bool make a link to Sourdant:author if page exists
function p.get_authors_list(authors, link)
	link = link or false
	if type(authors) == 'string' then
		authors = {authors} -- string to table
	end
	if type(authors) == 'table' then
		if link then
			for i, author in ipairs(authors) do
				local page = mw.title.new(author, "Sourdant")
				if page.exists then
					authors[i] = "[[Sourdant:"..author.."|"..author.."]]"
				else
					authors[i] = author
				end
			end
		end
		return table.concat(authors, ", ")
	end
	return nil
end

return p