File: abbrv.py

package info (click to toggle)
pybliographer 1.2.12-4squeeze1
  • links: PTS
  • area: main
  • in suites: squeeze
  • size: 3,560 kB
  • ctags: 1,413
  • sloc: python: 9,817; xml: 2,560; sh: 813; makefile: 621
file content (334 lines) | stat: -rw-r--r-- 9,095 bytes parent folder | download | duplicates (5)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
# -*- coding: UTF-8 -*-
# This file is part of pybliographer
# 
# Copyright (C) 1998-2004 Frederic GOBRY
# Email : gobry@pybliographer.org
# 	   
# 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 2 
# 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, write to the Free Software
# Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
# 
# 

# This module is based on Generic.py. It can be used for generating string, 
# bibdb, author-year, and unsorted numeric keys. See also pybtext.py and 
# abbrv*.xml styles.
# Created by Zoltán Kóta. March, 2004.
#
''' Generic XML bibliographic style handler '''

import string

from Pyblio.Style import Parser
from Pyblio import Autoload, recode

from Pyblio import pybtextvar

def author_desc (group, coding, initials = 0, reverse = 0):
    """ Create a nice string describing a group of authors.

    	coding   : name of the output coding (as requested for recode)
        initials : if = 1, uses initials instead of complete first names
        reverse  :
        	-1 use First Last format
        	0  use Last, First, excepted for the first entry
        	1  use Last, First for all the authors, not only the first
    """
    
    l = len (group)
            
    fulltext = ""
    
    for i in range (0, l):
        (honorific, first, last, lineage) = group [i].format (coding)

        if initials:
            first = group [i].initials (coding)

        text = ""

        if reverse == 1 or (i == 0 and reverse == 0):
            if last:    text = text + last
            if lineage: text = text + ", " + lineage
            if first:   text = text + ", " + first
        else:
            if first:   text = first + " "
            if last:    text = text + last
            if lineage: text = text + ", " + lineage

        if text:
            if i < l - 2:
                text = text + ", "
            elif i == l - 2:
                text = text + " and "

        fulltext = fulltext + text

    # avoid a dot at the end of the author list
    if fulltext [-1] == '.':
        fulltext = fulltext [0:-1]
    
    return fulltext


def string_key (entry, fmt, table, oldnew):
    
    """ Generates an alphabetical key for an entry. fmt is the
    output coding """

    rc = recode.recode ("latin1.." + fmt)

    if   entry.has_key ('author'): aut = entry ['author']
    elif entry.has_key ('editor'): aut = entry ['editor']
    else:                          aut = ()

    if len (aut) > 0:
        if len (aut) > 1:
            key = ''
            for a in aut:
                honorific, first, last, lineage = a.format (fmt)
                key = key + string.join (map (lambda x:
                                              x [0], string.split (last, ' ')), '')
                if len (key) >= 3:
                    if len (aut) > 3:
                        key = key + '+'
                    break
        else:
            honorific, first, last, lineage = aut [0].format (fmt)
            parts = string.split (last, ' ')

            if len (parts) == 1:
                key = parts [0][0:3]
            else:
                key = string.join (map (lambda x: x [0], parts), '')

    else:
        key = rc (entry.key.key [0:3])

    if entry.has_key ('date'):
        year = entry ['date'].format (fmt) [0]

        if year:
            key = key + year [2:]

    if table.has_key (key) or table.has_key (key + 'a'):

        if table.has_key (key):
            # rename the old entry
            new = key + 'a'

            oldnew [table [key].key] = new
            table [new] = table [key]
            del table [key]

        base = key
        suff = ord ('b')
        key  = base + chr (suff)

        while table.has_key (key):
            suff = suff + 1
            key  = base + chr (suff)

    return key


def bibdb_key (entry, fmt, table):

    """ Generates a key, the same as that in the bib file, for an entry. 
    fmt is the output coding """

    key = entry.key.key

    return key


