Ejemplo n.º 1
0
 def test_unclosed_tag(self):
     """ Test if the tree builder mark unclosed tags as unclosed """
     known_tags = (DummyTreeNode, )
     document_tree = parse_skcode('[test]',
                                  recognized_tags=known_tags,
                                  mark_unclosed_tags_as_erroneous=False)
     self.assertIsInstance(document_tree, RootTreeNode)
     self.assertEqual(1, len(document_tree.children))
     test_node = document_tree.children[0]
     self.assertIsInstance(test_node, DummyTreeNode)
     self.assertEqual('test', test_node.name)
     self.assertEqual(0, len(test_node.children))
     self.assertEqual('[test]', test_node.source_open_tag)
     self.assertEqual('', test_node.source_close_tag)
     self.assertEqual('', test_node.error_message)
     document_tree = parse_skcode('[test]',
                                  recognized_tags=known_tags,
                                  mark_unclosed_tags_as_erroneous=True)
     self.assertIsInstance(document_tree, RootTreeNode)
     self.assertEqual(1, len(document_tree.children))
     test_node = document_tree.children[0]
     self.assertIsInstance(test_node, DummyTreeNode)
     self.assertEqual('test', test_node.name)
     self.assertEqual(0, len(test_node.children))
     self.assertEqual('[test]', test_node.source_open_tag)
     self.assertEqual('', test_node.source_close_tag)
     self.assertEqual('Unclosed tag', test_node.error_message)
Ejemplo n.º 2
0
 def test_unclosed_tag(self):
     """ Test if the tree builder mark unclosed tags as unclosed """
     known_tags = (
         DummyTreeNode,
     )
     document_tree = parse_skcode('[test]', recognized_tags=known_tags, mark_unclosed_tags_as_erroneous=False)
     self.assertIsInstance(document_tree, RootTreeNode)
     self.assertEqual(1, len(document_tree.children))
     test_node = document_tree.children[0]
     self.assertIsInstance(test_node, DummyTreeNode)
     self.assertEqual('test', test_node.name)
     self.assertEqual(0, len(test_node.children))
     self.assertEqual('[test]', test_node.source_open_tag)
     self.assertEqual('', test_node.source_close_tag)
     self.assertEqual('', test_node.error_message)
     document_tree = parse_skcode('[test]', recognized_tags=known_tags, mark_unclosed_tags_as_erroneous=True)
     self.assertIsInstance(document_tree, RootTreeNode)
     self.assertEqual(1, len(document_tree.children))
     test_node = document_tree.children[0]
     self.assertIsInstance(test_node, DummyTreeNode)
     self.assertEqual('test', test_node.name)
     self.assertEqual(0, len(test_node.children))
     self.assertEqual('[test]', test_node.source_open_tag)
     self.assertEqual('', test_node.source_close_tag)
     self.assertEqual('Unclosed tag', test_node.error_message)
Ejemplo n.º 3
0
 def test_no_text(self):
     """ Test the tree builder with no input text """
     document_tree = parse_skcode('')
     self.assertIsInstance(document_tree, RootTreeNode)
     self.assertEqual(0, len(document_tree.children))
     document_tree = parse_skcode('     ')
     self.assertIsInstance(document_tree, RootTreeNode)
     self.assertEqual(0, len(document_tree.children))
Ejemplo n.º 4
0
 def test_no_text(self):
     """ Test the tree builder with no input text """
     document_tree = parse_skcode('')
     self.assertIsInstance(document_tree, RootTreeNode)
     self.assertEqual(0, len(document_tree.children))
     document_tree = parse_skcode('     ')
     self.assertIsInstance(document_tree, RootTreeNode)
     self.assertEqual(0, len(document_tree.children))
Ejemplo n.º 5
0
 def test_newline(self):
     """ Test the tree builder with some newlines """
     document_tree = parse_skcode('Foo\nbar')
     self.assertIsInstance(document_tree, RootTreeNode)
     self.assertEqual(3, len(document_tree.children))
     child_node = document_tree.children[0]
     self.assertIsInstance(child_node, TextTreeNode)
     self.assertEqual(document_tree, child_node.root_tree_node)
     self.assertEqual(document_tree, child_node.parent)
     self.assertEqual({}, child_node.attrs)
     self.assertEqual('Foo', child_node.content)
     self.assertEqual(0, len(child_node.children))
     self.assertEqual('', child_node.source_open_tag)
     self.assertEqual('', child_node.source_close_tag)
     self.assertEqual('', child_node.error_message)
     child_node = document_tree.children[1]
     self.assertIsInstance(child_node, NewlineTreeNode)
     self.assertEqual(document_tree, child_node.root_tree_node)
     self.assertEqual(document_tree, child_node.parent)
     self.assertEqual({}, child_node.attrs)
     self.assertEqual('', child_node.content)
     self.assertEqual(0, len(child_node.children))
     self.assertEqual('', child_node.source_open_tag)
     self.assertEqual('', child_node.source_close_tag)
     self.assertEqual('', child_node.error_message)
     child_node = document_tree.children[2]
     self.assertIsInstance(child_node, TextTreeNode)
     self.assertEqual(document_tree, child_node.root_tree_node)
     self.assertEqual(document_tree, child_node.parent)
     self.assertEqual({}, child_node.attrs)
     self.assertEqual('bar', child_node.content)
     self.assertEqual(0, len(child_node.children))
     self.assertEqual('', child_node.source_open_tag)
     self.assertEqual('', child_node.source_close_tag)
     self.assertEqual('', child_node.error_message)
Ejemplo n.º 6
0
 def test_newline(self):
     """ Test the tree builder with some newlines """
     document_tree = parse_skcode('Foo\nbar')
     self.assertIsInstance(document_tree, RootTreeNode)
     self.assertEqual(3, len(document_tree.children))
     child_node = document_tree.children[0]
     self.assertIsInstance(child_node, TextTreeNode)
     self.assertEqual(document_tree, child_node.root_tree_node)
     self.assertEqual(document_tree, child_node.parent)
     self.assertEqual({}, child_node.attrs)
     self.assertEqual('Foo', child_node.content)
     self.assertEqual(0, len(child_node.children))
     self.assertEqual('', child_node.source_open_tag)
     self.assertEqual('', child_node.source_close_tag)
     self.assertEqual('', child_node.error_message)
     child_node = document_tree.children[1]
     self.assertIsInstance(child_node, NewlineTreeNode)
     self.assertEqual(document_tree, child_node.root_tree_node)
     self.assertEqual(document_tree, child_node.parent)
     self.assertEqual({}, child_node.attrs)
     self.assertEqual('', child_node.content)
     self.assertEqual(0, len(child_node.children))
     self.assertEqual('', child_node.source_open_tag)
     self.assertEqual('', child_node.source_close_tag)
     self.assertEqual('', child_node.error_message)
     child_node = document_tree.children[2]
     self.assertIsInstance(child_node, TextTreeNode)
     self.assertEqual(document_tree, child_node.root_tree_node)
     self.assertEqual(document_tree, child_node.parent)
     self.assertEqual({}, child_node.attrs)
     self.assertEqual('bar', child_node.content)
     self.assertEqual(0, len(child_node.children))
     self.assertEqual('', child_node.source_open_tag)
     self.assertEqual('', child_node.source_close_tag)
     self.assertEqual('', child_node.error_message)
