Modul:Citation: Unterschied zwischen den Versionen

Aus skandinavien-wiki.net
K (+Abruf)
(Umstellung)
Zeile 2: Zeile 2:
local scrollGallery = {
local scrollGallery = {
suite  = 'Citation',
suite  = 'Citation',
serial = '2020-03-22',
serial = '2020-03-23',
}
}


Zeile 14: Zeile 14:
local ci = {}
local ci = {}


-- global variables
local types = {
local types = {
book      = { 'book', 'Buch' },
book      = { 'book', 'Buch' },
Zeile 33: Zeile 34:
abbr      = { 'Abk', types = { 'collection', 'journal', 'newspaper' } },
abbr      = { 'Abk', types = { 'collection', 'journal', 'newspaper' } },
url        = { 'Online', 'URL' },
url        = { 'Online', 'URL' },
articleUrl = { 'Onlineaufsatz', types = { 'collection', 'journal', 'newspaper' } },
articleUrl = { 'Onlineaufsatz', 'Aufsatz-URL', types = { 'collection', 'journal', 'newspaper' } },
jstor      = { 'JSTOR', types = { 'journal', 'newspaper' } },
jstor      = { 'JSTOR', types = { 'journal', 'newspaper' } },
kBytes     = { 'kBytes' },
KBytes     = { 'KBytes', 'kBytes', 'kbytes' },
accessDate = { 'Zugriff', 'Abruf' },
accessDate = { 'Zugriff', 'Abruf' },
scale      = { 'Maßstab', types = 'map' },
scale      = { 'Maßstab', types = 'map' },
Zeile 69: Zeile 70:
tiScale = 'Maßstab: %s. ',
tiScale = 'Maßstab: %s. ',
tiIn = 'In: ',
tiIn = 'In: ',
tiChapter = "Kapitel ''%s''",
puAll = '%s.',
puAll = '%s.',
puPlaceDate = '%s, %s',
puPlaceDate = '%s, %s',
Zeile 93: Zeile 95:
dbIssn = '[[w:Internationale Standardnummer für fortlaufende Sammelwerke|ISSN]] [http://zdb-katalog.de/list.xhtml?t=iss%3D%22xx1xx%22&key=cql xx1xx]',
dbIssn = '[[w:Internationale Standardnummer für fortlaufende Sammelwerke|ISSN]] [http://zdb-katalog.de/list.xhtml?t=iss%3D%22xx1xx%22&key=cql xx1xx]',
dbJstor = '[[w:JSTOR|JSTOR]] [https://www.jstor.org/stable/%s %s]',
dbJstor = '[[w:JSTOR|JSTOR]] [https://www.jstor.org/stable/%s %s]',
wbAccess = 'Internetabruf am %s',
wbKBytes = 'Dateigröße %s KByte'
}
}


Zeile 108: Zeile 112:


local errorMsgs = {}
local errorMsgs = {}
local dateFormat = ''


-- check if table contains the value
-- check if table contains the value
Zeile 139: Zeile 144:
end
end


local function cleanupParameters( value )
-- string cleanup
if not value or value == '' then
local function cleanupParameters( s )
return value
if not s or s == '' then
return s
end
end
value = value:gsub( '[\009\010\013]', ' ' ) -- horizontal tab, line feed, carriage return
s = s:gsub( '[\009\010\013]', ' ' ) -- horizontal tab, line feed, carriage return
value = value:gsub( '[%z%c]', '' ) -- control characters
s = s:gsub( '[%z%c]', '' ) -- control characters
value = value:gsub( ' ', ' ' );
s = s:gsub( ' ', ' ' );
value = value:gsub( '\226\128\138', ' ' ); -- hair space
s = s:gsub( '\226\128\138', ' ' ); -- hair space
value = mw.ustring.gsub( value, '[\226\128\141\226\128\139\194\173]', '' ); -- zero-width joiner, zero-width space, soft hyphen
s = mw.ustring.gsub( s, '[\226\128\141\226\128\139\194\173]', '' ); -- zero-width joiner, zero-width space, soft hyphen
value = mw.ustring.gsub( value, '%.%.%.', '…' )
s = mw.ustring.gsub( s, '%.%.%.', '…' )
value = mw.ustring.gsub( value, '%.%.', '‥' )
s = mw.ustring.gsub( s, '%.%.', '‥' )
value = mw.ustring.gsub( value, '<br%s*/*>', '' )
s = mw.ustring.gsub( s, '<br%s*/*>', '' )
value = mw.ustring.gsub( value, '</*p%s*/*>', '' )
s = mw.ustring.gsub( s, '</*p%s*/*>', '' )
return mw.ustring.gsub( value, '</*span%s*/*>', '' )
return mw.ustring.gsub( s, '</*span%s*/*>', '' )
end
end


Zeile 177: Zeile 183:
end
end
args[ key ] = args[ key ] or ''
args[ key ] = args[ key ] or ''
end
end
-- no missing items
for key, value in pairs( params ) do
if not args[ key ] then
args[ key ] = ''
end
end
end
end
Zeile 193: Zeile 205:
end
end
return args
return args
end
local function getPageNumbers( pages )
if not pages then
return ''
end
pages = ( '' .. pages ):gsub( '–', '-' ); -- replace endashes with hyphens
return pages:gsub( '&%w+;', '-' ); -- replace html entities with hyphens
end
local function checkValues( args )
local i, key, value
args.pages = getPageNumbers( args.pages )
args.columns = getPageNumbers( args.columns )
args.noError = yn( args.noError, false )
if args.noError then
args.noError = 'true'
else
args.noError = 'false'
end
if args.date ~= '' then
if args.date:match( '^[12]%d%d%d%-[01]?%d%-[0123]?%d$' ) then
dateFormat = 'j. F Y'
elseif args.date:match( '^[12]%d%d%d%-[01]?%d%$' ) then
dateFormat = 'M Y'
elseif args.date:match( '^[12]%d%d%d%' ) then
dateFormat = 'Y'
else
args.date = args.date:match( '%d+' ) or ''
if args.date ~= '' then
i = tonumber( args.date )
if i >= 1000 and i < 2100 then
dateFormat = 'Y'
else
args.date = ''
table.insert( errorMsgs, texts.wrongDate )
end
end
end
end
if args.accessDate ~= '' and
not args.accessDate:match( '^20%d%d%-[01]?%d%-[0123]?%d$' ) then
args.accessDate = ''
table.insert( errorMsgs, texts.wrongDate )
end
i = 0
for key, value in ipairs( { 'url', 'articleUrl' } ) do
if args[ value ] ~= '' then
if uc.isUrl( args[ value ] ) > 2 then
args[ value ] = ''
i = i + 1
end
end
end
if i > 0 then
table.insert( errorMsgs, texts.noURL )
end
for key, value in ipairs( { 'dnb', 'oclc', 'jstor', 'KBytes' } ) do
if not args[ value ]:match( '^%d+$' ) then
args[ value ] = ''
end
end
args.edition = args.edition:match( '%d+' ) or ''
if args.edition == '1' then
args.edition = ''
end
if args.type ~= 'map' then
args.scale = ''
end
end
end


