Moduuli:Karttaviite
Siirry navigaatioon
Siirry hakuun
Moduuli
[muokkaa wikitekstiä]Käsittely {{Karttaviite}}-mallineen parametreille ja muotoilulle.
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 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, '%]', "]" )
end
return "[[Digital object identifier|doi]]:[" .. 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
local function formatisbn(isbn)
local Isxn = require('Moduuli:ISxN')
if Isxn._check_isbn(isbn) then
return '[[Toiminnot:Kirjalähteet/' .. isbn .. '|ISBN ' .. isbn ..']]'
end
return "<span class='error'>Virhe: Virheellinen ISBN-tunniste</span> [[Luokka:Sivut, joissa on virheellinen ISBN-tunniste]]"
end
local function formatissn(issn)
local Isxn = require('Moduuli:ISxN')
if Isxn._check_issn(issn) then
return '[http://www.worldcat.org/issn/' .. issn .. ' ISSN ' .. 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 ' .. eissn ..']'
end
return "<span class='error'>Virhe: Virheellinen ISSN-tunniste</span> [[Luokka:Sivut, joissa on virheellinen ISSN-tunniste]]"
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( koodi )
local tmpkoodi;
-- split two-part code into one for lookup
if mw.ustring.find(koodi, '-') then
local codes = mw.text.split( koodi, '-' )
tmpkoodi = mw.ustring.lower(mw.text.trim(codes[1]))
else
-- not a two-part code? try to find with as it is
tmpkoodi = mw.ustring.lower(mw.text.trim(koodi))
end
-- fast mode to get localized (short list)
local Symbolit = require('Moduuli:Kielisymbolit')
local kieliteksti = Symbolit._kieli( tmpkoodi )
if kieliteksti ~= "" then
-- localized form
return kieliteksti
end
-- check the code is ok
local Langue = require('Module:Fr:Langue')
local kielikoodi = Langue.getCodeLangue3fin( koodi )
if kielikoodi ~= "" then
-- get localized name for language (plain name)
return Langue.getLanguageName(kielikoodi)
end
return ""
end
local function karttaviite( args )
local wiki = {}
local function insert( value )
wiki[#wiki+1] = value
end
insert('<span class="karttaviite" title="Karttaviite">')
-- vauthors is just a list in vancouver system
local author = args['Tekijä'] or args['author'] or args['tekijä'] or args['vauthors'] or ""
--local coauthor = args['coauthors'] or "" -- additional field used in cite book
if author ~= "" then
insert(author)
insert(": ")
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('; ')
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(": ")
end
end
local otsikko = args['Nimeke'] or args['Nimike'] or args['nimeke'] or args['nimike'] or args['Otsikko'] or args['otsikko'] or args['title'] or ""
if otsikko ~= "" then
insert('<i>')
insert(otsikko)
insert('</i>')
local pilkku = true
-- jos nimeke päättyy kysymysmerkkiin ei lisätä välimerkkiä otsikon jälkeen
-- ei lisätä välipilkkua myöskään
local loppupiste = true
local viimeinen = mw.ustring.sub(otsikko, -1, -1)
if viimeinen == '?' then
loppupiste = false
pilkku = false
end
if viimeinen == '.' then
loppupiste = false
pilkku = false
end
-- jos otsikko päättyy pilkkuun ei tarvita toista pilkkua erottamaan muita kohtia
-- pilkun voisi poistaa artikkelista?
if viimeinen == ',' then
pilkku = false
end
local sivu = args['Sivu'] or args['sivu'] or args['page'] or ""
local sivut = args['Sivut'] or args['sivut'] or args['pages'] or ""
if sivu ~= "" then
if (pilkku == true) then
insert(',')
end
insert(' s. ')
insert(sivu)
loppupiste = true
end
if sivut ~= "" then
if (pilkku == true) then
insert(',')
end
insert(' s. ')
insert(sivut)
loppupiste = true
end
insert(" ")
else
insert("<span style='color:red; font-weight:bold;'>Määritä nimeke!</span>[[Luokka:Sivut, joiden viitemallineissa on virheitä]][[Luokka:Nimeke-parametri puuttuu]]")
end
local mittakaava = args['Mittakaava'] or args['mittakaava'] or ""
if mittakaava ~= "" then
insert(" ")
insert(mittakaava)
insert(".")
end
-- karttalehti
local lehti = args['Lehti'] or args['lehti'] or ""
if lehti ~= "" then
insert(' ')
insert(lehti)
insert(".")
insert(" ")
end
local selite = args['Selite'] or args['selite'] or ""
if selite ~= "" then
insert(' ')
insert('(')
insert(selite)
insert(')')
insert(" ")
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 ""
local ajankohta = args['Ajankohta'] or args['ajankohta'] or args['Vuosi'] or args['vuosi'] or args['year'] or args['date'] or args['publication-date'] or ""
if julkaisupaikka ~= "" then
insert(" ")
insert(julkaisupaikka)
-- semicolon if place is followed by publisher, otherwise dot
if julkaisija ~= "" and julkaisija ~= "-" then
insert(": ")
else
insert(".")
insert(' ')
end
end
-- julkaisija (ilman julkaisupaikkaa)
if julkaisija == "-" then
-- PASS
elseif julkaisija ~= "" then
insert(" ")
insert(julkaisija)
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(', ')
insert(ajankohta)
end
insert(".")
insert(' ')
--else
-- ei pakollinen karttaviitteelle
--insert("<span style='color:red; font-weight:bold;'>Määritä julkaisija!</span>[[Luokka:Sivut, joiden viitemallineissa on virheitä]][[Luokka:Julkaisija-parametri puuttuu]]")
end
-- tunnisteet, joita aiemmin ei ole tuettu: ismn issn jfm jstor lccn mr oclc ol osti pmc pmid rfc ssrn zbl id
local tunniste = args['Tunniste'] or args['tunniste'] or args['id'] or ""
if tunniste ~= "" then
insert(" ")
insert(tunniste)
--insert('.')
insert(' ')
end
local isbn = args['Isbn'] or args['ISBN'] or args['isbn'] or ""
if isbn ~= "" then
local isbnlink = formatisbn(isbn)
if isbnlink ~= "" then
insert(" ")
insert(isbnlink)
--insert('.')
insert(' ')
end
end
local issn = args['Issn'] or args['ISSN'] or args['issn'] or ""
if issn ~= "" then
local issnlink = formatissn(issn)
if issnlink ~= "" then
insert(" ")
insert(issnlink)
--insert('.')
insert(' ')
end
end
local eissn = args['Eissn'] or args['EISSN'] or args['eissn'] or ""
if eissn ~= "" then
local eissnlink = formateissn(eissn)
if eissnlink ~= "" then
insert(" ")
insert(eissnlink)
--insert('.')
insert(' ')
end
end
local oclc = args['Oclc'] or args['oclc'] or ""
if oclc ~= "" then
local oclclink = formatoclc(oclc)
if oclclink ~= "" then
insert(" ")
insert(oclclink)
--insert('.')
insert(' ')
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(" ")
insert(finnalink)
--insert('.')
insert(' ')
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
-- jos wikidata-url osoitteessa niin ei näytetä mitään?
if mw.ustring.match(url, "https://www.wikidata.org/wiki/") then
url = ""
end
end
if url ~= "" then
local linkkiteksti = args['www-teksti'] or "Teoksen verkkoversio"
insert(' ')
insert('[')
insert(url)
insert(' ')
insert(linkkiteksti)
insert(']')
local tiedostomuoto = args['Tiedostomuoto'] or args['tiedostomuoto'] or args['format'] or ""
if tiedostomuoto ~= "" then
insert(" ")
insert("<small>(")
insert(tiedostomuoto)
insert(")</small>")
end
if viitattu ~= "" then
if istimeinISO(viitattu) then
viitattu = localizedate(viitattu)
end
insert(" ")
insert('Viitattu ')
insert(viitattu)
end
insert('.')
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(' ')
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(' ')
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(' ')
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(' ')
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 karttaviite( args )
end
return t;