Show More
@@ -1,75 +1,75 | |||||
1 | """Module that pre-processes the notebook for export via Reveal. |
|
1 | """Module that pre-processes the notebook for export via Reveal. | |
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 | from .base import Preprocessor |
|
15 | from .base import Preprocessor | |
16 | from IPython.utils.traitlets import Unicode |
|
16 | from IPython.utils.traitlets import Unicode | |
17 |
|
17 | |||
18 | #----------------------------------------------------------------------------- |
|
18 | #----------------------------------------------------------------------------- | |
19 | # Classes and functions |
|
19 | # Classes and functions | |
20 | #----------------------------------------------------------------------------- |
|
20 | #----------------------------------------------------------------------------- | |
21 |
|
21 | |||
22 | class RevealHelpPreprocessor(Preprocessor): |
|
22 | class RevealHelpPreprocessor(Preprocessor): | |
23 |
|
23 | |||
24 | url_prefix = Unicode('reveal.js', config=True, |
|
24 | url_prefix = Unicode('reveal.js', config=True, | |
25 | help="""The URL prefix for reveal.js. |
|
25 | help="""The URL prefix for reveal.js. | |
26 | This can be a a relative URL for a local copy of reveal.js, |
|
26 | This can be a a relative URL for a local copy of reveal.js, | |
27 | or point to a CDN. |
|
27 | or point to a CDN. | |
28 |
|
28 | |||
29 | For speaker notes to work, a local reveal.js prefix must be used. |
|
29 | For speaker notes to work, a local reveal.js prefix must be used. | |
30 | """ |
|
30 | """ | |
31 | ) |
|
31 | ) | |
32 |
|
32 | |||
33 | def preprocess(self, nb, resources): |
|
33 | def preprocess(self, nb, resources): | |
34 | """ |
|
34 | """ | |
35 | Called once to 'preprocess' contents of the notebook. |
|
35 | Called once to 'preprocess' contents of the notebook. | |
36 |
|
36 | |||
37 | Parameters |
|
37 | Parameters | |
38 | ---------- |
|
38 | ---------- | |
39 | nb : NotebookNode |
|
39 | nb : NotebookNode | |
40 | Notebook being converted |
|
40 | Notebook being converted | |
41 | resources : dictionary |
|
41 | resources : dictionary | |
42 | Additional resources used in the conversion process. Allows |
|
42 | Additional resources used in the conversion process. Allows | |
43 | preprocessors to pass variables into the Jinja engine. |
|
43 | preprocessors to pass variables into the Jinja engine. | |
44 | """ |
|
44 | """ | |
45 |
|
45 | |||
46 | for worksheet in nb.worksheets: |
|
46 | for worksheet in nb.worksheets: | |
47 | for index, cell in enumerate(worksheet.cells): |
|
47 | for index, cell in enumerate(worksheet.cells): | |
48 |
|
48 | |||
49 | #Make sure the cell has slideshow metadata. |
|
49 | #Make sure the cell has slideshow metadata. | |
50 | cell.metadata.slide_type = cell.get('metadata', {}).get('slideshow', {}).get('slide_type', '-') |
|
50 | cell.metadata.slide_type = cell.get('metadata', {}).get('slideshow', {}).get('slide_type', '-') | |
51 |
|
51 | |||
52 | #Get the slide type. If type is start of subslide or slide, |
|
52 | #Get the slide type. If type is start of subslide or slide, | |
53 | #end the last subslide/slide. |
|
53 | #end the last subslide/slide. | |
54 | if cell.metadata.slide_type in ['slide']: |
|
54 | if cell.metadata.slide_type in ['slide']: | |
55 | worksheet.cells[index - 1].metadata.slide_helper = 'slide_end' |
|
55 | worksheet.cells[index - 1].metadata.slide_helper = 'slide_end' | |
56 | if cell.metadata.slide_type in ['subslide']: |
|
56 | if cell.metadata.slide_type in ['subslide']: | |
57 | worksheet.cells[index - 1].metadata.slide_helper = 'subslide_end' |
|
57 | worksheet.cells[index - 1].metadata.slide_helper = 'subslide_end' | |
58 | #Prevent the rendering of "do nothing" cells before fragments |
|
58 | #Prevent the rendering of "do nothing" cells before fragments | |
59 | #Group fragments passing frag_number to the data-fragment-index |
|
59 | #Group fragments passing frag_number to the data-fragment-index | |
60 | if cell.metadata.slide_type in ['fragment']: |
|
60 | if cell.metadata.slide_type in ['fragment']: | |
61 | worksheet.cells[index].metadata.frag_number = index |
|
61 | worksheet.cells[index].metadata.frag_number = index | |
62 | i = 1 |
|
62 | i = 1 | |
63 | while i < len(worksheet.cells) - index: |
|
63 | while i < len(worksheet.cells) - index: | |
64 | worksheet.cells[index + i].metadata.frag_helper = 'fragment_end' |
|
64 | worksheet.cells[index + i].metadata.frag_helper = 'fragment_end' | |
65 | worksheet.cells[index + i].metadata.frag_number = index |
|
65 | worksheet.cells[index + i].metadata.frag_number = index | |
66 | i += 1 |
|
66 | i += 1 | |
67 | #Restart the slide_helper when the cell status is changed |
|
67 | #Restart the slide_helper when the cell status is changed | |
68 |
#to |
|
68 | #to other types. | |
69 | if cell.metadata.slide_type in ['-']: |
|
69 | if cell.metadata.slide_type in ['-', 'skip', 'notes', 'fragment']: | |
70 | worksheet.cells[index - 1].metadata.slide_helper = '-' |
|
70 | worksheet.cells[index - 1].metadata.slide_helper = '-' | |
71 |
|
71 | |||
72 | if not isinstance(resources['reveal'], dict): |
|
72 | if not isinstance(resources['reveal'], dict): | |
73 | resources['reveal'] = {} |
|
73 | resources['reveal'] = {} | |
74 | resources['reveal']['url_prefix'] = self.url_prefix |
|
74 | resources['reveal']['url_prefix'] = self.url_prefix | |
75 | return nb, resources |
|
75 | return nb, resources |
@@ -1,93 +1,94 | |||||
1 | """ |
|
1 | """ | |
2 | Module with tests for the revealhelp preprocessor |
|
2 | Module with tests for the revealhelp preprocessor | |
3 | """ |
|
3 | """ | |
4 |
|
4 | |||
5 | #----------------------------------------------------------------------------- |
|
5 | #----------------------------------------------------------------------------- | |
6 | # Copyright (c) 2013, the IPython Development Team. |
|
6 | # Copyright (c) 2013, the IPython Development Team. | |
7 | # |
|
7 | # | |
8 | # Distributed under the terms of the Modified BSD License. |
|
8 | # Distributed under the terms of the Modified BSD License. | |
9 | # |
|
9 | # | |
10 | # The full license is in the file COPYING.txt, distributed with this software. |
|
10 | # The full license is in the file COPYING.txt, distributed with this software. | |
11 | #----------------------------------------------------------------------------- |
|
11 | #----------------------------------------------------------------------------- | |
12 |
|
12 | |||
13 | #----------------------------------------------------------------------------- |
|
13 | #----------------------------------------------------------------------------- | |
14 | # Imports |
|
14 | # Imports | |
15 | #----------------------------------------------------------------------------- |
|
15 | #----------------------------------------------------------------------------- | |
16 |
|
16 | |||
17 | from IPython.nbformat import current as nbformat |
|
17 | from IPython.nbformat import current as nbformat | |
18 |
|
18 | |||
19 | from .base import PreprocessorTestsBase |
|
19 | from .base import PreprocessorTestsBase | |
20 | from ..revealhelp import RevealHelpPreprocessor |
|
20 | from ..revealhelp import RevealHelpPreprocessor | |
21 |
|
21 | |||
22 |
|
22 | |||
23 | #----------------------------------------------------------------------------- |
|
23 | #----------------------------------------------------------------------------- | |
24 | # Class |
|
24 | # Class | |
25 | #----------------------------------------------------------------------------- |
|
25 | #----------------------------------------------------------------------------- | |
26 |
|
26 | |||
27 | class Testrevealhelp(PreprocessorTestsBase): |
|
27 | class Testrevealhelp(PreprocessorTestsBase): | |
28 | """Contains test functions for revealhelp.py""" |
|
28 | """Contains test functions for revealhelp.py""" | |
29 |
|
29 | |||
30 | def build_notebook(self): |
|
30 | def build_notebook(self): | |
31 |
"""Build a reveal slides notebook in memory for use with tests. |
|
31 | """Build a reveal slides notebook in memory for use with tests. | |
32 | Overrides base in PreprocessorTestsBase""" |
|
32 | Overrides base in PreprocessorTestsBase""" | |
33 |
|
33 | |||
34 | outputs = [nbformat.new_output(output_type="stream", stream="stdout", output_text="a")] |
|
34 | outputs = [nbformat.new_output(output_type="stream", stream="stdout", output_text="a")] | |
35 |
|
35 | |||
36 | slide_metadata = {'slideshow' : {'slide_type': 'slide'}} |
|
36 | slide_metadata = {'slideshow' : {'slide_type': 'slide'}} | |
37 | subslide_metadata = {'slideshow' : {'slide_type': 'subslide'}} |
|
37 | subslide_metadata = {'slideshow' : {'slide_type': 'subslide'}} | |
38 |
|
38 | |||
39 | cells=[nbformat.new_code_cell(input="", prompt_number=1, outputs=outputs), |
|
39 | cells=[nbformat.new_code_cell(input="", prompt_number=1, outputs=outputs), | |
40 | nbformat.new_text_cell('markdown', source="", metadata=slide_metadata), |
|
40 | nbformat.new_text_cell('markdown', source="", metadata=slide_metadata), | |
41 | nbformat.new_code_cell(input="", prompt_number=2, outputs=outputs), |
|
41 | nbformat.new_code_cell(input="", prompt_number=2, outputs=outputs), | |
42 | nbformat.new_text_cell('markdown', source="", metadata=slide_metadata), |
|
42 | nbformat.new_text_cell('markdown', source="", metadata=slide_metadata), | |
43 | nbformat.new_text_cell('markdown', source="", metadata=subslide_metadata)] |
|
43 | nbformat.new_text_cell('markdown', source="", metadata=subslide_metadata)] | |
44 | worksheets = [nbformat.new_worksheet(name="worksheet1", cells=cells)] |
|
44 | worksheets = [nbformat.new_worksheet(name="worksheet1", cells=cells)] | |
45 |
|
45 | |||
46 | return nbformat.new_notebook(name="notebook1", worksheets=worksheets) |
|
46 | return nbformat.new_notebook(name="notebook1", worksheets=worksheets) | |
47 |
|
47 | |||
48 |
|
48 | |||
49 | def build_preprocessor(self): |
|
49 | def build_preprocessor(self): | |
50 | """Make an instance of a preprocessor""" |
|
50 | """Make an instance of a preprocessor""" | |
51 | preprocessor = RevealHelpPreprocessor() |
|
51 | preprocessor = RevealHelpPreprocessor() | |
52 | preprocessor.enabled = True |
|
52 | preprocessor.enabled = True | |
53 | return preprocessor |
|
53 | return preprocessor | |
54 |
|
54 | |||
55 |
|
55 | |||
56 | def test_constructor(self): |
|
56 | def test_constructor(self): | |
57 | """Can a RevealHelpPreprocessor be constructed?""" |
|
57 | """Can a RevealHelpPreprocessor be constructed?""" | |
58 | self.build_preprocessor() |
|
58 | self.build_preprocessor() | |
59 |
|
59 | |||
60 |
|
60 | |||
61 | def test_reveal_attribute(self): |
|
61 | def test_reveal_attribute(self): | |
62 | """Make sure the reveal url_prefix resources is set""" |
|
62 | """Make sure the reveal url_prefix resources is set""" | |
63 | nb = self.build_notebook() |
|
63 | nb = self.build_notebook() | |
64 | res = self.build_resources() |
|
64 | res = self.build_resources() | |
65 | preprocessor = self.build_preprocessor() |
|
65 | preprocessor = self.build_preprocessor() | |
66 | nb, res = preprocessor(nb, res) |
|
66 | nb, res = preprocessor(nb, res) | |
67 | assert 'reveal' in res |
|
67 | assert 'reveal' in res | |
68 | assert 'url_prefix' in res['reveal'] |
|
68 | assert 'url_prefix' in res['reveal'] | |
69 |
|
69 | |||
70 |
|
70 | |||
71 | def test_reveal_output(self): |
|
71 | def test_reveal_output(self): | |
72 | """Make sure that the reveal preprocessor """ |
|
72 | """Make sure that the reveal preprocessor """ | |
73 | nb = self.build_notebook() |
|
73 | nb = self.build_notebook() | |
74 | res = self.build_resources() |
|
74 | res = self.build_resources() | |
75 | preprocessor = self.build_preprocessor() |
|
75 | preprocessor = self.build_preprocessor() | |
76 | nb, res = preprocessor(nb, res) |
|
76 | nb, res = preprocessor(nb, res) | |
77 | cells = nb.worksheets[0].cells |
|
77 | cells = nb.worksheets[0].cells | |
78 |
|
78 | |||
79 | # Make sure correct metadata tags are available on every cell. |
|
79 | # Make sure correct metadata tags are available on every cell. | |
80 | for cell in cells: |
|
80 | for cell in cells: | |
81 | assert 'slide_type' in cell.metadata |
|
81 | assert 'slide_type' in cell.metadata | |
82 |
|
82 | |||
83 | # Make sure slide end is only applied to the cells preceeding slide |
|
83 | # Make sure slide end is only applied to the cells preceeding slide | |
84 | # cells. |
|
84 | # cells. | |
85 |
assert 'slide_helper' |
|
85 | assert 'slide_helper' in cells[1].metadata | |
|
86 | self.assertEqual(cells[1].metadata['slide_helper'], '-') | |||
86 |
|
87 | |||
87 | # Verify 'slide-end' |
|
88 | # Verify 'slide-end' | |
88 | assert 'slide_helper' in cells[0].metadata |
|
89 | assert 'slide_helper' in cells[0].metadata | |
89 | self.assertEqual(cells[0].metadata['slide_helper'], 'slide_end') |
|
90 | self.assertEqual(cells[0].metadata['slide_helper'], 'slide_end') | |
90 | assert 'slide_helper' in cells[2].metadata |
|
91 | assert 'slide_helper' in cells[2].metadata | |
91 | self.assertEqual(cells[2].metadata['slide_helper'], 'slide_end') |
|
92 | self.assertEqual(cells[2].metadata['slide_helper'], 'slide_end') | |
92 | assert 'slide_helper' in cells[3].metadata |
|
93 | assert 'slide_helper' in cells[3].metadata | |
93 | self.assertEqual(cells[3].metadata['slide_helper'], 'subslide_end') |
|
94 | self.assertEqual(cells[3].metadata['slide_helper'], 'subslide_end') |
General Comments 0
You need to be logged in to leave comments.
Login now