Zeile 212: Zeile 300:


local function makeTitle( title, volume, url, scale )
local function makeTitle( title, volume, url, scale )
if not title or title == '' then
if title == '' then
title = texts.noTitle
title = texts.noTitle
table.insert( errorMsgs, texts.unknownTitle )
table.insert( errorMsgs, texts.unknownTitle )
end
end
if url and url ~= '' then
if url ~= '' then
if uc.isUrl( url ) > 2 then
title = '[' .. url .. ' ' .. title .. ']'
table.insert( errorMsgs, texts.noURL )
else
title = '[' .. url .. ' ' .. title .. ']'
end
end
end
Zeile 242: Zeile 326:
end
end


local function makeIssnLink( issn )
local function makeIssnLink( issn, noError )
local text = ''
if noError ~= 'true' then
text = texts.wrongIssn
end
return formatters.dbIssn:gsub( 'xx1xx', issn )
return formatters.dbIssn:gsub( 'xx1xx', issn )
.. cx.check_issn( { args = { [ 1 ] = issn, error = texts.wrongIssn } } )
.. cx.check_issn( { args = { [ 1 ] = issn, error = text } } )
end
 
local function getNoError( noEerror )
noError = yn( noError, false )
if noError then
return 'true'
else
return 'false'
end
end
end


local function makePublished( args )
local function makePublisher( args, tab )
args.edition = args.edition:match( '%d+' ) or ''
if args.edition == '1' then
args.edition = ''
end
if args.dnb ~= '' and not tonumber( args.dnb ) then
args.dnb = ''
end
if args.oclc ~= '' and not tonumber( args.oclc ) then
args.oclc = ''
end
 
args.noError = getNoError( args.noError )
 
local theDate = args.date
local theDate = args.date
local published = ''
local published = ''
if theDate ~= '' and args.edition ~= '' then
if theDate ~= '' and args.edition ~= '' then
theDate = mw.ustring.format( formatters.puDateEdition, args.date, args.edition )
theDate = mw.ustring.format( formatters.puDateEdition, args.date, args.edition )
Zeile 287: Zeile 354:
published = mw.ustring.format( formatters.puPlaceDate, published, theDate )
published = mw.ustring.format( formatters.puPlaceDate, published, theDate )
end
end
if published ~= '' then
table.insert( tab, published )
end
end


local additions = { published }
local function makeSeries( args, tab )
if args.series ~= '' then
local key, value
table.insert( additions, mw.ustring.format( formatters.addSeries, args.series ) )
for key, value in ipairs( { 'series', 'series2', 'series3' } ) do
end
if args[ value ] ~= '' then
if args.series2 ~= '' then
table.insert( tab, mw.ustring.format( formatters.addSeries, args[ value ] ) )
table.insert( additions, mw.ustring.format( formatters.addSeries, args.series2 ) )
end
end
if args.series3 ~= '' then
table.insert( additions, mw.ustring.format( formatters.addSeries, args.series3 ) )
end
end
end
local function makeIsbn( args, tab )
if args.isbn ~= '' then
if args.isbn ~= '' then
table.insert( additions,
table.insert( tab,
li._linkISBNSet( { isbn = args.isbn, noerror = args.noError } ) )
li._linkISBNSet( { isbn = args.isbn, noerror = args.noError } ) )
elseif args.dnb ~= '' then
elseif args.dnb ~= '' then
table.insert( additions, mw.ustring.format( formatters.dbDnb, args.dnb, args.dnb ) )
table.insert( tab, mw.ustring.format( formatters.dbDnb, args.dnb, args.dnb ) )
elseif args.oclc ~= '' then
elseif args.oclc ~= '' then
table.insert( additions, mw.ustring.format( formatters.dbOclc, args.oclc, args.oclc ) )
table.insert( tab, mw.ustring.format( formatters.dbOclc, args.oclc, args.oclc ) )
end
end
if args.issn ~= '' then
if args.issn ~= '' then
table.insert( additions, makeIssnLink( args.issn ) )
table.insert( tab, makeIssnLink( args.issn, args.noError ) )
end
end
end
local function makePages( args, tab )
if args.pages ~= '' then
if args.pages ~= '' then
table.insert( additions,
table.insert( tab,
mw.ustring.format( formatters.addPages, args.pages:gsub( '-', '–' ) ) )
mw.ustring.format( formatters.addPages, args.pages:gsub( '-', '–' ) ) )
end
end
if args.columns ~= '' then
if args.columns ~= '' then
table.insert( additions,
table.insert( tab,
mw.ustring.format( formatters.addColumns, args.columns:gsub( '-', '–' ) ) )
mw.ustring.format( formatters.addColumns, args.columns:gsub( '-', '–' ) ) )
end
end
end


