def test_setup_smileys_replacement_trailing_slash_base_url(self):
     """ Test the ``setup_smileys_replacement`` helper. """
     document_tree = RootTreeNode()
     setup_smileys_replacement(document_tree, 'http://example.com')
     self.assertEqual(
         'http://example.com/test.png',
         document_tree.attrs[EMOTICONS_BASE_URL_ATTR_NAME]('test.png'))
 def test_setup_smileys_replacement(self):
     """ Test the ``setup_smileys_replacement`` helper. """
     document_tree = RootTreeNode()
     self.assertNotIn(EMOTICONS_MAP_ATTR_NAME, document_tree.attrs)
     self.assertNotIn(EMOTICONS_REGEX_ATTR_NAME, document_tree.attrs)
     self.assertNotIn(EMOTICONS_BASE_URL_ATTR_NAME, document_tree.attrs)
     self.assertNotIn(EMOTICONS_HTML_CLASS_ATTR_NAME, document_tree.attrs)
     setup_smileys_replacement(document_tree, 'http://example.com/', (
         (':)', 'smile.png'),
         (':<test?:', 'test.png'),
     ), 'custom_css')
     self.assertIn(EMOTICONS_MAP_ATTR_NAME, document_tree.attrs)
     self.assertEqual({
         ':)': 'smile.png',
         ':&lt;test?:': 'test.png'
     }, document_tree.attrs[EMOTICONS_MAP_ATTR_NAME])
     self.assertIn(EMOTICONS_REGEX_ATTR_NAME, document_tree.attrs)
     self.assertEqual(
         r"re.compile('(^|\\s+)(?P<emoticon>\\:\\)|\\:\\&lt\\;test\\?\\:)(\\s+|$)')",
         str(document_tree.attrs[EMOTICONS_REGEX_ATTR_NAME]))
     self.assertIn(EMOTICONS_BASE_URL_ATTR_NAME, document_tree.attrs)
     self.assertEqual(
         'http://example.com/test.png',
         document_tree.attrs[EMOTICONS_BASE_URL_ATTR_NAME]('test.png'))
     self.assertIn(EMOTICONS_HTML_CLASS_ATTR_NAME, document_tree.attrs)
     self.assertEqual('custom_css',
                      document_tree.attrs[EMOTICONS_HTML_CLASS_ATTR_NAME])
Exemple #3
0
 def test_do_smileys_replacement_no_css_class(self):
     """ Test the ``do_smileys_replacement`` function. """
     root_tree_node = RootTreeNode()
     setup_smileys_replacement(root_tree_node, '/smiley/', html_class='')
     for smiley, filename in DEFAULT_EMOTICONS_MAP:
         smiley = escape_html(smiley)
         output = do_smileys_replacement(root_tree_node, 'Foo %s bar' % smiley)
         self.assertEqual('Foo <img src="/smiley/%s" alt="%s"> bar' % (filename, smiley), output)
Exemple #4
0
 def test_do_smileys_replacement_internal_error(self):
     """ Test the ``do_smileys_replacement`` function when the internal emoticons map is corrupted. """
     root_tree_node = RootTreeNode()
     setup_smileys_replacement(root_tree_node, '/smiley/', html_class='')
     root_tree_node.attrs[EMOTICONS_MAP_ATTR_NAME].pop('&lt;3')
     tree_node = root_tree_node.new_child(None, TextTreeNode, content='Test <3')
     output = tree_node.render_html('')
     self.assertEqual('Test &lt;3', output)
 def test_setup_smileys_replacement_callable_base_url(self):
     """ Test the ``setup_smileys_replacement`` helper. """
     document_tree = RootTreeNode()
     setup_smileys_replacement(document_tree,
                               lambda x: 'http://example.com/lambda/' + x)
     self.assertEqual(
         'http://example.com/lambda/test.png',
         document_tree.attrs[EMOTICONS_BASE_URL_ATTR_NAME]('test.png'))