def authoryear_key (entry, fmt, table, oldnew):
    
    """ Generates an author-year type key for an entry. fmt is the
    output coding """

    rc = recode.recode ("latin1.." + fmt)

    if   entry.has_key ('author'): aut = entry ['author']
    elif entry.has_key ('editor'): aut = entry ['editor']
    else:                          aut = ()

    key = ''
    if len (aut) > 0:
        if len (aut) > 1:
            if len (aut) == 2:
                for a in aut:
                    honorific, first, last, lineage = a.format (fmt)
                    parts = string.split (last, ' ')
                    if key:
                        key = key + ' and '

                    key = key + parts [0]

            else:
                a = aut [0]
                honorific, first, last, lineage = a.format (fmt)
                parts = string.split (last, ' ')
                key = parts [0] + ' et al.'

        else:
            honorific, first, last, lineage = aut [0].format (fmt)
            parts = string.split (last, ' ')
            key = parts [0]

    else:
        key = rc (entry.key.key)


    if entry.has_key ('date'):
        year = entry ['date'].format (fmt) [0]

        if year:
            key = key + ', ' + year


    if table.has_key (key) or table.has_key (key + 'a'):

        if table.has_key (key):
            # rename the old entry
            new = key + 'a'

            oldnew [table [key].key] = new
            table [new] = table [key]
            del table [key]

        base = key
        suff = ord ('b')
        key  = base + chr (suff)

        while table.has_key (key):
            suff = suff + 1
            key  = base + chr (suff)

    return key


def unsrtnum_key (entry, fmt, table):

    """ Generates a numeric key (according to the citation order).
    fmt is the output coding """

    count = 1
    while table.has_key (str (count)):
        count = count + 1
        
    return str (count)


def create_string_key (database, keys, fmt):
    table = {}
    oldnew = {}
    for key in keys:
        s = string_key (database [key], fmt, table, oldnew)
        table [s] = key
        oldnew [database [key].key.key] = s

    skeys = table.keys ()
    skeys.sort ()
    pybtextvar.oldnew = oldnew

    return table, skeys


def create_bibdb_key (database, keys, fmt):
    table = {}
    oldnew = {}
    for key in keys:
        s = bibdb_key (database [key], fmt, table)
        table [s] = key
        oldnew [database [key].key.key] = s

    skeys = table.keys ()
    skeys.sort ()
    pybtextvar.oldnew = oldnew

    return table, skeys


def create_authoryear_key (database, keys, fmt):
    table = {}
    oldnew = {}
    for key in keys:
        s = authoryear_key (database [key], fmt, table, oldnew)
        table [s] = key
        oldnew [database [key].key.key] = s

    skeys = table.keys ()
    skeys.sort ()
    pybtextvar.oldnew = oldnew

    return table, skeys


def create_unsrtnum_key (database, keys, fmt):
    table = {}
    skeys = []
    oldnew = {}
    for key in keys:
        s = unsrtnum_key (database [key], fmt, table)
        table [s] = key
        skeys.append (s)
        oldnew [database [key].key.key] = s

    pybtextvar.oldnew = oldnew

    return table, skeys


def standard_date (entry, coding):
    (text, month, day) = entry.format (coding)

    if month: text = "%s/%s" % (month, text)
    if day  : text = "%s/%s" % (day, text)

    return text


def last_first_full_authors (entry, coding):
    return author_desc (entry, coding, 0, 1)

def first_last_full_authors (entry, coding):
    return author_desc (entry, coding, 0, -1)

def full_authors (entry, coding):
    return author_desc (entry, coding, 0, 0)


def initials_authors (entry, coding):
    return author_desc (entry, coding, 1, 0)

def first_last_initials_authors (entry, coding):
    return author_desc (entry, coding, 1, -1)

def last_first_initials_authors (entry, coding):
    return author_desc (entry, coding, 1, 1)


Autoload.register ('style', 'abbrv', {
    'first_last_full_authors'     : first_last_full_authors,
    'last_first_full_authors'     : last_first_full_authors,
    'full_authors'     : full_authors,
    'first_last_initials_authors' : first_last_initials_authors,
    'last_first_initials_authors' : last_first_initials_authors,
    'initials_authors' : initials_authors,
    'string_keys'      : create_string_key,
    'bibdb_keys'       : create_bibdb_key,
    'authoryear_keys'  : create_authoryear_key,
    'unsrtnum_keys'    : create_unsrtnum_key,
    'european_date'    : standard_date,
    })