Ejemplo n.º 7
0
 def test_unknown_tag(self):
     """ Test the tree builder with an unknown tag name """
     document_tree = parse_skcode('[test][/test]', recognized_tags=())
     self.assertIsInstance(document_tree, RootTreeNode)
     self.assertEqual(2, len(document_tree.children))
     open_error = document_tree.children[0]
     self.assertIsInstance(open_error, TextTreeNode)
     self.assertEqual(document_tree, open_error.root_tree_node)
     self.assertEqual(document_tree, open_error.parent)
     self.assertEqual({}, open_error.attrs)
     self.assertEqual('', open_error.content)
     self.assertEqual(0, len(open_error.children))
     self.assertEqual('[test]', open_error.source_open_tag)
     self.assertEqual('', open_error.source_close_tag)
     self.assertEqual('Unknown tag name', open_error.error_message)
     close_error = document_tree.children[1]
     self.assertIsInstance(close_error, TextTreeNode)
     self.assertEqual(document_tree, close_error.root_tree_node)
     self.assertEqual(document_tree, close_error.parent)
     self.assertEqual({}, close_error.attrs)
     self.assertEqual('', close_error.content)
     self.assertEqual(0, len(close_error.children))
     self.assertEqual('[/test]', close_error.source_open_tag)
     self.assertEqual('', close_error.source_close_tag)
     self.assertEqual('Unknown tag name', close_error.error_message)
Ejemplo n.º 8
0
 def test_unknown_tag(self):
     """ Test the tree builder with an unknown tag name """
     document_tree = parse_skcode('[test][/test]', recognized_tags=())
     self.assertIsInstance(document_tree, RootTreeNode)
     self.assertEqual(2, len(document_tree.children))
     open_error = document_tree.children[0]
     self.assertIsInstance(open_error, TextTreeNode)
     self.assertEqual(document_tree, open_error.root_tree_node)
     self.assertEqual(document_tree, open_error.parent)
     self.assertEqual({}, open_error.attrs)
     self.assertEqual('', open_error.content)
     self.assertEqual(0, len(open_error.children))
     self.assertEqual('[test]', open_error.source_open_tag)
     self.assertEqual('', open_error.source_close_tag)
     self.assertEqual('Unknown tag name', open_error.error_message)
     close_error = document_tree.children[1]
     self.assertIsInstance(close_error, TextTreeNode)
     self.assertEqual(document_tree, close_error.root_tree_node)
     self.assertEqual(document_tree, close_error.parent)
     self.assertEqual({}, close_error.attrs)
     self.assertEqual('', close_error.content)
     self.assertEqual(0, len(close_error.children))
     self.assertEqual('[/test]', close_error.source_open_tag)
     self.assertEqual('', close_error.source_close_tag)
     self.assertEqual('Unknown tag name', close_error.error_message)
Ejemplo n.º 9
0
 def test_weak_parent_close_nested(self):
     """ Test if the tree builder handle nested weak children tag when handling a parent closing tag """
     known_tags = (
         DummyTreeNode,
         get_dummy_node(canonical_tag_name='foobar',
                        weak_parent_close=True),
     )
     document_tree = parse_skcode('[test][foobar][foobar][/test]',
                                  recognized_tags=known_tags)
     self.assertIsInstance(document_tree, RootTreeNode)
     self.assertEqual(1, len(document_tree.children))
     test_node = document_tree.children[0]
     self.assertIsInstance(test_node, DummyTreeNode)
     self.assertEqual('test', test_node.name)
     self.assertEqual(1, len(test_node.children))
     self.assertEqual('[test]', test_node.source_open_tag)
     self.assertEqual('[/test]', test_node.source_close_tag)
     self.assertEqual('', test_node.error_message)
     foobar_node = test_node.children[0]
     self.assertIsInstance(foobar_node, DummyTreeNode)
     self.assertEqual('foobar', foobar_node.name)
     self.assertEqual(1, len(foobar_node.children))
     self.assertEqual('[foobar]', foobar_node.source_open_tag)
     self.assertEqual('', foobar_node.source_close_tag)
     self.assertEqual('', foobar_node.error_message)
     foobar_node = foobar_node.children[0]
     self.assertIsInstance(foobar_node, DummyTreeNode)
     self.assertEqual('foobar', foobar_node.name)
     self.assertEqual(0, len(foobar_node.children))
     self.assertEqual('[foobar]', foobar_node.source_open_tag)
     self.assertEqual('', foobar_node.source_close_tag)
     self.assertEqual('', foobar_node.error_message)
Ejemplo n.º 10
0
 def test_weak_parent_close_nested(self):
     """ Test if the tree builder handle nested weak children tag when handling a parent closing tag """
     known_tags = (
         DummyTreeNode,
         get_dummy_node(canonical_tag_name='foobar', weak_parent_close=True),
     )
     document_tree = parse_skcode('[test][foobar][foobar][/test]', recognized_tags=known_tags)
     self.assertIsInstance(document_tree, RootTreeNode)
     self.assertEqual(1, len(document_tree.children))
     test_node = document_tree.children[0]
     self.assertIsInstance(test_node, DummyTreeNode)
     self.assertEqual('test', test_node.name)
     self.assertEqual(1, len(test_node.children))
     self.assertEqual('[test]', test_node.source_open_tag)
     self.assertEqual('[/test]', test_node.source_close_tag)
     self.assertEqual('', test_node.error_message)
     foobar_node = test_node.children[0]
     self.assertIsInstance(foobar_node, DummyTreeNode)
     self.assertEqual('foobar', foobar_node.name)
     self.assertEqual(1, len(foobar_node.children))
     self.assertEqual('[foobar]', foobar_node.source_open_tag)
     self.assertEqual('', foobar_node.source_close_tag)
     self.assertEqual('', foobar_node.error_message)
     foobar_node = foobar_node.children[0]
     self.assertIsInstance(foobar_node, DummyTreeNode)
     self.assertEqual('foobar', foobar_node.name)
     self.assertEqual(0, len(foobar_node.children))
     self.assertEqual('[foobar]', foobar_node.source_open_tag)
     self.assertEqual('', foobar_node.source_close_tag)
     self.assertEqual('', foobar_node.error_message)
Ejemplo n.º 11
0
def convert_text_to_html(text, markup="bbcode"):
    if markup == 'bbcode':
        document = parse_skcode(text)
        text = render_to_html(document)
        text = text.replace('\n', '<br>')
        return text
    else:
        raise ValueError('Invalid markup property: {0}'.format(markup))
Ejemplo n.º 12
0
 def test_bad_closing(self):
     """ Test if the tree builder detect bad closing tag """
     document_tree = parse_skcode('[/test]', recognized_tags=(DummyTreeNode, ))
     self.assertIsInstance(document_tree, RootTreeNode)
     self.assertEqual(1, len(document_tree.children))
     child_node = document_tree.children[0]
     self.assertIsInstance(child_node, TextTreeNode)
     self.assertEqual(0, len(child_node.children))
     self.assertEqual('[/test]', child_node.source_close_tag)
     self.assertEqual('Unexpected closing tag', child_node.error_message)
Ejemplo n.º 13
0
 def test_bad_closing(self):
     """ Test if the tree builder detect bad closing tag """
     document_tree = parse_skcode('[/test]',
                                  recognized_tags=(DummyTreeNode, ))
     self.assertIsInstance(document_tree, RootTreeNode)
     self.assertEqual(1, len(document_tree.children))
     child_node = document_tree.children[0]
     self.assertIsInstance(child_node, TextTreeNode)
     self.assertEqual(0, len(child_node.children))
     self.assertEqual('[/test]', child_node.source_close_tag)
     self.assertEqual('Unexpected closing tag', child_node.error_message)
Ejemplo n.º 14
0
 def test_self_closing_tag(self):
     """ Test if the tree builder handle standalone self closing tag """
     known_tags = (get_dummy_node(standalone=True), )
     document_tree = parse_skcode('[test/]', recognized_tags=known_tags)
     self.assertIsInstance(document_tree, RootTreeNode)
     self.assertEqual(1, len(document_tree.children))
     child_node = document_tree.children[0]
     self.assertIsInstance(child_node, DummyTreeNode)
     self.assertEqual(0, len(child_node.children))
     self.assertEqual('[test/]', child_node.source_open_tag)
     self.assertEqual('', child_node.source_close_tag)
     self.assertEqual('', child_node.error_message)