Exemple #6
0
 def test_do_smileys_replacement_text(self):
     """ Test the ``do_smileys_replacement`` function. """
     root_tree_node = RootTreeNode()
     setup_smileys_replacement(root_tree_node, '/smiley/', html_class='')
     tree_node = root_tree_node.new_child(None, TextTreeNode, content='Test :)')
     output = tree_node.render_html('')
     self.assertEqual('Test <img src="/smiley/smile.png" alt=":)">', output)
     output = tree_node.render_text('')
     self.assertEqual('Test :)', output)
 def test_do_smileys_replacement_internal_error(self):
     """ Test the ``do_smileys_replacement`` function when the internal emoticons map is corrupted. """
     root_tree_node = RootTreeNode()
     setup_smileys_replacement(root_tree_node, '/smiley/', html_class='')
     root_tree_node.attrs[EMOTICONS_MAP_ATTR_NAME].pop('&lt;3')
     tree_node = root_tree_node.new_child(None,
                                          TextTreeNode,
                                          content='Test <3')
     output = tree_node.render_html('')
     self.assertEqual('Test &lt;3', output)
 def test_do_smileys_replacement_no_css_class(self):
     """ Test the ``do_smileys_replacement`` function. """
     root_tree_node = RootTreeNode()
     setup_smileys_replacement(root_tree_node, '/smiley/', html_class='')
     for smiley, filename in DEFAULT_EMOTICONS_MAP:
         smiley = escape_html(smiley)
         output = do_smileys_replacement(root_tree_node,
                                         'Foo %s bar' % smiley)
         self.assertEqual(
             'Foo <img src="/smiley/%s" alt="%s"> bar' % (filename, smiley),
             output)
 def test_do_smileys_replacement_text(self):
     """ Test the ``do_smileys_replacement`` function. """
     root_tree_node = RootTreeNode()
     setup_smileys_replacement(root_tree_node, '/smiley/', html_class='')
     tree_node = root_tree_node.new_child(None,
                                          TextTreeNode,
                                          content='Test :)')
     output = tree_node.render_html('')
     self.assertEqual('Test <img src="/smiley/smile.png" alt=":)">', output)
     output = tree_node.render_text('')
     self.assertEqual('Test :)', output)
Exemple #10
0
 def test_setup_smileys_replacement(self):
     """ Test the ``setup_smileys_replacement`` helper. """
     document_tree = RootTreeNode()
     self.assertNotIn(EMOTICONS_MAP_ATTR_NAME, document_tree.attrs)
     self.assertNotIn(EMOTICONS_REGEX_ATTR_NAME, document_tree.attrs)
     self.assertNotIn(EMOTICONS_BASE_URL_ATTR_NAME, document_tree.attrs)
     self.assertNotIn(EMOTICONS_HTML_CLASS_ATTR_NAME, document_tree.attrs)
     setup_smileys_replacement(document_tree, 'http://example.com/', (
         (':)', 'smile.png'),
         (':<test?:', 'test.png'),
     ), 'custom_css')
     self.assertIn(EMOTICONS_MAP_ATTR_NAME, document_tree.attrs)
     self.assertEqual({
         ':)': 'smile.png',
         ':&lt;test?:': 'test.png'
     }, document_tree.attrs[EMOTICONS_MAP_ATTR_NAME])
     self.assertIn(EMOTICONS_REGEX_ATTR_NAME, document_tree.attrs)
     self.assertEqual(r"re.compile('(^|\\s+)(?P<emoticon>\\:\\)|\\:\\&lt\\;test\\?\\:)(\\s+|$)')",
                      str(document_tree.attrs[EMOTICONS_REGEX_ATTR_NAME]))
     self.assertIn(EMOTICONS_BASE_URL_ATTR_NAME, document_tree.attrs)
     self.assertEqual('http://example.com/test.png', document_tree.attrs[EMOTICONS_BASE_URL_ATTR_NAME]('test.png'))
     self.assertIn(EMOTICONS_HTML_CLASS_ATTR_NAME, document_tree.attrs)
     self.assertEqual('custom_css', document_tree.attrs[EMOTICONS_HTML_CLASS_ATTR_NAME])
Exemple #11
0
 def test_setup_smileys_replacement_callable_base_url(self):
     """ Test the ``setup_smileys_replacement`` helper. """
     document_tree = RootTreeNode()
     setup_smileys_replacement(document_tree, lambda x: 'http://example.com/lambda/' + x)
     self.assertEqual('http://example.com/lambda/test.png',
                      document_tree.attrs[EMOTICONS_BASE_URL_ATTR_NAME]('test.png'))
Exemple #12
0
 def test_setup_smileys_replacement_trailing_slash_base_url(self):
     """ Test the ``setup_smileys_replacement`` helper. """
     document_tree = RootTreeNode()
     setup_smileys_replacement(document_tree, 'http://example.com')
     self.assertEqual('http://example.com/test.png', document_tree.attrs[EMOTICONS_BASE_URL_ATTR_NAME]('test.png'))
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)