11#!/usr/bin/env python
22"""Make our data into HTML!
3- These reports should be usable as email attachments, so either inline or cdn *everything*.
3+ These reports should be usable as email attachments, offline.
4+ This means we need to embed *all* our assets.
5+
6+ TODO: #11 optimize the filesize
47"""
58from __future__ import print_function
9+ from __future__ import unicode_literals
610
711# Copyright 2012 Buck Golemon <buck@yelp.com>
812#
3539from copy import deepcopy
3640from itertools import islice
3741
42+
43+ def open (filename , mode = 'r' ):
44+ """All files are treated as UTF-8, unless explicitly binary."""
45+ from io import open
46+ if 'b' in mode :
47+ return open (filename , mode )
48+ else :
49+ return open (filename , mode , encoding = 'UTF-8' )
50+
51+
3852class HtmlPage (object ):
3953 """Represent one html page."""
4054 def __init__ (self , codefile , data ):
@@ -57,31 +71,24 @@ def head(self):
5771 }),
5872 E .TITLE ('%s -- GCC Python Plugin' % self .data ['filename' ]),
5973 )
60- head .append (E .LINK (
61- rel = 'stylesheet' ,
62- href = 'http://cdnjs.cloudflare.com/ajax/libs/meyer-reset/2.0/reset.min.css' ,
63- type = 'text/css'
64- ))
6574 head .extend (
6675 E .STYLE (
6776 file_contents (css + '.css' ),
6877 media = 'screen' ,
6978 type = 'text/css'
7079 )
71- for css in ('pygments_c' , 'style' )
80+ for css in ('extlib/reset-20110126' , ' pygments_c' , 'style' )
7281 )
7382 head .extend (
74- E .SCRIPT (src = js )
75- for js in (
76- 'http://cdnjs.cloudflare.com/ajax/libs/prefixfree/1.0.7/prefixfree.min.js' ,
77- 'http://cdnjs.cloudflare.com/ajax/libs/jquery/1.7.2/jquery.min.js' ,
78- )
79- )
80- head .append (
8183 E .SCRIPT (
82- file_contents ('script .js' ),
84+ file_contents (js + ' .js' ),
8385 type = 'text/javascript' ,
8486 )
87+ for js in (
88+ 'extlib/prefixfree-1.0.4.min' ,
89+ 'extlib/jquery-1.7.1.min' ,
90+ 'script'
91+ )
8592 )
8693 return head
8794
@@ -256,7 +263,8 @@ def body(self):
256263 )
257264
258265def data_uri (mimetype , filename ):
259- data = open (join (HERE , filename )).read ().encode ('base64' ).replace ('\n ' , '' )
266+ "represent a file as a data uri"
267+ data = open (join (HERE , filename ), 'rb' ).read ().encode ('base64' ).replace ('\n ' , '' )
260268 return 'data:%s;base64,%s' % (mimetype , data )
261269
262270def file_contents (filename ):
0 commit comments