Module:Document1

La bibliothèque libre.

La documentation pour ce module peut être créée à Module:Document1/Documentation

function clean_args(args)
	local cleaned = {}
	for k,v in pairs(args) do
		v = mw.text.trim(v)
		if v == '' then
			v = nil
		end
		cleaned[k] = v
	end
	return cleaned
end

function normalize_title(title)
	if title.isRedirect then
		return normalize_title(title.redirectTarget)
	else
		return title
	end
end

function date_link(date)
	if tonumber(date) ~= nil then
        if tonumber(date) < 0 then
		  date = tostring(math.abs(tonumber(date))) .. ' av. J.-C.'
	    end
		--TODO elzevir
	   local categorie_date = mw.title.makeTitle('Catégorie', date)
	   if categorie_date then 
		  return '[[:' .. categorie_date.fullText .. '|' .. date .. ']]'
	   else
		  return date
	   end
    else
        return date
    end
end

function optional_link(text)
	local title = mw.title.new(text)
	if title and title.exists then
		return '[[:' .. title.fullText .. '|' .. text .. ']]'
	else
		return text
	end
end

function new_row(style, sort)
	local html = '\n| '
	if style then 
		html = html .. 'style="' .. style .. '" '
	end
	if sort then
		html = html .. 'data-sort-value="' .. mw.text.nowiki(sort) .. '" '
	end
	return html .. '|'
end

function error_message(text)
	-- Return a html formated version of text stylized as an error.
	local html = mw.html.create('div')
	html:addClass('error')
		:wikitext(text)
		:wikitext('[[Catégorie:Pages faisant un appel erroné au modèle Document]]')
	return tostring(html)
end

local p = {}