additions = { table.concat( additions, formatters.addDelimiter1 ) }
local function makeDoi( args, tab )
if args.extent ~= '' then
if args.extent ~= '' then
table.insert( additions, args.extent )
table.insert( tab, args.extent )
end
end
if args.doi ~= '' then
if args.doi ~= '' then
table.insert( additions,
table.insert( tab,
mw.ustring.format( formatters.addDoi, makeDoiLink( args.doi ) ) )
mw.ustring.format( formatters.addDoi, makeDoiLink( args.doi ) ) )
end
end
if args.jstor ~= '' then
table.insert( tab,
mw.ustring.format( formatters.dbJstor, args.jstor, args.jstor ) )
end
end
local function makeWebInfo( args, tab )
if args.url ~= '' or args.articleUrl ~= '' then
if args.accessDate ~= '' then
args.accessDate = getDate( args.accessDate, 'j. F Y' )
if args.accessDate ~= '' then
table.insert( tab,
mw.ustring.format( formatters.wbAccess, args.accessDate ) )
end
end
if args.KBytes ~= '' then
table.insert( tab,
mw.ustring.format( formatters.wbKBytes, args.KBytes ) )
end
end
end


additions = table.concat( additions, formatters.addDelimiter2 )
local function makeLanguageComment( args, result )
if args.language ~= '' then
if args.language ~= '' then
additions = additions .. mw.ustring.format( formatters.addLanguage, args.language )
result = result .. mw.ustring.format( formatters.addLanguage, args.language )
end
end
if additions ~= '' then
if result ~= '' then
additions = mw.ustring.format( formatters.puAll, additions )
result = mw.ustring.format( formatters.puAll, result )
end
end
if args.comment ~= '' then
if args.comment ~= '' then
additions = additions .. mw.ustring.format( formatters.addComment, args.comment )
result = result .. mw.ustring.format( formatters.addComment, args.comment )
end
end
return result
end


return additions
local function makePublished( args )
result = {}
makePublisher( args, result )
makeSeries( args, result )
makeIsbn( args, result )
if args.chapter ~= '' then
table.insert( mw.ustring.format( formatters.tiChapter, args.chapter ) )
end
makePages( args, result )
if #result > 0 then
result = { table.concat( result, formatters.addDelimiter1 ) }
end
 
makeDoi( args, result )
makeWebInfo( args, result )
result = table.concat( result, formatters.addDelimiter2 )
 
return makeLanguageComment( args, result )
end
end


local function makeBook( args )
local function makeBook( args )
if args.type ~= 'map' then
args.scale = ''
end
local author, title
local author, title
Zeile 377: Zeile 488:
title = args.newspaper
title = args.newspaper
end
end
if not title or title == '' then
if title == '' then
title = texts.noTitel
title = texts.noTitel
table.insert( errorMsgs, texts.unknownPeriodical )
table.insert( errorMsgs, texts.unknownPeriodical )
Zeile 412: Zeile 523:
end
end


local function makePeriodical( args )
local function makeJournalInfo( args, tab )
args.noError = getNoError( args.noError )
local year = ''
 
if args.date ~= '' then
local result = { makeAuthor( args.author, '' )
year = getDate( args.date, dateFormat )
.. makeTitle( args.title, '', args.articleUrl, '' ) .. formatters.tiIn
.. makeJournalTitle( args ) }
 
if args.isbn and args.isbn ~= '' then
table.insert( result,
li._linkISBNSet( { isbn = args.isbn, noerror = args.noError } ) )
end
end
if args.issn and args.issn ~= '' then
local volume = ''
table.insert( result, makeIssnLink( args.issn ) )
if args.volume ~= '' then
end
volume = mw.ustring.format( formatters.prVolume, args.volume )
 
if args.issue ~= '' then
if args.type == 'journal' then
volume = mw.ustring.format( formatters.prVolIssue, volume, args.issue )
local aFormat = 'Y'
if args.date:match( '^[12]%d+%-%d+' ) then
aFormat = 'M Y'
end
end
local year = getDate( args.date, aFormat )
if year ~= '' then
if year == '' and args.date ~= '' then
volume = mw.ustring.format( formatters.prVolYear, volume, year )
args.date = args.date:match( '%d+' ) or ''
if args.date ~= '' then
aFormat = 'Y'
year = getDate( args.date, aFormat )
end
end
end
local volume = ''
else
if args.volume ~= '' then
if args.issue ~= '' then
volume = mw.ustring.format( formatters.prVolume, args.volume )
volume = mw.ustring.format( formatters.prIssue, args.issue )
if args.issue ~= '' then
volume = mw.ustring.format( formatters.prVolIssue, volume, args.issue )
end
if year ~= '' then
if year ~= '' then
volume = mw.ustring.format( formatters.prVolYear, volume, year )
volume = mw.ustring.format( formatters.prVolYear, volume, year )
end
end
else
else
if args.issue ~= '' then
volume = year
volume = mw.ustring.format( formatters.prIssue, args.issue )
if year ~= '' then
volume = mw.ustring.format( formatters.prVolYear, volume, year )
end
else
volume = year
end
end
end
if volume ~= '' then
end
table.insert( result, volume )
if volume ~= '' then
end
table.insert( tab, volume )
else
end
if args.volume ~= '' then
end
table.insert( result,
 
mw.ustring.format( formatters.nsVolume, args.volume ) )
local function makeNewspaperInfo( args, tab )
end
if args.volume ~= '' then
if args.date:match( '^[12]%d+%-%d+%-%d+$' ) then
table.insert( tab,
mw.ustring.format( formatters.nsVolume, args.volume ) )
end
 
day = ''
if args.date ~= '' then
if args.date:match( '^[12]%d%d%d%-[01]?%d%-[0123]?%d$' ) then
day = getDate( args.date, 'l, j. F Y' )
day = getDate( args.date, 'l, j. F Y' )
else
else
day = ''
table.insert( errorMsgs, texts.wrongDate )
table.insert( errorMsgs, texts.wrongDate )
end
local issue = ''
if args.issue ~= '' then
issue = mw.ustring.format( formatters.prIssue, args.issue )
if day ~= '' then
issue = mw.ustring.format( formatters.nsNrOfDay, issue, day )
end
else
issue = day
end
if issue ~= '' then
table.insert( result, issue )
end
end
end
end


