[go: up one dir, main page]

Moduuli

Käsittely {{Lehtiviite}}-mallineen parametreille ja muotoilulle.

Tuki myös englanninkielisille parametreille helpottamaan siirtämistä.


local t={}

-- patterns copied from template Onko päiväys ISO 8601 -muodossa?
local function istimeinISO( datestring )
	if not datestring then
		return false
	end
	local result
	result = mw.ustring.match( datestring, "^%s*-?%d%d%d%d%-%d%d%-%d%d%s*$", 1 ) -- 2016-05-05
	if result then 
		return true
	end
	result = mw.ustring.match( datestring, "^%s*-?%d%d%d%d%-%d%d%-%d%dT%d%d:%d%d:%d%d?Z%s*$", 1 ) -- 2016-05-05T10:09:04Z
	if result then 
		return true
	end
	result = mw.ustring.match( datestring, "^%s*-?%d%d%d%d%d%d%d%dT%d%d%d%d%d%d?Z%s*$", 1 ) -- 20160505T100904Z
	if result then 
		return true
	end
	return false
end

local function localizedate( datestring )
	local tmp = datestring
	-- check for non-breakable spaces
	tmp = tmp:gsub( '\194\160', ' ' ):gsub( ' ', ' ' )

	local lang = mw.language.getContentLanguage()
	-- same as #time parser
	return lang:formatDate( "j.n.Y", tmp, true )
end

local function encbrackets( s )
	local tmp = s
	if mw.ustring.find(tmp, '%]') then
		tmp = mw.ustring.gsub( tmp, '%]', "]" )
	end
	if mw.ustring.find(tmp, '%[') then
		tmp = mw.ustring.gsub( tmp, '%[', "[" )
	end
	return tmp
end

local function formatpubmedid( pmid )
	-- some extra validation that id is a number
	local num = tonumber(pmid)
	if num ~= nil then
		return "[[PubMed]]:[https://pubmed.ncbi.nlm.nih.gov/".. pmid .. " " .. pmid .."]"
	end
	-- could just give error instead?
	return "PubMed: <span class='error'>Virheellinen PMID: ".. pmid .."</span> [[Luokka:Sivut, joissa on virheellinen PubMed-tunniste]]"
end

local function formatpubmedcentral( pmcid )
	-- should be plain number after PMC?
	local pmc_num = mw.ustring.sub( pmcid, 1, 3 )
	if pmc_num == "PMC" then
		-- begins with PMC
		pmc_num = mw.ustring.sub( pmcid, 4 )
	else
		-- plain number given
		pmc_num = pmcid
	end

	-- add PMC to url if there isn't,
	-- check there are only numbers in string
	local num = tonumber(pmc_num)
	if num ~= nil then
		return "[[PubMed Central]]:[https://www.ncbi.nlm.nih.gov/pmc/articles/PMC".. pmc_num .. " " .. pmc_num .."]"
	end
	return "PubMed Central: <span class='error'>Virheellinen PMC: ".. pmc_num .."</span> [[Luokka:Sivut, joissa on virheellinen PMC-tunniste]]"
end

local function formatdoi( doiid )
	local url = '//dx.doi.org/' .. mw.uri.encode( doiid ) 
	local htmlenc = mw.text.encode(doiid)
	if mw.ustring.find(htmlenc, '%]') then
		-- check: what was this case for? should just give error?
		htmlenc = mw.ustring.gsub( htmlenc, '%]', "&rsqb;" )
	end
	return "[[Digital object identifier|doi]]:[" .. url .. " " .. htmlenc .."]"
end

local function formathdl( hdlid )
	local url = 'https://hdl.handle.net/' .. mw.uri.encode( hdlid ) 
	local htmlenc = mw.text.encode(hdlid)
	return "HDL:[" .. url .. " " .. htmlenc .."]"
end

local function formatbibcode( bibcode )
	local CheckBibcode = require('Moduuli:Check bibcode')
	local err = CheckBibcode._verify_bibcode(bibcode)
	if err == nil then
		local url = 'http://adsabs.harvard.edu/abs/' .. mw.uri.encode( bibcode )
		local htmlenc = mw.text.encode(bibcode)
		return "[[Bibcode]]:[" .. url .. " " .. htmlenc .."]" -- .. err
	end
	return "Bibcode:<span class='error'>Virheellinen Bibcode: ".. bibcode ..", ".. err .."</span> [[Luokka:Sivut, joissa on virheellinen Bibcode-tunniste]]"