Ejemplo n.º 15
0
 def test_after_building_unclosed_weak_parent_close(self):
     """ Test if the tree builder handle weak children tag when reached the end of the document """
     known_tags = (get_dummy_node(weak_parent_close=True), )
     document_tree = parse_skcode('[test]', recognized_tags=known_tags)
     self.assertIsInstance(document_tree, RootTreeNode)
     self.assertEqual(1, len(document_tree.children))
     test_node = document_tree.children[0]
     self.assertIsInstance(test_node, DummyTreeNode)
     self.assertEqual('test', test_node.name)
     self.assertEqual(0, len(test_node.children))
     self.assertEqual('[test]', test_node.source_open_tag)
     self.assertEqual('', test_node.source_close_tag)
     self.assertEqual('', test_node.error_message)
Ejemplo n.º 16
0
 def test_self_closing_tag_2(self):
     """ Test if the tree builder handle wrong non standalone self closing tag """
     known_tags = (
         get_dummy_node(standalone=False),
     )
     document_tree = parse_skcode('[test/]', recognized_tags=known_tags)
     self.assertIsInstance(document_tree, RootTreeNode)
     self.assertEqual(1, len(document_tree.children))
     child_node = document_tree.children[0]
     self.assertIsInstance(child_node, TextTreeNode)
     self.assertEqual(0, len(child_node.children))
     self.assertEqual('[test/]', child_node.source_open_tag)
     self.assertEqual('', child_node.source_close_tag)
     self.assertEqual('Unexpected self closing tag', child_node.error_message)
Ejemplo n.º 17
0
 def test_after_building_unclosed_weak_parent_close(self):
     """ Test if the tree builder handle weak children tag when reached the end of the document """
     known_tags = (
         get_dummy_node(weak_parent_close=True),
     )
     document_tree = parse_skcode('[test]', recognized_tags=known_tags)
     self.assertIsInstance(document_tree, RootTreeNode)
     self.assertEqual(1, len(document_tree.children))
     test_node = document_tree.children[0]
     self.assertIsInstance(test_node, DummyTreeNode)
     self.assertEqual('test', test_node.name)
     self.assertEqual(0, len(test_node.children))
     self.assertEqual('[test]', test_node.source_open_tag)
     self.assertEqual('', test_node.source_close_tag)
     self.assertEqual('', test_node.error_message)
Ejemplo n.º 18
0
 def test_data_custom_text_cls(self):
     """ Test the tree builder with some raw text data and a custom text node class """
     document_tree = parse_skcode('Foobar', text_node_cls=CustomTextTreeNode)
     self.assertIsInstance(document_tree, RootTreeNode)
     self.assertEqual(1, len(document_tree.children))
     child_node = document_tree.children[0]
     self.assertIsInstance(child_node, CustomTextTreeNode)
     self.assertEqual(document_tree, child_node.root_tree_node)
     self.assertEqual(document_tree, child_node.parent)
     self.assertEqual({}, child_node.attrs)
     self.assertEqual('Foobar', child_node.content)
     self.assertEqual(0, len(child_node.children))
     self.assertEqual('', child_node.source_open_tag)
     self.assertEqual('', child_node.source_close_tag)
     self.assertEqual('', child_node.error_message)
Ejemplo n.º 19
0
 def test_data(self):
     """ Test the tree builder with some raw text data """
     document_tree = parse_skcode('Foobar')
     self.assertIsInstance(document_tree, RootTreeNode)
     self.assertEqual(1, len(document_tree.children))
     child_node = document_tree.children[0]
     self.assertIsInstance(child_node, TextTreeNode)
     self.assertEqual(document_tree, child_node.root_tree_node)
     self.assertEqual(document_tree, child_node.parent)
     self.assertEqual({}, child_node.attrs)
     self.assertEqual('Foobar', child_node.content)
     self.assertEqual(0, len(child_node.children))
     self.assertEqual('', child_node.source_open_tag)
     self.assertEqual('', child_node.source_close_tag)
     self.assertEqual('', child_node.error_message)
Ejemplo n.º 20
0
 def test_data_block(self):
     """ Test the tree builder with data block tag (``parse_embedded=False``) """
     known_tags = (get_dummy_node(parse_embedded=False), )
     document_tree = parse_skcode('[test][i]Hello[test] world![/i][/test]',
                                  recognized_tags=known_tags)
     self.assertIsInstance(document_tree, RootTreeNode)
     self.assertEqual(1, len(document_tree.children))
     code_block = document_tree.children[0]
     self.assertEqual(document_tree, code_block.root_tree_node)
     self.assertEqual(document_tree, code_block.parent)
     self.assertEqual('test', code_block.name)
     self.assertEqual({}, code_block.attrs)
     self.assertEqual('[i]Hello[test] world![/i]', code_block.content)
     self.assertEqual(0, len(code_block.children))
     self.assertEqual('[test]', code_block.source_open_tag)
     self.assertEqual('[/test]', code_block.source_close_tag)
     self.assertEqual('', code_block.error_message)
Ejemplo n.º 21
0
 def test_bad_closing_2(self):
     """ Test if the tree builder detect bad closing tag (nested tag) """
     known_tags = (
         get_dummy_node(canonical_tag_name='inline'),
         DummyTreeNode,
     )
     document_tree = parse_skcode('[inline][/test]', recognized_tags=known_tags)
     self.assertIsInstance(document_tree, RootTreeNode)
     self.assertEqual(1, len(document_tree.children))
     child_node = document_tree.children[0]
     self.assertIsInstance(child_node, DummyTreeNode)
     self.assertEqual('inline', child_node.name)
     self.assertEqual(1, len(child_node.children))
     sub_child_node = child_node.children[0]
     self.assertIsInstance(sub_child_node, TextTreeNode)
     self.assertEqual(0, len(sub_child_node.children))
     self.assertEqual('[/test]', sub_child_node.source_close_tag)
     self.assertEqual('Unexpected closing tag', sub_child_node.error_message)
Ejemplo n.º 22
0
 def test_data_block(self):
     """ Test the tree builder with data block tag (``parse_embedded=False``) """
     known_tags = (
         get_dummy_node(parse_embedded=False),
     )
     document_tree = parse_skcode('[test][i]Hello[test] world![/i][/test]', recognized_tags=known_tags)
     self.assertIsInstance(document_tree, RootTreeNode)
     self.assertEqual(1, len(document_tree.children))
     code_block = document_tree.children[0]
     self.assertEqual(document_tree, code_block.root_tree_node)
     self.assertEqual(document_tree, code_block.parent)
     self.assertEqual('test', code_block.name)
     self.assertEqual({}, code_block.attrs)
     self.assertEqual('[i]Hello[test] world![/i]', code_block.content)
     self.assertEqual(0, len(code_block.children))
     self.assertEqual('[test]', code_block.source_open_tag)
     self.assertEqual('[/test]', code_block.source_close_tag)
     self.assertEqual('', code_block.error_message)