if args.pages ~= '' then
local issue = ''
table.insert( result,
if args.issue ~= '' then
mw.ustring.format( formatters.addPages, args.pages:gsub( '-', '–' ) ) )
issue = mw.ustring.format( formatters.prIssue, args.issue )
if day ~= '' then
issue = mw.ustring.format( formatters.nsNrOfDay, issue, day )
end
else
issue = day
end
end
if args.columns ~= '' then
if issue ~= '' then
table.insert( result,
table.insert( tab, issue )
mw.ustring.format( formatters.addColumns, args.columns:gsub( '-', '–' ) ) )
end
end
if args.doi ~= '' then
end
table.insert( result,
 
mw.ustring.format( formatters.addDoi, makeDoiLink( args.doi ) ) )
local function makePeriodical( args )
end
local result = { makeAuthor( args.author, '' )
if not args.jstor or args.jstor ~= '' and not tonumber( args.jstor ) then
.. makeTitle( args.title, '', args.articleUrl, '' ) .. formatters.tiIn
args.jstor = ''
.. makeJournalTitle( args ) }
end
 
if args.jstor ~= '' then
makeIsbn( args, result )
table.insert( result,
if args.type == 'journal' then
mw.ustring.format( formatters.dbJstor, args.jstor, args.jstor ) )
makeJournalInfo( args, result )
else
makeNewspaperInfo( args, result )
end
end
makePages( args, result )
makeDoi( args, result )
makeWebInfo( args, result )
result = table.concat( result, formatters.addDelimiter1 )


result = table.concat( result, formatters.addDelimiter1 )
return makeLanguageComment( args, result )
if args.language ~= '' then
result = result .. mw.ustring.format( formatters.addLanguage, args.language )
end
if result ~= '' then
result = mw.ustring.format( formatters.puAll, result )
end
if args.comment ~= '' then
result = result .. mw.ustring.format( formatters.addComment, args.comment )
end
return result
end
end


local function getPageNumbers( pages )
local function prepareForCOinS( args )
if not pages then
args.date = args.date:match( '%d+' ) or ''
return ''
args.pages = args.pages:match( '%d+%-%d+' ) or args.pages:match( '%d+' )
args.columns = args.columns:match( '%d+%-%d+' ) or args.columns:match( '%d+' )
 
-- only first author
local at = args.author:find( ';' ), count
if at then
args.author = mw.text.trim( args.author:sub( 1, at - 1 ) )
else
_,count = args.author:gsub( '@', '@' )
if count > 1 then
at = args.author:find( ',' )
args.author = mw.text.trim( args.author:sub( 1, at - 1 ) )
end
end
end
pages = ( '' .. pages ):gsub( '–', '-' ); -- replace endashes with hyphens
return pages:gsub( '&%w+;', '-' ); -- replace html entities with hyphens
end
end


Zeile 562: Zeile 652:
return '<span class="Z3988" title="' .. rft .. '"></span>'
return '<span class="Z3988" title="' .. rft .. '"></span>'
end
end
end
-- remove adjoining punctuation marks
local function finalCleanup( s )
s = mw.ustring.gsub( s, '%.+%.', '.' )
s = mw.ustring.gsub( s, '([!%?])(%s%.+)', '%1' )
return mw.ustring.gsub( s, '([,;:])(%s%.+)', '.' )
end
end


Zeile 567: Zeile 664:
local args, citation
local args, citation
args = checkParams( frameArgs )
args = checkParams( frameArgs )
args.pages = getPageNumbers( args.pages )
args.columns = getPageNumbers( args.columns )


if args.type == 'map' or args.type == 'book' then
if args.type == 'map' or args.type == 'book' then
Zeile 579: Zeile 674:
citation = texts.wrongType
citation = texts.wrongType
end
end
-- remove adjoining punctuation marks
citation = mw.ustring.gsub( citation, '([!%?,:])(%.+)', '%1' )


-- prepare for COinS
citation = finalCleanup( citation )
args.date = args.date:match( '%d+' ) or ''
prepareForCOinS( args )
args.pages = args.pages:match( '%d+%-%d+' ) or args.pages:match( '%d+' )
args.columns = args.columns:match( '%d+%-%d+' ) or args.columns:match( '%d+' )
local at = args.author:find( ';' )
if at then
args.author = mw.text.trim( args.author:sub( 1, at - 1 ) )
end


return table.concat( errorMsgs, ' ' )
return table.concat( errorMsgs, ' ' )

Version vom 23. März 2020, 08:20 Uhr

Die Dokumentation für dieses Modul kann unter Modul:Citation/doc erstellt werden

-- documentation
local scrollGallery = {
	suite  = 'Citation',
	serial = '2020-03-23',
}

-- module import
local cx = require( 'Module:Check isxn' )
local li = require( 'Module:LinkISBN' )
local uc = require( 'Module:UrlCheck' )
local yn = require( 'Module:Yesno' )

-- module variable
local ci = {}

-- global variables
local types = {
	book       = { 'book', 'Buch' },
	map        = { 'map', 'Karte' },
	collection = { 'collection', 'Sammelwerk' },
	journal    = { 'journal', 'Zeitschrift' },
	newspaper  = { 'newspaper', 'Zeitung' }
}

