-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathshowInvisibles.py
executable file
·301 lines (249 loc) · 9.64 KB
/
showInvisibles.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
#!/usr/bin/env python3
#
# showInvisibles: make control and whitespace chars visible.
# 2007-01-1: Written by Steven J. DeRose.
#
import sys
import os
import codecs
import argparse
from sjdUtils import sjdUtils
#import strfchr -- whassup with this???
__metadata__ = {
"title" : "showInvisibles",
"rightsHolder" : "Steven J. DeRose",
"creator" : "http://viaf.org/viaf/50334488",
"type" : "http://purl.org/dc/dcmitype/Software",
"language" : "Python 2.7.6, 3.6",
"created" : "2007-01-16",
"modified" : "2022-10-07",
"publisher" : "http://github.com/sderose",
"license" : "https://creativecommons.org/licenses/by-sa/3.0/"
}
__version__ = __metadata__['modified']
def warning(msg):
if (args.verbose): sys.stderr.write(msg+"\n")
descr = """
=Description=
showinvisible.py [options]
(Python version; also available in Perl)
Make control characters and space visible by substituting the Unicode
"control symbols" for them.
Also make non-ASCII characters visible by substituting XML numeric
character references (`ߦ` etc).
Can also colorize the changed characters.
Useful for visualizing return/linefeed, space/tab, etc. Can also be used
to escape undesired characters in a file to ease later processing (in that
case, specify `--nocolor -s`).
=Known bugs and limitations=
Options `-b` and `-u` are unfinished.
=Related commands=
`ord`, `CharDisplay`. `strfchr.py`.
In zsh you can get a similar effect
with '${(V)x}' -- though I haven't found any characters it actually fixes.
=History=
* 2007-01-16: Written by Steven J. DeRose.
* 2007-12-31 sjd: Getopt, version, etc.
* 2010-09-27 sjd: Cleanup, -base, -pad, -color, factor out makeCharRef().
* 2011-01-24 sjd: Add control pictures and alternates. binmode STDOUT.
* 2012-01-23 sjd: Fix -color and -base. Use sjdUtils.
* Optimize color-escaping instead of doing on/off for every char.
* 2012-01-23: Converted by perl2python.
* 2012-01-25 sjd: Cleanup.
* 2015-10-13: Update argparse usage. pylint.
* 2021-04-08ff: Better option handling. Drop -s/leaveSpace for --spaceAs SELF.
Hook up to new strfchr.py. Add lots of formats from there.
* 2022-10-07: Drop Python 2 remains.
=To do=
* Options for what to do with line-ends?
* Support bgcolor (nice way to show whitespace chars).
=Rights=
Copyright 2007 by Steven J. DeRose. This work is licensed under a Creative Commons
Attribution-Share Alike 3.0 Unported License. For further information on
this license, see [http://creativecommons.org/licenses/by-sa/3.0].
For the most recent version, see [http://www.derose.net/steve/utilities] or
[http://github.com/sderose].
=Options=
"""
###############################################################################
#
spaceChoices = [ "B", "U", "SP", "SELF", ]
lfChoices = [ "LF", "NL", "SELF", ]
names = [
"NUL", "SOH", "STX", "ETX", "EOT", "ENQ", "ACK", "BEL",
"BS", "HT", "NL", "VT", "FF", "CR", "SO", "SI",
"DLE", "DC1", "DC2", "DC3", "DC4", "NAK", "SYN", "ETB",
"CAN", "EM", "SUB", "ESC", "FS", "GS", "RS", "US",
"SP"]
if (names[32] != "SP"):
print("names messed up.")
exit(0)
backslashCodes = [
"\\0", "", "", "", "", "", "", "",
"", "\\t", "\\n", "\\v", "\\f", "\\r", "", "",
"", "", "", "", "", "", "", "",
"", "", "", "\\e", "", "", "", "",
"\\s"
]
assert (backslashCodes[32] == "\\s"), "BackslashCodes messed up."
assert os.environ["PYTHONIOENCODING"] == "utf_8", "PYTHONIOENCODING not utf_8."
###############################################################################
# Construct an XML numeric character reference to the given code point.
# Use the appropriate args.base.
#
def makeCharRef(n):
if (args.base == 10):
theFm = '{0:0' + str(args.width) + 'd}'
ref = "&#" + theFm.format(n) + ";"
else:
theFm = '{0:0' + str(args.width) + 'x}'
ref = "&#x" + theFm.format(n) + ";"
return ref
###############################################################################
# Called only for chars <= 32
# (could actually be called for everything, so we can catch \\, %, etc.)
#
def mapControlChar(charNum, what=""):
if (args.spaceUnchanged and chr(charNum).isspace()):
return chr(charNum)
if (charNum == 32):
if (args.spaceAs == "SELF"): return(" ")
elif (args.spaceAs == "B"): return(chr(0x2422))
elif (args.spaceAs == "U"): return(chr(0x2423))
elif (args.spaceAs == "SP"): return(chr(0x2420))
else: assert False, "Unsupported spaceAs value '%s'" % (args.spaceAs)
if (charNum == 10):
if (args.lfAs == "SELF"): return("\n")
if (args.lfAs == "LF"): return(chr(0x240A))
if (args.lfAs == "NL"): return(chr(0x2424))
else: assert False, "Unsupported lfAs value '%s'" % (args.lfAs)
if (args.pics):
return chr(0x2400 + charNum)
if (what=="ENTITY16"):
return chr(0x2400 + charNum)
if (args.name):
if (charNum > len(names)):
warning("Control U+%04x out of range for names -- check code." % (charNum))
return "*%s*" % (names[charNum])
if (args.backSlashes and backslashCodes[charNum]):
return backslashCodes[charNum]
if (args.uri):
return "%02x" % (charNum)
return strfchr.strfchr(charNum, what)
###############################################################################
#
def doOneFile(path, fh):
nControls = nHigh = 0
colorState = 0
rec = fh.readline()
while (rec):
for i in (range(0, len(rec))):
c = rec[i]
o = ord(c)
toprint = ""
if (o < 32):
nControls += 1
toprint = mapControlChar(o)
elif (o > 127):
nHigh += 1
toprint = makeCharRef(o)
if (toprint):
if (not colorState):
print(cs, end='')
colorState = 1
print(toprint, end="")
else:
if (colorState):
print(ce, end="")
colorState = 0
print(c, end="")
if (colorState):
print(ce, end="")
colorState = 0
print("")
rec = fh.readline()
if (not args.quiet): return
warning("File '%s': Control characters: %d, chars > 127: %d." %
(path, nControls, nHigh))
###############################################################################
# Main
#
oformatChoices = strfchr.__mnemonicMap__.keys()
def processOptions():
parser = argparse.ArgumentParser(description=descr)
# TODO: Combine these options into --oformat [x]
parser.add_argument(
"--backSlashes", action='store_true', default=True,
help='Use \\ hex-codes to display characters.')
parser.add_argument(
"--name", action='store_true',
help='Show names for control chars, instead of entities or symbols.')
parser.add_argument(
"--pics", "--pix", action='store_true', default=True,
help='Use Unicode control pictures (U+2400...) for control chars.')
parser.add_argument(
"--uri", action='store_true',
help='Show URI-style (%XX) escapes for invisible characters.')
parser.add_argument(
"--base", type=int, default=10, choices=[ 10, 16 ],
help='Use base 10 or 16 for XML character references.')
parser.add_argument(
"--color", action='store_true',
help='Colorize the formerely-invisible characters.')
parser.add_argument(
"--nocolor", action='store_false', dest="color",
help='Turn off colorizing.')
parser.add_argument(
"--iencoding", type=str, metavar="E", default="utf-8",
help="Assume this character coding for input. Default: utf-8.")
parser.add_argument(
"--lfAs", type=str, default="LF", choices=lfChoices,
help='Whether to show Line Feeds as SELF, LF: LF, or NL: NL symbol.')
parser.add_argument(
"--oformat", "--outputFormat", "--output-format",
type=str, default="ENTITY16", choices=oformatChoices,
help="How to write out invisible characters.")
parser.add_argument(
"--quiet", "-q", action='store_true',
help='Suppress most messages.')
parser.add_argument(
"-spaceUnchanged", action='store_true',
help='Leave whitespace as-is.')
parser.add_argument(
"--spaceAs", type=str, default="SELF", choices = spaceChoices,
help='Show spaces as: SELF, B: slashed b, U: serifed _, or SP: SP symbol.')
parser.add_argument(
"--verbose", "-v", action='count', default=0,
help='Show more messages (repeatable).')
parser.add_argument(
"--version", action='version', version=__version__,
help='Display version information, then exit.')
parser.add_argument(
"--width", "--pad", type=int, default=4,
help='How many digits (minimum) for XML numeric character references.')
parser.add_argument(
'files', nargs=argparse.REMAINDER,
help='Path(s) to input file(s).')
args0 = parser.parse_args()
return args0
args = processOptions()
warning(repr(args))
su = sjdUtils()
cs = ""
ce = ""
if (args.color):
cs = su.getColorString("red")
ce = su.getColorString("off")
if (args.verbose):
print("Turned on " + cs + "color" + ce + ".")
if (not args.files):
if (sys.stdin.isatty()):
sys.stderr.write("Waiting on STDIN...\n")
doOneFile("[stdin]", sys.stdin)
sys.exit()
for path0 in args.files:
if (not os.path.isfile(path0)):
warning("Can't find file '%s'." % (path0))
else:
with codecs.open(path0, "rb", encoding=args.iencoding) as fh0:
doOneFile(path0, fh0)