Ejemplo n.º 23
0
 def test_newline_close(self):
     """ Test the tree builder handle ``newline_closes=True`` when handling a newline """
     known_tags = (
         get_dummy_node(newline_closes=True),
     )
     document_tree = parse_skcode('[test][test]\nfoobar', recognized_tags=known_tags)
     self.assertIsInstance(document_tree, RootTreeNode)
     self.assertEqual(3, len(document_tree.children))
     child_node = document_tree.children[0]
     self.assertEqual(document_tree, child_node.root_tree_node)
     self.assertEqual(document_tree, child_node.parent)
     self.assertEqual({}, child_node.attrs)
     self.assertEqual('test', child_node.name)
     self.assertEqual(1, len(child_node.children))
     self.assertEqual('[test]', child_node.source_open_tag)
     self.assertEqual('', child_node.source_close_tag)
     self.assertEqual('', child_node.error_message)
     sub_child_node = child_node.children[0]
     self.assertEqual(document_tree, sub_child_node.root_tree_node)
     self.assertEqual(child_node, sub_child_node.parent)
     self.assertEqual({}, sub_child_node.attrs)
     self.assertEqual('test', sub_child_node.name)
     self.assertEqual(0, len(sub_child_node.children))
     self.assertEqual('[test]', child_node.source_open_tag)
     self.assertEqual('', child_node.source_close_tag)
     self.assertEqual('', child_node.error_message)
     child_node = document_tree.children[1]
     self.assertIsInstance(child_node, NewlineTreeNode)
     self.assertEqual(document_tree, child_node.root_tree_node)
     self.assertEqual(document_tree, child_node.parent)
     self.assertEqual({}, child_node.attrs)
     self.assertEqual(0, len(child_node.children))
     self.assertEqual('', child_node.source_open_tag)
     self.assertEqual('', child_node.source_close_tag)
     self.assertEqual('', child_node.error_message)
     child_node = document_tree.children[2]
     self.assertEqual(document_tree, child_node.root_tree_node)
     self.assertEqual(document_tree, child_node.parent)
     self.assertEqual({}, child_node.attrs)
     self.assertEqual('foobar', child_node.content)
     self.assertEqual(0, len(child_node.children))
     self.assertEqual('', child_node.source_open_tag)
     self.assertEqual('', child_node.source_close_tag)
     self.assertEqual('', child_node.error_message)
Ejemplo n.º 24
0
 def test_standalone_tag(self):
     """ Test the tree builder handle ``standalone=True`` when handling a tag """
     known_tags = (get_dummy_node(standalone=True), )
     document_tree = parse_skcode('[test][test][test]',
                                  recognized_tags=known_tags)
     self.assertIsInstance(document_tree, RootTreeNode)
     self.assertEqual(3, len(document_tree.children))
     child_node = document_tree.children[0]
     self.assertEqual('test', child_node.name)
     self.assertEqual(0, len(child_node.children))
     self.assertEqual('', child_node.error_message)
     child_node = document_tree.children[1]
     self.assertEqual('test', child_node.name)
     self.assertEqual(0, len(child_node.children))
     self.assertEqual('', child_node.error_message)
     child_node = document_tree.children[2]
     self.assertEqual('test', child_node.name)
     self.assertEqual(0, len(child_node.children))
     self.assertEqual('', child_node.error_message)
Ejemplo n.º 25
0
 def test_cls_options_overload_self_closing_tag(self):
     """ Test if the tree builder handle class options overload on self closing tag """
     known_tags = (get_dummy_node(standalone=True, foo='bar'), )
     document_tree = parse_skcode('[test/]',
                                  recognized_tags=known_tags,
                                  cls_options_overload={
                                      known_tags[0]: {
                                          'foo': 'baz',
                                      },
                                  })
     self.assertIsInstance(document_tree, RootTreeNode)
     self.assertEqual(1, len(document_tree.children))
     child_node = document_tree.children[0]
     self.assertIsInstance(child_node, DummyTreeNode)
     self.assertEqual(0, len(child_node.children))
     self.assertEqual('[test/]', child_node.source_open_tag)
     self.assertEqual('', child_node.source_close_tag)
     self.assertEqual('', child_node.error_message)
     self.assertEqual('baz', child_node.foo)
Ejemplo n.º 26
0
 def test_newline_close(self):
     """ Test the tree builder handle ``newline_closes=True`` when handling a newline """
     known_tags = (get_dummy_node(newline_closes=True), )
     document_tree = parse_skcode('[test][test]\nfoobar',
                                  recognized_tags=known_tags)
     self.assertIsInstance(document_tree, RootTreeNode)
     self.assertEqual(3, len(document_tree.children))
     child_node = document_tree.children[0]
     self.assertEqual(document_tree, child_node.root_tree_node)
     self.assertEqual(document_tree, child_node.parent)
     self.assertEqual({}, child_node.attrs)
     self.assertEqual('test', child_node.name)
     self.assertEqual(1, len(child_node.children))
     self.assertEqual('[test]', child_node.source_open_tag)
     self.assertEqual('', child_node.source_close_tag)
     self.assertEqual('', child_node.error_message)
     sub_child_node = child_node.children[0]
     self.assertEqual(document_tree, sub_child_node.root_tree_node)
     self.assertEqual(child_node, sub_child_node.parent)
     self.assertEqual({}, sub_child_node.attrs)
     self.assertEqual('test', sub_child_node.name)
     self.assertEqual(0, len(sub_child_node.children))
     self.assertEqual('[test]', child_node.source_open_tag)
     self.assertEqual('', child_node.source_close_tag)
     self.assertEqual('', child_node.error_message)
     child_node = document_tree.children[1]
     self.assertIsInstance(child_node, NewlineTreeNode)
     self.assertEqual(document_tree, child_node.root_tree_node)
     self.assertEqual(document_tree, child_node.parent)
     self.assertEqual({}, child_node.attrs)
     self.assertEqual(0, len(child_node.children))
     self.assertEqual('', child_node.source_open_tag)
     self.assertEqual('', child_node.source_close_tag)
     self.assertEqual('', child_node.error_message)
     child_node = document_tree.children[2]
     self.assertEqual(document_tree, child_node.root_tree_node)
     self.assertEqual(document_tree, child_node.parent)
     self.assertEqual({}, child_node.attrs)
     self.assertEqual('foobar', child_node.content)
     self.assertEqual(0, len(child_node.children))
     self.assertEqual('', child_node.source_open_tag)
     self.assertEqual('', child_node.source_close_tag)
     self.assertEqual('', child_node.error_message)
Ejemplo n.º 27
0
 def test_bad_closing_2(self):
     """ Test if the tree builder detect bad closing tag (nested tag) """
     known_tags = (
         get_dummy_node(canonical_tag_name='inline'),
         DummyTreeNode,
     )
     document_tree = parse_skcode('[inline][/test]',
                                  recognized_tags=known_tags)
     self.assertIsInstance(document_tree, RootTreeNode)
     self.assertEqual(1, len(document_tree.children))
     child_node = document_tree.children[0]
     self.assertIsInstance(child_node, DummyTreeNode)
     self.assertEqual('inline', child_node.name)
     self.assertEqual(1, len(child_node.children))
     sub_child_node = child_node.children[0]
     self.assertIsInstance(sub_child_node, TextTreeNode)
     self.assertEqual(0, len(sub_child_node.children))
     self.assertEqual('[/test]', sub_child_node.source_close_tag)
     self.assertEqual('Unexpected closing tag',
                      sub_child_node.error_message)
Ejemplo n.º 28
0
    def test_count_words(self):
        """ Test the ``count_words`` with no text """
        document = parse_skcode(
            """Lorem ipsum dolor sit amet, consectetur adipiscing elit.
            Nulla eget erat sed nisi tincidunt pellentesque vel iaculis eros.
            In dui sapien, auctor et dui eget, elementum sodales justo.
            Fusce at condimentum magna, sed tincidunt lacus.
            [b]In quis scelerisque sem.[/b]

            [icode]This should NOT be count as text.[/icode]

            Phasellus congue, elit in dictum imperdiet, massa justo viverra libero, ac viverra nunc lectus vel turpis.
            Vestibulum placerat arcu quis sem euismod, at condimentum dui aliquam. Vivamus vitae cursus velit.
            [i]Praesent eget tempus lacus.[/i]

            Proin augue leo, consectetur ut risus gravida, varius cursus libero.
            Etiam lobortis iaculis elit, vel aliquam nibh rhoncus ut.
            Nunc auctor sapien vitae neque sodales congue. Suspendisse.""")
        words_count = count_words(document)
        self.assertEqual(100, words_count)
