Beispiel #1
0
def text_to_html(value,
                 *,
                 capitalize_first=False,
                 format_links=False,
                 preserve_line_breaks=False,
                 open_links_in_new_tab=False,
                 **kwargs):
    """Convert a string to a HTML string, optionally modifying it first.

    :param bool capitalize_first: If True, the first letter of any text will be capitalized
    :param bool format_links: If True any HTTP URLs in any text will be turned into HTML <a> elements
    :param bool preserve_line_breaks: If True HTTP newline sequences (\\r\\n) will be turned into HTML <br> elements
    :param bool open_links_in_new_tab: If True formatted HTTP URL <a> elements will open in a new tab
    """
    if capitalize_first is True:
        value = filters.capitalize_first(value)

    if format_links is True:
        value = filters.format_links(value, open_links_in_new_tab)

    if preserve_line_breaks is True:
        # replace_newlines_with_breaks escapes its input anyway
        # so wrapping with Markup here is fine.
        value = Markup(filters.replace_newlines_with_breaks(value))

    return escape(value)
 def test_multiple_urls(self):
     text = 'This is the first link http://www.example.com and this is the second http://secondexample.com.'  # noqa
     formatted_text = 'This is the first link <a href="http://www.example.com" class="govuk-link" '\
         'rel="external">http://www.example.com</a> and this is the second '\
         '<a href="http://secondexample.com" class="govuk-link" rel="external">' \
         'http://secondexample.com</a>.'
     assert format_links(text) == formatted_text
Beispiel #3
0
 def test_format_link_does_not_die_horribly(self):
     text = 'This is the URL that made a previous regex die horribly' \
            'https://something&lt;span&gt;what&lt;/span&gt;something.com'
     formatted_text = 'This is the URL that made a previous regex die horribly' \
                      '<a href="https://something&amp;lt;span&amp;gt;what&amp;lt;/span&amp;gt;something.com" ' \
                      'class="app-break-link" rel="external">https://something&amp;lt;span&amp;gt;what&amp;lt;/span'\
                      '&amp;gt;something.com</a>'
     assert format_links(text) == formatted_text
def test_format_link_handles_markup_objects_with_protocol():
    text = Markup(
        '<td class="summary-item-field">\n\n<span>Hurray - http://www.example.com is great</span></td>'
    )
    formatted_text = Markup(
        '<td class="summary-item-field">\n\n<span>Hurray - <a href="http://www.example.com" class="break-link" rel="external">http://www.example.com</a> is great</span></td>'
    )  # noqa
    assert format_links(text) == formatted_text
def test_format_link_handles_markup_objects_without_protocol():
    text = Markup(
        '<td class="summary-item-field">\n\n<span>Hurray - www.example.com is great</span></td>'
    )
    formatted_text = Markup(
        '<td class="summary-item-field">\n\n<span>Hurray - <span class="break-link">www.example.com</span> is great</span></td>'
    )  # noqa
    assert format_links(text) == formatted_text
def test_format_link_does_not_die_horribly():
    text = 'This is the URL that made a previous regex die horribly' \
           'https://something&lt;span&gt;what&lt;/span&gt;something.com'
    formatted_text = 'This is the URL that made a previous regex die horribly' \
                     '<a href="https://something&amp;lt;span&amp;gt;what&amp;lt;/span&amp;gt;something.com" ' \
                     'class="break-link" rel="external">https://something&amp;lt;span&amp;gt;what&amp;lt;/span'\
                     '&amp;gt;something.com</a>'
    assert format_links(text) == formatted_text
 def test_format_link_without_protocol(self):
     link = 'www.example.com'
     formatted_link = 'www.example.com'
     assert format_links(link) == formatted_link
Beispiel #8
0
 def test_format_link_and_text_escapes_extra_html(self):
     text = 'This is the <strong>link</strong>: http://www.example.com'
     formatted_text = 'This is the &lt;strong&gt;link&lt;/strong&gt;: <a href="http://www.example.com" class="app-break-link" rel="external">http://www.example.com</a>'  # noqa
     assert format_links(text) == formatted_text
