2012-09-16 13:17:56 +03:00
|
|
|
"""
|
|
|
|
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
|
|
|
|
|
2013-10-13 16:28:51 +03:00
|
|
|
try:
|
|
|
|
import markdown
|
|
|
|
except ImportError:
|
|
|
|
markdown = None
|
2012-09-16 13:17:56 +03:00
|
|
|
|
2013-10-13 16:30:25 +03:00
|
|
|
import base64
|
2013-11-02 21:14:36 +02:00
|
|
|
import mimetypes
|
2013-10-13 16:30:25 +03:00
|
|
|
|
2012-09-16 13:17:56 +03:00
|
|
|
def shorten(s, width = 60):
|
|
|
|
if len(s) < 60:
|
|
|
|
return s
|
|
|
|
return s[:57] + "..."
|
|
|
|
|
2012-11-14 02:48:46 +02:00
|
|
|
def can_colorize(s):
|
|
|
|
"""True if we can colorize the string, False otherwise."""
|
|
|
|
if pygments is None:
|
|
|
|
return False
|
|
|
|
|
|
|
|
# Pygments can take a huge amount of time with long files, or with very
|
|
|
|
# long lines; these are heuristics to try to avoid those situations.
|
|
|
|
if len(s) > (512 * 1024):
|
|
|
|
return False
|
|
|
|
|
|
|
|
# If any of the first 5 lines is over 300 characters long, don't colorize.
|
|
|
|
start = 0
|
|
|
|
for i in range(5):
|
|
|
|
pos = s.find('\n', start)
|
|
|
|
if pos == -1:
|
|
|
|
break
|
|
|
|
|
|
|
|
if pos - start > 300:
|
|
|
|
return False
|
|
|
|
start = pos + 1
|
|
|
|
|
|
|
|
return True
|
2012-09-16 13:17:56 +03:00
|
|
|
|
2013-11-02 23:12:50 +02:00
|
|
|
def can_markdown(repo, fname):
|
2013-10-13 16:28:51 +03:00
|
|
|
"""True if we can process file through markdown, False otherwise."""
|
|
|
|
if markdown is None:
|
|
|
|
return False
|
|
|
|
|
2013-11-02 23:12:50 +02:00
|
|
|
if not repo.info.embed_markdown:
|
|
|
|
return False
|
|
|
|
|
2013-10-13 16:28:51 +03:00
|
|
|
return fname.endswith(".md")
|
|
|
|
|
2013-11-02 23:12:50 +02:00
|
|
|
def can_embed_image(repo, fname):
|
2013-10-13 16:30:25 +03:00
|
|
|
"""True if we can embed image file in HTML, False otherwise."""
|
2013-11-02 23:12:50 +02:00
|
|
|
if not repo.info.embed_images:
|
|
|
|
return False
|
2013-10-13 16:30:25 +03:00
|
|
|
|
2013-11-02 23:12:50 +02:00
|
|
|
return (('.' in fname) and
|
|
|
|
(fname.split('.')[-1].lower() in [ 'jpg', 'jpeg', 'png', 'gif' ]))
|
2013-10-13 16:30:25 +03:00
|
|
|
|
2012-09-16 13:17:56 +03:00
|
|
|
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:
|
2012-11-18 12:39:45 +02:00
|
|
|
lexer = lexers.guess_lexer_for_filename(fname, s, encoding = 'utf-8')
|
2012-09-16 13:17:56 +03:00
|
|
|
except lexers.ClassNotFound:
|
2012-11-27 01:29:08 +02:00
|
|
|
# Only try to guess lexers if the file starts with a shebang,
|
|
|
|
# otherwise it's likely a text file and guess_lexer() is prone to
|
|
|
|
# make mistakes with those.
|
|
|
|
lexer = lexers.TextLexer(encoding = 'utf-8')
|
|
|
|
if s.startswith('#!'):
|
|
|
|
try:
|
|
|
|
lexer = lexers.guess_lexer(s[:80], encoding = 'utf-8')
|
|
|
|
except lexers.ClassNotFound:
|
|
|
|
pass
|
2012-11-18 12:39:45 +02:00
|
|
|
|
2012-09-16 13:17:56 +03:00
|
|
|
formatter = HtmlFormatter(encoding = 'utf-8',
|
|
|
|
cssclass = 'source_code',
|
2014-06-30 10:59:39 +03:00
|
|
|
linenos = 'table',
|
|
|
|
anchorlinenos = True,
|
|
|
|
lineanchors = 'line')
|
2012-09-16 13:17:56 +03:00
|
|
|
|
|
|
|
return highlight(s, lexer, formatter)
|
|
|
|
|
2013-10-13 16:28:51 +03:00
|
|
|
def markdown_blob(s):
|
|
|
|
return markdown.markdown(s)
|
|
|
|
|
2013-10-13 16:30:25 +03:00
|
|
|
def embed_image_blob(repo, dirname, fname):
|
2013-11-02 21:14:36 +02:00
|
|
|
mimetype = mimetypes.guess_type(fname)[0]
|
2013-10-13 16:30:25 +03:00
|
|
|
|
|
|
|
# Unfortunately, bottle seems to require utf-8 encoded data.
|
|
|
|
# We have to refetch the blob with raw=True, because the utf-8 encoded
|
|
|
|
# version of the blob available in the bottle template discards binary data.
|
|
|
|
raw_blob = repo.blob(dirname + fname, raw = True)
|
|
|
|
|
|
|
|
return '<img style="max-width:100%;" src="data:{0};base64,{1}" />'.format( \
|
|
|
|
mimetype, base64.b64encode(raw_blob))
|
|
|
|
|