Exemplo n.º 1
0
 def toHTML(self, external_links=False):
     tag_map = {
         'strong'        : 'strong',
         'emphasis'      : 'em',
         'link'          : 'a',
         'superscript'   : 'sup',
         'subscript'     : 'sub',
     }
     markup = NOAT(self.content)
     for anno in self.annotations:
         a = copy.deepcopy(anno)
         if 'url' in a and a['url'] is not None:
             a['href'] = a.pop('url').replace('"','"').replace('\n',' ')
             if len(a['href'].split('://')) > 1 and external_links:
                 a['target'] = '_blank'
         try:
             start = a.pop('start')
             end = a.pop('end')
             a_type = a.pop('type')
         except KeyError:
             pass # Ignore annotations without any of those properties.
         else:
             try:
                 markup.add(tag_map.get(a_type, 'span'), start, end, **a)
             except IndexError as e:
                 pass
     return unicode(markup).replace('\n', '<br/>')
Exemplo n.º 2
0
 def toHTML(self, external_links=False):
     tag_map = {
         'strong': 'strong',
         'emphasis': 'em',
         'link': 'a',
         'superscript': 'sup',
         'subscript': 'sub',
     }
     markup = NOAT(self.content)
     for anno in self.annotations:
         a = copy.deepcopy(anno)
         if 'url' in a and a['url'] is not None:
             a['href'] = a.pop('url').replace('"',
                                              '&#34;').replace('\n', ' ')
             if len(a['href'].split('://')) > 1 and external_links:
                 a['target'] = '_blank'
         try:
             start = a.pop('start')
             end = a.pop('end')
             a_type = a.pop('type')
         except KeyError:
             pass  # Ignore annotations without any of those properties.
         else:
             try:
                 markup.add(tag_map.get(a_type, 'span'), start, end, **a)
             except IndexError as e:
                 pass
     return unicode(markup).replace('\n', '<br/>')
Exemplo n.º 3
0
def test_backwardStartEnd():
    markup = NOAT("0123456789")
    try:
        markup.add("em", 7, 4)
    except IndexError:
        assert True
    else:
        assert AssertionError("Did not raise IndexError")
Exemplo n.º 4
0
def test_invalidEndRange():
    markup = NOAT("0123456789")
    try:
        markup.add("em", 5, 100)
    except IndexError:
        assert True
    else:
        assert AssertionError("Did not raise IndexError")
Exemplo n.º 5
0
def test_singleAnnotationUnicode():
    markup = NOAT("0123456789é")
    target = "012<em>3456</em>789é"
    markup.add("em", 3, 7)
    checkMarkupAsString(markup, target)
Exemplo n.º 6
0
def test_floatsAsRange():
    markup = NOAT("0123456789")
    target = "0123<em>45</em>6789"
    markup.add("em", 4.1, 6.8)
    checkMarkupAsString(markup, target)
Exemplo n.º 7
0
def test_multipleOverlappingAnnotations():
    markup = NOAT("0123456789")
    target = '0<a href="/">12<em>3</em></a><em>456</em>789'
    markup.add("a", 1, 4, href="/")
    markup.add("em", 3, 7)
    checkMarkupAsString(markup, target)
Exemplo n.º 8
0
def test_multipleNonOverlappingAnnotations():
    markup = NOAT("0123456789")
    target = "<strong>012</strong>345<em>67</em>89"
    markup.add("strong", 0, 3)
    markup.add("em", 6, 8)
    checkMarkupAsString(markup, target)
Exemplo n.º 9
0
def test_singleAnnotationEntireString():
    markup = NOAT("0123456789")
    target = "<strong>0123456789</strong>"
    markup.add("strong", 0, 10)
    checkMarkupAsString(markup, target)
Exemplo n.º 10
0
def test_singleAnnotationWithMultipleAttributes():
    markup = NOAT("0123456789")
    target_a = '0<a href="/" id="foo">123</a>456789'
    target_b = '0<a id="foo" href="/">123</a>456789'
    markup.add("a", 1, 4, href="/", id="foo")
    checkMarkupAsString(markup, [target_a, target_b])
Exemplo n.º 11
0
def test_singleAnnotationWithEntityAttribute():
    markup = NOAT("0123456789")
    target = '0<a href="?foo=false&amp;bar=true">123</a>456789'
    markup.add("a", 1, 4, href="?foo=false&bar=true")
    checkMarkupAsString(markup, target)
Exemplo n.º 12
0
def test_singleAnnotationWithUnicodeAttribute():
    markup = NOAT("0123456789")
    target = '0<a class="é">123</a>456789'
    markup.add("a", 1, 4, _class="é")
    checkMarkupAsString(markup, target)
Exemplo n.º 13
0
def test_singleAnnotationWithSingleAttribute():
    markup = NOAT("0123456789")
    target = '0<a href="/">123</a>456789'
    markup.add("a", 1, 4, href="/")
    checkMarkupAsString(markup, target)
Exemplo n.º 14
0
def test_emptyStringWithAnnotations():
    markup = NOAT("")
    target = "<em></em>"
    markup.add("em", 0)
    checkMarkupAsString(markup, target)
Exemplo n.º 15
0
def test_singleAnnotationAndEscaping():
    markup = NOAT("0123<&6789")
    target = "012<em>3&lt;&amp;6</em>789"
    markup.add("em", 3, 7)
    checkMarkupAsString(markup, target)
Exemplo n.º 16
0
def test_attributesAsDictAndKwargs():
    markup = NOAT("0123456789")
    target = '0123<span class="foo">4</span>56789'
    markup.add("span", 4, 5, {"class": "marker"}, _class="foo")
    checkMarkupAsString(markup, target)
Exemplo n.º 17
0
def test_attributesAsDictCollapsed():
    markup = NOAT("0123456789")
    target = '0123<span class="marker"></span>456789'
    markup.add("span", 4, {"class": "marker"})
    checkMarkupAsString(markup, target)
Exemplo n.º 18
0
def test_substituteClass():
    markup = NOAT("0123456789")
    target = '0123<span class="marker">4</span>56789'
    markup.add("span", 4, 5, _class="marker")
    checkMarkupAsString(markup, target)
Exemplo n.º 19
0
def test_collapsedRange():
    markup = NOAT("0123456789")
    target = '0123<span class="marker"></span>456789'
    attrs = {"class": "marker"}
    markup.add("span", 4, **attrs)
    checkMarkupAsString(markup, target)