Beispiel #9
0
 def test_format_link_with_text(self):
     text = 'This is the Greek Γ Δ Ε Ζ Η Θ Ι Κ Λ link: http://www.exΔmple.com'
     formatted_text = 'This is the Greek Γ Δ Ε Ζ Η Θ Ι Κ Λ link: <a href="http://www.exΔmple.com" class="app-break-link" rel="external">http://www.exΔmple.com</a>'  # noqa
     assert format_links(text) == formatted_text
Beispiel #10
0
 def test_format_link_without_protocol(self):
     link = 'www.example.com'
     formatted_link = '<span class="app-break-link">www.example.com</span>'
     assert format_links(link) == formatted_link
Beispiel #11
0
 def test_format_link(self):
     link = 'http://www.example.com'
     formatted_link = '<a href="http://www.example.com" class="app-break-link" rel="external">http://www.example.com</a>' # noqa
     assert format_links(link) == formatted_link
 def test_handles_url_in_brackets(self):
     text = "(http://www.example.com)"
     formatted_text = '(<a href="http://www.example.com" class="govuk-link" '\
         'rel="external">http://www.example.com</a>)'
     assert format_links(text) == formatted_text
def test_format_link_without_protocol():
    link = 'www.example.com'
    formatted_link = '<span class="break-link">www.example.com</span>'
    assert format_links(link) == formatted_link
Beispiel #14
0
 def test_format_links_open_links_in_new_tab(self):
     link = 'http://www.example.com'
     link_new_tab = '<a href="http://www.example.com" class="app-break-link" rel="external noreferrer noopener" target="_blank">http://www.example.com</a>'  # noqa
     assert format_links(link, open_links_in_new_tab=True) == link_new_tab
 def test_handles_url_in_angle_brackets(self):
     text = "<http://www.example.com>"
     formatted_text = '&lt;<a href="http://www.example.com" class="govuk-link" '\
         'rel="external">http://www.example.com</a>&gt;'
     assert format_links(text) == formatted_text
def test_format_link_with_text():
    text = 'This is the Greek Γ Δ Ε Ζ Η Θ Ι Κ Λ link: http://www.exΔmple.com'
    formatted_text = 'This is the Greek Γ Δ Ε Ζ Η Θ Ι Κ Λ link: <a href="http://www.exΔmple.com" class="break-link" rel="external">http://www.exΔmple.com</a>'  # noqa
    assert format_links(text) == formatted_text
def test_no_links_no_change():
    text = 'There are no Greek Γ Δ Ε Ζ Η Θ Ι Κ Λ links.'
    assert format_links(text) == text
def test_multiple_urls():
    text = 'This is the first link http://www.example.com and this is the second http://secondexample.com.'  # noqa
    formatted_text = 'This is the first link <a href="http://www.example.com" class="break-link" '\
        'rel="external">http://www.example.com</a> and this is the second '\
        '<a href="http://secondexample.com" class="break-link" rel="external">http://secondexample.com</a>.'
    assert format_links(text) == formatted_text
def test_format_link_and_text_escapes_extra_html():
    text = 'This is the <strong>link</strong>: http://www.example.com'
    formatted_text = 'This is the &lt;strong&gt;link&lt;/strong&gt;: <a href="http://www.example.com" class="break-link" rel="external">http://www.example.com</a>'  # noqa
    assert format_links(text) == formatted_text
Beispiel #20
0
 def test_no_links_no_change(self):
     text = 'There are no Greek Γ Δ Ε Ζ Η Θ Ι Κ Λ links.'
     assert format_links(text) == text
def test_format_link():
    link = 'http://www.example.com'
    formatted_link = '<a href="http://www.example.com" class="break-link" rel="external">http://www.example.com</a>'
    assert format_links(link) == formatted_link