##// END OF EJS Templates
Merge pull request #4868 from juliantaylor/static-path-fixes...
Min RK -
r14752:d5868c13 merge
parent child Browse files
Show More
@@ -1,57 +1,60 b''
1 #-----------------------------------------------------------------------------
1 #-----------------------------------------------------------------------------
2 # Copyright (C) 2010-2011 The IPython Development Team.
2 # Copyright (C) 2010-2011 The IPython Development Team.
3 #
3 #
4 # Distributed under the terms of the BSD License.
4 # Distributed under the terms of the BSD License.
5 #
5 #
6 # The full license is in the file COPYING.txt, distributed with this software.
6 # The full license is in the file COPYING.txt, distributed with this software.
7 #-----------------------------------------------------------------------------
7 #-----------------------------------------------------------------------------
8 import os
8 import os
9
9
10 import nose.tools as nt
10 import nose.tools as nt
11
11
12 from IPython.core import display
12 from IPython.core import display
13 from IPython.utils import path as ipath
13 from IPython.utils import path as ipath
14
14
15 def test_image_size():
15 def test_image_size():
16 """Simple test for display.Image(args, width=x,height=y)"""
16 """Simple test for display.Image(args, width=x,height=y)"""
17 thisurl = 'http://www.google.fr/images/srpr/logo3w.png'
17 thisurl = 'http://www.google.fr/images/srpr/logo3w.png'
18 img = display.Image(url=thisurl, width=200, height=200)
18 img = display.Image(url=thisurl, width=200, height=200)
19 nt.assert_equal(u'<img src="%s" width="200" height="200"/>' % (thisurl), img._repr_html_())
19 nt.assert_equal(u'<img src="%s" width="200" height="200"/>' % (thisurl), img._repr_html_())
20 img = display.Image(url=thisurl, width=200)
20 img = display.Image(url=thisurl, width=200)
21 nt.assert_equal(u'<img src="%s" width="200"/>' % (thisurl), img._repr_html_())
21 nt.assert_equal(u'<img src="%s" width="200"/>' % (thisurl), img._repr_html_())
22 img = display.Image(url=thisurl)
22 img = display.Image(url=thisurl)
23 nt.assert_equal(u'<img src="%s"/>' % (thisurl), img._repr_html_())
23 nt.assert_equal(u'<img src="%s"/>' % (thisurl), img._repr_html_())
24
24
25 def test_retina_png():
25 def test_retina_png():
26 here = os.path.dirname(__file__)
26 here = os.path.dirname(__file__)
27 img = display.Image(os.path.join(here, "2x2.png"), retina=True)
27 img = display.Image(os.path.join(here, "2x2.png"), retina=True)
28 nt.assert_equal(img.height, 1)
28 nt.assert_equal(img.height, 1)
29 nt.assert_equal(img.width, 1)
29 nt.assert_equal(img.width, 1)
30 data, md = img._repr_png_()
30 data, md = img._repr_png_()
31 nt.assert_equal(md['width'], 1)
31 nt.assert_equal(md['width'], 1)
32 nt.assert_equal(md['height'], 1)
32 nt.assert_equal(md['height'], 1)
33
33
34 def test_retina_jpeg():
34 def test_retina_jpeg():
35 here = os.path.dirname(__file__)
35 here = os.path.dirname(__file__)
36 img = display.Image(os.path.join(here, "2x2.jpg"), retina=True)
36 img = display.Image(os.path.join(here, "2x2.jpg"), retina=True)
37 nt.assert_equal(img.height, 1)
37 nt.assert_equal(img.height, 1)
38 nt.assert_equal(img.width, 1)
38 nt.assert_equal(img.width, 1)
39 data, md = img._repr_jpeg_()
39 data, md = img._repr_jpeg_()
40 nt.assert_equal(md['width'], 1)
40 nt.assert_equal(md['width'], 1)
41 nt.assert_equal(md['height'], 1)
41 nt.assert_equal(md['height'], 1)
42
42
43 def test_image_filename_defaults():
43 def test_image_filename_defaults():
44 '''test format constraint, and validity of jpeg and png'''
44 '''test format constraint, and validity of jpeg and png'''
45 tpath = ipath.get_ipython_package_dir()
45 tpath = ipath.get_ipython_package_dir()
46 nt.assert_raises(ValueError, display.Image, filename=os.path.join(tpath, 'testing/tests/badformat.gif'),
46 nt.assert_raises(ValueError, display.Image, filename=os.path.join(tpath, 'testing/tests/badformat.gif'),
47 embed=True)
47 embed=True)
48 nt.assert_raises(ValueError, display.Image)
48 nt.assert_raises(ValueError, display.Image)
49 nt.assert_raises(ValueError, display.Image, data='this is not an image', format='badformat', embed=True)
49 nt.assert_raises(ValueError, display.Image, data='this is not an image', format='badformat', embed=True)
50 from IPython.html import DEFAULT_STATIC_FILES_PATH
50 from IPython.html import DEFAULT_STATIC_FILES_PATH
51 imgfile = os.path.join(DEFAULT_STATIC_FILES_PATH, 'base/images/ipynblogo.png')
51 # check boths paths to allow packages to test at build and install time
52 imgfile = os.path.join(tpath, 'html/static/base/images/ipynblogo.png')
53 if not os.path.exists(imgfile):
54 imgfile = os.path.join(DEFAULT_STATIC_FILES_PATH, 'base/images/ipynblogo.png')
52 img = display.Image(filename=imgfile)
55 img = display.Image(filename=imgfile)
53 nt.assert_equal('png', img.format)
56 nt.assert_equal('png', img.format)
54 nt.assert_is_not_none(img._repr_png_())
57 nt.assert_is_not_none(img._repr_png_())
55 img = display.Image(filename=os.path.join(tpath, 'testing/tests/logo.jpg'), embed=False)
58 img = display.Image(filename=os.path.join(tpath, 'testing/tests/logo.jpg'), embed=False)
56 nt.assert_equal('jpeg', img.format)
59 nt.assert_equal('jpeg', img.format)
57 nt.assert_is_none(img._repr_jpeg_())
60 nt.assert_is_none(img._repr_jpeg_())
@@ -1,105 +1,106 b''
1 """Module that pre-processes the notebook for export to HTML.
1 """Module that pre-processes the notebook for export to HTML.
2 """
2 """
3 #-----------------------------------------------------------------------------
3 #-----------------------------------------------------------------------------
4 # Copyright (c) 2013, the IPython Development Team.
4 # Copyright (c) 2013, the IPython Development Team.
5 #
5 #
6 # Distributed under the terms of the Modified BSD License.
6 # Distributed under the terms of the Modified BSD License.
7 #
7 #
8 # The full license is in the file COPYING.txt, distributed with this software.
8 # The full license is in the file COPYING.txt, distributed with this software.
9 #-----------------------------------------------------------------------------
9 #-----------------------------------------------------------------------------
10
10
11 #-----------------------------------------------------------------------------
11 #-----------------------------------------------------------------------------
12 # Imports
12 # Imports
13 #-----------------------------------------------------------------------------
13 #-----------------------------------------------------------------------------
14
14
15 import os
15 import os
16 import io
16 import io
17
17
18 from IPython.utils import path
18 from IPython.utils import path
19
19
20 from .base import Preprocessor
20 from .base import Preprocessor
21
21
22 from IPython.utils.traitlets import Unicode
22 from IPython.utils.traitlets import Unicode
23
23
24 #-----------------------------------------------------------------------------
24 #-----------------------------------------------------------------------------
25 # Classes and functions
25 # Classes and functions
26 #-----------------------------------------------------------------------------
26 #-----------------------------------------------------------------------------
27
27
28 class CSSHTMLHeaderPreprocessor(Preprocessor):
28 class CSSHTMLHeaderPreprocessor(Preprocessor):
29 """
29 """
30 Preprocessor used to pre-process notebook for HTML output. Adds IPython notebook
30 Preprocessor used to pre-process notebook for HTML output. Adds IPython notebook
31 front-end CSS and Pygments CSS to HTML output.
31 front-end CSS and Pygments CSS to HTML output.
32 """
32 """
33
33
34 header = []
34 header = []
35
35
36 highlight_class = Unicode('.highlight', config=True,
36 highlight_class = Unicode('.highlight', config=True,
37 help="CSS highlight class identifier")
37 help="CSS highlight class identifier")
38
38
39 def __init__(self, config=None, **kw):
39 def __init__(self, config=None, **kw):
40 """
40 """
41 Public constructor
41 Public constructor
42
42
43 Parameters
43 Parameters
44 ----------
44 ----------
45 config : Config
45 config : Config
46 Configuration file structure
46 Configuration file structure
47 **kw : misc
47 **kw : misc
48 Additional arguments
48 Additional arguments
49 """
49 """
50
50
51 super(CSSHTMLHeaderPreprocessor, self).__init__(config=config, **kw)
51 super(CSSHTMLHeaderPreprocessor, self).__init__(config=config, **kw)
52
52
53 if self.enabled :
53 if self.enabled :
54 self._regen_header()
54 self._regen_header()
55
55
56
56
57 def preprocess(self, nb, resources):
57 def preprocess(self, nb, resources):
58 """Fetch and add CSS to the resource dictionary
58 """Fetch and add CSS to the resource dictionary
59
59
60 Fetch CSS from IPython and Pygments to add at the beginning
60 Fetch CSS from IPython and Pygments to add at the beginning
61 of the html files. Add this css in resources in the
61 of the html files. Add this css in resources in the
62 "inlining.css" key
62 "inlining.css" key
63
63
64 Parameters
64 Parameters
65 ----------
65 ----------
66 nb : NotebookNode
66 nb : NotebookNode
67 Notebook being converted
67 Notebook being converted
68 resources : dictionary
68 resources : dictionary
69 Additional resources used in the conversion process. Allows
69 Additional resources used in the conversion process. Allows
70 preprocessors to pass variables into the Jinja engine.
70 preprocessors to pass variables into the Jinja engine.
71 """
71 """
72
72
73 resources['inlining'] = {}
73 resources['inlining'] = {}
74 resources['inlining']['css'] = self.header
74 resources['inlining']['css'] = self.header
75
75
76 return nb, resources
76 return nb, resources
77
77
78
78
79 def _regen_header(self):
79 def _regen_header(self):
80 """
80 """
81 Fills self.header with lines of CSS extracted from IPython
81 Fills self.header with lines of CSS extracted from IPython
82 and Pygments.
82 and Pygments.
83 """
83 """
84 from pygments.formatters import HtmlFormatter
84 from pygments.formatters import HtmlFormatter
85
85
86 #Clear existing header.
86 #Clear existing header.
87 header = []
87 header = []
88
88
89 #Construct path to IPy CSS
89 #Construct path to IPy CSS
90 sheet_filename = os.path.join(path.get_ipython_package_dir(),
90 from IPython.html import DEFAULT_STATIC_FILES_PATH
91 'html', 'static', 'style', 'style.min.css')
91 sheet_filename = os.path.join(DEFAULT_STATIC_FILES_PATH,
92 'style', 'style.min.css')
92
93
93 #Load style CSS file.
94 #Load style CSS file.
94 with io.open(sheet_filename, encoding='utf-8') as file:
95 with io.open(sheet_filename, encoding='utf-8') as file:
95 file_text = file.read()
96 file_text = file.read()
96 header.append(file_text)
97 header.append(file_text)
97
98
98 #Add pygments CSS
99 #Add pygments CSS
99 formatter = HtmlFormatter()
100 formatter = HtmlFormatter()
100 pygments_css = formatter.get_style_defs(self.highlight_class)
101 pygments_css = formatter.get_style_defs(self.highlight_class)
101 header.append(pygments_css)
102 header.append(pygments_css)
102
103
103 #Set header
104 #Set header
104 self.header = header
105 self.header = header
105
106
General Comments 0
You need to be logged in to leave comments. Login now