Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Test #30

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open

Test #30

Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions README.rst
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
13 changes: 9 additions & 4 deletions inlinestyler/converter.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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')

Expand All @@ -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)
Expand Down
4 changes: 2 additions & 2 deletions inlinestyler/utils.py
Original file line number Diff line number Diff line change
@@ -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

Expand All @@ -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