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