Module:Proposition tweet

 Documentation[créer] [purger]
local p = {}

local function is_arg_set(arg)
	if arg and not arg:match( "^[%s—]*$" ) then
		return true
	end
	return false
end

local function normalise(args)
	local lookup_table = {}
	lookup_table['media']= 'média'
	lookup_table['signature'] = 'proposé par'
	
	for k, v in pairs(lookup_table) do
		if is_arg_set(args[k]) ~= nil then
			args[v] = args[k]
		end
	end
	
	if not is_arg_set(args['proposé par']) then
		args['proposé par'] = '—'
	end
	if args['mode'] == 'bot' and not is_arg_set(args['validé par']) then
		args['validé par'] = '—'
	end
	if not is_arg_set(args['publié par']) then
		args['publié par'] = '—'
	end
	
	return args
end

--Compute the length (using twitter's definition) of the text
local function count_chars(texte)
	local parsed_text = ''
	local nb_links = 0
	if texte == nil then
		texte = ''
	end
	parsed_text, nb_links = mw.ustring.gsub(texte, 'https*://[^ <>]*[^.,;:!?<> ]', '')
	
	return mw.ustring.len(parsed_text) + (nb_links*23)
end


function p.display(frame)
	local args = normalise(frame:getParent().args)
	local nb_chars = count_chars(args.texte)
	local color = '#f9f9f9'
	local state = "proposé"
	local mode_icon = "Circle-icons-pencil 2.svg"
	local media = ""
	local thumb = ""
	
	--Choose the color of the box and the status depending of the state of the submission
	if is_arg_set(args['publié par']) then
		color = '#bdffb2'
		state = "publié"
	elseif is_arg_set(args['erreur']) then
		color = '#f98881'
		state = "erreur"
	elseif is_arg_set(args['validé par']) then
		color = '#f6ffb2'
		state = "validé"
	elseif is_arg_set(args['rejeté par']) then
		color = '#cecece'
		state = "rejeté"
	end
	if nb_chars > 280 then
		color = '#f98881'
	end
	
	--Prepare media output
	if is_arg_set(args['média']) then
		if mw.ustring.find(args['média'], 'https*://[^ <>]*[^.,;:!?<> ]') then
			media = 'Média : ' .. args['média'] .. '<br>'
		else
			thumb = '[[Fichier:' .. args['média'] .. '|vignette|125px]]'
			media = 'Média : [[:File:' .. args['média'] .. '|' .. args['média'] .. ']]<br>'
		end
	end
	
	--Switch between manual and bot mode
	if args.mode == 'bot' then
		mode_icon = 'Logo wikibot.svg'
	end
	
	--Generate output
	local output = '<div style="margin: 1em 0; padding: 0.7em; border: 2px solid #ccc; background-color: ' .. color .. '; position: relative;">'
	output = output .. '<div style="position: absolute; right: 5px; bottom: 5px;">[[File:' .. mode_icon .. '|frameless|50px]]</div>'
	output = output .. thumb
	output = output .. '<div style="font-style: italic;">' .. frame:expandTemplate{ title = 'citation bloc', args = { args.texte  } } .. '</div>\n\n\n'
	output = output .. media
	output = output .. 'État de la proposition : ' ..state .. '<br>'
	if nb_chars > 280 then
		output = output .. '<span style="color:red;">\'\'\'Nombre de caractères : ' .. nb_chars .. '/280\'\'\'</span><br>'
	else
		output = output .. 'Nombre de caractères : ' .. nb_chars .. '/280<br>'
	end
	output = output .. 'Proposé par : ' .. args['proposé par'] .. '<br>'
	if args['mode'] == "bot" or is_arg_set(args['validé par']) then
		output = output .. 'Validé par : ' .. args['validé par'] .. '<br>'
	end
	if is_arg_set(args['rejeté par']) then
		output = output .. 'Rejeté par : ' .. args['rejeté par'] .. '<br>'
	else
		output = output .. 'Publié par : ' .. args['publié par'] .. '<br>'
	end
	if is_arg_set(args.erreur) and not is_arg_set(args['publié par']) then
		output = output .. '<span style="color:red;">\'\'\'Erreur lors de la publication : ' .. args.erreur .. '\'\'\'</span><br>'
	end
	output = output .. '</div>'
	
	return output
end


return p