ਮੌਡਿਊਲ:Index template

ਵਿਕੀਸਰੋਤ ਤੋਂ

Documentation for this module may be created at ਮੌਡਿਊਲ:Index template/doc

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 .. '|ਵਿਕੀਡਾਟਾ ਤੇ ਦੇਖੋ]]')
    	if category ~= nil then
    		tag:wikitext('[[category:' .. 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('[[Category: ਵਿਕੀਡਾਟਾ ID ਵਾਲੀਆਂ ਕਿਤਾਬਾਂ]]<indicator name="wikidata">[[File:Wikidata.svg|20px|élément Wikidata|link=d:' .. item.id .. ']]</indicator>')
        else
        html:wikitext('[[Category: ਬਿਨਾਂ ਵਿਕੀਡਾਟਾ ID ਵਾਲੀਆਂ ਕਿਤਾਬਾਂ]]')
	end

    --Left part
    local left = html:tag('div')
    if args.remarks or args.notes 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 .. '|250px]]')
	        else
	            imageContainer:wikitext('[[File:' .. imageName .. '|250px]]')
	        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.title then
       if item then
    		addRow(metadataTable, 'ਸਿਰਲੇਖ', withWikidataLink(args.title))
		else 
    		addRow(metadataTable, 'ਸਿਰਲੇਖ', '[[' .. args.title .. ']]')
    	end
    else
    	mw.addWarning('ਕਿਰਪਾ ਕਰਕੇ ਕਿਤਾਬ ਦਾ ਸਿਰਲੇਖ ਦਾਓ')
    end

	addRow(metadataTable, 'ਉਪ-ਸਿਰਲੇਖ', withWikidataLink(args.subtitle))

if args.volume then
     addRow(metadataTable, 'ਜਿਲਦ',  ''.. args.volume .. '')
     html:wikitext('[[Category:ਇੱਕ ਤੋਂ ਵੱਧ ਜਿਲਦ ਵਾਲੀਆਂ ਕਿਤਾਬਾਂ]]')
else 
	html:wikitext('[[Category:ਇੱਕ ਜਿਲਦ ਵਾਲੀਆਂ ਕਿਤਾਬਾਂ]]')
end

if args.edition then
	addRow(metadataTable, 'ਅਡੀਸ਼ਨ', '' .. args.edition .. '')
	html:wikitext('[[Category:ਅਡੀਸ਼ਨ ਵਾਲੀਆਂ ਕਿਤਾਬਾਂ]]')
else 
	html:wikitext('[[Category:ਬਿਨਾਂ ਅਡੀਸ਼ਨ ਵਾਲੀਆਂ ਕਿਤਾਬਾਂ]]')
end

if args.author then
if item then
    addRow(metadataTable, 'ਲੇਖਕ', withWikidataLink(args.author))
   html:wikitext('[[Category:ਲੇਖਕ ਵਾਲੀਆਂ ਕਿਤਾਬਾਂ]]')
