##############################################################
# zNote Bibliography Management Tool v0.6
#
# copyright 2001, 2002 John Maxwell - jmax@portal.ca
#
# This product is licensed under the GNU General Public License. 
# Please see license.txt for details.
#
#  zNote 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 2 of the License, 
#  or (at your option) any later version.
# 
#  zNote 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.
#
#################################################################


#################################################################
# This is the actual output styling method. It uses data 
# gathered by the format.py module.
#
# You should be able to copy this module and make changes to it
# to define new output styles. Then have zNote.py import your
# new module, add it as an option to the manage_editForm, and 
# you'll have more stying options. If you want to contribute new 
# style modules, I'll distribute them in subsequent versions of 
# the zNote product.


def Chicago(self, entry, brief=None):
    "Chicago Manual Style 'A' formatted entry"
    # from the Manual of Style ch 16, 13th ed.
    
    root = entry.documentElement
    
    # First, gather all the bits:
    #
    Authors = self.getAuthors(root)
    Editors = self.getEditors(root)
    # who is the primary creator of the entry?
    if Authors:
        A = self.names(Authors) # parse to dictionary
    elif Editors:
        A = self.names(Editors) # parse to dictionary
    TR = self.names(self.getTrans(root))
    # regular bits:
    D = self.getChunk(root, 'date')
    T = self.getChunk(root, 'title')
    E = self.getChunk(root, 'edition')
    C = self.getChunk(root, 'city')
    P = self.getChunk(root, 'publisher')
    U = self.getChunk(root, 'url')
    I = self.getChunk(root, 'isbn')
    L = self.getChunk(root, 'lcnum')
    # for booksections only:
    Book = root.getElementsByTagName('inb')
    if Book.length != 0:
        BT = self.getChunk(Book[0], 'title')
        BE = self.names(self.getEditors(Book[0]))
        BTR = self.names(self.getTrans(Book[0]))
        PG = self.getChunk(Book[0], 'pages')
    # for jarticles only:
    Jnl = root.getElementsByTagName('inj')
    if Jnl.length != 0:
        J = self.getChunk(Jnl[0], 'journal')
        V = self.getChunk(Jnl[0], 'volume')
        N = self.getChunk(Jnl[0], 'number')
        JD = self.getChunk(Jnl[0], 'date')
        PG = self.getChunk(Jnl[0], 'pages')


# Below, names are accessed in a list (of names) with a three-part 
# dictionary object: 'L' is lastname, 'F' is firstname (and middle 
# names, if there are any), and 'f' is the firstname represented 
# as initials. See the names() method in format.py.

# rs is the whole, formatted string we're building, and which 
# gets returned to the web page.

# Chicago-specific stuff:

    # put the main author string together    
    if len(A) == 1:
        # one author
        auStr = '%s, %s' \
              % (A[0]['L'], A[0]['F'])
    elif len(A) == 2:
        # two authors
        auStr = '%s, %s, &amp; %s %s' \
              % ( A[0]['L'], A[0]['F'], A[1]['F'], A[1]['L'] )
    elif len(A) > 2:
        # more than two authors
        mids = "";
        for n in A[1:-1]:
            mids = mids + '%s %s, ' % ( n['F'], n['L'] )
        auStr = '%s, %s, %s&amp; %s %s' \
              % ( A[0]['L'], A[0]['F'], mids, A[-1]['F'], A[-1]['L'] )
    else:
        # something wrong?
        auStr = str(A)
    if Editors:
        if len(A) == 1:
            auStr = auStr + ', ed'
        else:
            auStr = auStr + ', eds'
    auStr= auStr + '.'
    

    # put the main translators string together    
    if len(TR) == 1:
        # one translator
        trStr = '%s %s.' \
              % (TR[0]['F'], TR[0]['L'])
    elif len(TR) == 2:
        # two translators
        trStr = '%s %s, &amp; %s %s.' \
              % ( TR[0]['F'], TR[0]['L'], TR[1]['F'], TR[1]['L'] )
    elif len(TR) > 2:
        # more than two translators
        mids = "";
        for n in TR[1:-1]:
            mids = mids + '%s %s, ' % ( n['F'], n['L'] )
        trStr = '%s %s, %s&amp; %s %s.' \
              % ( TR[0]['F'], TR[0]['L'], mids, TR[-1]['F'], TR[-1]['L'] )
    else:
        # something wrong?
        trStr = str(TR)
        

