예제 #1
0
 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)
예제 #2
0
    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'))
예제 #3
0
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)
예제 #4
0
    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."
예제 #5
0
    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>'
예제 #6
0
    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>'
예제 #7
0
 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'
예제 #8
0
    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
예제 #9
0
    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
예제 #10
0
    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>'
예제 #11
0
    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>'
예제 #12
0
    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>'
예제 #13
0
    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
예제 #14
0
 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')
예제 #15
0
 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')
예제 #16
0
 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'
예제 #17
0
 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)
예제 #18
0
파일: bibliography.py 프로젝트: khaeru/py
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"]
예제 #19
0
    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'))
예제 #20
0
 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!'))
예제 #21
0
 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!'))
예제 #22
0
    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', '')
예제 #23
0
    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')
예제 #24
0
    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')
예제 #25
0
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
예제 #26
0
    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)
예제 #27
0
    def rich_last_names(self):
        """
        A list of last names converted to :ref:`rich text <rich-text>`.

        .. versionadded:: 0.20
        """
        return [Text.from_latex(name) for name in self.last_names]
예제 #28
0
    def rich_lineage_names(self):
        """
        A list of lineage (aka Jr) name parts converted to :ref:`rich text <rich-text>`.

        .. versionadded:: 0.20
        """
        return [Text.from_latex(name) for name in self.lineage_names]
예제 #29
0
    def rich_prelast_names(self):
        """
        A list of pre-last (aka von) name parts converted to :ref:`rich text <rich-text>`.

        .. versionadded:: 0.20
        """
        return [Text.from_latex(name) for name in self.prelast_names]
예제 #30
0
    def rich_prelast_names(self):
        """
        A list of pre-last (aka von) name parts converted to :ref:`rich text <rich-text>`.

        .. versionadded:: 0.20
        """
        return [Text.from_latex(name) for name in self.prelast_names]
예제 #31
0
    def rich_last_names(self):
        """
        A list of last names converted to :ref:`rich text <rich-text>`.

        .. versionadded:: 0.20
        """
        return [Text.from_latex(name) for name in self.last_names]
예제 #32
0
    def rich_lineage_names(self):
        """
        A list of lineage (aka Jr) name parts converted to :ref:`rich text <rich-text>`.

        .. versionadded:: 0.20
        """
        return [Text.from_latex(name) for name in self.lineage_names]
예제 #33
0
 def __getitem__(self, key):
     return Text.from_latex(self._fields[key])