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

Add CLI option to exclude strings from encoding #171

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
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