def test_append(self): text = Tag('strong', 'Chuck Norris') assert (text + ' wins!').render_as('html') == '<strong>Chuck Norris</strong> wins!' assert text.append(' wins!').render_as('html') == '<strong>Chuck Norris wins!</strong>' text = HRef('/', 'Chuck Norris') assert (text + ' wins!').render_as('html') == '<a href="/">Chuck Norris</a> wins!' assert text.append(' wins!').render_as('html') == '<a href="/">Chuck Norris wins!</a>'
def test__unicode__(self): empty = Tag('em') assert unicode(empty.lower()) == '' assert unicode(empty.capitalize()) == '' assert unicode(empty.add_period()) == '' assert unicode(Tag('strong', u'ねここねこ')) == u'ねここねこ'
def test_isalpha(self): assert not Text().isalpha() assert not Text('a b c').isalpha() assert Text('abc').isalpha() assert Text(u'文字').isalpha() assert Text('ab', Tag('em', 'cd'), 'ef').isalpha() assert not Text('ab', Tag('em', '12'), 'ef').isalpha()
def test_append(self): text = Tag('strong', 'Chuck Norris') assert (text + ' wins!').render_as('html') == '<strong>Chuck Norris</strong> wins!' assert text.append(' wins!').render_as('html') == '<strong>Chuck Norris wins!</strong>' text = HRef('/', 'Chuck Norris') assert (text + ' wins!').render_as('html') == '<a href="/">Chuck Norris</a> wins!' assert text.append(' wins!').render_as('html') == '<a href="/">Chuck Norris wins!</a>'
def test__init__(self): empty = Tag('em') assert six.text_type(empty) == '' text = Text('This ', Tag('em', 'is'), ' good') assert 'This is' in six.text_type(text) assert six.text_type(text).startswith('This is') assert six.text_type(text).endswith('is good')
def test__contains__(self): tag = Tag('em', Text(), Text('mary ', 'had ', 'a little lamb')) assert 'mary' in tag assert 'Mary' not in tag assert 'had a little' in tag text = Text('This ', Tag('em', 'is'), ' good') assert not 'This is' in text
def test_append(self): text = Tag('strong', 'Chuck Norris') assert (text + ' wins!').render_as('html') == '<strong>Chuck Norris</strong> wins!' assert text.append(' wins!').render_as('html') == '<strong>Chuck Norris wins!</strong>' text = Tag('em', 'Look here') assert (text + '!').render_as('html') == '<em>Look here</em>!' assert text.append('!').render_as('html') == '<em>Look here!</em>'
def test__len__(self): assert len(Text()) == 0 assert len(Text('Never', ' ', 'Knows', ' ', 'Best')) == len('Never Knows Best') assert len(Text('Never', ' ', Tag('em', 'Knows', ' '), 'Best')) == len('Never Knows Best') assert len( Text('Never', ' ', Tag('em', HRef('/', 'Knows'), ' '), 'Best')) == len('Never Knows Best')
def get_formatted_entries( self, bibliography_key: "BibliographyKey", docnames: List[str]) -> Iterable[Tuple["Entry", "FormattedEntry"]]: """Get sorted bibliography entries along with their pybtex labels, with additional sorting and formatting applied from the pybtex style. """ bibliography = self.bibliographies[bibliography_key] entries = dict(self.get_sorted_entries(bibliography_key, docnames)) style = cast( "BaseStyle", pybtex.plugin.find_plugin('pybtex.style.formatting', bibliography.style)()) sorted_entries = style.sort(entries.values()) labels = style.format_labels(sorted_entries) for label, entry in zip(labels, sorted_entries): try: yield ( entry, style.format_entry(bibliography.labelprefix + label, entry), ) except FieldIsMissing as exc: logger.warning(str(exc), location=(bibliography_key.docname, bibliography.line)) yield (entry, FormattedEntry(entry.key, Tag('b', str(exc)), bibliography.labelprefix + label))
def format_title(self, entry, which_field, as_sentence=True): formatted_title = field( which_field, apply_func=lambda text: Tag('b', text.capitalize())) if as_sentence: return sentence[formatted_title] else: return formatted_title
def test_split(self): empty = Tag('em') assert empty.split() == [] assert empty.split('abc') == [Tag('em')] em = Tag('em', 'Emphasized text') assert em.split() == [Tag('em', 'Emphasized'), Tag('em', 'text')] assert em.split(' ') == [Tag('em', 'Emphasized'), Tag('em', 'text')] assert em.split('no such text') == [em] text = Text('Bonnie ', Tag('em', 'and'), ' Clyde') assert text.split('and') == [Text('Bonnie '), Text(' Clyde')] assert text.split(' and ') == [text] text = Text('Bonnie', Tag('em', ' and '), 'Clyde') assert text.split('and') == [Text('Bonnie', Tag('em', ' ')), Text(Tag('em', ' '), 'Clyde')] assert text.split(' and ') == [Text('Bonnie'), Text('Clyde')] text = Text('From ', Tag('em', 'the very beginning'), ' of things') assert text.split() == [ Text('From'), Text(Tag('em', 'the')), Text(Tag('em', 'very')), Text(Tag('em', 'beginning')), Text('of'), Text('things'), ] parts = text.split() assert parts == [ Text('From'), Text(Tag('em', 'the')), Text(Tag('em', 'very')), Text(Tag('em', 'beginning')), Text('of'), Text('things'), ]
def format_misc(self, entry): context = entry["entry"] ret = Text("") ret = make_author(ret, context) ret = make_title(ret, context) ret += context.fields["journal"] ret = make_year(ret, context) ret = make_links(ret, context) return Tag("li", ret)
def test_join(self): assert six.text_type(Text(' ').join(['a', Text('b c')])) == 'a b c' assert six.text_type(Text(nbsp).join(['a', 'b', 'c'])) == 'a<nbsp>b<nbsp>c' assert six.text_type(nbsp.join(['a', 'b', 'c'])) == 'a<nbsp>b<nbsp>c' assert six.text_type(String('-').join(['a', 'b', 'c'])) == 'a-b-c' result = Tag('em', ' and ').join(['a', 'b', 'c']).render_as('html') assert result == 'a<em> and </em>b<em> and </em>c' result = HRef('/', ' and ').join(['a', 'b', 'c']).render_as('html') assert result == 'a<a href="/"> and </a>b<a href="/"> and </a>c'
def format_article(self, e): text = super(CoSMoRefStyle, self).format_article(e) summary = e.fields.get('cosmomvpa-summary', None) if summary is not None: summary_text = Tag('emph', Text('[ %s ]' % summary)) text.extend([Symbol('newblock'), summary_text]) return text
def format_phdthesis(self, entry): context = entry["entry"] ret = Text("") ret = make_author(ret, context) ret = make_title(ret, context) if "school" in context.fields: ret = ret + Text(", ") + enrich(context.fields["school"]) ret = make_year(ret, context) ret = make_links(ret, context) return Tag("li", ret)
def format(self, person: Person, abbr: bool = False) -> Text: text_bold = [Tag("b", Text.from_latex(name)) for name in person.last_names] return join[ name_part(tie=True, abbr=abbr)[ person.rich_first_names + person.rich_middle_names ], name_part(tie=True)[person.rich_prelast_names], name_part[text_bold], name_part(before=", ")[person.rich_lineage_names], ]
def test_join(self): text = Text('From ', Tag('em', 'the very beginning'), ' of things') dashified = String('-').join(text.split()) assert dashified == Text('From-', Tag('em', 'the'), '-', Tag('em', 'very'), '-', Tag('em', 'beginning'), '-of-things') dashified = Tag('em', '-').join(text.split()) assert dashified == Text('From', Tag('em', '-the-very-beginning-'), 'of', Tag('em', '-'), 'things')
def format_book(self, entry): context = entry["entry"] ret = Text("") ret = make_author(ret, context) ret = make_title(ret, context) ret = make_editor(ret, context) if "isbn" in context.fields: ret = ret + "ISBN: " + context.fields["pages"] ret = make_year(ret, context) ret = make_links(ret, context) return Tag("li", ret)
def test__eq__(self): assert HRef('/', '') != '' assert HRef('/', '') != Text() assert HRef('/', '') != HRef('', '') assert HRef('/', '') == HRef('/', '') assert HRef('/', 'good') != HRef('', 'bad') assert HRef('/', 'good') != Text('good') assert HRef('/', 'good') == HRef('/', 'good') assert not (HRef('/', 'good') != HRef('/', 'good')) assert HRef('strong', '') != Tag('strong', '')
def test__init__(self): assert unicode(Text('a', '', 'c')) == 'ac' assert unicode(Text('a', Text(), 'c')) == 'ac' text = Text(Text(), Text('mary ', 'had ', 'a little lamb')) assert unicode(text) == 'mary had a little lamb' text = unicode(Text('a', Text('b', 'c'), Tag('em', 'x'), Symbol('nbsp'), 'd')) assert text == 'abcx<nbsp>d' assert_raises(ValueError, Text, {}) assert_raises(ValueError, Text, 0, 0)
def test__unicode__(self): empty = Tag('em') assert unicode(empty.lower()) == '' assert unicode(empty.capitalize()) == '' assert unicode(empty.add_period()) == '' assert unicode(Tag('strong', u'ねここねこ')) == u'ねここねこ'
def test_endswith(self): tag = Tag('em', Text(), Text('mary ', 'had ', 'a little lamb')) assert not tag.endswith('B') assert tag.endswith('b') tag = Tag('em', 'a', 'b', 'c') assert tag.endswith('bc') tag = Tag('em', 'This is good') assert tag.endswith(('good', 'wonderful')) assert not tag.endswith(('bad', 'awful')) text = Text('This ', Tag('em', 'is'), ' good') assert not text.endswith('is good')
def test__str__(self): empty = Tag('em') assert six.text_type(empty.lower()) == '' assert six.text_type(empty.capitalize()) == '' assert six.text_type(empty.add_period()) == '' assert six.text_type(Tag('strong', u'ねここねこ')) == u'ねここねこ'
def test_startswith(self): tag = Tag('em', Text(), Text('mary ', 'had ', 'a little lamb')) assert not tag.startswith('M') assert tag.startswith('m') tag = Tag('em', 'a', 'b', 'c') assert tag.startswith('ab') tag = Tag('em', 'This is good') assert tag.startswith(('This', 'That')) assert not tag.startswith(('That', 'Those')) text = Text('This ', Tag('em', 'is'), ' good') assert not text.startswith('This is')
def test_append(self): text = Tag('strong', 'Chuck Norris') assert (text + ' wins!').render_as('html') == '<strong>Chuck Norris</strong> wins!' assert text.append(' wins!').render_as('html') == '<strong>Chuck Norris wins!</strong>' text = Tag('em', 'Look here') assert (text + '!').render_as('html') == '<em>Look here</em>!' assert text.append('!').render_as('html') == '<em>Look here!</em>'
def _format_default(self, entry): entry: Entry = entry['entry'] names = [] for p in entry.persons['author']: names.append(self.format_name(p, self.abbreviate_names)) names = join(sep=', ', last_sep=', … ')[names].format_data(None) return Text( # 'Article ', # self.format_names('author'), names, # entry.fields['author'], ' ', Tag('em', entry.fields['title']) )
def test__init__(self): assert six.text_type(Text('a', '', 'c')) == 'ac' assert six.text_type(Text('a', Text(), 'c')) == 'ac' text = Text(Text(), Text('mary ', 'had ', 'a little lamb')) assert six.text_type(text) == 'mary had a little lamb' text = six.text_type(Text('a', Text('b', 'c'), Tag('em', 'x'), Symbol('nbsp'), 'd')) assert text == 'abcx<nbsp>d' with pytest.raises(ValueError): Text({}) with pytest.raises(ValueError): Text(0, 0)
def test__getitem__(self): t = Text('123', Text('456', Text('78'), '9'), '0') with pytest.raises(TypeError): 1 in t assert t == Text('1234567890') assert t[:0] == Text('') assert t[:1] == Text('1') assert t[:3] == Text('123') assert t[:5] == Text('12345') assert t[:7] == Text('1234567') assert t[:10] == Text('1234567890') assert t[:100] == Text('1234567890') assert t[:-100] == Text('') assert t[:-10] == Text('') assert t[:-9] == Text('1') assert t[:-7] == Text('123') assert t[:-5] == Text('12345') assert t[:-3] == Text('1234567') assert t[-100:] == Text('1234567890') assert t[-10:] == Text('1234567890') assert t[-9:] == Text('234567890') assert t[-7:] == Text('4567890') assert t[-5:] == Text('67890') assert t[-3:] == Text('890') assert t[1:] == Text('234567890') assert t[3:] == Text('4567890') assert t[5:] == Text('67890') assert t[7:] == Text('890') assert t[10:] == Text('') assert t[100:] == Text('') assert t[0:10] == Text('1234567890') assert t[0:100] == Text('1234567890') assert t[2:3] == Text('3') assert t[2:4] == Text('34') assert t[3:7] == Text('4567') assert t[4:7] == Text('567') assert t[4:7] == Text('567') assert t[7:9] == Text('89') assert t[100:200] == Text('') t = Text('123', Tag('em', '456', HRef('/', '789')), '0') assert t[:3] == Text('123') assert t[:5] == Text('123', Tag('em', '45')) assert t[:7] == Text('123', Tag('em', '456', HRef('/', '7'))) assert t[:10] == Text('123', Tag('em', '456', HRef('/', '789')), '0') assert t[:100] == Text('123', Tag('em', '456', HRef('/', '789')), '0') assert t[:-7] == Text('123') assert t[:-5] == Text('123', Tag('em', '45')) assert t[:-3] == Text('123', Tag('em', '456', HRef('/', '7')))
def format_article(self, entry): context = entry["entry"] ret = Text("") ret = make_author(ret, context) ret = make_title(ret, context) ret += context.fields["journal"] if "volume" in context.fields: ret += Symbol("nbsp") + context.fields["volume"] if "number" in context.fields: ret += Symbol("nbsp") + "(" + context.fields["number"] + ")" if "pages" in context.fields: ret = ret + ":" + context.fields["pages"] ret = make_year(ret, context) ret = make_links(ret, context) return Tag("li", ret)
def et_al(children, data, sep="", sep2=None, last_sep=None): # type: ignore[no-untyped-def] 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 Text(*parts) elif len(parts) == 2: return Text(sep2).join(parts) elif len(parts) == 3: return Text(last_sep).join([Text(sep).join(parts[:-1]), parts[-1]]) else: return Text(parts[0], Tag("em", " et al"))
def format_article(self, e): text = super(CoSMoRefStyle, self).format_article(e) summary = e.fields.get('cosmomvpa-summary', None) if summary is not None: summary_text = Tag('emph', Text('[ %s ]' % summary)) summary_symbol = [Symbol('newblock'), summary_text] if hasattr(text, 'extend'): target_to_extend = text elif hasattr(text, 'parts'): target_to_extend = text.parts else: raise ValueError('Unsupported type %s', type(text)) target_to_extend.extend(summary_symbol) return text
class PersonStyle: """A class providing additional data and helper functions to facilitate formatting of person names. """ #: Plugin name of the style used for formatting person names. style: str = 'last' #: Plugin class instance used for formatting person names. #: Automatically initialised from :attr:`style`. style_plugin: "BaseNameStyle" = dataclasses.field(init=False) #: Whether or not to abbreviate first names. abbreviate: bool = True #: Separator between persons. sep: Union["BaseText", str] = ', ' #: Separator between persons, if only two persons. sep2: Optional[Union["BaseText", str]] = ' and ' #: Separator between persons, for last person if three or more persons. last_sep: Optional[Union["BaseText", str]] = ', and ' #: Abbreviation text if three or more persons. other: Optional[Union["BaseText", str]] = \ Text(' ', Tag('em', 'et al.')) def __post_init__(self): self.style_plugin = pybtex.plugin.find_plugin('pybtex.style.names', name=self.style)() def names(self, role: str, full: bool) -> "Node": """Returns a template formatting the persons with correct separators and using the full person list if so requested. """ return names( role=role, sep=self.sep, sep2=self.sep2, last_sep=self.last_sep, other=None if full else self.other, )
def process_latex(text): parts = text.parts for i,part in enumerate(parts): if isinstance(part, String): # Format \emph as Tag('em') if (part.endswith(r'\emph ') and isinstance(parts[i+1], Protected)): parts[i] = parts[i][:-6] parts[i+1] = Tag('em', *parts[i+1].parts) # Handle \textsuperscript elif (part.endswith(r'\textsuperscript ') and isinstance(parts[i+1], Protected)): parts[i] = parts[i][:-17] if (str(parts[i+1]) == '2'): parts[i+1] = '²' elif isinstance(part, Protected): parts[i] = process_latex(part) if (isinstance(text, Text)): return Text(*parts) elif (isinstance(text, Protected)): return Protected(*parts)
def format_inproceedings(self, entry): context = entry["entry"] ret = Text("") ret = make_author(ret, context) ret = make_title(ret, context) ret += Text("In ") + enrich(context.fields["booktitle"]) if "volume" in context.fields: ret += Symbol("nbsp") + context.fields["volume"] if "number" in context.fields: ret += Symbol("nbsp") + "(" + context.fields["number"] + ")" if "pages" in context.fields: ret = ret + ":" + context.fields["pages"] if "address" in context.fields: ret = ret + Text(", ") + enrich(context.fields["address"]) if "note" in context.fields: ret = ret + Text(", ") + context.fields["note"] ret = make_year(ret, context) ret = make_links(ret, context) return Tag("li", ret)
def test_capitalize(self): tag = Tag('em', Text(), Text('mary ', 'had ', 'a Little Lamb')) assert tag.capitalize().render_as('html') == '<em>Mary had a little lamb</em>'
def test_render_as(self): empty = Tag('em') assert empty.render_as('html') == '' assert empty.render_as('latex') == '' tag = Tag('em', 'a', 'b') assert tag.render_as('html') == '<em>ab</em>' assert tag.render_as('latex') == '\\emph{ab}' em = Tag('em', 'Emphasized text') assert em.render_as('latex') == '\\emph{Emphasized text}' assert em.upper().render_as('latex') == '\\emph{EMPHASIZED TEXT}' assert em.lower().render_as('latex') == '\\emph{emphasized text}' assert em.render_as('html') == '<em>Emphasized text</em>' t = Tag(u'em', u'123', Tag(u'em', u'456', Text(u'78'), u'9'), u'0') assert t[:2].render_as('html') == '<em>12</em>' assert t[2:4].render_as('html') == '<em>3<em>4</em></em>' tag = Tag('em', Text(), Text('mary ', 'had ', 'a little lamb')) assert tag.render_as('html') == '<em>mary had a little lamb</em>'
def test_add_period(self): text = Tag('em', Text("That's all, folks")) assert text.add_period().render_as('html') == "<em>That's all, folks.</em>" assert text.add_period().add_period().render_as('html') == "<em>That's all, folks.</em>" text = Text("That's all, ", Tag('em', 'folks')) assert text.add_period().render_as('html') == "That's all, <em>folks</em>." assert text.add_period().add_period().render_as('html') == "That's all, <em>folks</em>." text = Text("That's all, ", Tag('em', 'folks.')) assert text.add_period().render_as('html') == "That's all, <em>folks.</em>" text = Text("That's all, ", Tag('em', 'folks')) assert text.add_period('!').render_as('html') == "That's all, <em>folks</em>!" text = text.add_period('!').add_period('.').render_as('html') assert text == "That's all, <em>folks</em>!" tag = Tag('em', Text(), Text('mary ', 'had ', 'a little lamb')) assert tag.add_period().render_as('html') == '<em>mary had a little lamb.</em>' assert tag.add_period().add_period().render_as('html') == '<em>mary had a little lamb.</em>'
def test_render_as(self): empty = Tag('em') assert empty.render_as('html') == '' assert empty.render_as('latex') == '' tag = Tag('em', 'a', 'b') assert tag.render_as('html') == '<em>ab</em>' assert tag.render_as('latex') == '\\emph{ab}' em = Tag('em', 'Emphasized text') assert em.render_as('latex') == '\\emph{Emphasized text}' assert em.upper().render_as('latex') == '\\emph{EMPHASIZED TEXT}' assert em.lower().render_as('latex') == '\\emph{emphasized text}' assert em.render_as('html') == '<em>Emphasized text</em>' t = Tag(u'em', u'123', Tag(u'em', u'456', Text(u'78'), u'9'), u'0') assert t[:2].render_as('html') == '<em>12</em>' assert t[2:4].render_as('html') == '<em>3<em>4</em></em>' tag = Tag('em', Text(), Text('mary ', 'had ', 'a little lamb')) assert tag.render_as('html') == '<em>mary had a little lamb</em>'
def test_upper(self): tag = Tag('em', Text(), Text('mary ', 'had ', 'a little lamb')) assert tag.upper().render_as('html') == '<em>MARY HAD A LITTLE LAMB</em>'
def test_split(self): empty = HRef('/') assert empty.split() == [] assert empty.split('abc') == [empty] href = HRef('/', 'World Wide Web') assert href.split() == [HRef('/', 'World'), HRef('/', 'Wide'), HRef('/', 'Web')] result = Text('Estimated size of the ', href).split() assert result == [ Text('Estimated'), Text('size'), Text('of'), Text('the'), Text(HRef('/', 'World')), Text(HRef('/', 'Wide')), Text(HRef('/', 'Web')), ] text = Text(Tag('em', Text(Tag('strong', HRef('/', ' Very, very'), ' bad'), ' guys')), '! ') assert text.render_as('html') == '<em><strong><a href="/"> Very, very</a> bad</strong> guys</em>! ' assert text.split(', ') == [ Text(Tag('em', Tag('strong', HRef('/', ' Very')))), Text(Tag('em', Tag('strong', HRef('/', 'very'), ' bad'), ' guys'), '! '), ] assert text.split(' ') == [ Text(), Text(), Text(Tag('em', Tag('strong', HRef('/', 'Very,')))), Text(Tag('em', Tag('strong', HRef('/', 'very')))), Text(Tag('em', Tag('strong', 'bad'))), Text(Tag('em', 'guys'), '!'), Text(), ] assert text.split(' ', keep_empty_parts=False) == [ Text(Tag('em', Tag('strong', HRef('/', 'Very,')))), Text(Tag('em', Tag('strong', HRef('/', 'very')))), Text(Tag('em', Tag('strong', 'bad'))), Text(Tag('em', 'guys'), '!'), ] assert text.split() == [ Text(Tag('em', Tag('strong', HRef('/', 'Very,')))), Text(Tag('em', Tag('strong', HRef('/', 'very')))), Text(Tag('em', Tag('strong', 'bad'))), Text(Tag('em', 'guys'), '!'), ] assert text.split(keep_empty_parts=True) == [ Text(), Text(Tag('em', Tag('strong', HRef('/', 'Very,')))), Text(Tag('em', Tag('strong', HRef('/', 'very')))), Text(Tag('em', Tag('strong', 'bad'))), Text(Tag('em', 'guys'), '!'), Text(), ] text = Text(' A', Tag('em', ' big', HRef('/', ' ', Tag('strong', 'no-no'), '! '))) assert text.render_as('html') == ' A<em> big<a href="/"> <strong>no-no</strong>! </a></em>' assert text.split('-') == [ Text(' A', Tag('em', ' big', HRef('/', ' ', Tag('strong', 'no')))), Text(Tag('em', HRef('/', Tag('strong', 'no'), '! '))), ] assert text.split(' ') == [ Text(), Text('A'), Text(Tag('em', 'big')), Text(Tag('em', HRef('/', Tag('strong', 'no-no'), '!'))), Text(), Text(), ] assert text.split(' ', keep_empty_parts=False) == [ Text('A'), Text(Tag('em', 'big')), Text(Tag('em', HRef('/', Tag('strong', 'no-no'), '!'))), ] assert text.split() == [ Text('A'), Text(Tag('em', 'big')), Text(Tag('em', HRef('/', Tag('strong', 'no-no'), '!'))), ] assert text.split(keep_empty_parts=True) == [ Text(), Text('A'), Text(Tag('em', 'big')), Text(Tag('em', HRef('/', Tag('strong', 'no-no'), '!'))), Text(), ]
def test_lower(self): tag = Tag('em', Text(), Text('mary ', 'had ', 'a little lamb')) assert tag.lower().render_as('html') == '<em>mary had a little lamb</em>'