« Module:Index template » : différence entre les versions

La bibliothèque libre.
Contenu supprimé Contenu ajouté
mAucun résumé des modifications
Ligne 215 : Ligne 215 :
html:wikitext('[[Catégorie:Index - Thèses]]')
html:wikitext('[[Catégorie:Index - Thèses]]')
end
end
html:wikitext('[[Catégorie:Index]]')


if args.source ~= 'djvu' then
if args.source ~= 'djvu' then

Version du 8 juillet 2021 à 20:02

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

function withWikidataLink(wikitext, category)
	if wikitext == nil then
		return nil
	end
	new_wikitext = mw.ustring.gsub(wikitext, '%[%[([^|%]]*)%]%]', function(page)
		return addWikidataToLink(page, mw.ustring.gsub(page, '%.*/', '') , category)
	end)
	if new_wikitext ~= wikitext then
		return new_wikitext
	end
	return mw.ustring.gsub(wikitext, '%[%[([^|]*)|([^|%]]*)%]%]', function(page, link)
		return addWikidataToLink(page, link, category)
	end)
end

function addWikidataToLink(page, label, category)
    local title = mw.title.new( page )
    if title == nil then
    	return '[[' .. page .. '|' .. label .. ']]'
    end
    if title.isRedirect then
        title = title.redirectTarget
    end

    local tag = mw.html.create('span')
    local itemId = mw.wikibase.getEntityIdForTitle(title.fullText)
	tag:wikitext('[[' .. page .. '|' .. label .. ']]')
    if itemId ~= nil then
    	tag:wikitext(' [[Image:Wikidata.svg|10px|link=d:' .. itemId .. '|Voir l\'entité sur Wikidata]]')
    	if category ~= nil then
    		tag:wikitext('[[Catégorie:' .. category .. ']]')
    	end
    end
    return tostring(tag)
end

function addRow(metadataTable, key, value)
	if value then
		metadataTable:tag('tr')
			:tag('th')
				:attr('score', 'row')
				:css('vertical-align', 'top')
				:wikitext(key)
				:done()
			:tag('td'):wikitext(value)
	end
end

function splitFileNameInFileAndPage(title)
    local slashPosition = string.find(title.text, "/")
    if slashPosition == nil then
    	return title.text,nil
    else
    	return string.sub(title.text, 1, slashPosition - 1), string.sub(title.text, slashPosition + 1)
    end
end