--   local author = {{#invoke:delink|delink|mw.text.nowiki(args.author)}}
--  local author = {{delink|mw.text.nowiki(args.author)}}
--      html:wikitext('[[Category:' .. author.text ..' ਦੀਆਂ ਕਿਤਾਬਾਂ]]')

  else 
   addRow(metadataTable, 'ਲੇਖਕ', '{{Al|' .. args.author  .. '}}')
 end
 else
 	html:wikitext('[[Category:ਬਿਨਾਂ ਲੇਖਕ ਵਾਲੀਆਂ ਕਿਤਾਬਾਂ]]')
end

if args.translator then
if item then
	addRow(metadataTable, 'ਅਨੁਵਾਦਕ', withWikidataLink(args.translator))
	html:wikitext('[[Category:ਅਨੁਵਾਦਕ ਵਾਲੀਆਂ ਕਿਤਾਬਾਂ]]')
  else 
   addRow(metadataTable, 'ਅਨੁਵਾਦਕ', '' .. args.translator .. '')
 end
 else
 	html:wikitext('[[Category:ਬਿਨਾਂ ਅਨੁਵਾਦਕ ਵਾਲੀਆਂ ਕਿਤਾਬਾਂ]]')
end

if args.editor then
if item then
	addRow(metadataTable, 'ਸੰਪਾਦਕ', withWikidataLink(args.editor))
	html:wikitext('[[Category:ਸੰਪਾਦਕ ਵਾਲੀਆਂ ਕਿਤਾਬਾਂ]]')
 else
   addRow(metadataTable, 'ਸੰਪਾਦਕ', '' .. args.editor .. '')
end
  else 
  	html:wikitext('[[Category:ਬਿਨਾਂ ਸੰਪਾਦਕ ਦੀਆਂ ਕਿਤਾਬਾਂ]]')
 end
if args.illustrator then
     addRow(metadataTable, 'ਚਿੱਤਰਕਾਰ', withWikidataLink(args.illustrator))
     html:wikitext('[[Category:ਚਿੱਤਰਕਾਰ ਵਾਲੀਆਂ ਕਿਤਾਬਾਂ]]')
else 
	html:wikitext('[[Category:ਬਿਨਾਂ ਚਿੱਤਰਕਾਰ ਵਾਲੀਆਂ ਕਿਤਾਬਾਂ]]')
end

if args.publisher then
if item then
	addRow(metadataTable, 'ਪ੍ਰਕਾਸ਼ਕ', withWikidataLink(args.publisher))
	html:wikitext('[[Category:ਪ੍ਰਕਾਸ਼ਕ ਵਾਲੀਆਂ ਕਿਤਾਬਾਂ]]')
   -- {{suppress categories|html:wikitext[[Category:No Publisher]]}} 
--    {{#invoke:Suppress categories|main|html:wikitext[[Category:No Publisher]]}} 
  else 
	addRow(metadataTable, 'ਪ੍ਰਕਾਸ਼ਕ', withWikidataLink(args.publisher))
	html:wikitext('[[Category:ਪ੍ਰਕਾਸ਼ਕ ਵਾਲੀਆਂ ਕਿਤਾਬਾਂ]]')
 end
 else
 		html:wikitext('[[Category:ਬਿਨਾਂ ਪ੍ਰਕਾਸ਼ਕ ਵਾਲੀਆਂ ਕਿਤਾਬਾਂ]]')

end

if args.publishedin then
     addRow(metadataTable, 'ਪ੍ਰਕਾਸ਼ਨ ਦੇਸ਼', withWikidataLink(args.publishedin))
     html:wikitext('[[Category:ਪ੍ਰਕਾਸ਼ਨ ਦੇਸ਼ ਵਾਲੀਆਂ ਕਿਤਾਬਾਂ]]')
else 
	html:wikitext('[[Category:ਬਿਨਾਂ ਪ੍ਰਕਾਸ਼ਨ ਦੇਸ਼ ਵਾਲੀਆਂ ਕਿਤਾਬਾਂ]]')
end
 
if args.address then
     addRow(metadataTable, 'ਪਤਾ', withWikidataLink(args.address))
     html:wikitext('[[Category:ਪਤੇ ਵਾਲੀਆਂ ਕਿਤਾਬਾਂ]]')
else 
	html:wikitext('[[Category:ਬਿਨਾਂ ਪਤੇ ਵਾਲੀਆਂ ਕਿਤਾਬਾਂ]]')
end
	
    if args.year then
	addRow(metadataTable, 'ਸਾਲ', withWikidataLink(args.year))
	html:wikitext('[[Category:ਸਾਲ ਵਾਲੀਆਂ ਕਿਤਾਬਾਂ]]')
else 
	html:wikitext('[[Category:ਬਿਨਾਂ ਸਾਲ ਵਾਲੀਆਂ ਕਿਤਾਬਾਂ]]')
end

if args.printer then
     addRow(metadataTable, 'ਛਾਪਕ', withWikidataLink(args.printer))
     html:wikitext('[[Category:ਛਾਪਕ ਵਾਲੀਆਂ ਕਿਤਾਬਾਂ]]')
else 
	html:wikitext('[[Category:ਬਿਨਾਂ ਛਾਪਕ ਵਾਲੀਆਂ ਕਿਤਾਬਾਂ]]')
end

	if args.source == 'djvu' or args.source == 'pdf' then
		addRow(metadataTable, 'ਸਰੋਤ', '[[: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://bn.wikisource.org/> . }\n  OPTIONAL { ?item wdt:P304 ?pages . }\n  SERVICE wikibase:label { bd:serviceParam wikibase:language "[AUTO_LANGUAGE],bn".\n}}'
		html:wikitext('<indicator name="index-scan-wikidata">[[File:Wikidata Query Service Favicon.svg|20px|ਵਿਕੀਡਾਟਾ ਆਈਟਮਾਂ|link=https://query.wikidata.org/embed.html#' .. mw.uri.encode(query, 'PATH') .. ']]</indicator>')
	else
		addRow(metadataTable, 'ਸਰੋਤ', args.source)
	end
	if args.progress == 'T' then
		addRow(metadataTable, 'ਪ੍ਰਗਤੀ', '[[Category:ਸੰਪੂਰਨ ਕਿਤਾਬਾਂ]] [[:Category:ਸੰਪੂਰਨ ਕਿਤਾਬਾਂ|ਸੰਪੂਰਨ]]')
	elseif args.progress == 'V' then
		addRow(metadataTable, 'ਪ੍ਰਗਤੀ', '[[category:ਵੈਲੀਡੇਟ ਕਰਨ ਵਾਲੀਆਂ ਕਿਤਾਬਾਂ]] [[:Category:ਵੈਲੀਡੇਟ ਕਰਨ ਵਾਲੀਆਂ ਕਿਤਾਬਾਂ|ਵੈਲੀਡੇਟ ਕਰਨਾ ਬਾਕੀ]]')
	elseif args.progress == 'C' then
		addRow(metadataTable, 'ਪ੍ਰਗਤੀ', '[[category:ਪਰੂਫ਼ਰੀਡ ਕਰਨ ਵਾਲੀਆਂ ਕਿਤਾਬਾਂ]] [[:category:ਪਰੂਫ਼ਰੀਡ ਕਰਨ ਵਾਲੀਆਂ ਕਿਤਾਬਾਂ|ਪਰੂਫ਼ਰੀਡ ਕਰਨਾ ਬਾਕੀ]]')
	elseif args.progress == 'L' then
		addRow(metadataTable, 'ਪ੍ਰਗਤੀ', '[[category:ਨੁਕਸ ਵਾਲੀਆਂ ਕਿਤਾਬਾਂ]] <span style = "color: # FF0000;"> [[:category:ਨੁਕਸ ਵਾਲੀਆਂ ਕਿਤਾਬਾਂ|ਸਰੋਤ ਫਾਈਲ ਵਿੱਚ ਨੁਕਸ]]</span>')
	else
		addRow(metadataTable, 'ਪ੍ਰਗਤੀ', '[[Category:ਬਗ਼ੈਰ ਪ੍ਰਗਤੀ ਵਾਲੀਆਂ ਕਿਤਾਬਾਂ]] [[:category:ਬਗ਼ੈਰ ਪ੍ਰਗਤੀ ਵਾਲੀਆਂ ਕਿਤਾਬਾਂ|ਬਗ਼ੈਰ ਪ੍ਰਗਤੀ ਵਾਲੀਆਂ ਕਿਤਾਬਾਂ]]')
	end
	addRow(metadataTable, 'ਲੜੀ', args.volumes)
	
	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('You must enter the pagination of the facsimile (Pages field) ')
	end

	if args.remarks or args.notes then
		local right = html:tag('div'):css({
			width = '44%;',
			['padding-left'] = '1em',
			float = 'right'
		})
		if args.remarks then
			right:tag('div'):attr('id', 'remarks'):wikitext(args.remarks)
		end
		if args.notes then
			right:tag('hr'):css({
				['margin-top'] = '1em',
				['margin-bottom'] = '1em'
			})
			right:tag('div'):attr('id', 'notes'):wikitext(args.notes)
		end
	end

	if args.source == 'djvu' then
		html:wikitext('[[Category:DJVU ਫਾਰਮੈਟ ਵਾਲੀ ਫਾਈਲ]] ')
	elseif args.source == 'pdf' then
		html: wikitext ('[[Category:PDF ਫਾਰਮੈਟ ਵਾਲੀ ਫਾਈਲ]]') 
	elseif args.source == 'ogg' then
		html: wikitext ('[[Category:OGG ਫਾਰਮੈਟ ਵਾਲੀ ਫਾਈਲ]]') 
	elseif args.source == 'webm' then
		html: wikitext ('[[Category:WEBM ਫਾਰਮੈਟ ਵਾਲੀ ਫਾਈਲ]]') 
	else
		html: wikitext ('[[Category:ਬਿਨਾਂ ਫਾਰਮੈਟ ਵਾਲੀ ਫਾਈਲ]]')
	end
	if not args.remarks then
		html: wikitext ('[[Category:ਇੰਡੈਕਸ ਵਾਲੇ ਸਫ਼ੇ]]') 
	end

	return tostring(html)
end

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