Ejemplo n.º 29
0
 def test_standalone_tag(self):
     """ Test the tree builder handle ``standalone=True`` when handling a tag """
     known_tags = (
         get_dummy_node(standalone=True),
     )
     document_tree = parse_skcode('[test][test][test]', recognized_tags=known_tags)
     self.assertIsInstance(document_tree, RootTreeNode)
     self.assertEqual(3, len(document_tree.children))
     child_node = document_tree.children[0]
     self.assertEqual('test', child_node.name)
     self.assertEqual(0, len(child_node.children))
     self.assertEqual('', child_node.error_message)
     child_node = document_tree.children[1]
     self.assertEqual('test', child_node.name)
     self.assertEqual(0, len(child_node.children))
     self.assertEqual('', child_node.error_message)
     child_node = document_tree.children[2]
     self.assertEqual('test', child_node.name)
     self.assertEqual(0, len(child_node.children))
     self.assertEqual('', child_node.error_message)
Ejemplo n.º 30
0
 def test_new_tags_declaration_style(self):
     """ Test if the new tags declarations style (list based) works """
     known_tags = (
         get_dummy_node(alias_tag_names=('alias', )),
     )
     document_tree = parse_skcode('[test][/test] [alias][/alias]', recognized_tags=known_tags)
     self.assertIsInstance(document_tree, RootTreeNode)
     self.assertEqual(3, len(document_tree.children))
     tree_node = document_tree.children[0]
     self.assertEqual(document_tree, tree_node.root_tree_node)
     self.assertEqual(document_tree, tree_node.parent)
     self.assertEqual('test', tree_node.name)
     self.assertIsInstance(tree_node, DummyTreeNode)
     self.assertEqual({}, tree_node.attrs)
     self.assertEqual('', tree_node.content)
     self.assertEqual(0, len(tree_node.children))
     self.assertEqual('[test]', tree_node.source_open_tag)
     self.assertEqual('[/test]', tree_node.source_close_tag)
     self.assertEqual('', tree_node.error_message)
     tree_node = document_tree.children[1]
     self.assertEqual(document_tree, tree_node.root_tree_node)
     self.assertEqual(document_tree, tree_node.parent)
     self.assertIsNone(tree_node.name)
     self.assertIsInstance(tree_node, TextTreeNode)
     self.assertEqual({}, tree_node.attrs)
     self.assertEqual(' ', tree_node.content)
     self.assertEqual(0, len(tree_node.children))
     self.assertEqual('', tree_node.source_open_tag)
     self.assertEqual('', tree_node.source_close_tag)
     self.assertEqual('', tree_node.error_message)
     tree_node = document_tree.children[2]
     self.assertEqual(document_tree, tree_node.root_tree_node)
     self.assertEqual(document_tree, tree_node.parent)
     self.assertEqual('alias', tree_node.name)
     self.assertIsInstance(tree_node, DummyTreeNode)
     self.assertEqual({}, tree_node.attrs)
     self.assertEqual('', tree_node.content)
     self.assertEqual(0, len(tree_node.children))
     self.assertEqual('[alias]', tree_node.source_open_tag)
     self.assertEqual('[/alias]', tree_node.source_close_tag)
     self.assertEqual('', tree_node.error_message)
Ejemplo n.º 31
0
 def test_close_inline(self):
     """ Test the tree builder handle ``close_inlines=True`` when handling a tag """
     known_tags = (
         get_dummy_node(canonical_tag_name='inline', inline=True, close_inlines=False),
         get_dummy_node(inline=False, close_inlines=True),
     )
     document_tree = parse_skcode('[inline][inline][test]', recognized_tags=known_tags)
     self.assertIsInstance(document_tree, RootTreeNode)
     self.assertEqual(2, len(document_tree.children))
     child_node = document_tree.children[0]
     self.assertEqual('inline', child_node.name)
     self.assertEqual(1, len(child_node.children))
     self.assertEqual('', child_node.error_message)
     sub_child_node = child_node.children[0]
     self.assertEqual('inline', sub_child_node.name)
     self.assertEqual(0, len(sub_child_node.children))
     self.assertEqual('', sub_child_node.error_message)
     child_node = document_tree.children[1]
     self.assertEqual('test', child_node.name)
     self.assertEqual(0, len(child_node.children))
     self.assertEqual('', child_node.error_message)
Ejemplo n.º 32
0
    def test_pre_post_processing_sanitizing(self):
        """ Test if the tree builder start the pre/post processing and sanitizing process """

        class TestTreeNode(TreeNode):
            """ Test tag options for tests """

            canonical_tag_name = 'test'
            pre_processed = False
            sanitized = False
            breadcrumb = None
            post_processed = False

            def pre_process_node(self):
                self.pre_processed = True

            def sanitize_node(self, breadcrumb):
                self.sanitized = True
                self.breadcrumb = breadcrumb

            def post_process_node(self):
                self.post_processed = True

        known_tags = (
            TestTreeNode,
        )
        document_tree = parse_skcode('[test][test]', recognized_tags=known_tags)
        self.assertIsInstance(document_tree, RootTreeNode)
        self.assertEqual(1, len(document_tree.children))
        test_node = document_tree.children[0]
        self.assertEqual(1, len(test_node.children))
        self.assertTrue(test_node.pre_processed)
        self.assertTrue(test_node.sanitized)
        self.assertEqual([], test_node.breadcrumb)
        self.assertTrue(test_node.post_processed)
        sub_test_node = test_node.children[0]
        self.assertEqual(0, len(sub_test_node.children))
        self.assertTrue(sub_test_node.pre_processed)
        self.assertTrue(sub_test_node.sanitized)
        self.assertEqual([test_node], sub_test_node.breadcrumb)
        self.assertTrue(sub_test_node.post_processed)
Ejemplo n.º 33
0
 def test_new_tags_declaration_style(self):
     """ Test if the new tags declarations style (list based) works """
     known_tags = (get_dummy_node(alias_tag_names=('alias', )), )
     document_tree = parse_skcode('[test][/test] [alias][/alias]',
                                  recognized_tags=known_tags)
     self.assertIsInstance(document_tree, RootTreeNode)
     self.assertEqual(3, len(document_tree.children))
     tree_node = document_tree.children[0]
     self.assertEqual(document_tree, tree_node.root_tree_node)
     self.assertEqual(document_tree, tree_node.parent)
     self.assertEqual('test', tree_node.name)
     self.assertIsInstance(tree_node, DummyTreeNode)
     self.assertEqual({}, tree_node.attrs)
     self.assertEqual('', tree_node.content)
     self.assertEqual(0, len(tree_node.children))
     self.assertEqual('[test]', tree_node.source_open_tag)
     self.assertEqual('[/test]', tree_node.source_close_tag)
     self.assertEqual('', tree_node.error_message)
     tree_node = document_tree.children[1]
     self.assertEqual(document_tree, tree_node.root_tree_node)
     self.assertEqual(document_tree, tree_node.parent)
     self.assertIsNone(tree_node.name)
     self.assertIsInstance(tree_node, TextTreeNode)
     self.assertEqual({}, tree_node.attrs)
     self.assertEqual(' ', tree_node.content)
     self.assertEqual(0, len(tree_node.children))
     self.assertEqual('', tree_node.source_open_tag)
     self.assertEqual('', tree_node.source_close_tag)
     self.assertEqual('', tree_node.error_message)
     tree_node = document_tree.children[2]
     self.assertEqual(document_tree, tree_node.root_tree_node)
     self.assertEqual(document_tree, tree_node.parent)
     self.assertEqual('alias', tree_node.name)
     self.assertIsInstance(tree_node, DummyTreeNode)
     self.assertEqual({}, tree_node.attrs)
     self.assertEqual('', tree_node.content)
     self.assertEqual(0, len(tree_node.children))
     self.assertEqual('[alias]', tree_node.source_open_tag)
     self.assertEqual('[/alias]', tree_node.source_close_tag)
     self.assertEqual('', tree_node.error_message)
