rennat / pynliner

Python CSS-to-inline-styles conversion tool for HTML using BeautifulSoup and cssutils
http://pythonhosted.org/pynliner/
180 stars 93 forks source link

Python 3 #33

Closed dcramer closed 8 years ago

dcramer commented 10 years ago

Let's get the convo started :)

It looks like for starters we'd need to upgrade to BeautifulSoup4 (the package is actually named differently on pypi it seems): https://pypi.python.org/pypi/beautifulsoup4/4.3.2

dcramer commented 10 years ago

https://github.com/dcramer/pynliner/compare/python3

Will send a PR when tests pass, assuming I actually figure it out

dcramer commented 10 years ago

There's some complex selectors which aren't supported by bs4's select. I'm going to adjust the tests for now, let me know what you want to do (it's definitely a regression).

i.e. div > :last-child doesnt work in bs4

IMO it's a minor thing, but I dont know what people's use cases are

sandrotosi commented 9 years ago

what's the status of the porting to py3k?

rennat commented 8 years ago

done!

utapyngo commented 7 years ago
  File "...python3.5/site-packages/pynliner/__init__.py", line 222, in _get_external_styles
    self.style_string += self._get_url(url)
TypeError: Can't convert 'bytes' object to str implicitly

self._get_url() returns bytes, and self.style_string is str:

    def _get_external_styles(self):
        """Gets <link> element styles
        """
        if not self.style_string:
            self.style_string = u''
        else:
            self.style_string += u'\n'

        link_tags = self.soup.findAll('link', {'rel': 'stylesheet'})
        for tag in link_tags:
            url = tag['href']

            # Convert the relative URL to an absolute URL ready to pass to urllib
            base_url = self.relative_url or self.root_url
            url = urljoin(base_url, url)

            self.style_string += self._get_url(url)
            tag.extract()

This test passes because _get_url() is mocked and returns str:

    def test_fromURL(self):
        """Test 'fromURL' constructor"""
        url = 'http://media.tannern.com/pynliner/test.html'
        p = Pynliner()
        with mock.patch.object(Pynliner, '_get_url') as mocked:
            mocked.return_value = u"""<?xml version='1.0' encoding='utf-8'?>
    <html xmlns="http://www.w3.org/1999/xhtml">
    <head>
    <title>test</title>
    <link rel="stylesheet" type="text/css" href="test.css"/>
    <style type="text/css">h1 {color: #fc0;}</style>
    </head>
    <body>
    <h1>Hello World!</h1>
    <p>:)</p>
    </body>
    </html>"""
            p.from_url(url)
        self.assertEqual(p.root_url, 'http://media.tannern.com')
        self.assertEqual(p.relative_url, 'http://media.tannern.com/pynliner/')