local params = {
	type       = { 'type', 'Typ' },
	author     = { 'Autor', COinS = 'rft.au' },
	editor     = { 'Herausgeber', 'Hrsg' },
	title      = { 'Titel', COinS = 'rft.title' },
	chapter    = { 'Kapitel', types = 'book', COinS = 'rft.atitle' },
	collection = { 'Sammelwerk', types = 'collection' },
	journal    = { 'Zeitschrift', 'Sammelwerk', types = 'journal', COinS = 'rft.jtitle' },
	newspaper  = { 'Zeitung', 'Sammelwerk', types = 'newspaper', COinS = 'rft.jtitle' },
	abbr       = { 'Abk', types = { 'collection', 'journal', 'newspaper' } },
	url        = { 'Online', 'URL' },
	articleUrl = { 'Onlineaufsatz', 'Aufsatz-URL', types = { 'collection', 'journal', 'newspaper' } },
	jstor      = { 'JSTOR', types = { 'journal', 'newspaper' } },
	KBytes     = { 'KBytes', 'kBytes', 'kbytes' },
	accessDate = { 'Zugriff', 'Abruf' },
	scale      = { 'Maßstab', types = 'map' },
	place      = { 'Ort', COinS = 'rft.place' },
	publisher  = { 'Verlag', types = { 'book', 'map', 'collection' }, COinS = 'rft.pub' },
	date       = { 'Datum', 'Jahr', COinS = 'rft.date' },
	edition    = { 'Auflage', types = { 'book', 'map', 'collection' }, COinS = 'rft.edition' },
	volume     = { 'Band', COinS = 'rft.volume' },
	issue      = { 'Ausgabe', 'Nummer', types = { 'journal', 'newspaper' }, COinS = 'rft.issue' },
	pages      = { 'Seite', 'Seiten', types = { 'book', 'collection', 'journal', 'newspaper' }, COinS = 'rft.pages' },
	columns    = { 'Spalten', types = { 'book', 'collection', 'journal', 'newspaper' }, COinS = 'rft.pages' },
	extent     = { 'Umfang', 'Seitenanzahl', types = { 'book', 'collection', 'map' } },
	series     = { 'Serie', 'Reihe', types = { 'book', 'collection', 'map' }, COinS = 'rft.series' },
	series2    = { 'Serie2', 'Reihe2', types = { 'book', 'collection', 'map' } },
	series3    = { 'Serie3', 'Reihe3', types = { 'book', 'collection', 'map' } },
	isbn       = { 'ISBN', types = { 'book', 'collection', 'map', 'journal' }, COinS = 'rft.isbn' },
	dnb        = { 'DNB', types = { 'book', 'collection', 'map' } },
	oclc       = { 'OCLC', types = { 'book', 'collection', 'map' }, COinS_id = 'info:oclcnum' },
	issn       = { 'ISSN', COinS = 'rft.issn' },
	doi        = { 'DOI', COinS_id = 'info:doi' },
	language   = { 'Sprache' },
	comment    = { 'Kommentar' },
	noError    = { 'noError', 'noerror', 'keinFefler', 'kein Fehler' }
}

local formatters = {
	auAuthor = '%s: ',
	auEditor = '%s (Hrsg.)',
	auAuthorEditor = '%s ; %s',
	tiAll = "%s. ",
	tiTitle = "''%s''",
	tiVolume = "''%s''; Bd. %s",
	tiScale = 'Maßstab: %s. ',
	tiIn = 'In: ',
	tiChapter = "Kapitel ''%s''",
	puAll = '%s.',
	puPlaceDate = '%s, %s',
	puPlacePub = '%s: %s',
	puDateEdition = '%s (%d. Auflage)',
	addSeries = '(%s)',
	addPages = 'S. %s',
	addColumns = 'Sp. %s',
	addDelimiter1 = ', ',
	addDelimiter2 = '; ',
	addDoi = '[[w:Digital Object Identifier|doi]]:%s', 
	addLanguage = ' (%s)',
	addComment = ' %s',
	prPlace = ' <%s>',
	prAbbr = ' (%s)',
	prVolume = 'Bd. %s',
	prIssue = 'Nr. %s',
	prVolIssue = '%s,%s',
	prVolYear = '%s (%s)',
	nsVolume = 'Jg. %s',
	nsNrOfDay = '%s vom %s',
	dbDnb = '[[w:Deutsche Nationalbibliothek|DNB]] [http://d-nb.info/%s %s]',
	dbOclc = '[[w:Online Computer Library Center|OCLC]] [https://worldcat.org/oclc/%s %s]',
	dbIssn = '[[w:Internationale Standardnummer für fortlaufende Sammelwerke|ISSN]] [http://zdb-katalog.de/list.xhtml?t=iss%3D%22xx1xx%22&key=cql xx1xx]',
	dbJstor = '[[w:JSTOR|JSTOR]] [https://www.jstor.org/stable/%s %s]',
	wbAccess = 'Internetabruf am %s',
	wbKBytes = 'Dateigröße %s KByte'
}

local texts = {
	noTitel       = '<span class="error">Fehlender Titel</span>',
	noURL         = '<span class="error">Fehlerhafte URL</span>[[Category:Literatur: fehlerhafte URL]]',
	unknownParam  = '<span class="error">Fehlerhafter Parameter: %s</span>[[Category:Literatur: fehlerhafte Parameter]]',
	unknownParams = '<span class="error">Fehlerhafte Parameter: %s</span>[[Category:Literatur: fehlerhafte Parameter]]',
	unknownTitle  = '[[Category:Literatur: fehlender Titel]]',
	unknownPeriodical = '[[Category:Literatur: fehlender Titel eines Periodikums]]',
	wrongDate     = '<span class="error">Ungültiges Datum</span>[[Category:Literatur: ungültiges Datum]]',
	wrongIssn     = '<span class="error">Ungültige ISSN</span>[[Category:Seiten mit ISSN-Fehlern]]',
	wrongType     = '<span class="error">Fehlerhafter Typ</span>[[Category:Literatur: fehlerhafter Typ]]'
}

local errorMsgs = {}
local dateFormat = ''

-- check if table contains the value
local function inArray( tab, val )
	if type( tab ) == 'string' then
		return tab == val
	end

	local index, value
	for index, value in ipairs( tab ) do
		if value == val then
			return true
		end
	end

	return false
end

-- returns a single value from frame argument table
local function getArgValue( param, args )
	value = '', k, v
	if params[ param ] then
		for k, v in ipairs( params[ param ] ) do
			if args[ v ] and args[ v ] ~= '' then
				value = args[ v ]
				break
			end
		end
	end
	return value
end

