about summary refs log tree commit diff
path: root/filters/syntax-highlighting.py
blob: bcf32c82af2309302324a93c87291a98c90b484e (plain) (blame)
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
#!/usr/bin/env python2

# This script uses Pygments and Python2. You must have both installed
# for this to work.
#
# http://pygments.org/
# http://python.org/
#
# It may be used with the source-filter or repo.source-filter settings
# in cgitrc.
#
# The following environment variables can be used to retrieve the
# configuration of the repository for which this script is called:
# CGIT_REPO_URL        ( = repo.url       setting )
# CGIT_REPO_NAME       ( = repo.name      setting )
# CGIT_REPO_PATH       ( = repo.path      setting )
# CGIT_REPO_OWNER      ( = repo.owner     setting )
# CGIT_REPO_DEFBRANCH  ( = repo.defbranch setting )
# CGIT_REPO_SECTION    ( = section        setting )
# CGIT_REPO_CLONE_URL  ( = repo.clone-url setting )


import sys
from pygments import highlight
from pygments.util import ClassNotFound
from pygments.lexers import TextLexer
from pygments.lexers import guess_lexer
from pygments.lexers import guess_lexer_for_filename
from pygments.formatters import HtmlFormatter


# read stdin and decode to utf-8. ignore any unkown signs.
data = sys.stdin.read().decode(encoding='utf-8', errors='ignore')
filename = sys.argv[1]
formatter = HtmlFormatter(encoding='utf-8', style='pastie')

try:
	lexer = guess_lexer_for_filename(filename, data, encoding='utf-8')
except ClassNotFound:
	# check if there is any shebang
	if data[0:2] == '#!':
		lexer = guess_lexer(data, encoding='utf-8')
	else:
		lexer = TextLexer(encoding='utf-8')
except TypeError:
	lexer = TextLexer(encoding='utf-8')

# highlight! :-)
# printout pygments' css definitions as well
sys.stdout.write('<style>')
sys.stdout.write(formatter.get_style_defs('.highlight'))
sys.stdout.write('</style>')
highlight(data, lexer, formatter, outfile=sys.stdout)
b82261c2fc51227&follow=1'>Add nvim configurationJune McEnroe 2016-02-04More iTerm configuration tweaksJune McEnroe 2016-02-03Update to iTerm 2.9 betaJune McEnroe 2016-01-31Spawn non-login shells in iTermJune McEnroe 2016-01-28Remove linux gvim aliasJune McEnroe 2016-01-28Rewrite zshrcJune McEnroe 2016-01-28Strip out useless commentsJune McEnroe 2016-01-28Trim tmux configurationJune McEnroe 2016-01-28Remove psql format wrappedJune McEnroe 2016-01-25Wow, that is so annoying, gitJune McEnroe 2016-01-25Clean up .gitconfigJune McEnroe 2016-01-15Remove gitignoreJune McEnroe 2016-01-15Eliminate escape key delay in zshJune McEnroe 2016-01-15Move pretty git log format to .gitconfigJune McEnroe 2016-01-14Remove unused git aliasesJune McEnroe 2016-01-06Hash SSH known hostsJune McEnroe 2015-12-30Ignore directory .swp filesJune McEnroe 2015-12-16Color prompt yellow in vi normal modeJune McEnroe 2015-11-23Remove colorcolumn at 120June McEnroe 2015-11-23Always git rebase --autosquashJune McEnroe 2015-11-18Add inverted shift to KarabinerJune McEnroe 2015-11-06Clean up git configsJune McEnroe 2015-11-06Add prune scriptJune McEnroe 2015-11-03Update vendored Gruvbox colorschemeJune McEnroe 2015-11-02Redefine _newline_precmd in _newline_precmdJune McEnroe 2015-11-02Print newline before every prompt after firstJune McEnroe 2015-11-02Remove first prompt placementJune McEnroe 2015-11-02Newline before prompt and start at bottom of terminalJune McEnroe 2015-10-27Add chruby to zshrcJune McEnroe