#    gRefer is a Bibliographic Management System that uses Google Docs
#    as shared storage.
#
#    Copyright (C) 2011  NigelB
#
#    This program is free software: you can redistribute it and/or modify
#    it under the terms of the GNU General Public License as published by
#    the Free Software Foundation, either version 3 of the License, or
#    (at your option) any later version.
#
#    This program is distributed in the hope that it will be useful,
#    but WITHOUT ANY WARRANTY; without even the implied warranty of
#    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
#    GNU General Public License for more details.
#
#    You should have received a copy of the GNU General Public License
#    along with this program.  If not, see <http://www.gnu.org/licenses/>.

from gRefer.process.interfaces import QuirksTransformation
from gRefer.conv.internal.bibtex.constants import SOURCE, type_key, CONFERENCE, INPROCEEDINGS, BOOKTITLE, JOURNAL, AUTHOR, EDITOR
from gRefer.log import trace

class Bibtex_Scopus_Quirks(QuirksTransformation):

    @trace
    def isApplicable(self, entry):
        toRet = False
        if SOURCE in entry:
            for source in entry[SOURCE]:
                if "Scopus" in source:
                    toRet = True
        return toRet

    def transform(self, entry):
        if entry[type_key].lower() in [CONFERENCE, INPROCEEDINGS] and BOOKTITLE not in entry and JOURNAL in entry :
            entry[BOOKTITLE] = entry.pop(JOURNAL)
        if AUTHOR in entry:
            aut = []
            for author in entry[AUTHOR]:
                aut.append(author.replace(" , "," and "))
            entry[AUTHOR] = aut
        if EDITOR in entry:
            edi = []
            for editor in entry[EDITOR]:
                edi.append(editor.replace(" , "," and "))
            entry[EDITOR] = edi
