diff options
author | Vanya Sergeev <vsergeev@gmail.com> | 2013-10-13 15:30:25 +0200 |
---|---|---|
committer | Alberto Bertogli <albertito@blitiri.com.ar> | 2013-11-02 20:07:59 +0100 |
commit | 21522f8a3a4a010ad717f5bdcbbc6584d9ba7527 (patch) | |
tree | fd2327266ab4fb524bb03b5ea933a621367c7f3e /utils.py | |
parent | f62ca211ebf8badd17fef5d15d61eb8ff00875c1 (diff) | |
download | git-arr-fork-21522f8a3a4a010ad717f5bdcbbc6584d9ba7527.zip |
Add embed data URI image blob support
Diffstat (limited to 'utils.py')
-rw-r--r-- | utils.py | 28 |
1 files changed, 28 insertions, 0 deletions
@@ -17,6 +17,8 @@ try: except ImportError: markdown = None +import base64 + def shorten(s, width = 60): if len(s) < 60: return s @@ -52,6 +54,15 @@ def can_markdown(fname): return fname.endswith(".md") +def can_embed_image(fname): + """True if we can embed image file in HTML, False otherwise.""" + + exts = [ 'jpg', 'jpeg', 'png', 'gif', 'svg' ] + if '.' in fname and fname.split('.')[-1] in exts: + return True + + return False + def colorize_diff(s): lexer = lexers.DiffLexer(encoding = 'utf-8') formatter = HtmlFormatter(encoding = 'utf-8', @@ -82,3 +93,20 @@ def colorize_blob(fname, s): def markdown_blob(s): return markdown.markdown(s) +def embed_image_blob(repo, dirname, fname): + ext_to_mimetype = {'jpg': 'image/jpeg', + 'jpeg': 'image/jpeg', + 'png': 'image/png', + 'gif': 'image/gif', + 'svg': 'image/svg+xml',} + + mimetype = ext_to_mimetype[fname.split('.')[-1]] + + # 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)) + |