diff options
| -rwxr-xr-x | filters/syntax-highlighting.py | 19 |
1 files changed, 9 insertions, 10 deletions
diff --git a/filters/syntax-highlighting.py b/filters/syntax-highlighting.py index bcf32c8..b5d615e 100755 --- a/filters/syntax-highlighting.py +++ b/filters/syntax-highlighting.py | |||
| @@ -1,6 +1,6 @@ | |||
| 1 | #!/usr/bin/env python2 | 1 | #!/usr/bin/env python3 |
| 2 | 2 | ||
| 3 | # This script uses Pygments and Python2. You must have both installed | 3 | # This script uses Pygments and Python3. You must have both installed |
| 4 | # for this to work. | 4 | # for this to work. |
| 5 | # | 5 | # |
| 6 | # http://pygments.org/ | 6 | # http://pygments.org/ |
| @@ -29,25 +29,24 @@ from pygments.lexers import guess_lexer_for_filename | |||
| 29 | from pygments.formatters import HtmlFormatter | 29 | from pygments.formatters import HtmlFormatter |
| 30 | 30 | ||
| 31 | 31 | ||
| 32 | # read stdin and decode to utf-8. ignore any unkown signs. | 32 | data = sys.stdin.read() |
| 33 | data = sys.stdin.read().decode(encoding='utf-8', errors='ignore') | ||
| 34 | filename = sys.argv[1] | 33 | filename = sys.argv[1] |
| 35 | formatter = HtmlFormatter(encoding='utf-8', style='pastie') | 34 | formatter = HtmlFormatter(style='pastie') |
| 36 | 35 | ||
| 37 | try: | 36 | try: |
| 38 | lexer = guess_lexer_for_filename(filename, data, encoding='utf-8') | 37 | lexer = guess_lexer_for_filename(filename, data) |
| 39 | except ClassNotFound: | 38 | except ClassNotFound: |
| 40 | # check if there is any shebang | 39 | # check if there is any shebang |
| 41 | if data[0:2] == '#!': | 40 | if data[0:2] == '#!': |
| 42 | lexer = guess_lexer(data, encoding='utf-8') | 41 | lexer = guess_lexer(data) |
| 43 | else: | 42 | else: |
| 44 | lexer = TextLexer(encoding='utf-8') | 43 | lexer = TextLexer() |
| 45 | except TypeError: | 44 | except TypeError: |
| 46 | lexer = TextLexer(encoding='utf-8') | 45 | lexer = TextLexer() |
| 47 | 46 | ||
| 48 | # highlight! :-) | 47 | # highlight! :-) |
| 49 | # printout pygments' css definitions as well | 48 | # printout pygments' css definitions as well |
| 50 | sys.stdout.write('<style>') | 49 | sys.stdout.write('<style>') |
| 51 | sys.stdout.write(formatter.get_style_defs('.highlight')) | 50 | sys.stdout.write(formatter.get_style_defs('.highlight')) |
| 52 | sys.stdout.write('</style>') | 51 | sys.stdout.write('</style>') |
| 53 | highlight(data, lexer, formatter, outfile=sys.stdout) | 52 | sys.stdout.write(highlight(data, lexer, formatter, outfile=None)) |