function p.document(frame)
	parentFrame = frame:getParent()
    args = clean_args(parentFrame.args)
    --args = clean_args(frame.args)

	--Rendu
	local html = '\n|- style="vertical-align:top;" '
	
	if args.date and (args.livre or args['éditions'] or args.traduction) then
		html = html .. new_row('white-space:nowrap;min-width:4.8em', nil)
	else
		html = html .. new_row('white-space:nowrap;', nil)
	end
	if args.date then
		html = html .. date_link(args.date) .. ' :&nbsp;'
	elseif args.dates then
		local formatted = {}
		local node = mw.html.create('div')
			:css('line-height', '90%')
			:css('font-size', '100%')
			:css('float', 'left')
		for k,date in pairs(mw.text.split(args.dates, '/')) do
			formatted[k] = date_link(date)
		end
		node:wikitext(table.concat(formatted, '<br/>') ..' :')
		html = html .. tostring(node)
	end

	local item_id = nil
	if args['éditions'] then
		local editions_title = mw.title.new(args['éditions'])
		if editions_title then
			item_id = mw.wikibase.getEntityIdForTitle(editions_title.fullText)
		end
		html = html .. '[[Image:Disambig.svg|Plusieurs éditions disponibles|right|link=|20px]]'
		html = html .. new_row('padding-right: 2em;', args['titre alpha']) .. '<span class="lien-oeuvre">' .. optional_link(args['éditions']) .. '</span> '
	elseif args.traductions then
		local editions_title = mw.title.new(args.traductions)
		if editions_title then
			item_id = mw.wikibase.getEntityIdForTitle(editions_title.fullText)
		end
		html = html .. '[[Image:Disambig.svg|Plusieurs traductions disponibles|right|link=|20px]]'
		html = html .. new_row('padding-right: 2em;', args['titre alpha']) .. '<span class="lien-oeuvre">' .. optional_link(args.traductions) .. '</span> '
	elseif args.titre then
		local edition_title = args.titre
		local edition_text = args.titre
		local edition_comment = ''
		for c1, c2, c3 in mw.ustring.gmatch( args.titre .. ' ', '%[%[([^|]*)|(.*)%]%](.*)') do
			edition_title = c1
			edition_text = c2
			edition_comment = c3 --string.sub(c3,1,string.len (c3) - 1)
		end
		for c, c0  in mw.ustring.gmatch( args.titre .. ' ', '%[%[([^|]*)%]%](.*)') do
			edition_title = c
			edition_text = c
			edition_comment = c0  --string.sub(c0,1,string.len (c0) - 1)
		end
		edition_title = normalize_title(mw.title.new(edition_title))
		if edition_title then
			item_id = mw.wikibase.getEntityIdForTitle(edition_title.fullText)
		end
		if args.livre then
			html = html .. '[[Image:Open book nae 02.svg|right|20px|link=Livre:' .. args.livre .. ']]'
		end			
		html = html .. new_row('padding-right:2em;', args['titre alpha'])
		if edition_title and edition_title.exists then
			html = html .. '[[' .. edition_title.fullText .. '|' .. edition_text .. ']]' .. edition_comment .. '&nbsp;'
		else
			html = html .. edition_text .. edition_comment .. '&nbsp;'
		end
	else
		return '|-'
		--TODO: return an error? return error_message('Le paramètre "titre" doit être renseigné')
	end
	if not args.avancement and item_id then
		item = mw.wikibase.getEntity(item_id)
		if item and item.sitelinks and item.sitelinks["frwikisource"] then
			for _,badge in pairs(item.sitelinks["frwikisource"].badges) do
				if badge == 'Q20748091' then
					args.avancement = '2'
				elseif badge == 'Q20748094' then
					args.avancement = '1'
				elseif badge == 'Q20748092' then
					args.avancement = '4'
				elseif badge == 'Q20748093' then
					args.avancement = '5'
				end
			end
		end
	end
	if args.avancement == '0' then
		html = html .. frame:expandTemplate{title = '0/4'}
	elseif args.avancement == '1' then
		html = html .. frame:expandTemplate{title = '1/4'}
	elseif args.avancement == '2' then
		html = html .. frame:expandTemplate{title = '2/4'}
	elseif args.avancement == '3' then
		html = html .. frame:expandTemplate{title = '3/4'}
	elseif args.avancement == '4' then
		html = html .. frame:expandTemplate{title = '4/4'}
	elseif args.avancement == '5' then
		html = html .. frame:expandTemplate{title = 'validé'}
	elseif args.avancement then
		mw.addWarning( 'La valeur' .. mw.text.jsonEncode(args.avancement) .. ' du paramètre avancement n\'est pas supporté par le modèle document' )
	end	
	if args[1] == 'epub' or args[1] == 'export' then
		html = html .. '&nbsp;' .. frame:expandTemplate{title = args[1], args = {args.titre}}
	elseif args[1] then
		html = html .. error_message('Paramètre non nommé inconnu: ' .. args[1])
	end
	if args['epub'] then
		html = html .. '&nbsp;' .. frame:expandTemplate{title = 'epub', args = {args.epub}}
	end
	if args['export'] then
		html = html .. '&nbsp;' .. frame:expandTemplate{title = 'export', args = {args.export}}
	end
	
	if args['presentation'] then
	html = html .. '&nbsp;&nbsp;' .. frame:expandTemplate{title = 'Information_icon4'}
	end
	
	if args.presentation then
	html = html .. '&nbsp;&nbsp;' .. frame:expandTemplate{title = 'Information_icon4'}
	end
	
	if args.DP == 'EU' then
	html = html .. frame:expandTemplate{title = 'DP-EU-Icon'}
	end
	if item_id then
		html = html .. ' [[Image:Blue pencil.svg|10px|link=d:' .. item_id .. '|Voir et modifier les données sur Wikidata]]'
	end

	if args.vo then
		html = html .. new_row('font-size:90%; padding-right:2em:', args['vo alpha']) .. args.vo
	end
	
	if args.auteur then
		local auteur_title = args.auteur
		local auteur_text = args.auteur
		for c1, c2 in mw.ustring.gmatch( args.auteur, '%[%[([^|]*)|(.*)%]%]') do
			auteur_title = c1
			auteur_text = c2
		end
		for c in mw.ustring.gmatch( args.auteur, '%[%[([^|]*)%]%]') do
			auteur_title = c
			auteur_text = c
		end
    	html = html .. new_row('font-size:90%; padding-right:2em;', args['auteur alpha'])
		local auteur_title = mw.title.new(auteur_title, 'Auteur')
		if auteur_title and auteur_title.exists then
			html = html .. '[[' .. auteur_title.fullText .. '|' .. auteur_text .. ']]'
		else
			html = html .. auteur_text
		end
	end
	
	if args.genre then
		html = html .. new_row('font-size:90%; padding-right:2em;', nil) .. args.genre
	end
	
	if args.scan then
		html = html .. new_row('font-size:90%; padding-right:2em;', nil) .. '<span class="plainlink">' .. args.scan .. '</span>'
	end
	
	if args.divers then
		html = html .. new_row('font-size:90%; padding-right:2em;', args['divers alpha']) .. args.divers
	end
	
	if args.revue then
		local revue_parts = mw.text.split(args.revue, '/')
		if revue_parts[1] == 'RDDM' and len(revue_parts) == 2 then
			html = html .. new_row('font-size:90%; padding-right:2em;', nil) .. args.divers .. "''Revue des Deux Mondes'', n°" .. revue_parts[2] .. ', ' .. args.date
		end
	end
	
	return html
end


return p