summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--CHANGES4
-rwxr-xr-xpygmentize3
-rw-r--r--pygments/cmdline.py28
3 files changed, 28 insertions, 7 deletions
diff --git a/CHANGES b/CHANGES
index 208b957a..5e330f89 100644
--- a/CHANGES
+++ b/CHANGES
@@ -81,6 +81,10 @@ Version 2.0
- Added "inencoding" option for lexers to override "encoding" analogous
to "outencoding" (#800).
+- Added line-by-line "streaming" mode for pygmentize with the "-s" option.
+ (PR#165) Only fully works for lexers that have no constructs spanning
+ lines!
+
- Updated the Makefile lexer to yield a little more useful highlighting.
- Lexer aliases passed to ``get_lexer_by_name()`` are now case-insensitive.
diff --git a/pygmentize b/pygmentize
index 8b3b2067..aea38727 100755
--- a/pygmentize
+++ b/pygmentize
@@ -1,6 +1,7 @@
#!/usr/bin/env python2
-import sys, pygments.cmdline
+import sys
+import pygments.cmdline
try:
sys.exit(pygments.cmdline.main(sys.argv))
except KeyboardInterrupt:
diff --git a/pygments/cmdline.py b/pygments/cmdline.py
index c77f0a32..907c51f0 100644
--- a/pygments/cmdline.py
+++ b/pygments/cmdline.py
@@ -30,7 +30,7 @@ from pygments.styles import get_all_styles, get_style_by_name
USAGE = """\
Usage: %s [-l <lexer> | -g] [-F <filter>[:<options>]] [-f <formatter>]
- [-O <options>] [-P <option=value>] [-o <outfile>] [<infile>]
+ [-O <options>] [-P <option=value>] [-s] [-o <outfile>] [<infile>]
%s -S <style> -f <formatter> [-a <arg>] [-O <options>] [-P <option=value>]
%s -L [<which> ...]
@@ -42,6 +42,10 @@ Highlight the input file and write the result to <outfile>.
If no input file is given, use stdin, if -o is not given, use stdout.
+If -s is passed, lexing will be done in "streaming" mode, reading and
+highlighting one line at a time. This will only work properly with
+lexers that have no constructs spanning multiple lines!
+
<lexer> is a lexer name (query all lexer names with -L). If -l is not
given, the lexer is guessed from the extension of the input file name
(this obviously doesn't work if the input is stdin). If -g is passed,
@@ -380,10 +384,10 @@ def main(args=sys.argv):
print(usage, file=sys.stderr)
return 2
- #if '-s' in opts:
- #print >>sys.stderr, 'Error: -s option not usable when input ' + \
- # 'file specified'
- #return 1
+ if '-s' in opts:
+ print('Error: -s option not usable when input file specified',
+ file=sys.stderr)
+ return 1
infn = args[0]
try:
@@ -457,13 +461,25 @@ def main(args=sys.argv):
# process whole input as per normal...
highlight(code, lexer, fmter, outfile)
else:
+ if not lexer:
+ print('Error: when using -s a lexer has to be selected with -l',
+ file=sys.stderr)
+ return 1
# line by line processing of stdin (eg: for 'tail -f')...
try:
while 1:
- line = sys.stdin.readline()
+ if sys.version_info > (3,):
+ # Python 3: we have to use .buffer to get a binary stream
+ line = sys.stdin.buffer.readline()
+ else:
+ line = sys.stdin.readline()
if not line:
break
+ if not inencoding:
+ line = guess_decode_from_terminal(line, sys.stdin)[0]
highlight(line, lexer, fmter, outfile)
+ if hasattr(outfile, 'flush'):
+ outfile.flush()
except KeyboardInterrupt:
return 0