Beispiel #1
0
def test_it_supports_nested_unordered_lists():
    assert html_to_text("<ul>"
                        "<li>boop<ul><li>oof</li></ul></li>"
                        "<li>bap</li>"
                        "</ul>") == ("* boop\n\n"
                                     "- oof\n\n"
                                     "* bap")
Beispiel #2
0
def react_render_email(
    site_type: str,
    locale: str,
    url: str,
    user: Optional[JustfixUser] = None,
    session: Optional[Dict[str, Any]] = None,
    locale_prefix_url: bool = True,
    is_html_email: bool = False,
) -> Email:
    """
    Renders an email in the front-end, using the given locale,
    and returns it.
    """

    lr = react_render(
        site_type,
        locale,
        url,
        ContentType.HTML if is_html_email else ContentType.PLAINTEXT,
        user=user,
        session=session,
        locale_prefix_url=locale_prefix_url,
    )
    return Email(
        subject=lr.http_headers["X-JustFix-Email-Subject"],
        body=html_to_text(lr.html),
        html_body=lr.html if is_html_email else None,
    )
Beispiel #3
0
def test_it_supports_nested_mixed_lists():
    assert html_to_text("<ol>"
                        "<li>boop<ul><li>oof</li></ul></li>"
                        "<li>bap</li>"
                        "</ol>") == ("1. boop\n\n"
                                     "* oof\n\n"
                                     "2. bap")
Beispiel #4
0
def test_it_supports_nested_ordered_lists():
    assert html_to_text(
        "<ol>"
        '<li>boop<ol type="a"><li>hi</li><li>bye</li></ol></li>'
        "<li>bap</li>"
        "</ol>") == ("1. boop\n\n"
                     "a. hi\n\n"
                     "b. bye\n\n"
                     "2. bap")
Beispiel #5
0
def render_lambda_static_content(lr: LambdaResponse):
    ctype = lr.http_headers.get("Content-Type")
    if ctype is None:
        res = HttpResponse(lr.html, status=lr.status)
    elif ctype == "application/pdf":
        from loc.views import pdf_response

        res = pdf_response(lr.html)
    elif ctype == "text/plain; charset=utf-8":
        from project.util.html_to_text import html_to_text

        res = HttpResponse(html_to_text(lr.html).encode("utf-8"), status=lr.status)
    else:
        raise ValueError(f"Invalid Content-Type from lambda response: {ctype}")

    for key, value in lr.http_headers.items():
        res[key] = value
    return res
Beispiel #6
0
def test_it_ignores_style_tags():
    assert html_to_text("<style>html { color: pink; }</style>") == ""
Beispiel #7
0
def test_it_shows_hrefs_only():
    assert (html_to_text(
        '<p>visit <a href="https://boop.com" '
        "data-jf-show-href-only-in-plaintext>boop.com</a>.</p>") ==
            "visit https://boop.com.")
Beispiel #8
0
def test_it_supports_ordered_lists():
    assert html_to_text("<ol><li>boop</li><li>bap</li></ol>") == ("1. boop\n\n"
                                                                  "2. bap")
Beispiel #9
0
def test_it_replaces_non_decorative_images_with_urls():
    assert html_to_text('<img src="blah.jpg" alt="Blah" />') == "blah.jpg"
Beispiel #10
0
def test_it_supports_lists_with_blocks():
    assert html_to_text(
        "<ul><li><p>boop</p><p>hi</p></li><li>bap</li></ul>") == ("* boop\n\n"
                                                                  "hi\n\n"
                                                                  "* bap")
Beispiel #11
0
def test_it_ignores_decorative_images():
    assert html_to_text('<img src="blah.jpg" alt="" />') == ""
Beispiel #12
0
def test_it_ignores_useless_hrefs(href, text):
    assert html_to_text(f'<p><a href="{href}">{text}</a></p>') == text
Beispiel #13
0
def test_it_ignores_anchors_without_hrefs():
    assert html_to_text("<p><a>visit it</a></p>") == ("visit it")
Beispiel #14
0
def test_it_works():
    assert html_to_text("<p>paragraph one</p><p>paragraph two</p>") == (
        "paragraph one\n\n"
        "paragraph two")
Beispiel #15
0
def test_it_raises_value_error_on_unsupported_type():
    with pytest.raises(ValueError, match='Unknown <ol> type "z"'):
        html_to_text('<ol type="z"></ol>')
Beispiel #16
0
def test_it_adds_anchor_hrefs():
    assert html_to_text('<p><a href="https://boop">visit it</a></p>') == (
        "visit it: https://boop")
Beispiel #17
0
def test_it_ignores_empty_blocks():
    assert html_to_text("<p>bop</p><p></p><p>bop</p>") == ("bop\n\nbop")
Beispiel #18
0
def test_it_supports_br():
    assert html_to_text("<p>paragraph<br/>one</p>") == ("paragraph\none")
Beispiel #19
0
def test_it_ignores_class_name():
    assert html_to_text('<p class="blah">a</p>') == "a"
Beispiel #20
0
def test_it_embellishes_headings(html, text):
    assert html_to_text(html) == text
Beispiel #21
0
def test_it_ignores_title_tags():
    assert html_to_text("<title>boop</title>") == ""
Beispiel #22
0
def test_it_does_not_currently_support_roman_numerals():
    with pytest.raises(NotImplementedError,
                       match="Roman numerals in <ol> are unsupported"):
        html_to_text('<ol type="i"></ol>')