]>
git.cameronkatri.com Git - cgit.git/blob - filters/syntax-highlighting.py
3 # This script uses Pygments and Python3. You must have both installed
9 # It may be used with the source-filter or repo.source-filter settings
12 # The following environment variables can be used to retrieve the
13 # configuration of the repository for which this script is called:
14 # CGIT_REPO_URL ( = repo.url setting )
15 # CGIT_REPO_NAME ( = repo.name setting )
16 # CGIT_REPO_PATH ( = repo.path setting )
17 # CGIT_REPO_OWNER ( = repo.owner setting )
18 # CGIT_REPO_DEFBRANCH ( = repo.defbranch setting )
19 # CGIT_REPO_SECTION ( = section setting )
20 # CGIT_REPO_CLONE_URL ( = repo.clone-url setting )
25 from pygments
import highlight
26 from pygments
.util
import ClassNotFound
27 from pygments
.lexers
import TextLexer
28 from pygments
.lexers
import guess_lexer
29 from pygments
.lexers
import guess_lexer_for_filename
30 from pygments
.formatters
import HtmlFormatter
33 sys
.stdin
= io
.TextIOWrapper(sys
.stdin
.buffer, encoding
='utf-8', errors
='replace')
34 sys
.stdout
= io
.TextIOWrapper(sys
.stdout
.buffer, encoding
='utf-8', errors
='replace')
35 data
= sys
.stdin
.read()
36 filename
= sys
.argv
[1]
37 formatter
= HtmlFormatter(style
='pastie', nobackground
=True)
40 lexer
= guess_lexer_for_filename(filename
, data
)
42 # check if there is any shebang
44 lexer
= guess_lexer(data
)
51 # printout pygments' css definitions as well
52 sys
.stdout
.write('<style>')
53 sys
.stdout
.write(formatter
.get_style_defs('.highlight'))
54 sys
.stdout
.write('</style>')
55 sys
.stdout
.write(highlight(data
, lexer
, formatter
, outfile
=None))