-- string cleanup
local function cleanupParameters( s )
	if not s or s == '' then
		return s
	end
	s = s:gsub( '[\009\010\013]', ' ' ) -- horizontal tab, line feed, carriage return
	s = s:gsub( '[%z%c]', '' ) -- control characters
	s = s:gsub( '&nbsp;', ' ' );
	s = s:gsub( '\226\128\138', ' ' ); -- hair space
	s = mw.ustring.gsub( s, '[\226\128\141\226\128\139\194\173]', '' ); -- zero-width joiner, zero-width space, soft hyphen
	s = mw.ustring.gsub( s, '%.%.%.', '…' )
	s = mw.ustring.gsub( s, '%.%.', '‥' )
	s = mw.ustring.gsub( s, '<br%s*/*>', '' )
	s = mw.ustring.gsub( s, '</*p%s*/*>', '' )
	return mw.ustring.gsub( s, '</*span%s*/*>', '' )
end

-- check for possible arguments against params table
local function checkParams( frameArgs )
	local args = {}, key, key2, ok, value, value2
	local complete = {}
	local wrongParams = {}
	local aType = getArgValue( 'type', frameArgs )
	-- convert type values if translated
	for key, value in pairs( types ) do
		if inArray( value, aType ) then
			aType = key
			break
		end
	end

	for key, value in pairs( params ) do
		if not value.types or inArray( value.types, aType ) then
			for key2, value2 in ipairs( value ) do
				complete[ value2 ] = key
				args[ key ] = args[ key ] or
					cleanupParameters( frameArgs[ value2 ] )
			end
			args[ key ] = args[ key ] or ''
		end
	end
	-- no missing items
	for key, value in pairs( params ) do
		if not args[ key ] then
			args[ key ] = ''
		end
	end

	for key, value in pairs( frameArgs ) do
		if not complete[ key ] then
			table.insert( wrongParams, key )
		end
	end
	if #wrongParams == 1 then
		table.insert( errorMsgs,
			mw.ustring.format( texts.unknownParam, table.concat( wrongParams, ', ' ) ) )
	elseif #wrongParams > 0 then
		table.insert( errorMsgs,
			mw.ustring.format( texts.unknownParams, table.concat( wrongParams, ', ' ) ) )
	end
	return args
end

local function getPageNumbers( pages )
	if not pages then
		return ''
	end
	pages = ( '' .. pages ):gsub( '–', '-' ); -- replace endashes with hyphens
	return pages:gsub( '&%w+;', '-' ); -- replace html entities with hyphens
end

local function checkValues( args )
	local i, key, value

	args.pages = getPageNumbers( args.pages )
	args.columns = getPageNumbers( args.columns )

	args.noError = yn( args.noError, false )
	if args.noError then
		args.noError = 'true'
	else
		args.noError = 'false'
	end

	if args.date ~= '' then
		if args.date:match( '^[12]%d%d%d%-[01]?%d%-[0123]?%d$' ) then
			dateFormat = 'j. F Y'
		elseif args.date:match( '^[12]%d%d%d%-[01]?%d%$' ) then
			dateFormat = 'M Y'
		elseif args.date:match( '^[12]%d%d%d%' ) then
			dateFormat = 'Y'
		else
			args.date = args.date:match( '%d+' ) or ''
			if args.date ~= '' then
				i = tonumber( args.date )
				if i >= 1000 and i < 2100 then
					dateFormat = 'Y'
				else
					args.date = ''
					table.insert( errorMsgs, texts.wrongDate )
				end
			end
		end
	end
	if args.accessDate ~= '' and
		not args.accessDate:match( '^20%d%d%-[01]?%d%-[0123]?%d$' ) then
		args.accessDate = ''
		table.insert( errorMsgs, texts.wrongDate )
	end

	i = 0
	for key, value in ipairs( { 'url', 'articleUrl' } ) do
		if args[ value ] ~= '' then
			if uc.isUrl( args[ value ] ) > 2 then
				args[ value ] = ''
				i = i + 1
			end
		end
	end
	if i > 0 then
		table.insert( errorMsgs, texts.noURL )
	end

	for key, value in ipairs( { 'dnb', 'oclc', 'jstor', 'KBytes' } ) do
		if not args[ value ]:match( '^%d+$' ) then
			args[ value ] = ''
		end
	end

	args.edition = args.edition:match( '%d+' ) or ''
	if args.edition == '1' then
		args.edition = ''
	end

	if args.type ~= 'map' then
		args.scale = ''
	end
end

local function makeAuthor( author, editor )
	if editor ~= '' then
		editor = mw.ustring.format( formatters.auEditor, editor )
	end
	if author ~= '' and editor ~= '' then
		author = mw.ustring.format( formatters.auAuthorEditor, author, editor )
	elseif author == '' and editor ~= '' then
		author = editor
	end
	if author ~= '' then
		return mw.ustring.format( formatters.auAuthor, author )
	else
		return ''
	end
end

local function makeTitle( title, volume, url, scale )
	if title == '' then
		title = texts.noTitle
		table.insert( errorMsgs, texts.unknownTitle )
	end
	if url ~= '' then
		title = '[' .. url .. ' ' .. title .. ']'
	end
	
	if volume and volume ~= '' then
		title = mw.ustring.format( formatters.tiVolume, title, volume )
	else
		title = mw.ustring.format( formatters.tiTitle, title )
	end
	title = mw.ustring.format( formatters.tiAll, title )
	if scale and scale ~= '' then
		title = title .. mw.ustring.format( formatters.tiScale, scale )
	end
	return title
end

local function makeDoiLink( doi )
	doi = mw.ustring.gsub( doi, 'https?://doi.org/', '' )
	return '[' .. 'https://doi.org/' .. mw.uri.encode( doi )
		.. ' ' .. doi .. ']'
end

local function makeIssnLink( issn, noError )
	local text = ''
	if noError ~= 'true' then
		text = texts.wrongIssn
	end
	return formatters.dbIssn:gsub( 'xx1xx', issn )
		.. cx.check_issn( { args = { [ 1 ] = issn, error = text } } )
end

local function makePublisher( args, tab )
	local theDate = args.date
	local published = ''

	if theDate ~= '' and args.edition ~= '' then
		theDate = mw.ustring.format( formatters.puDateEdition, args.date, args.edition )
	end
	if args.place ~= '' and args.publisher ~= '' then
		published = mw.ustring.format( formatters.puPlacePub, args.place, args.publisher )
	elseif args.place ~= '' then
		published = args.place
	elseif args.publisher ~= '' then
		published = args.publisher
	end
	if published == '' then
		published = theDate
	elseif theDate ~= '' then
		published = mw.ustring.format( formatters.puPlaceDate, published, theDate )
	end
	if published ~= '' then
		table.insert( tab, published )
	end
