diff options
author | Alberto Bertogli <albertito@blitiri.com.ar> | 2012-09-16 12:17:56 +0200 |
---|---|---|
committer | Alberto Bertogli <albertito@blitiri.com.ar> | 2012-11-10 18:49:54 +0100 |
commit | 80ef0017d47f536bf2c8c6af4b514efa50071a23 (patch) | |
tree | db630a50bf30abca5a62cd206d8bc9abed61b4e0 /utils.py | |
download | git-arr-fork-0.01.zip |
Initial commit0.01
Signed-off-by: Alberto Bertogli <albertito@blitiri.com.ar>
Diffstat (limited to 'utils.py')
-rw-r--r-- | utils.py | 41 |
1 files changed, 41 insertions, 0 deletions
diff --git a/utils.py b/utils.py new file mode 100644 index 0000000..3bd281f --- /dev/null +++ b/utils.py @@ -0,0 +1,41 @@ +""" +Miscellaneous utilities. + +These are mostly used in templates, for presentation purposes. +""" + +try: + import pygments + from pygments import highlight + from pygments import lexers + from pygments.formatters import HtmlFormatter +except ImportError: + pygments = None + + +def shorten(s, width = 60): + if len(s) < 60: + return s + return s[:57] + "..." + +def has_colorizer(): + return pygments is not None + +def colorize_diff(s): + lexer = lexers.DiffLexer(encoding = 'utf-8') + formatter = HtmlFormatter(encoding = 'utf-8', + cssclass = 'source_code') + + return highlight(s, lexer, formatter) + +def colorize_blob(fname, s): + try: + lexer = lexers.guess_lexer_for_filename(fname, s) + except lexers.ClassNotFound: + lexer = lexers.TextLexer(encoding = 'utf-8') + formatter = HtmlFormatter(encoding = 'utf-8', + cssclass = 'source_code', + linenos = 'table') + + return highlight(s, lexer, formatter) + |