Ejemplo n.º 34
0
    def test_pre_post_processing_sanitizing(self):
        """ Test if the tree builder start the pre/post processing and sanitizing process """
        class TestTreeNode(TreeNode):
            """ Test tag options for tests """

            canonical_tag_name = 'test'
            pre_processed = False
            sanitized = False
            breadcrumb = None
            post_processed = False

            def pre_process_node(self):
                self.pre_processed = True

            def sanitize_node(self, breadcrumb):
                self.sanitized = True
                self.breadcrumb = breadcrumb

            def post_process_node(self):
                self.post_processed = True

        known_tags = (TestTreeNode, )
        document_tree = parse_skcode('[test][test]',
                                     recognized_tags=known_tags)
        self.assertIsInstance(document_tree, RootTreeNode)
        self.assertEqual(1, len(document_tree.children))
        test_node = document_tree.children[0]
        self.assertEqual(1, len(test_node.children))
        self.assertTrue(test_node.pre_processed)
        self.assertTrue(test_node.sanitized)
        self.assertEqual([], test_node.breadcrumb)
        self.assertTrue(test_node.post_processed)
        sub_test_node = test_node.children[0]
        self.assertEqual(0, len(sub_test_node.children))
        self.assertTrue(sub_test_node.pre_processed)
        self.assertTrue(sub_test_node.sanitized)
        self.assertEqual([test_node], sub_test_node.breadcrumb)
        self.assertTrue(sub_test_node.post_processed)
Ejemplo n.º 35
0
 def test_close_inline(self):
     """ Test the tree builder handle ``close_inlines=True`` when handling a tag """
     known_tags = (
         get_dummy_node(canonical_tag_name='inline',
                        inline=True,
                        close_inlines=False),
         get_dummy_node(inline=False, close_inlines=True),
     )
     document_tree = parse_skcode('[inline][inline][test]',
                                  recognized_tags=known_tags)
     self.assertIsInstance(document_tree, RootTreeNode)
     self.assertEqual(2, len(document_tree.children))
     child_node = document_tree.children[0]
     self.assertEqual('inline', child_node.name)
     self.assertEqual(1, len(child_node.children))
     self.assertEqual('', child_node.error_message)
     sub_child_node = child_node.children[0]
     self.assertEqual('inline', sub_child_node.name)
     self.assertEqual(0, len(sub_child_node.children))
     self.assertEqual('', sub_child_node.error_message)
     child_node = document_tree.children[1]
     self.assertEqual('test', child_node.name)
     self.assertEqual(0, len(child_node.children))
     self.assertEqual('', child_node.error_message)
Ejemplo n.º 36
0
    # Get user input
    print(
        'Use CTRL+C, CTRL+D or simply type "EOF" on a single line to stop text input'
    )
    text = []
    try:
        while True:
            line = input('skcode> ')
            if line == 'EOF':
                break
            text.append(line)
    except (KeyboardInterrupt, EOFError):
        pass
    text = '\n'.join(text)

    # Parse text
    ast = parse_skcode(text)

    # Dump parsing info
    print('----- Input text -----')
    print(text)
    print('----- Document tree -----')
    debug_print_ast(ast)
    print('----- HTML output -----')
    print(render_to_html(ast))
    print('----- TEXT output -----')
    print(render_to_text(ast))

    # End of script
    input('Press enter to exit.')
Ejemplo n.º 37
0
    print('-- SkCode testing terminal --')

    # Get user input
    print('Use CTRL+C, CTRL+D or simply type "EOF" on a single line to stop text input')
    text = []
    try:
        while True:
            line = input('skcode> ')
            if line == 'EOF':
                break
            text.append(line)
    except (KeyboardInterrupt, EOFError):
        pass
    text = '\n'.join(text)

    # Parse text
    ast = parse_skcode(text)

    # Dump parsing info
    print('----- Input text -----')
    print(text)
    print('----- Document tree -----')
    debug_print_ast(ast)
    print('----- HTML output -----')
    print(render_to_html(ast))
    print('----- TEXT output -----')
    print(render_to_text(ast))

    # End of script
    input('Press enter to exit.')
