Skip to content
Open
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
24 changes: 23 additions & 1 deletion premailer/__main__.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
from __future__ import absolute_import, unicode_literals

import random
import string
import sys
import argparse

Expand Down Expand Up @@ -112,6 +115,11 @@ def main(args):
action="store_true",
help="Pretty-print the outputted HTML.",
)

parser.add_argument(
"--preserve", action="append",
help="Token to preserve from xml/html encoding."
)

options = parser.parse_args(args)

Expand All @@ -123,6 +131,13 @@ def main(args):
html = options.infile.read()
if hasattr(html, 'decode'): # Forgive me: Python 2 compatability
html = html.decode('utf-8')

replacements = {}
if options.preserve:
for token in options.preserve:
replacement = ''.join(random.choice(string.ascii_letters) for _ in range(32))
replacements[replacement] = token
html = html.replace(token, replacement)

p = Premailer(
html=html,
Expand All @@ -140,7 +155,14 @@ def main(args):
disable_basic_attributes=options.disable_basic_attributes,
disable_validation=options.disable_validation
)
options.outfile.write(p.transform(pretty_print=options.pretty))

inlined = p.transform(pretty_print=options.pretty)

if options.preserve:
for replacement, token in replacements.items():
inlined = inlined.replace(replacement, token)

options.outfile.write(inlined)
return 0


Expand Down