# This file is part of pybliographer # # Copyright (C) 1998,1999,2000 Frederic GOBRY # Email : gobry@idiap.ch # # 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. # # $Id: Utils.py,v 1.3.2.5 2001/11/28 18:11:12 fredgo Exp $ from string import * from Pyblio import Key, Autoload, recode _flat = recode.recode ('latin1..flat') def format (string, width, first, next): ''' Format a string on a given width ''' out = [] current = first # if the entry does not fit the current width while len (string) > width - current: pos = width - current - 1 # search a previous space while pos > 0 and string [pos] <> ' ': pos = pos - 1 # if there is no space before... if pos == 0: pos = width - current taille = len (string) while pos < taille and string [pos] <> ' ': pos = pos + 1 out.append (' ' * current + string [0:pos]) string = string [pos+1:] current = next out.append (' ' * current + string) return rstrip (join (out, '\n')) __entry = 0 def generate_key (entry, table): if entry.has_key ('author'): aut = entry ['author'] elif entry.has_key ('editor'): aut = entry ['editor'] else: aut = () if len (aut) == 0: global __entry key = 'entry-%d' % __entry __entry = __entry + 1 else: if len (aut) > 1: key = '' for a in aut: honorific, first, last, lineage = a.format () key = key + join (map (lambda x: x [0], split (last, ' ')), '') if len (key) >= 3: if len (aut) > 3: key = key + '+' break else: honorific, first, last, lineage = aut [0].format () parts = split (last, ' ') if len (parts) == 1: key = parts [0][0:3] else: key = join (map (lambda x: x [0], parts), '') if entry.has_key ('date'): year = entry ['date'].year if year: key = key + str (year) [2:] base = _flat (key) key = Key.Key (table, base) if table.has_key (key): suff = ord ('a') while table.has_key (key): suff = suff + 1 if suff > ord ('z'): suff = ord ('a') base = base + 'a' key = Key.Key (table, base + chr (suff)) return Key.Key (table, key) Autoload.register ('key', 'Default', generate_key) class StringStream: ''' This class simulates a stream and stores it into a simple string. ''' def __init__ (self): self.text = '' return def write (self, text): self.text = self.text + text return