end

local function makeSeries( args, tab )
	local key, value
	for key, value in ipairs( { 'series', 'series2', 'series3' } ) do
		if args[ value ] ~= '' then
			table.insert( tab, mw.ustring.format( formatters.addSeries, args[ value ] ) )
		end
	end
end

local function makeIsbn( args, tab )
	if args.isbn ~= '' then
		table.insert( tab,
			li._linkISBNSet( { isbn = args.isbn, noerror = args.noError } ) )
	elseif args.dnb ~= '' then
		table.insert( tab, mw.ustring.format( formatters.dbDnb, args.dnb, args.dnb ) )
	elseif args.oclc ~= '' then
		table.insert( tab, mw.ustring.format( formatters.dbOclc, args.oclc, args.oclc ) )
	end
	if args.issn ~= '' then
		table.insert( tab, makeIssnLink( args.issn, args.noError ) )
	end
end

local function makePages( args, tab )
	if args.pages ~= '' then
		table.insert( tab,
			mw.ustring.format( formatters.addPages, args.pages:gsub( '-', '–' ) ) )
	end
	if args.columns ~= '' then
		table.insert( tab,
			mw.ustring.format( formatters.addColumns, args.columns:gsub( '-', '–' ) ) )
	end
end

local function makeDoi( args, tab )
	if args.extent ~= '' then
		table.insert( tab, args.extent )
	end
	if args.doi ~= '' then
		table.insert( tab,
			mw.ustring.format( formatters.addDoi, makeDoiLink( args.doi ) ) )
	end
	if args.jstor ~= '' then
		table.insert( tab,
			mw.ustring.format( formatters.dbJstor, args.jstor, args.jstor ) )
	end
end

local function makeWebInfo( args, tab )
	if args.url ~= '' or args.articleUrl ~= '' then
		if args.accessDate ~= '' then
			args.accessDate = getDate( args.accessDate, 'j. F Y' )
			if args.accessDate ~= '' then
				table.insert( tab,
					mw.ustring.format( formatters.wbAccess, args.accessDate ) )
			end
		end
		if args.KBytes ~= '' then
			table.insert( tab,
				mw.ustring.format( formatters.wbKBytes, args.KBytes ) )
		end
	end
end

local function makeLanguageComment( args, result )
	if args.language ~= '' then
		result = result .. mw.ustring.format( formatters.addLanguage, args.language )
	end
	if result ~= '' then
		result = mw.ustring.format( formatters.puAll, result )
	end
	if args.comment ~= '' then
		result = result .. mw.ustring.format( formatters.addComment, args.comment )
	end
	return result
end

local function makePublished( args )
	result = {}
	makePublisher( args, result )
	makeSeries( args, result )
	makeIsbn( args, result )
	if args.chapter ~= '' then
		table.insert( mw.ustring.format( formatters.tiChapter, args.chapter ) )
	end
	makePages( args, result )
	if #result > 0 then
		result = { table.concat( result, formatters.addDelimiter1 ) }
	end

	makeDoi( args, result )
	makeWebInfo( args, result )
	result = table.concat( result, formatters.addDelimiter2 )

	return makeLanguageComment( args, result )
end

local function makeBook( args )
	local author, title
	
	if args.type == 'map' or args.type == 'book' then
		author = makeAuthor( args.author, args.editor )
		title = makeTitle( args.title, args.volume, args.url, args.scale )
	else
		author = makeAuthor( '', args.editor )
		title = makeTitle( args.collection, args.volume, args.url, args.scale )
	end
	return author .. title .. makePublished( args )
end

local function makeCollection( args )
	local author, collection, editor, title

	author = makeAuthor( args.author, '' )
	title = makeTitle( args.title, args.volume, args.articleUrl, '' )
	editor = makeAuthor( '', args.editor )
	collection = makeTitle( args.collection, args.volume, args.articleUrl, '' )

	return author .. title .. formatters.tiIn .. editor .. collection
		.. makePublished( args )
end

local function makeJournalTitle( args )
	local title
	if args.type == 'journal' then
		title = args.journal
	else
		title = args.newspaper
	end
	if title == '' then
		title = texts.noTitel
		table.insert( errorMsgs, texts.unknownPeriodical )
	end
	if args.url ~= '' then
		title = '[' .. args.url .. ' ' .. title .. ']'
	end
	if args.place ~= '' then
		title = title .. mw.ustring.format( formatters.prPlace, args.place )
	end
	if args.abbr ~= '' then
		title = title .. mw.ustring.format( formatters.prAbbr, args.abbr )
	end
	return title
end

local function getDate( aDate, aFormat )
	local function formatDate( aDate, aFormat )
		return mw.getContentLanguage():formatDate( aFormat, aDate, true )
	end

	if aDate ~='' then
		local success, t;
		success, t = pcall( formatDate, aDate, aFormat )
		if success then
			return t
		else
			table.insert( errorMsgs, texts.wrongDate )
			return ''
		end
	else
		return ''
	end
end

local function makeJournalInfo( args, tab )
	local year = ''
	if args.date ~= '' then
		year = getDate( args.date, dateFormat )
	end
	local volume = ''
	if args.volume ~= '' then
		volume = mw.ustring.format( formatters.prVolume, args.volume )
		if args.issue ~= '' then
			volume = mw.ustring.format( formatters.prVolIssue, volume, args.issue )
		end
		if year ~= '' then
			volume = mw.ustring.format( formatters.prVolYear, volume, year )
		end
	else
		if args.issue ~= '' then
			volume = mw.ustring.format( formatters.prIssue, args.issue )
			if year ~= '' then
				volume = mw.ustring.format( formatters.prVolYear, volume, year )
			end
		else
			volume = year
		end
	end
	if volume ~= '' then
		table.insert( tab, volume )
	end
