Beispiel #1
0
    def get_part(self, type, abbr=False):
        names = getattr(self, "_" + type)
        if abbr:
            from pybtex.textutils import abbreviate

            names = [abbreviate(name) for name in names]
        return names
    def get_part(self, type, abbr=False):
        """Get a list of name parts by `type`.

        >>> knuth = Person('Donald E. Knuth')
        >>> knuth.get_part('first')
        ['Donald']
        >>> knuth.get_part('last')
        ['Knuth']
        """

        names = getattr(self, type + '_names')
        if abbr:
            import warnings
            warnings.warn('Person.get_part(abbr=True) is deprecated since 0.19: use pybtex.textutils.abbreviate()', stacklevel=2)
            from pybtex.textutils import abbreviate
            names = [abbreviate(name) for name in names]
        return names
Beispiel #3
0
 def getparts(p, x):
     for part in p.get_part(x, False):
         if len(part) == 1:
             yield abbreviate(part)
         else:
             yield part
Beispiel #4
0
def _abbr(parts):
    return (abbreviate(part) for part in parts)
Beispiel #5
0
    def filter_bibentry(self, entry):
        #
        # entry is a pybtex.database.Entry object
        #

        for role in self.roles:
            if role not in entry.persons:
                continue

            for k in range(len(entry.persons[role])):

                p = entry.persons[role][k]

                ### NO: this kills any protection, e.g., with braces, etc.
                #
                # # de-latex the person first
                # pstr = unicodestr(p)
                # # BUG: FIXME: remove space after any macros
                # pstr = re.sub(r'(\\[a-zA-Z]+)\s+', r'\1{}', pstr) # replace "blah\macro blah" by "blah\macro{}blah"
                #if (self._names_to_utf8):
                #    pstr = latex2text.latex2text(pstr)
                #
                #p = Person(pstr)

                if self._names_to_utf8:
                    # delatex everything to UTF-8, but honor names protected by braces and keep those
                    rxmacrospace = re.compile(r'(\\[a-zA-Z]+)\s+')
                    l2t = LatexNodes2Text(keep_braced_groups=True, strict_latex_spaces=True)
                    protected_detex_fn = lambda x: l2t.latex_to_text(rxmacrospace.sub(r'\1{}', x)).strip()

                    # join name again to correctly treat accents like "Fran\c cois" or "\AA berg"
                    p = Person(protected_detex_fn(unicodestr(p)))

                    # do_detex = lambda lst: [ protected_detex(x) for x in lst ]
                    # p.first_names = do_detex(p.first_names)
                    # p.middle_names = do_detex(p.middle_names)
                    # p.prelast_names = do_detex(p.prelast_names)
                    # p.last_names = do_detex(p.last_names)
                    # p.lineage = do_detex(p.lineage_names)


                if self._only_single_letter_firsts:
                    do_abbrev = lambda x: abbreviate(x) if len(x) == 1 else x
                else:
                    do_abbrev = abbreviate

                first_names = p.first_names
                middle_names = p.middle_names
                if self._only_one_initial:
                    first_names = first_names[0:1]
                    middle_names = []
                if self._strip_first_names:
                    first_names = []
                    middle_names = []

                pnew = Person(string='',
                              first=" ".join([do_abbrev(x)  for x in first_names]),
                              middle=" ".join([do_abbrev(x)  for x in middle_names]),
                              prelast=" ".join(p.prelast_names),
                              last=" ".join(p.last_names),
                              lineage=" ".join(p.lineage_names))

                entry.persons[role][k] = pnew
                #logger.debug("nameinitials: %r became %r" % (p, pnew))

        return