######################################################    
    # Book-specific formatting
    if root.nodeName == 'book':
        rs = '%s <I>%s.</I> ' % ( auStr, T )
        if E: rs=rs+ E + '. '
        if TR: rs=rs+ 'Translated by ' + trStr + ' '
        if C: rs=rs+ C + ': '
        rs=rs+ P + ', ' + D + '.'


######################################################    
    # Booksection-specific formatting
    elif root.nodeName == 'booksection':
    
        # put the article translators string together    
        if len(BTR) == 1:
            # one translator
            btrStr = '%s %s' \
                  % (BTR[0]['F'], BTR[0]['L'])
        elif len(BTR) == 2:
            # two translators
            btrStr = '%s %s, &amp; %s %s' \
                  % ( BTR[0]['F'], BTR[0]['L'], BTR[1]['F'], BTR[1]['L'] )
        elif len(BTR) > 2:
            # more than two translators
            mids = "";
            for n in BTR[1:-1]:
                mids = mids + '%s %s, ' % ( n['F'], n['L'] )
            btrStr = '%s %s, %s&amp; %s %s' \
                  % ( BTR[0]['F'], BTR[0]['L'], mids, BTR[-1]['F'], BTR[-1]['L'] )
        else:
            # something wrong?
            btrStr = str(BTR)


        # put the Book Editors string together    
        if len(BE) == 1:
            # one editor
            beStr = '%s %s' \
                  % (BE[0]['F'], BE[0]['L'])
        elif len(BE) == 2:
            # two editors
            beStr = '%s %s &amp; %s %s' \
                  % ( BE[0]['F'], BE[0]['L'], BE[1]['F'], BE[1]['L'] )
        elif len(BE) > 2:
            # more than two editors
            mids = "";
            for n in BE[1:-1]:
                mids = mids + '%s %s, ' % ( n['F'], n['L'] )
            beStr = '%s %s, %s&amp; %s %s' \
                  % ( BE[0]['F'], BE[0]['L'], mids, BE[-1]['F'], BE[-1]['L'] )
        else:
            # something wrong?
            beStr = str(BE)
            
        rs = '%s "%s." ' % ( auStr, T )
        if TR: rs=rs+ 'Translated by ' + trStr
        rs=rs+ 'In '
        rs=rs+ '<I>' + BT + ',</I> '
        if BE: rs=rs+ 'edited by ' + beStr
        if BTR: rs=rs+ 'translated by ' + btrStr
        if PG: rs=rs+ ', ' + PG
        rs=rs+ '. '
        if E: rs=rs+ E + '. '
        if C: rs=rs+ C + ': '
        rs=rs+ P + ', ' + D + '.'

######################################################    
    # JArticle-specific formatting
    elif root.nodeName == 'jarticle':
        rs = '%s "%s." <I>%s</I> %s' % ( auStr, T, J, V )
        if N: rs=rs+ '(' + N + ')'
        if JD: rs=rs+ '(' + JD + ')'
        if PG: rs=rs+ ':' + PG + '.'
        rs=rs+ D + '.' 

######################################################    
    # Proceedings-specific formatting
    elif root.nodeName == 'proceedings':
        rs = '%s. "%s." ' % ( auStr, T )
        rs=rs+ P + '. '
        if C: rs=rs+ C + ', '
        rs=rs+ D + '.'

######################################################    
    # Thesis-specific formatting
    elif root.nodeName == 'thesis':
        rs = '%s "%s". ' % ( auStr, T )
        rs=rs+ P + ', ' + D + '.'

######################################################    
    # ESource-specific formatting
    elif root.nodeName == 'esource':
        rs = '%s <I>%s</I>. ' % ( auStr, T )
        if E: rs=rs+ E + '. '
        if C: rs=rs+ C + ': '
        if P: rs=rs+ P + ', '
        rs=rs+ D + '.'

######################################################    
    # else we have something else
    else:
         rs = root.nodeName


    # display these regardless of type, but only if not 'brief'
    if not brief:
        rs=rs+ '\n<DIV CLASS="ViewNumbers">\n'
        if I: rs=rs+ '\n<BR>\nISBN: ' + I 
        if L: rs=rs+ '\n<BR>\nLCCN: ' + L 
        if U: rs = rs + '\n<BR>\n<A HREF="' + U + '" TARGET="OUT">' + U + '</A>' 
        rs=rs+ '\n</DIV>\n'

    return rs
