def format_pages(text): dash_re = re.compile(r'-+') pages = Text(Symbol('ndash')).join( re.split(dash_re, six.text_type(text))) if re.search('[-‒–—―]', six.text_type(text)): return Text("pp.", Symbol('nbsp'), pages) return Text("p.", Symbol('nbsp'), pages)
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_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 name_part(children, data, before='', tie=False): parts = together [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 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__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__contains__(self): text = Text('mary ', 'had ', 'a little lamb') assert 'mary' in text assert not 'Mary' in text assert 'had a little' in text text = Text('a', 'b', 'c') assert 'abc' in text
def test_add_period(self): assert Text().endswith(('.', '!', '?')) == False assert textutils.is_terminated(Text()) == False assert six.text_type(Text().add_period()) == '' text = Text("That's all, folks") assert six.text_type(text.add_period()) == "That's all, folks."
def test__add__(self): assert String('Python') + String(' 3') != 'Python 3' assert String('Python') + String(' 3') == Text('Python 3') assert String('A').lower() == String('a') assert six.text_type(String('Python') + String(' ') + String('3')) == 'Python 3' assert six.text_type(String('Python') + Text(' ') + String('3')) == 'Python 3' assert six.text_type(String('Python') + ' ' + '3') == 'Python 3' assert six.text_type(String('Python').append(' 3')) == 'Python 3'
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__eq__(self): assert Symbol('nbsp') == Symbol('nbsp') assert not Symbol('nbsp') != Symbol('nbsp') assert not Symbol('nbsp') == Symbol('ndash') assert Symbol('nbsp') != Symbol('ndash') assert Text(nbsp, nbsp) == Text(Symbol('nbsp'), Symbol('nbsp'))
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_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_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_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 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__eq__(self): assert Tag('em', '') != '' assert Tag('em', '') != Text() assert Tag('em', '') != Tag('strong', '') assert Tag('em', '') == Tag('em', '') assert Tag('em', 'good') != Tag('em', 'bad') assert Tag('em', 'good') != Text('good') assert Tag('em', 'good') == Tag('em', 'good') assert not (Tag('em', 'good') != Tag('em', 'good'))
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 date(children, data, type="article"): assert not children if type == "event": return data.rich_fields["eventdate"] elif type == "report": return Text(MONTHS[data.fields["month"]]) + Text( " ") + data.rich_fields["year"] else: return data.rich_fields["year"]
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 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__add__(self): assert Tag('em', '') + Tag('em', '') == Text(Tag('em', '')) assert Tag('em', '') + Tag('strong', '') == Text(Tag('em', ''), Tag('strong', '')) assert Tag('em', 'Good') + Tag('em', '') == Text(Tag('em', 'Good')) assert Tag('em', 'Good') + Tag('em', ' job!') == Text(Tag('em', 'Good job!')) assert Tag('em', 'Good') + Tag('strong', ' job!') == Text(Tag('em', 'Good'), Tag('strong', ' job!')) assert Tag('em', 'Good') + Text(' job!') == Text(Tag('em', 'Good'), ' job!') assert Text('Good') + Tag('em', ' job!') == Text('Good', Tag('em', ' job!'))
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 make_editor(ret, context): authors = context.persons["editor"] af = Text("edited by ") for i, a in enumerate(authors): # fn = a.get_part("first")[0].abbreviate() # ln = a.get_part("last")[0] fn = a.rich_first_names[0].abbreviate() ln = a.rich_last_names[0] sep = Text(", ") if i == len(authors) - 1 and i != 0: fn = Text("and ") + fn af += fn + Text(" ") + ln + sep ret += af return ret
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_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__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 join(children, data, sep='', sep2=None, last_sep=None, other=None): """Join text fragments together.""" 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 other is None: return Text(last_sep).join([Text(sep).join(parts[:-1]), parts[-1]]) else: return Text(parts[0], other)
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 make_links(ret, context): links = Text(" ") sep = Text(" ") if "doi" in context.fields: icon = HtmlTag("i", 'class="fa fa-link"', " ") + " DOI" links += HtmlTag( "a", 'href="https://doi.org/{}" target="_blank"'.format( context.fields["doi"]), icon) sep = Text(" | ") if "url" in context.fields: icon = HtmlTag("i", 'class="fa fa-download"', " ") + " URL" links += sep + HtmlTag( "a", 'href="{}" target="_blank"'.format(context.fields["url"]), icon) ret += HtmlTag("span", 'class="biblinks"', links) return ret