Skip to content

Commit 4d90fb0

Browse files
authored
Add CLI option to exclude strings from encoding
Intended as an illustration of a solution to issues #72 and #130 (and also #93 et al.) I assume this actually wants pushing into `transform` somewhere.
1 parent cfe687b commit 4d90fb0

File tree

1 file changed

+23
-1
lines changed

1 file changed

+23
-1
lines changed

premailer/__main__.py

Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,7 @@
11
from __future__ import absolute_import, unicode_literals
2+
3+
import random
4+
import string
25
import sys
36
import argparse
47

@@ -112,6 +115,11 @@ def main(args):
112115
action="store_true",
113116
help="Pretty-print the outputted HTML.",
114117
)
118+
119+
parser.add_argument(
120+
"--preserve", action="append",
121+
help="Token to preserve from xml/html encoding."
122+
)
115123

116124
options = parser.parse_args(args)
117125

@@ -123,6 +131,13 @@ def main(args):
123131
html = options.infile.read()
124132
if hasattr(html, 'decode'): # Forgive me: Python 2 compatability
125133
html = html.decode('utf-8')
134+
135+
replacements = {}
136+
if options.preserve:
137+
for token in options.preserve:
138+
replacement = ''.join(random.choice(string.ascii_letters) for _ in range(32))
139+
replacements[replacement] = token
140+
html = html.replace(token, replacement)
126141

127142
p = Premailer(
128143
html=html,
@@ -140,7 +155,14 @@ def main(args):
140155
disable_basic_attributes=options.disable_basic_attributes,
141156
disable_validation=options.disable_validation
142157
)
143-
options.outfile.write(p.transform(pretty_print=options.pretty))
158+
159+
inlined = p.transform(pretty_print=options.pretty)
160+
161+
if options.preserve:
162+
for replacement, token in replacements.items():
163+
inlined = inlined.replace(replacement, token)
164+
165+
options.outfile.write(inlined)
144166
return 0
145167

146168

0 commit comments

Comments
 (0)