Example #1
0
def together(children, data, last_tie=False):
    """
    Try to keep words together, like BibTeX does.

    >>> print(six.text_type(together ['very', 'long', 'road'].format()))
    very long road
    >>> print(six.text_type(together(last_tie=True) ['very', 'long', 'road'].format()))
    very long<nbsp>road
    >>> print(six.text_type(together ['a', 'very', 'long', 'road'].format()))
    a<nbsp>very long road
    >>> print(six.text_type(together ['chapter', '8'].format()))
    chapter<nbsp>8
    >>> print(six.text_type(together ['chapter', '666'].format()))
    chapter 666
    """
    from pybtex.textutils import tie_or_space
    tie = richtext.nbsp
    space = richtext.String(' ')
    parts = [part for part in _format_list(children, data) if part]
    if not parts:
        return richtext.Text()
    if len(parts) <= 2:
        tie2 = tie if last_tie else tie_or_space(
            parts[0], tie, space, other_word=parts[-1])
        return tie2.join(parts)
    else:
        last_tie = tie if last_tie else tie_or_space(parts[-1], tie, space)
        return richtext.Text(parts[0], tie_or_space(parts[0], tie, space),
                             space.join(parts[1:-1]), last_tie, parts[-1])
Example #2
0
def name_part(children, data, before='', tie=False, abbr=False):
    if abbr:
        children = [child.abbreviate() for child in children]
    parts = together(last_tie=True)[children].format_data(data)
    if not parts:
        return Text()
    if tie:
        return Text(before, parts, tie_or_space(parts, nbsp, ' '))
    else:
        return Text(before, parts)
def together(children, data, last_tie=True):
    """
    Try to keep words together, like BibTeX does.

    >>> print unicode(together ['very', 'long', 'road'].format())
    very long<nbsp>road
    >>> print unicode(together ['a', 'very', 'long', 'road'].format())
    a<nbsp>very long<nbsp>road
    """
    from pybtex.textutils import tie_or_space
    tie = richtext.nbsp
    space = richtext.String(' ')
    parts = [part for part in _format_list(children, data) if part]
    if not parts:
        return richtext.Text()
    if len(parts) <= 2:
        tie2 = tie if last_tie else tie_or_space(parts[0], tie, space)
        return tie2.join(parts)
    else:
        last_tie = tie if last_tie else tie_or_space(parts[-1], tie, space)
        return richtext.Text(
            parts[0], tie_or_space(parts[0], tie, space),
            space.join(parts[1:-1]), last_tie, parts[-1]
        )