Ejemplo n.º 38
0
def render_document(input_text,
                    allow_titles=False,
                    allow_code_blocks=False,
                    allow_alerts_box=False,
                    allow_text_formating=False,
                    allow_text_extra=False,
                    allow_text_alignments=False,
                    allow_text_directions=False,
                    allow_text_modifiers=False,
                    allow_text_colors=False,

                    allow_spoilers=False,
                    allow_figures=False,
                    allow_lists=False,
                    allow_todo_lists=False,
                    allow_definition_lists=False,
                    allow_tables=False,

                    allow_quotes=False,
                    allow_footnotes=False,
                    allow_acronyms=False,
                    allow_links=False,
                    allow_medias=False,

                    allow_cdm_extra=False,

                    force_nofollow=True,
                    preview_mode=False,
                    hard_newline=False,
                    render_text_version=False,
                    render_extra_dict=False,
                    merge_footnotes_html=False,
                    merge_footnotes_text=False,
                    make_auto_paragraphs=True):
    """
    Render the given document as HTML, text (if requested) and output extra runtime information.
    :param input_text: The input document text (not safe).
    """

    # Shortcut for empty documents
    input_text = input_text.strip()
    if not input_text:
        return '', '', {
            'footnotes_html': '',
            'footnotes_text': '',
            'summary_html': '',
            'summary_text': '',
        }

    # Filter tags according to permissions
    allowed_tags = {}
    copy_tags_if_allowed(('h1', 'h2', 'h3',
                          'h4', 'h5', 'h6'), DEFAULT_RECOGNIZED_TAGS, allowed_tags, allow_titles)
    copy_tags_if_allowed(('code', 'python', 'cpp',
                          'java', 'html', 'php'), DEFAULT_RECOGNIZED_TAGS, allowed_tags, allow_code_blocks)
    copy_tags_if_allowed(('alert', 'error', 'danger',
                          'warning', 'info', 'success',
                          'note', 'question'), DEFAULT_RECOGNIZED_TAGS, allowed_tags, allow_alerts_box)
    copy_tags_if_allowed(('b', 'bold', 'strong',
                          'i', 'italic', 'em',
                          's', 'strike', 'del',
                          'u', 'underline', 'ins',
                          'sub', 'sup', 'pre',
                          'icode', 'kbd', 'keyboard',
                          'glow', 'highlight', 'mark',
                          'cite', 'not'), DEFAULT_RECOGNIZED_TAGS, allowed_tags, allow_text_formating)
    copy_tags_if_allowed(('nobbc', 'noparse', 'hr', 'br'), DEFAULT_RECOGNIZED_TAGS, allowed_tags, allow_text_extra)
    copy_tags_if_allowed(('center', 'left', 'right'), DEFAULT_RECOGNIZED_TAGS, allowed_tags, allow_text_alignments)
    copy_tags_if_allowed(('bdo', 'ltr', 'rtl'), DEFAULT_RECOGNIZED_TAGS, allowed_tags, allow_text_directions)
    copy_tags_if_allowed(('lowercase', 'uppercase', 'capitalize'), DEFAULT_RECOGNIZED_TAGS, allowed_tags, allow_text_modifiers)
    copy_tags_if_allowed(('color', 'black', 'blue',
                          'gray', 'green', 'orange',
                          'purple', 'red', 'white', 'yellow'), DEFAULT_RECOGNIZED_TAGS, allowed_tags, allow_text_colors)
    copy_tags_if_allowed(('spoiler', 'hide', 'ispoiler'), DEFAULT_RECOGNIZED_TAGS, allowed_tags, allow_spoilers)
    copy_tags_if_allowed(('figure', 'figcaption'), DEFAULT_RECOGNIZED_TAGS, allowed_tags, allow_figures)
    copy_tags_if_allowed(('list', 'ul', 'ol', 'li'), DEFAULT_RECOGNIZED_TAGS, allowed_tags, allow_lists)
    copy_tags_if_allowed(('todolist', 'task'), DEFAULT_RECOGNIZED_TAGS, allowed_tags, allow_todo_lists)
    copy_tags_if_allowed(('dl', 'dt', 'dd'), DEFAULT_RECOGNIZED_TAGS, allowed_tags, allow_definition_lists)
    copy_tags_if_allowed(('table', 'tr', 'th', 'td'), DEFAULT_RECOGNIZED_TAGS, allowed_tags, allow_tables)
    copy_tags_if_allowed(('quote', 'blockquote'), DEFAULT_RECOGNIZED_TAGS, allowed_tags, allow_quotes)
    copy_tags_if_allowed(('footnote', 'fn', 'fnref'), DEFAULT_RECOGNIZED_TAGS, allowed_tags, allow_footnotes)
    copy_tags_if_allowed(('abbr', 'acronym'), DEFAULT_RECOGNIZED_TAGS, allowed_tags, allow_acronyms)
    copy_tags_if_allowed(('anchor', 'goto', 'url', 'link', 'email'), DEFAULT_RECOGNIZED_TAGS, allowed_tags, allow_links)
    copy_tags_if_allowed(('img', 'youtube'), DEFAULT_RECOGNIZED_TAGS, allowed_tags, allow_medias)
    # copy_tags_if_allowed(('', ''), DEFAULT_RECOGNIZED_TAGS, allowed_tags, allow_cdm_extra)

    # Handle preview_mode and hard_newline options
    erroneous_text_opts = ErroneousTextTagOptions() if preview_mode else TextTagOptions()
    newlines_opts = HardNewlineTagOptions() if hard_newline else NewlineTagOptions()

    # Parse the document
    document = parse_skcode(input_text,
                            allow_tagvalue_attr=True,
                            allow_self_closing_tags=True,
                            erroneous_text_node_opts=erroneous_text_opts,
                            newline_node_opts=newlines_opts,
                            drop_unrecognized=False,
                            texturize_unclosed_tags=False)

    # Setup smileys and cosmetics replacement
    def _base_url(filename):
        return static(EMOTICONS_IMG_DIR + filename)
    setup_cosmetics_replacement(document)
    setup_smileys_replacement(document, _base_url)

    # Setup absolute URL conversion
    if RELATIVE_URL_BASE:
        setup_relative_urls_conversion(document, RELATIVE_URL_BASE)

    # Make paragraphs
    if make_auto_paragraphs:
        make_paragraphs(document)

    # Make auto titles IDs
    make_auto_title_ids(document)

    # Handle footnotes
    if allow_footnotes and (render_extra_dict or merge_footnotes_html or merge_footnotes_text):
        footnotes = extract_footnotes(document)
        footnotes_output_html = render_footnotes_html(footnotes)
        footnotes_output_text = render_footnotes_text(footnotes) if render_text_version else ''
    else:
        footnotes_output_html = ''
        footnotes_output_text = ''

    # Extra information dictionary
    if render_extra_dict:

        # Extract all titles
        titles = extract_titles(document)

        # Turn the titles list into a hierarchy
        titles_hierarchy = make_titles_hierarchy(titles)

        # Render titles hierarchy
        titles_summary_output_html = render_titles_hierarchy_html(titles_hierarchy)
        titles_summary_output_text = render_titles_hierarchy_text(titles_hierarchy) if render_text_version else ''

    else:
        titles_summary_output_html = ''
        titles_summary_output_text = ''

    # Render the document
    content_html = render_to_html(document, force_rel_nofollow=force_nofollow)
    content_text = render_to_text(document) if render_text_version else ''

    # Merge footnotes if requested
    if merge_footnotes_html and footnotes_output_html:
        content_html += '<hr>\n' + footnotes_output_html
    if merge_footnotes_text and footnotes_output_text:
        content_text += '----------\n\n' + footnotes_output_text

    # Return the result
    return content_html, content_text, {
        'footnotes_html': footnotes_output_html,
        'footnotes_text': footnotes_output_text,
        'summary_html': titles_summary_output_html,
        'summary_text': titles_summary_output_text,
    }
Ejemplo n.º 39
0
def home_page(request,
              template_name='home/home.html',
              test_input_form=TestSkCodeInputForm,
              extra_context=None):
    """
    PySkCode tester home page with form for testing the parser.
    :param request: The current request.
    :param template_name: The template name to be used.
    :param test_input_form: The test input form class to be used.
    :param extra_context: Any extra template context information.
    :return: TemplateResponse
    """

    # Default values
    output_content_html = ''
    output_content_text = ''
    summary_content_html = ''
    summary_content_text = ''
    footnotes_content_html = ''
    footnotes_content_text = ''
    document_has_errors = False

    # Handle the form
    if request.method == "POST":
        form = test_input_form(request.POST, request.FILES)
        if form.is_valid():

            # Parse the input text
            newline_node_cls = HardNewlineTreeNode if form.cleaned_data['hard_newline'] else NewlineTreeNode
            html_error_template = DEFAULT_ERROR_HTML_TEMPLATE if form.cleaned_data['preview_mode'] else SUPPRESS_ERROR_HTML_TEMPLATE
            document = parse_skcode(form.cleaned_data['content'],
                                    allow_tagvalue_attr=form.cleaned_data['allow_tagvalue_attr'],
                                    allow_self_closing_tags=form.cleaned_data['allow_self_closing_tags'],
                                    mark_unclosed_tags_as_erroneous=form.cleaned_data['mark_unclosed_tags'],
                                    newline_node_cls=newline_node_cls)
            document_has_errors = document.has_errors()

            # Handle smileys and cosmetics
            if form.cleaned_data['replace_cosmetics']:
                setup_cosmetics_replacement(document)
            if form.cleaned_data['replace_smileys']:

                def _base_url(filename):
                    return static('images/smileys/' + filename)
                setup_smileys_replacement(document, _base_url)

            # Handle relative urls
            if form.cleaned_data['convert_relative_url_to_absolute']:
                current_site = get_current_site(request)
                setup_relative_urls_conversion(document, 'http://%s/' % current_site.domain)

            # Get requested render mode
            rendering_mode = form.cleaned_data['rendering_mode']

            # Apply paragraph utilities
            if form.cleaned_data['make_paragraphs']:
                make_paragraphs(document)

            # Apply footnotes utilities
            if form.cleaned_data['render_footnotes_html']:

                # Extract all footnotes
                footnotes = extract_footnotes(document)

                # Render all footnotes
                if rendering_mode == RENDERING_MODE_HTML:
                    footnotes_content_html = render_footnotes_html(footnotes,
                                                                   html_error_template=html_error_template)
                elif rendering_mode == RENDERING_MODE_TEXT:
                    footnotes_content_text = render_footnotes_text(footnotes)

            # Apply titles utilities (part 1 of 2)
            if form.cleaned_data['make_auto_title_ids']:
                make_auto_title_ids(document)

            # Apply titles utilities (part 2 of 2)
            if form.cleaned_data['extract_titles']:

                # Extract all titles
                titles = extract_titles(document)

                # Turn the titles list into a hierarchy
                titles_hierarchy = list(make_titles_hierarchy(titles))

                # Render the output
                if rendering_mode == RENDERING_MODE_HTML:
                    summary_content_html = render_titles_hierarchy_html(titles_hierarchy)
                elif rendering_mode == RENDERING_MODE_TEXT:
                    summary_content_text = render_titles_hierarchy_text(titles_hierarchy)

            # Render the document
            if rendering_mode == RENDERING_MODE_HTML:
                output_content_html = render_to_html(document, html_error_template=html_error_template)
            elif rendering_mode == RENDERING_MODE_TEXT:
                output_content_text = render_to_text(document)

    else:
        form = test_input_form()

    print(output_content_html)

    # Render the template
    context = {
        'form': form,
        'document_has_errors': document_has_errors,
        'output_content_html': output_content_html,
        'output_content_text': output_content_text,
        'summary_content_html': summary_content_html,
        'summary_content_text': summary_content_text,
        'footnotes_content_html': footnotes_content_html,
        'footnotes_content_text': footnotes_content_text,
        'title': _('Home page'),
    }
    if extra_context is not None:
        context.update(extra_context)

    return TemplateResponse(request, template_name, context)
