import pytest


def test_img(formatter):
    formatter.feed('<img src="foo.jpg" alt="foobar" />')

    assert formatter.markdown == "(image: foo.jpg alt: foobar)"

    formatter._reset()
    formatter.feed('<img src="foo.jpg">')

    assert formatter.markdown == "(image: foo.jpg)"


def test_linked_image(formatter):
    formatter.feed('<a href="http://liip.com"><img src="liiplogo.jpg"></a>')

    exp = "(image: liiplogo.jpg link: http://liip.com)"

    assert formatter.markdown == exp


@pytest.mark.parametrize("html,md", [
    ('h1', '#'), ('h2', '##'), ('h3', '###'),
    ('h4', '####'), ('h5', '#####'), ('h6', '######'),
])
def test_headings(formatter, html, md):
    formatter.feed('<{tag}>Heading uno</{tag}>'.format(tag=html))

    assert formatter.markdown == "{} Heading uno\n\n".format(md)


def test_strong(formatter):
    formatter.feed("<b>lala</b>")
    assert formatter.markdown == "**lala** "

    formatter._reset()
    formatter.feed("<strong>lala</strong>")
    assert formatter.markdown == "**lala** "

    formatter._reset()
    formatter.feed("<strong>lala</strong>something coming right after")
    assert formatter.markdown == "**lala** something coming right after"


@pytest.mark.xfail(reason="Problem with _ followed by text")
def test_italic(formatter):
    formatter.feed("<i>lala</i>")
    assert formatter.markdown == "_lala_"

    formatter._reset()
    formatter.feed("<em>lala</em>")
    assert formatter.markdown == "_lala_"

    formatter._reset()
    formatter.feed("<em>lala</em>something coming right after")
    assert formatter.markdown == "_lala_ something coming right after"


def test_paragraph(formatter):
    formatter.feed("<p>some paragraph</p>")

    assert formatter.markdown == "\n\nsome paragraph\n\n"


def test_link(formatter):
    formatter.feed("""<a href="http://www.google.ch" title="Alternative">
    Google</a>""")

    res = "(link: http://www.google.ch title: Alternative text: Google)"
    assert formatter.markdown == res

    formatter._reset()
    formatter.feed("""<a href="http://www.google.ch">Google</a>""")

    res = "(link: http://www.google.ch text: Google)"
    assert formatter.markdown == res


def test_bold_link(formatter):
    formatter.feed("""<a href="foo"><b>bar</b></a>""")

    assert formatter.markdown == "(link: foo text: **bar**)"

    formatter._reset()
    formatter.feed("""<b><a href="foo">bar</a></b>""")

    assert formatter.markdown == "** (link: foo text: bar)** "


def test_list(formatter):
    formatter.feed("""
        <ul>
            <li>First item</li>
            <li>Second item</li>
        </ul>
    """)

    exp = "* First item\n* Second item"
    assert exp == formatter.markdown.strip()


def test_nested_lists(formatter):
    formatter.feed("""
        <ul>
            <li>First item
                <ul>
                    <li>Child item</li>
                    <li>Second child item</li>
                </ul>
            </li>
            <li>Second item</li>
        </ul>
    """)

    exp = """* First item
    * Child item
    * Second child item
* Second item"""

    assert exp == formatter.markdown.strip()


def test_complicated_lists(formatter):
    formatter.feed("""
        <ul>
            <li>First <b>Item bold</b></li>
            <li>Second item has a <a href="">Link</a></li>
        </ul>""")

    exp = """* First **Item bold**
* Second item has a (link:  text: Link)"""

    assert exp == formatter.markdown.strip()


def test_ordered_list(formatter):
    formatter.feed("""
        <ol>
            <li>First item</li>
            <li>Second item</li>
        </ol>
    """)

    exp = "1. First item\n1. Second item"
    assert exp == formatter.markdown.strip()


def test_blocks(formatter):
    """TODO: improve this test with the newlines
    """
    formatter.feed("""
        <pre>
            from winterfell import jon

            assert jon.knows == "nothing"
        </pre>
    """)

    exp = """


```
            from winterfell import jon

            assert jon.knows == "nothing"
```

"""

    assert exp.strip() == formatter.markdown.strip()


def test_code(formatter):
    formatter.feed("""<code>git blame</code>""")

    exp = "`git blame`"

    assert exp == formatter.markdown


def test_quotes(formatter):
    formatter.feed("""
    <blockquote>
        Welcome. Welcome to City 17.
        You have chosen or been chosen to relocate into one of our
        finest remaining urban centres
    </blockquote>
    """)

    exp = """> Welcome. Welcome to City 17.
> You have chosen or been chosen to relocate into one of our
> finest remaining urban centres

"""

    assert exp == formatter.markdown


def test_keep_strike(formatter):
    code = """<strike>fu</strike>"""
    formatter.feed(code)

    assert code == formatter.markdown


def test_unescape(formatter):
    formatter.feed("GottaGo &#8211; iPhone bring me home")

    exp = "GottaGo – iPhone bring me home"
    assert exp == formatter.markdown

@@ -4952,28 +4952,396 @@
 t exp == formatter.markdown%0A
+%0A%0Adef test_apostrophe(formatter):%0A    text = %22I'm %60proud' to present one of the first swiss-made native iPhone applications, called %60%3Cstrong%3EGottaGo%3C/strong%3E'%22  # noqa: E501%0A%0A    formatter.feed(text)%0A%0A%0A    exp = %22I'm 'proud' to present one of the first swiss-made native iPhone applications, called ' **GottaGo** '%22  # noqa: E501%0A%0A    assert exp == formatter.markdown%0A

fixup! Handle the special case of apostrophes