function indexTemplate(frame)
	--create a clean table of parameters with blank parameters removed
	local data = (require 'Module:Index_data').indexDataWithWikidata(frame)
	local args = data.args
	local item = data.item
	
	local page = mw.title.getCurrentTitle()
	local html = mw.html.create()
	
	if item then
		html:wikitext('[[Catégorie:Livres avec un identifiant Wikidata]]<indicator name="wikidata">[[File:Wikidata.svg|20px|élément Wikidata|link=d:' .. item.id .. ']]</indicator>')
	end

    --Left part
    local left = html:tag('div')
    if args.sommaire or args.epigraphe then
    	left:css('width', '53%')
    end
    left:css('float', 'left')
    --Image
    if args.image then
        local imageContainer = left:tag('div')
            :css({
                float = 'left',
                overflow = 'hidden',
                border = 'thin grey solid'
            })
        local imageTitle = nil
        if tonumber(args.image) ~= nil then
            -- this is a page number
            imageTitle = mw.title.getCurrentTitle():subPageTitle(args.image)
        else
            -- this is an other file
            imageTitle = mw.title.new(args.image, "Media")
            -- TODO mettre une catégorie pour les livres ayant une couverture qui ne provient pas du DJVU/PDF
        end
        if imageTitle == nil then
            imageContainer:wikitext(args.image)
            -- TODO mettre une catégorie de maintenance ici lorsque la couverture est manquante
        else
            local imageName, imagePage = splitFileNameInFileAndPage(imageTitle)
            if imagePage ~= nil then
	            imageContainer:wikitext('[[File:' .. imageName .. '|page=' .. imagePage .. '|160px]]')
	        else
	            imageContainer:wikitext('[[File:' .. imageName .. '|160px]]')
	        end
        end
    end
    --Metadata
    local metadataContainer = left:tag('div')
    if args.image then
    	metadataContainer:css('margin-left', '150px')
    end
    local metadataTable = metadataContainer:tag('table')

    if args.titre then
	    if args.type == 'journal' then
    		addRow(metadataTable, 'Journal', withWikidataLink(args.titre))
    	else
    		addRow(metadataTable, 'Titre', withWikidataLink(args.titre, 'Livres avec un lien Wikidata'))
    	end
    else
    	mw.addWarning('Vous devez rentrer le champ titre du formulaire')
    end
	addRow(metadataTable, 'Sous-titre', args.sous_titre)
	addRow(metadataTable, 'Volume', args.volume)
	addRow(metadataTable, 'Auteur', withWikidataLink(args.auteur))
	addRow(metadataTable, 'Traducteur', withWikidataLink(args.traducteur))
	addRow(metadataTable, 'Éditeur', withWikidataLink(args.editeur_scientifique))
	addRow(metadataTable, 'Illustrateur', withWikidataLink(args.illustrateur))
	addRow(metadataTable, 'École', withWikidataLink(args.school))
	addRow(metadataTable, 'Maison&nbsp;d’édition', withWikidataLink(args.editeur))
	addRow(metadataTable, 'Lieu&nbsp;d’édition', withWikidataLink(args.lieu))
	addRow(metadataTable, 'Année&nbsp;d’édition', args.annee)
	addRow(metadataTable, 'Publication&nbsp;originale', args.publication)
	if args.BNF_ARK then
		local arkLink = '[http://gallica.bnf.fr/ark:/12148/' .. args.BNF_ARK .. ' Bibliothèque nationale de France][[Category:Facsimilés issus de Gallica]]<br/>'
		if args.bibliotheque then
			args.bibliotheque = arkLink .. args.bibliotheque
		else
			args.bibliotheque = arkLink
		end
	end
	addRow(metadataTable, 'Bibliothèque', args.bibliotheque)
	if args.source == 'djvu' or args.source == 'pdf' then
		addRow(metadataTable, 'Fac-similés', '[[:File:' .. mw.title.getCurrentTitle().text .. '|' .. args.source .. ']]')

		--add an indicator linking to the usages
		local query = 'SELECT ?item ?itemLabel ?pages ?page WHERE {\n  ?item wdt:P996 <http://commons.wikimedia.org/wiki/Special:FilePath/' .. mw.uri.encode(mw.title.getCurrentTitle().text, 'PATH') .. '> .\n  OPTIONAL { ?page schema:about ?item ; schema:isPartOf <https://fr.wikisource.org/> . }\n  OPTIONAL { ?item wdt:P304 ?pages . }\n  SERVICE wikibase:label { bd:serviceParam wikibase:language "[AUTO_LANGUAGE],en".\n}}'
		html:wikitext('<indicator name="index-scan-wikidata">[[File:Wikidata Query Service Favicon.svg|20px|éléments Wikidata|link=https://query.wikidata.org/embed.html#' .. mw.uri.encode(query, 'PATH') .. ']]</indicator>')
	else
		addRow(metadataTable, 'Fac-similés', args.source)
	end
	if args.avancement == 'T' then
		addRow(metadataTable, 'Avancement', '[[Category:Livres terminés]][[:Category:Livres terminés|Terminé]]')
	elseif args.avancement == 'V' then
		addRow(metadataTable, 'Avancement', '[[category:Livres à valider]][[:category:Livres à valider|À valider]]')
	elseif args.avancement == 'C' then
		addRow(metadataTable, 'Avancement', '[[category:Livres à corriger]][[:category:Livres à corriger|À corriger]]')
	elseif args.avancement == 'MS' then
		addRow(metadataTable, 'Avancement', '[[category:Livres à découper]][[:category:Livres à découper|Texte prêt à être découpé]]')
	elseif args.avancement == 'OCR' then
		addRow(metadataTable, 'Avancement', '[[category:Livres sans couche texte]][[:category:Livres sans couche texte|Ajouter une couche texte d’OCR]]')
	elseif args.avancement == 'X' then
		addRow(metadataTable, 'Avancement', '[[category:Extraits et compilations]][[:category:Extraits et compilations|Source incomplète: extrait ou compilation]]')
	elseif args.avancement == 'D' then
		addRow(metadataTable, 'Avancement', '[[category:Doublons]][[:category:Doublons|Index en double]]')
	elseif args.avancement == 'L' then
		addRow(metadataTable, 'Avancement', '[[category:Livres à réparer]]<span style="color: #FF0000; ">[[:category:Livres à réparer|Fichier source défectueux]]</span>')
	else
		addRow(metadataTable, 'Avancement', '[[Category:Livres d’avancement inconnu]][[:category:Livres d’avancement inconnu|Avancement inconnu]]')
	end
	addRow(metadataTable, 'Série', args.tomes)
	
	if args.pages then
		left:tag('div'):css('clear', 'both')
		left:tag('h3'):wikitext('Pages')
		left:tag('div'):attr('id', 'pagelist'):css({
			background = '#F0F0F0',
			['padding-left'] = '0.5em',
			['text-align'] = 'justify'
		}):newline():wikitext(args.pages):newline()
	else
		mw.addWarning('Vous devez rentrer la pagination du fac-similé (champ Pages)')
	end

	if args.sommaire or args.epigraphe then
		local right = html:tag('div'):css({
			width = '44%;',
			['padding-left'] = '1em',
			float = 'right'
		})
		if args.sommaire then
			right:tag('div'):attr('id', 'sommaire'):wikitext(args.sommaire)
		end
		if args.epigraphe then
			right:tag('hr'):css({
				['margin-top'] = '1em',
				['margin-bottom'] = '1em'
			})
			right:tag('div'):attr('id', 'epigraphe'):wikitext(args.epigraphe)
		end
	end
	
	if args.clef then
		html:wikitext('{{DEFAULTSORT:' .. args.clef .. '}}')
	end
	
	if args.type == 'book' then
		html:wikitext('[[Catégorie:Index - Livres]]')
	elseif args.type == 'journal' then
		html:wikitext('[[Catégorie:Index - Périodiques]]')
	elseif args.type == 'collection' then
		html:wikitext('[[Catégorie:Index - Recueils]]')
	elseif args.type == 'dictionary' then
		html:wikitext('[[Catégorie:Index - Dictionnaires]]')
	elseif args.type == 'phdthesis' then
		html:wikitext('[[Catégorie:Index - Thèses]]')
	end

	if args.source ~= 'djvu' then
		html:wikitext('[[Category:Livre non djvu]]')
	end
	if not args.sommaire then
		html:wikitext('[[Catégorie:Sommaire manquant]]')
	end

	return tostring(html)
end

local p = {}
 
function p.indexTemplate( frame )
    return indexTemplate( frame )
end
 
return p