end

-- Semantic Scholar Corpus ID
local function formats2cid( s2cid )
	-- just a number
	local num = tonumber(s2cid)
	if num ~= nil then
		return "S2CID:[https://api.semanticscholar.org/CorpusID:".. s2cid .. " " .. s2cid .."]"
	end
	-- could just give error instead?
	return "S2CID:<span class='error'>Virheellinen S2CID: ".. s2cid .."</span> [[Luokka:Sivut, joissa on virheellinen S2CID-tunniste]]"
end

-- MathSciNet, Mathematical Reviews (MR)
local function formatmr( mrid )
	-- just a number
	local num = tonumber(mrid)
	if num ~= nil then
		return "[[Mathematical Reviews|MR]]:[https://mathscinet.ams.org/mathscinet-getitem?mr=".. mrid .. " " .. mrid .."]"
	end
	return "MR:<span class='error'>Virheellinen MR: ".. mrid .."</span> [[Luokka:Sivut, joissa on virheellinen MR-tunniste]]"
end

local function formatarxiv(arxiv)
	local arxivid = mw.uri.encode( arxiv )
	return '[[Arxiv|arXiv]]:[https://arxiv.org/abs/' .. arxivid .. ' ' .. arxiv .. ']'
end

local function formatjstor(jstor)
	-- should be number only?
	local jstorid = mw.uri.encode( jstor )
	return '[[JSTOR]]:[http://www.jstor.org/stable/' .. jstorid .. ' ' .. jstor .. ']'
end

local function formatisbn(isbn)
	local Isxn = require('Moduuli:ISxN')
	if Isxn._check_isbn(isbn) then
		return '[[Toiminnot:Kirjalähteet/' .. isbn .. '|ISBN&nbsp;' .. isbn ..']]'
	end
	return "<span class='error'>Virhe: Virheellinen ISBN-tunniste</span> [[Luokka:Sivut, joissa on virheellinen ISBN-tunniste]]"
end

local function formatissn(issn)
	return '[http://www.worldcat.org/issn/' .. issn .. ' ISSN&nbsp;' .. issn ..']'
	--local Isxn = require('Moduuli:ISxN')
	--if Isxn._check_issn(issn) then
		--return '[http://www.worldcat.org/issn/' .. issn .. ' ISSN&nbsp;' .. issn ..']'
	--end
	--return "<span class='error'>Virhe: Virheellinen ISSN-tunniste</span> [[Luokka:Sivut, joissa on virheellinen ISSN-tunniste]]"
end

local function formateissn(eissn)
	local Isxn = require('Moduuli:ISxN')
	if Isxn._check_issn(eissn) then
		return '[http://www.worldcat.org/issn/' .. eissn .. ' E-ISSN&nbsp;' .. eissn ..']'
	end
	return "<span class='error'>Virhe: Virheellinen ISSN-tunniste</span> [[Luokka:Sivut, joissa on virheellinen ISSN-tunniste]]"
end

local function formatismn(ismn)
	-- TODO: hakupalvelu?
	return 'ISMN:' .. ismn
end

local function formatoclc( oclc )
	local oclcid = mw.uri.encode( oclc )
	return '[[OCLC]]:[http://www.worldcat.org/oclc/' .. oclcid .. ' ' .. oclc .. ']'
end

-- Finna-tietueen tunniste, esimerkiksi vaari.1088896
local function formatfinna( finna )
	local finnaid = mw.uri.encode( finna )
	return 'Finna:[https://finna.fi/Record/' .. finnaid .. ' ' .. finna .. ']'
end

local function formatlanguagecode( kielinimi )
	return '<span style="font-size: 0.95em;">(' .. kielinimi .. ')</span>'
end

-- expecting two- or three-character code for language as input
local function getlanguagenamebycode( kieli )
	local Langue = require('Module:Fr:Langue')
	
	-- check the code is ok
	local kielikoodi = Langue.getCodeLangue3fin( kieli )
	if kielikoodi ~= "" then
		-- get localized name for language
		return Langue.getLanguageName(kielikoodi)
	end
	return ""
end