end

local function makeNewspaperInfo( args, tab )
	if args.volume ~= '' then
		table.insert( tab,
			mw.ustring.format( formatters.nsVolume, args.volume ) )
	end

	day = ''
	if args.date ~= '' then
		if args.date:match( '^[12]%d%d%d%-[01]?%d%-[0123]?%d$' ) then
			day = getDate( args.date, 'l, j. F Y' )
		else
			table.insert( errorMsgs, texts.wrongDate )
		end
	end

	local issue = ''
	if args.issue ~= '' then
		issue = mw.ustring.format( formatters.prIssue, args.issue )
		if day ~= '' then
			issue = mw.ustring.format( formatters.nsNrOfDay, issue, day )
		end
	else
		issue = day
	end
	if issue ~= '' then
		table.insert( tab, issue )
	end
end

local function makePeriodical( args )
	local result = { makeAuthor( args.author, '' )
		.. makeTitle( args.title, '', args.articleUrl, '' ) .. formatters.tiIn
		.. makeJournalTitle( args ) }

	makeIsbn( args, result )
	if args.type == 'journal' then
		makeJournalInfo( args, result )
	else
		makeNewspaperInfo( args, result )
	end
	makePages( args, result )
	makeDoi( args, result )
	makeWebInfo( args, result )
	result = table.concat( result, formatters.addDelimiter1 )

	return makeLanguageComment( args, result )
end

local function prepareForCOinS( args )
	args.date = args.date:match( '%d+' ) or ''
	args.pages = args.pages:match( '%d+%-%d+' ) or args.pages:match( '%d+' )
	args.columns = args.columns:match( '%d+%-%d+' ) or args.columns:match( '%d+' )

	-- only first author
	local at = args.author:find( ';' ), count
	if at then
		args.author = mw.text.trim( args.author:sub( 1, at - 1 ) )
	else
		_,count = args.author:gsub( '@', '@' )
		if count > 1 then
			at = args.author:find( ',' )
			args.author = mw.text.trim( args.author:sub( 1, at - 1 ) )
		end
	end
end

local function makeCOinS( args )
	local rft = {}, key, value

	for key, value in pairs( params ) do
		if value.COinS and value.COinS:find( 'rft.' ) and args[ key ] and args[ key ] ~= '' then
			table.insert( rft, value.COinS .. '=' .. mw.uri.encode( '' .. args[ key ] ) )
		end
	end

	if next( rft ) then
		if args.type == 'book' or args.type == 'map' or args.type == 'collection' then
			if args.type == 'collection' then
				table.insert( rft, 'rft.atitle=' .. mw.uri.encode( args.title ) )
				table.insert( rft, 'rft.btitle=' .. mw.uri.encode( args.collection ) )
				table.insert( rft, 1, 'rft.genre=bookitem' )
			else
				table.insert( rft, 'rft.btitle=' .. mw.uri.encode( args.title ) )
				table.insert( rft, 1, 'rft.genre=book' )
		end
			table.insert( rft, 1, 'rft_val_fmt=' .. mw.uri.encode( 'info:ofi/fmt:kev:mtx:book' ) )
		else
			table.insert( rft, 1, 'rft.genre=journal' )
			table.insert( rft, 1, 'rft_val_fmt=info:ofi/fmt:kev:mtx:journal' )
			if args.type == 'journal' then
				table.insert( rft, 'rft.jtitle=' .. mw.uri.encode( args.journal ) )
			else
				table.insert( rft, 'rft.jtitle=' .. mw.uri.encode( args.newspaper ) )
			end
			table.insert( rft, 'rft.atitle=' .. mw.uri.encode( args.title ) )
		end
		table.insert( rft, 1, 'ctx_ver=Z39.88-2004' )
		rft = table.concat( rft, '&' )
		return '<span class="Z3988" title="' .. rft .. '"></span>'
	end
end

-- remove adjoining punctuation marks
local function finalCleanup( s )
	s = mw.ustring.gsub( s, '%.+%.', '.' )
	s = mw.ustring.gsub( s, '([!%?])(%s%.+)', '%1' )
	return mw.ustring.gsub( s, '([,;:])(%s%.+)', '.' )
end

local function makeCitation( frameArgs )
	local args, citation
	args = checkParams( frameArgs )

	if args.type == 'map' or args.type == 'book' then
		citation = makeBook( args )
	elseif args.type == 'collection' then
		citation = makeCollection( args )
	elseif args.type == 'journal' or args.type == 'newspaper' then
		citation = makePeriodical( args )
	else
		citation = texts.wrongType
	end

	citation = finalCleanup( citation )
	prepareForCOinS( args )

	return table.concat( errorMsgs, ' ' )
		.. '<cite style="font-style: normal;" class="citation ' .. args.type
		.. ' printNoLink">' .. citation .. '</cite>' .. makeCOinS( args )
end

function ci.book( frame )
	local args = frame:getParent().args, ok
	args.type = 'book'
	return makeCitation( args )
end

function ci.map( frame )
	local args = frame:getParent().args
	args.type = 'map'
	return makeCitation( args )
end

function ci.collection( frame )
	local args = frame:getParent().args
	args.type = 'collection'
	return makeCitation( args )
end

function ci.journal( frame )
	local args = frame:getParent().args
	args.type = 'journal'
	return makeCitation( args )
end

function ci.newspaper( frame )
	local args = frame:getParent().args
	args.type = 'newspaper'
	return makeCitation( args )
end

function ci.citation( frame )
	local args = frame:getParent().args
--	local args = frame

	aType = getArgValue( 'type', args )
	if not aType or aType == '' or not inArray( types, aType ) then
		if getArgValue( 'scale', args ) ~= '' then
			aType = 'map'
		elseif getArgValue( 'collection', args ) ~= '' then
			aType = 'collection'
		elseif getArgValue( 'journal', args ) ~= '' then
			aType = 'journal'
		elseif getArgValue( 'newspaper', args ) ~= '' then
			aType = 'newspaper'
		else
			aType = 'book'
		end
		args.type = aType
	end
	return makeCitation( args )
end

return ci