def test_append(self): 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__contains__(self): tag = HRef('info.html', Text(), Text('Mary ', 'had ', 'a little lamb')) assert not 'mary' in tag assert 'Mary' in tag assert 'had a little' in tag text = Text('This ', HRef('/', 'is'), ' good') assert not 'This is' in text
def test_lower(self): assert HRef('/').lower() == HRef('/') href = HRef('http://www.example.com', 'Hyperlinked text.') assert href.lower().render_as('latex') == '\\href{http://www.example.com}{hyperlinked text.}' tag = HRef('info.html', Text(), Text('Mary ', 'had ', 'a little lamb')) assert tag.lower().render_as('html') == '<a href="info.html">mary had a little lamb</a>'
def test_upper(self): assert HRef('/').upper() == HRef('/') href = HRef('http://www.example.com', 'Hyperlinked text.') assert href.upper().render_as('latex') == '\\href{http://www.example.com}{HYPERLINKED TEXT.}' tag = HRef('info.html', Text(), Text('Mary ', 'had ', 'a little lamb')) assert tag.upper().render_as('html') == '<a href="info.html">MARY HAD A LITTLE LAMB</a>'
def test__getitem__(self): t = Tag('em', '1234567890') assert_raises(TypeError, lambda: 1 in t) assert t == Tag('em', '1234567890') assert t[:] == t assert t[:0] == Tag('em', '') assert t[:1] == Tag('em', '1') assert t[:3] == Tag('em', '123') assert t[:5] == Tag('em', '12345') assert t[:7] == Tag('em', '1234567') assert t[:10] == Tag('em', '1234567890') assert t[:100] == Tag('em', '1234567890') assert t[:-100] == Tag('em', '') assert t[:-10] == Tag('em', '') assert t[:-9] == Tag('em', '1') assert t[:-7] == Tag('em', '123') assert t[:-5] == Tag('em', '12345') assert t[:-3] == Tag('em', '1234567') assert t[-100:] == Tag('em', '1234567890') assert t[-10:] == Tag('em', '1234567890') assert t[-9:] == Tag('em', '234567890') assert t[-7:] == Tag('em', '4567890') assert t[-5:] == Tag('em', '67890') assert t[-3:] == Tag('em', '890') assert t[1:] == Tag('em', '234567890') assert t[3:] == Tag('em', '4567890') assert t[5:] == Tag('em', '67890') assert t[7:] == Tag('em', '890') assert t[10:] == Tag('em', '') assert t[100:] == Tag('em', '') assert t[0:10] == Tag('em', '1234567890') assert t[0:100] == Tag('em', '1234567890') assert t[2:3] == Tag('em', '3') assert t[2:4] == Tag('em', '34') assert t[3:7] == Tag('em', '4567') assert t[4:7] == Tag('em', '567') assert t[4:7] == Tag('em', '567') assert t[7:9] == Tag('em', '89') assert t[100:200] == Tag('em', '') t = Tag('strong', '123', Tag('em', '456', HRef('/', '789')), '0') assert t[:3] == Tag('strong', '123') assert t[:5] == Tag('strong', '123', Tag('em', '45')) assert t[:7] == Tag('strong', '123', Tag('em', '456', HRef('/', '7'))) assert t[:10] == Tag('strong', '123', Tag('em', '456', HRef('/', '789')), '0') assert t[:100] == Tag('strong', '123', Tag('em', '456', HRef('/', '789')), '0') assert t[:-7] == Tag('strong', '123') assert t[:-5] == Tag('strong', '123', Tag('em', '45')) assert t[:-3] == Tag('strong', '123', Tag('em', '456', HRef('/', '7')))
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 test_render_as(self): href = HRef('http://www.example.com', 'Hyperlinked text.') assert href.render_as('latex') == '\\href{http://www.example.com}{Hyperlinked text.}' assert href.render_as('html') == '<a href="http://www.example.com">Hyperlinked text.</a>' assert href.render_as('plaintext') == 'Hyperlinked text.' tag = HRef('info.html', Text(), Text('Mary ', 'had ', 'a little lamb')) assert tag.render_as('html') == '<a href="info.html">Mary had a little lamb</a>'
def test_href_text(): href = HRef('http://www.example.com', Text('hyperlinked', ' text')) nose.tools.assert_equal( render_str(href), '<reference refuri="http://www.example.com">' 'hyperlinked text' '</reference>')
def test_endswith(self): tag = HRef('info.html', Text(), Text('Mary ', 'had ', 'a little lamb')) assert tag.endswith('') assert not tag.endswith('B') assert tag.endswith('b') assert tag.endswith('lamb') tag = HRef('/', 'a', 'b', 'c') assert tag.endswith('bc') tag = HRef('/', 'This is good') assert tag.endswith(('good', 'wonderful')) assert not tag.endswith(('bad', 'awful')) text = Text('This ', HRef('/', 'is'), ' good') assert not text.endswith('is good')
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 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 test_capitalize(self): assert HRef('/').capitalize() == Text(HRef('/')) tag = HRef('info.html', Text(), Text('Mary ', 'had ', 'a Little Lamb')) assert tag.capitalize().render_as( 'html') == '<a href="info.html">Mary had a little lamb</a>' assert tag.lower().capitalize().render_as( 'html') == '<a href="info.html">Mary had a little lamb</a>'
def test_add_period(self): assert HRef('/').add_period() == HRef('/') tag = HRef('info.html', Text(), Text('Mary ', 'had ', 'a little lamb')) assert tag.add_period().render_as( 'html') == '<a href="info.html">Mary had a little lamb.</a>' assert tag.add_period().add_period().render_as( 'html') == '<a href="info.html">Mary had a little lamb.</a>'
def test_add_period(self): assert HRef('/').add_period() == HRef('/') tag = HRef('info.html', Text(), Text('Mary ', 'had ', 'a little lamb')) assert tag.add_period().render_as('html') == '<a href="info.html">Mary had a little lamb.</a>' assert tag.add_period().add_period().render_as('html') == '<a href="info.html">Mary had a little lamb.</a>'
def test_capitalize(self): assert HRef('/').capitalize() == Text(HRef('/')) tag = HRef('info.html', Text(), Text('Mary ', 'had ', 'a Little Lamb')) assert tag.capitalize().render_as('html') == '<a href="info.html">Mary had a little lamb</a>' assert tag.lower().capitalize().render_as('html') == '<a href="info.html">Mary had a little lamb</a>'
def format_article(self, param): entry = param["entry"] try: authors = [] for author in entry.persons["author"]: text = "" if author.first_names is not None: text += " ".join([s[0] + "." for s in author.first_names]) text += " " if author.middle_names is not None: text += " ".join([s[0] + "." for s in author.middle_names]) text += " " if author.prelast_names is not None: text += " ".join([s for s in author.prelast_names]) text += " " text += " ".join([s for s in author.last_names]) authors.append(Text(text + ", ")) title = "" in_protected = False for char in entry.fields["title"]: if char == "{": in_protected = True elif char == "}": in_protected = False else: if in_protected: title += char else: # Capitalize title in unprotected areas if len(title) == 0: title += char.upper() else: title += char.lower() title = Text('"', title, '," ') journal = Text(Tag("em", entry.fields["journal"]), ", ") if "volume" in entry.fields: volume = Text("vol. ", entry.fields["volume"], ", ") else: volume = Text() if "pages" in entry.fields: pages = Text("pp. ", entry.fields["pages"], ", ") else: pages = Text() date = entry.fields["year"] if "month" in entry.fields: date = entry.fields["month"] + " " + date date = Text(date, ". ") if "doi" in entry.fields: doi = Text( "doi: ", HRef("https://doi.org/" + entry.fields["doi"], entry.fields["doi"])) else: doi = Text() return Text(*authors, title, journal, volume, pages, date, doi) except: warnings.warn(f"Invalid BibTeX entry '{entry.key}'") return Text(entry.key)
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_append(self): 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__str__(self): empty = HRef('/') assert six.text_type(empty) == '' text = Text('This ', HRef('/', 'is'), ' good') six.text_type(text) == 'This is good'
def test__init__(self): empty = HRef('/') assert empty.url == '/' assert empty.parts == []
def test_startswith(self): tag = HRef('info.html', Text(), Text('Mary ', 'had ', 'a little lamb')) assert tag.startswith('') assert tag.startswith('M') assert tag.startswith('Mary') assert not tag.startswith('m') assert not tag.startswith('mary') tag = HRef('/', 'a', 'b', 'c') assert tag.startswith('ab') tag = HRef('/', 'This is good') assert tag.startswith(('This', 'That')) assert not tag.startswith(('That', 'Those')) text = Text('This ', HRef('/', 'is'), ' good') assert not text.startswith('This is')
def test_join(self): href = HRef('/', 'World Wide Web') result = Text('-').join(Text('Estimated size of the ', href).split()) assert result == Text('Estimated-size-of-the-', HRef('/', 'World'), '-', HRef('/', 'Wide'), '-', HRef('/', 'Web'))
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_href_text(): href = HRef('http://www.example.com', Text('hyperlinked', ' text')) assert render_str(href) == ('<reference refuri="http://www.example.com">' 'hyperlinked text' '</reference>')
def test__add__(self): assert HRef('/', '') + HRef('/', '') == Text(HRef('/', '')) assert HRef('/', '') + HRef('strong', '') == Text(HRef('/', ''), HRef('strong', '')) assert HRef('/', 'Good') + HRef('/', '') == Text(HRef('/', 'Good')) assert HRef('/', 'Good') + HRef('/', ' job!') == Text(HRef('/', 'Good job!')) assert HRef('/', 'Good') + HRef('strong', ' job!') == Text(HRef('/', 'Good'), HRef('strong', ' job!')) assert HRef('/', 'Good') + Text(' job!') == Text(HRef('/', 'Good'), ' job!') assert Text('Good') + HRef('/', ' job!') == Text('Good', HRef('/', ' job!'))
def test_isalpha(self): assert not HRef('/').isalpha() assert not HRef('/', 'a b c').isalpha() assert HRef('/', 'abc').isalpha() assert HRef('/', u'文字').isalpha()
def test__unicode__(self): empty = HRef('/') assert unicode(empty) == '' text = Text('This ', HRef('/', 'is'), ' good') unicode(text) == 'This is good'
def test__len__(self): val = 'Tomato apple!' assert len(HRef('index', val)) == len(val)