Ejemplo n.º 40
0
    def test_nesting_limit(self):
        """ Test the tree builder handle the ``max_nesting_depth`` limit """
        document_tree = parse_skcode('[test][test][test][test]',
                                     recognized_tags=(DummyTreeNode, ),
                                     max_nesting_depth=5)
        self.assertIsInstance(document_tree, RootTreeNode)
        self.assertEqual(1, len(document_tree.children))
        child_node = document_tree.children[0]
        self.assertEqual('test', child_node.name)
        self.assertEqual(1, len(child_node.children))
        self.assertEqual('', child_node.error_message)
        child_node = child_node.children[0]
        self.assertEqual('test', child_node.name)
        self.assertEqual(1, len(child_node.children))
        self.assertEqual('', child_node.error_message)
        child_node = child_node.children[0]
        self.assertEqual('test', child_node.name)
        self.assertEqual(1, len(child_node.children))
        self.assertEqual('', child_node.error_message)
        child_node = child_node.children[0]
        self.assertEqual('test', child_node.name)
        self.assertEqual(0, len(child_node.children))
        self.assertEqual('', child_node.error_message)

        document_tree = parse_skcode('[test][test][test][test]',
                                     recognized_tags=(DummyTreeNode, ),
                                     max_nesting_depth=4)
        self.assertIsInstance(document_tree, RootTreeNode)
        self.assertEqual(1, len(document_tree.children))
        child_node = document_tree.children[0]
        self.assertEqual('test', child_node.name)
        self.assertEqual(1, len(child_node.children))
        self.assertEqual('', child_node.error_message)
        child_node = child_node.children[0]
        self.assertEqual('test', child_node.name)
        self.assertEqual(1, len(child_node.children))
        self.assertEqual('', child_node.error_message)
        child_node = child_node.children[0]
        self.assertEqual('test', child_node.name)
        self.assertEqual(1, len(child_node.children))
        self.assertEqual('', child_node.error_message)
        child_node = child_node.children[0]
        self.assertEqual('test', child_node.name)
        self.assertEqual(0, len(child_node.children))
        self.assertEqual('', child_node.error_message)

        document_tree = parse_skcode('[test][test][test][test]',
                                     recognized_tags=(DummyTreeNode, ),
                                     max_nesting_depth=3)
        self.assertIsInstance(document_tree, RootTreeNode)
        self.assertEqual(1, len(document_tree.children))
        child_node = document_tree.children[0]
        self.assertEqual('test', child_node.name)
        self.assertEqual(1, len(child_node.children))
        self.assertEqual('', child_node.error_message)
        child_node = child_node.children[0]
        self.assertEqual('test', child_node.name)
        self.assertEqual(1, len(child_node.children))
        self.assertEqual('', child_node.error_message)
        child_node = child_node.children[0]
        self.assertEqual('test', child_node.name)
        self.assertEqual(1, len(child_node.children))
        self.assertEqual('', child_node.error_message)
        child_node = child_node.children[0]
        self.assertIsInstance(child_node, TextTreeNode)
        self.assertEqual('[test]', child_node.source_open_tag)
        self.assertEqual('', child_node.source_close_tag)
        self.assertEqual('Nesting depth limit reached',
                         child_node.error_message)
Ejemplo n.º 41
0
    def test_nesting_limit(self):
        """ Test the tree builder handle the ``max_nesting_depth`` limit """
        document_tree = parse_skcode('[test][test][test][test]',
                                     recognized_tags=(DummyTreeNode, ),
                                     max_nesting_depth=5)
        self.assertIsInstance(document_tree, RootTreeNode)
        self.assertEqual(1, len(document_tree.children))
        child_node = document_tree.children[0]
        self.assertEqual('test', child_node.name)
        self.assertEqual(1, len(child_node.children))
        self.assertEqual('', child_node.error_message)
        child_node = child_node.children[0]
        self.assertEqual('test', child_node.name)
        self.assertEqual(1, len(child_node.children))
        self.assertEqual('', child_node.error_message)
        child_node = child_node.children[0]
        self.assertEqual('test', child_node.name)
        self.assertEqual(1, len(child_node.children))
        self.assertEqual('', child_node.error_message)
        child_node = child_node.children[0]
        self.assertEqual('test', child_node.name)
        self.assertEqual(0, len(child_node.children))
        self.assertEqual('', child_node.error_message)

        document_tree = parse_skcode('[test][test][test][test]',
                                     recognized_tags=(DummyTreeNode,),
                                     max_nesting_depth=4)
        self.assertIsInstance(document_tree, RootTreeNode)
        self.assertEqual(1, len(document_tree.children))
        child_node = document_tree.children[0]
        self.assertEqual('test', child_node.name)
        self.assertEqual(1, len(child_node.children))
        self.assertEqual('', child_node.error_message)
        child_node = child_node.children[0]
        self.assertEqual('test', child_node.name)
        self.assertEqual(1, len(child_node.children))
        self.assertEqual('', child_node.error_message)
        child_node = child_node.children[0]
        self.assertEqual('test', child_node.name)
        self.assertEqual(1, len(child_node.children))
        self.assertEqual('', child_node.error_message)
        child_node = child_node.children[0]
        self.assertEqual('test', child_node.name)
        self.assertEqual(0, len(child_node.children))
        self.assertEqual('', child_node.error_message)

        document_tree = parse_skcode('[test][test][test][test]',
                                     recognized_tags=(DummyTreeNode,),
                                     max_nesting_depth=3)
        self.assertIsInstance(document_tree, RootTreeNode)
        self.assertEqual(1, len(document_tree.children))
        child_node = document_tree.children[0]
        self.assertEqual('test', child_node.name)
        self.assertEqual(1, len(child_node.children))
        self.assertEqual('', child_node.error_message)
        child_node = child_node.children[0]
        self.assertEqual('test', child_node.name)
        self.assertEqual(1, len(child_node.children))
        self.assertEqual('', child_node.error_message)
        child_node = child_node.children[0]
        self.assertEqual('test', child_node.name)
        self.assertEqual(1, len(child_node.children))
        self.assertEqual('', child_node.error_message)
        child_node = child_node.children[0]
        self.assertIsInstance(child_node, TextTreeNode)
        self.assertEqual('[test]', child_node.source_open_tag)
        self.assertEqual('', child_node.source_close_tag)
        self.assertEqual('Nesting depth limit reached', child_node.error_message)