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])
def optional(children, data): """If children contain a missing bibliography field, return None. Else return formatted children. >>> from pybtex.database import Entry >>> template = optional [field('volume'), optional['(', field('number'), ')']] >>> template.format_data({'entry': Entry('article')}) Text() """ try: return richtext.Text(*_format_list(children, data)) except FieldIsMissing: return richtext.Text()
def first_of(children, data): """Return first nonempty child.""" for child in _format_list(children, data): if child: return child return richtext.Text()
def together(children, data, last_tie=True): """ Try to keep words together, like BibTeX does. >>> print(together ['very', 'long', 'road'].format().plaintext()) very long<nbsp>road >>> print(together ['a', 'very', 'long', 'road'].format().plaintext()) a<nbsp>very long<nbsp>road """ from pybtex.bibtex.names import tie_or_space tie = richtext.Text(richtext.nbsp) space = richtext.Text(' ') 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])
def join(children, data, sep='', sep2=None, last_sep=None): """Join text fragments together. >>> print join.format().plaintext() <BLANKLINE> >>> print join ['a', 'b', 'c', 'd', 'e'].format().plaintext() abcde >>> print join(sep=', ', sep2=' and ', last_sep=', and ') ['Tom', 'Jerry'].format().plaintext() Tom and Jerry >>> print join(sep=', ', sep2=' and ', last_sep=', and ') ['Billy', 'Willy', 'Dilly'].format().plaintext() Billy, Willy, and Dilly """ if sep2 is None: sep2 = sep if last_sep is None: last_sep = sep parts = [part for part in _format_list(children, data) if part] if len(parts) <= 1: return richtext.Text(*parts) elif len(parts) == 2: return richtext.Text(sep2).join(parts) else: return richtext.Text(last_sep).join([richtext.Text(sep).join(parts[:-1]), parts[-1]])