#!/usr/bin/env python
"""Pynliner : Convert CSS to inline styles
Python CSS-to-inline-styles conversion tool for HTML using BeautifulSoup and cssutils
Copyright (c) 2011 Tanner Netterville
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
The generated output of this software shall not be used in a mass marketing service.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
"""
__version__ = "0.4.0"
import urllib2
import cssutils
from BeautifulSoup import BeautifulSoup
from soupselect import select
class Pynliner(object):
"""Pynliner class"""
soup = False
style_string = False
stylesheet = False
output = False
def __init__(self, log=None):
self.log = log
cssutils.log.enabled = False if log is None else True
def from_url(self, url):
"""Gets remote HTML page for conversion
Downloads HTML page from `url` as a string and passes it to the
`from_string` method. Also sets `self.root_url` and `self.relative_url`
for use in importing elements.
Returns self.
>>> p = Pynliner()
>>> p.from_url('http://somewebsite.com/file.html')
"""
self.url = url
self.relative_url = '/'.join(url.split('/')[:-1]) + '/'
self.root_url = '/'.join(url.split('/')[:3])
self.source_string = self._get_url(self.url)
return self
def from_string(self, string):
"""Generates a Pynliner object from the given HTML string.
Returns self.
>>> p = Pynliner()
>>> p.from_string('
Hello World!
')
"""
self.source_string = string
return self
def with_cssString(self, cssString):
"""Adds external CSS to the Pynliner object. Can be "chained".
Returns self.
>>> html = "
Hello World!
"
>>> css = "h1 { color:#ffcc00; }"
>>> p = Pynliner()
>>> p.from_string(html).with_cssString(css)
"""
if not self.style_string:
self.style_string = cssString + u'\n'
else:
self.style_string += cssString + u'\n'
return self
def run(self):
"""Applies each step of the process if they have not already been
performed.
Returns Unicode output with applied styles.
>>> html = "
Hello World!
"
>>> Pynliner().from_string(html).run()
u'
Hello World!
'
"""
if not self.soup:
self._get_soup()
if not self.stylesheet:
self._get_styles()
self._apply_styles()
return self._get_output()
def _get_url(self, url):
"""Returns the response content from the given url
"""
return urllib2.urlopen(url).read()
def _get_soup(self):
"""Convert source string to BeautifulSoup object. Sets it to self.soup.
If using mod_wgsi, use html5 parsing to prevent BeautifulSoup incompatibility.
"""
# Check if mod_wsgi is running - see http://code.google.com/p/modwsgi/wiki/TipsAndTricks
try:
from mod_wsgi import version
self.soup = BeautifulSoup(self.source_string, "html5lib")
except:
self.soup = BeautifulSoup(self.source_string)
def _get_styles(self):
"""Gets all CSS content from and removes all and