local function lehtiviite( args )
	local wiki = {}	
	local function insert( value )
		wiki[#wiki+1] = value
	end
	insert('<span class="lehtiviite" title="Lehtiviite">')

	-- vauthors is just a list in vancouver system
	local author = args['Tekijä'] or args['author'] or args['tekijä'] or args['vauthors'] or ""
	if author ~= "" then
		insert(author)
		insert(":&#32;")
	else
		local numeroton = false
		local sukunimi = args['Sukunimi'] or args['sukunimi'] or args['last'] or ""
		local etunimi = args['Etunimi'] or args['etunimi'] or args['first'] or ""
		if sukunimi  ~= "" and etunimi  ~= "" then
			insert(sukunimi)
			insert(", ")
			insert(etunimi)
			numeroton = true
		elseif sukunimi ~= "" then
			insert(sukunimi)
			numeroton = true
		elseif etunimi  ~= "" then
			insert(etunimi)
			numeroton = true
		end

		-- numeroidut rivit (jos käännös toisenkielistä wikistä)
		local numeroitu = false
		local rivinro = 1
		while (rivinro <= 9) do
			local nro = tostring(rivinro)
			sukunimi = args['Sukunimi' .. nro] or args['sukunimi' .. nro] or args['last' .. nro] or ""
			etunimi = args['Etunimi' .. nro] or args['etunimi' .. nro] or args['first' .. nro] or ""

			if sukunimi  ~= "" and etunimi  ~= "" then
				if (numeroton == true or rivinro > 1) then
					-- päätä edellinen ennen seuraavaa nimeä
					insert(';&#32;')
				end
				insert(sukunimi)
				insert(", ")
				insert(etunimi)
				numeroitu = true
			end
			rivinro = rivinro +1
		end
		if (numeroton == true or numeroitu == true) then
			-- päätä tekijä(t)
			insert(":&#32;")
		end
	end

	local otsikko = args['Otsikko'] or args['Nimeke'] or args['Nimike'] or args['nimeke'] or args['nimike'] or args['otsikko'] or args['title'] or ""
	if otsikko ~= "" then
		-- jos viimeinen merkki on kysymysmerkki?
		--local loppu = mw.ustring.sub( otsikko, -1 )
		local lopetusmerkki = args['Lopetusmerkki'] or ""
		insert(otsikko)
		-- if lopetusmerkki ~= "pois" and ~= "?" then
		if lopetusmerkki ~= "pois" then
			insert('.')
		end
		insert('&#32;')
	end
	local selite = args['Selite'] or args['selite'] or ""
	if selite ~= "" then
		insert("(")
		insert(selite)
		insert(")&#32;")
	end

	local julkaisu = args['Julkaisu'] or args['julkaisu'] or args['journal'] or args['magazine'] or args['newspaper'] or args['work'] or args['periodical'] or ""
	if julkaisu ~= "" then
		insert('<i>')
		insert(julkaisu)
		insert('</i>')
		insert(',&#32;')
	else
		insert("<span style='color:red; font-weight:bold;'>Määritä julkaisu!</span>[[Luokka:Sivut, joiden viitemallineissa on virheitä]]")
	end

	local ajankohta = args['Ajankohta'] or args['ajankohta'] or args['Vuosi'] or args['vuosi'] or args['year'] or args['date'] or ""
	if ajankohta ~= "" then
		--year, month, day eriteltynä? 
		--local month = args['month'] or ""
		--local day = args['day'] or ""
		if istimeinISO(ajankohta) then
			ajankohta = localizedate(ajankohta)
		end
		insert(ajankohta)

		local vuosikerta = args['Vuosikerta'] or args['vuosikerta'] or args['volume'] or ""
		local numero = args['Numero'] or args['numero'] or args['issue'] or ""
		local sivut = args['Sivut'] or args['sivut'] or args['pages'] or ""
		if vuosikerta ~= "" then
			insert(', ')
			insert(vuosikerta)
			insert('. vsk')
		end
		if numero ~= "" then
			insert(', nro ')
			insert(numero)
		end
		if sivut ~= "" then
			insert(', s. ')
			insert(sivut)
		end
		insert('.&#32;')
	else
		insert("<span style='color:red; font-weight:bold;'>Määritä ajankohta!</span>[[Luokka:Sivut, joiden viitemallineissa on virheitä]]")
	end

	local julkaisija = args['Julkaisija'] or args['julkaisija'] or args['publisher'] or args['agency'] or ""
	local julkaisupaikka = args['Julkaisupaikka'] or args['julkaisupaikka'] or args['location'] or ""
	if julkaisupaikka ~= "" then
		insert("&#32;")
		insert(julkaisupaikka)

		-- semicolon if place is followed by publisher, otherwise dot
		if julkaisija ~= "" then
			insert("&#58;&#32;")
		else
			insert(".")
		end
	end
	-- julkaisija (ilman julkaisupaikkaa)
	if julkaisija ~= "" then
		insert("&#32;")
		insert(julkaisija)
		insert(".")
	end

	local lainaus = args['Lainaus'] or args['lainaus'] or args['quote'] or ""
	if lainaus ~= "" then
		--<span class="citation">...</span>
		insert('&#32;”')
		insert(lainaus)
		insert('”')
	end

	local tunniste = args['Tunniste'] or args['tunniste'] or args['id'] or ""
	if tunniste ~= "" then
		insert("&#32;")
		insert(tunniste)
		--insert('.')
		insert('&#32;')
	end
	local pmid = args['PMID'] or args['Pmid'] or args['pmid'] or ""
	if pmid ~= "" then
		local pubmedlink = formatpubmedid(pmid)
		if pubmedlink ~= "" then
			insert("&#32;")
			insert(pubmedlink)
			--insert('.')
			insert('&#32;')
		end
	end
	local pmc = args['PMC'] or args['Pmc'] or args['pmc'] or ""
	if pmc ~= "" then
		local pmclink = formatpubmedcentral(pmc)
		if pmclink ~= "" then
			insert("&#32;")
			insert(pmclink)
			--insert('.')
			insert('&#32;')
		end
	end
	local doi = args['Doi'] or args['doi'] or ""
	if doi ~= "" then
		local doilink = formatdoi(doi)
		if doilink ~= "" then
			insert("&#32;")
			insert(doilink)
			--insert('.')
			insert('&#32;')
		end
	end
	local hdl = args['Hdl'] or args['hdl'] or args['HDL'] or ""
	if hdl ~= "" then
		local hdllink = formathdl(hdl)
		if hdllink ~= "" then
			insert("&#32;")
			insert(hdllink)
			--insert('.')
			insert('&#32;')
		end
	end
	
	local isbn = args['Isbn'] or args['ISBN'] or ""
	if isbn ~= "" then
		local isbnlink = formatisbn(isbn)
		if isbnlink ~= "" then
			insert("&#32;")
			insert(isbnlink)
			--insert('.')
			insert('&#32;')
		end
	end
	local issn = args['Issn'] or args['ISSN'] or args['issn'] or ""
	if issn ~= "" then
		local issnlink = formatissn(issn)
		if issnlink ~= "" then
			insert("&#32;")
			insert(issnlink)
			--insert('.')
			insert('&#32;')
		end
	end
	local eissn = args['Eissn'] or args['EISSN'] or args['eissn'] or ""
	if eissn ~= "" then
		local eissnlink = formateissn(eissn)
		if eissnlink ~= "" then
			insert("&#32;")
			insert(eissnlink)
			--insert('.')
			insert('&#32;')
		end
	end
	local ismn = args['Ismn'] or args['ISMN'] or args['ismn'] or ""
	if ismn ~= "" then
		local ismnlink = formatismn(ismn)
		if ismnlink ~= "" then
			insert("&#32;")
			insert(ismnlink)
			--insert('.')
			insert('&#32;')
		end
	end
	local bibcode = args['Bibcode'] or args['bibcode'] or ""
	if bibcode ~= "" then
		local bibcodelink = formatbibcode(bibcode)
		if bibcodelink ~= "" then
			insert("&#32;")
			insert(bibcodelink)
			--insert('.')
			insert('&#32;')
		end
	end
	local jstor = args['JSTOR'] or args['Jstor'] or args['jstor'] or ""
	if jstor ~= "" then
		local jstorlink = formatjstor(jstor)
		if jstorlink ~= "" then
			insert("&#32;")
			insert(jstorlink)
			--insert('.')
			insert('&#32;')
		end
	end
	-- Mathematical Reviews
	local mrid = args['MR'] or args['Mr'] or args['mr'] or ""
	if mrid ~= "" then
		local mridlink = formatmr(mrid)
		if mridlink ~= "" then
			insert("&#32;")
			insert(mridlink)
			--insert('.')
			insert('&#32;')
		end
	end
	-- Semantic Scholar
	local s2cid = args['S2CID'] or args['S2cid'] or args['s2cid'] or ""
	if s2cid ~= "" then
		local s2cidlink = formats2cid(s2cid)
		if s2cidlink ~= "" then
			insert("&#32;")
			insert(s2cidlink)
			--insert('.')
			insert('&#32;')
		end
	end
	local arxiv = args['Arxiv'] or args['arxiv'] or args['arXiv'] or ""
	if arxiv ~= "" then
		local arxivlink = formatarxiv(arxiv)
		if arxivlink ~= "" then
			insert("&#32;")
			insert(arxivlink)
			--insert('.')
			insert('&#32;')
		end
	end
	local oclc = args['Oclc'] or args['oclc'] or ""
	if oclc ~= "" then
		local oclclink = formatoclc(oclc)
		if oclclink ~= "" then
			insert("&#32;")
			insert(oclclink)
			--insert('.')
			insert('&#32;')
		end
	end

	-- Finna-tietueen tunniste, esimerkiksi vaari.1088896
	local finnaid = args['Finna'] or args['finna'] or ""
	if finnaid ~= "" then
		local finnalink = formatfinna(finnaid)
		if finnalink ~= "" then
			insert("&#32;")
			insert(finnalink)
			--insert('.')
			insert('&#32;')
		end
	end

	local viitattu = args['Viitattu'] or args['Luettu'] or args['viitattu'] or args['luettu'] or args['accessdate'] or ""
	local url = args['Osoite'] or args['www'] or args['url'] or args['osoite'] or args['URL'] or ""
	if url ~= "" then
		local linkkiteksti = args['www-teksti'] or "Artikkelin verkkoversio"
		insert(' ')
		insert('[')
		insert(url)
		insert(' ')
		insert(linkkiteksti)
		insert(']')
		insert('.')

		local tiedostomuoto = args['Tiedostomuoto'] or args['tiedostomuoto'] or args['format'] or ""
		if tiedostomuoto ~= "" then
			insert("&#32;")
			insert("<small>(")
			insert(tiedostomuoto)
			insert(")</small>")
		end
		if viitattu ~= "" then
			if istimeinISO(viitattu) then
				viitattu = localizedate(viitattu)
			end
			insert("&#32;")
			insert('Viitattu ')
			insert(viitattu)
			insert('.')
		end
	end
	local archiveurl = args['Arkisto'] or args['arkisto'] or args['archiveurl'] or args['archive-url'] or ""
	local arkistoaika = args['Arkistoitu'] or args['arkistoitu'] or args['archivedate'] or args['archive-date'] or ""
	if archiveurl ~= "" then
		insert(' ')
		insert('[')
		insert(archiveurl)
		insert(' ')
		insert('Arkistoitu')
		insert(']')
		
		if arkistoaika ~= "" then
			if istimeinISO(arkistoaika) then
				arkistoaika = localizedate(arkistoaika)
			end
			insert('&#32;')
			insert(arkistoaika)
		end
		insert('.')
	end

	local kieli = args['Kieli'] or args['kieli'] or args['language'] or ""
	if kieli ~= "" then
		-- already expanded to <span>lang</span> from a template
		local isexpanded = false
		if mw.ustring.find(kieli, "span") then
			isexpanded = true
		end
		
		if (isexpanded == true) then
			-- as-is
			insert('&#32;')
			insert(kieli)
		else
			-- might need conversion of language code?
			-- plain two-letter code?
			local kielinimi = getlanguagenamebycode(kieli)
			if kielinimi ~= "" then
				-- format to correct tag
				insert('&#32;')
				insert(formatlanguagecode(kielinimi))
			else
				-- language code not recognized -> can't give language name
				-- could give error of unknown language code?
				--
				--insert("<span class='error'>")
				insert('&#32;')
				insert(kieli)
				--insert("</span>")
			end
		end
	end

	insert('</span>')
	return table.concat( wiki )
end

function t.main( frame )
    local origArgs
    if frame == mw.getCurrentFrame() then
        origArgs = frame:getParent().args
        for k, v in pairs( frame.args ) do
            origArgs = frame.args
            break
        end
    else
        origArgs = frame
    end
    
    local args = {}
    for k, v in pairs( origArgs ) do
        if type( k ) == 'number' or v ~= '' then
            args[ k ] = v
        end
    end
    return lehtiviite( args )
end

return t;