diff --git a/README.rst b/README.rst index e5d5bfc..36ab204 100644 --- a/README.rst +++ b/README.rst @@ -9,6 +9,8 @@ inlinestyler - Making Styled HTML Email Easy `inlinestyler` is an easy way to locally inline CSS into an HTML email message. +test + Styling HTML email is a `black art`_. CSS works, but only when it's been placed inline on the individual elements (and event then, not always) - which makes development frustrating, and iteration slow. diff --git a/inlinestyler/converter.py b/inlinestyler/converter.py index 80895a6..6676afc 100644 --- a/inlinestyler/converter.py +++ b/inlinestyler/converter.py @@ -22,7 +22,7 @@ def __init__(self): self.supportPercentage = 100 self.convertedHTML = "" - def perform(self, document, sourceHTML, sourceURL, encoding='unicode'): + def perform(self, document, sourceHTML, sourceURL, encoding='unicode', remove_origin=True): aggregate_css = "" # Retrieve CSS rel links from html pasted and aggregate into one string @@ -36,10 +36,13 @@ def perform(self, document, sourceHTML, sourceURL, encoding='unicode'): parsed_url = urlparse.urlparse(sourceURL) csspath = urlparse.urljoin(parsed_url.scheme + "://" + parsed_url.hostname, csspath) - css_content = requests.get(csspath).text + # Get css file. Don't verify SSL certificates. It's just a css. + # Cloudfront does not have correct SSL certificate and it fails. + css_content = requests.get(csspath, verify=False).text aggregate_css += ''.join(css_content) - element.getparent().remove(element) + if remove_origin: + element.getparent().remove(element) except: raise IOError('The stylesheet ' + element.get("href") + ' could not be found') @@ -48,7 +51,9 @@ def perform(self, document, sourceHTML, sourceURL, encoding='unicode'): matching = CSSStyleSelector.evaluate(document) for element in matching: aggregate_css += element.text - element.getparent().remove(element) + + if remove_origin: + element.getparent().remove(element) # Convert document to a style dictionary compatible with etree styledict = self.get_view(document, aggregate_css) diff --git a/inlinestyler/utils.py b/inlinestyler/utils.py index 4259de8..e7a871a 100644 --- a/inlinestyler/utils.py +++ b/inlinestyler/utils.py @@ -1,7 +1,7 @@ from lxml import etree from inlinestyler.converter import Conversion -def inline_css(html_message, encoding='unicode'): +def inline_css(html_message, encoding='unicode', remove_origin=True): """ Inlines all CSS in an HTML string @@ -14,5 +14,5 @@ def inline_css(html_message, encoding='unicode'): document = etree.HTML(html_message) converter = Conversion() - converter.perform(document, html_message, '', encoding=encoding) + converter.perform(document, html_message, '', encoding=encoding, remove_origin=remove_origin) return converter.convertedHTML