Show More
@@ -1,4 +1,5 b'' | |||||
1 | from converters.markdown import ConverterMarkdown |
|
1 | from converters.markdown import ConverterMarkdown | |
|
2 | from IPython.utils.text import indent | |||
2 | import io |
|
3 | import io | |
3 | import os |
|
4 | import os | |
4 | import itertools |
|
5 | import itertools | |
@@ -7,11 +8,6 b' class ConverterReveal(ConverterMarkdown):' | |||||
7 | """Convert a notebook to a html slideshow. |
|
8 | """Convert a notebook to a html slideshow. | |
8 |
|
9 | |||
9 | It generates a static html slideshow based in markdown and reveal.js. |
|
10 | It generates a static html slideshow based in markdown and reveal.js. | |
10 | You have four ways to delimit the the slides: |
|
|||
11 | ##--- delimit horizontal slides |
|
|||
12 | ##<<< open vertical slides |
|
|||
13 | ##>>> close vertical slides |
|
|||
14 | ##>>><<< close vertical slides and open new vertical slides. |
|
|||
15 | """ |
|
11 | """ | |
16 |
|
12 | |||
17 | def __init__(self, infile, highlight_source=False, show_prompts=True, |
|
13 | def __init__(self, infile, highlight_source=False, show_prompts=True, | |
@@ -21,6 +17,30 b' class ConverterReveal(ConverterMarkdown):' | |||||
21 | self.show_prompts = show_prompts |
|
17 | self.show_prompts = show_prompts | |
22 | self.inline_prompt = inline_prompt |
|
18 | self.inline_prompt = inline_prompt | |
23 |
|
19 | |||
|
20 | def switch_meta(self, m_list): | |||
|
21 | if len(m_list) > 1: | |||
|
22 | if not (len(m_list) == 2 and m_list[1] == [u'new_fragment = True']): | |||
|
23 | m_list[0], m_list[1] = m_list[1], m_list[0] | |||
|
24 | return m_list | |||
|
25 | ||||
|
26 | def meta2str(self, meta): | |||
|
27 | meta_tuple = meta[u'slideshow'].items() | |||
|
28 | meta_list = [[x + ' = ' + unicode(y)] for x, y in meta_tuple] | |||
|
29 | meta_list = self.switch_meta(meta_list) | |||
|
30 | return u'\n'.join(list(itertools.chain(*meta_list))) | |||
|
31 | ||||
|
32 | def render_heading(self, cell): | |||
|
33 | return [self.meta2str(cell.metadata), '{0} {1}'.format('#' * cell.level, cell.source), ''] | |||
|
34 | ||||
|
35 | def render_markdown(self, cell): | |||
|
36 | return [self.meta2str(cell.metadata), cell.source, ''] | |||
|
37 | ||||
|
38 | def render_raw(self, cell): | |||
|
39 | if self.raw_as_verbatim: | |||
|
40 | return [indent(self.meta2str(cell.metadata)), indent(cell.source), ''] | |||
|
41 | else: | |||
|
42 | return [self.meta2str(cell.metadata), cell.source, ''] | |||
|
43 | ||||
24 | def convert(self, cell_separator='\n'): |
|
44 | def convert(self, cell_separator='\n'): | |
25 | """ |
|
45 | """ | |
26 | Generic method to converts notebook to a string representation. |
|
46 | Generic method to converts notebook to a string representation. | |
@@ -40,26 +60,40 b' class ConverterReveal(ConverterMarkdown):' | |||||
40 | """ |
|
60 | """ | |
41 | lines = [] |
|
61 | lines = [] | |
42 | lines.extend(self.optional_header()) |
|
62 | lines.extend(self.optional_header()) | |
43 |
|
|
63 | begin = ['<div class="reveal"><div class="slides">'] | |
44 |
lines.extend( |
|
64 | lines.extend(begin) | |
45 |
|
|
65 | slides_list = self.build_slides(cell_separator) | |
46 | left = '<section data-markdown><script type="text/template">' |
|
66 | lines.extend(slides_list) | |
47 |
|
|
67 | end = ['</div></div>'] | |
48 | for i,j in enumerate(text): |
|
|||
49 | if j == u'##---': |
|
|||
50 | text[i] = right + left |
|
|||
51 | if j == u'##<<<': |
|
|||
52 | text[i] = right + '<section>' + left |
|
|||
53 | if j == u'##>>>': |
|
|||
54 | text[i] = right + '</section>' + left |
|
|||
55 | if j == u'##>>><<<': |
|
|||
56 | text[i] = right + '</section><section>' + left |
|
|||
57 | lines.extend(text) |
|
|||
58 | end = ['</script></section></div></div>'] |
|
|||
59 | lines.extend(end) |
|
68 | lines.extend(end) | |
60 | lines.extend(self.optional_footer()) |
|
69 | lines.extend(self.optional_footer()) | |
61 | return u'\n'.join(lines) |
|
70 | return u'\n'.join(lines) | |
62 |
|
71 | |||
|
72 | def build_slides(self, cell_separator='\n'): | |||
|
73 | "build the slides from text list" | |||
|
74 | text = self.main_body(cell_separator) | |||
|
75 | text = [x for x in text if x != u'new_section = False' | |||
|
76 | and x != u'new_subsection = False' | |||
|
77 | and x != u'new_fragment = False'] | |||
|
78 | left = '<section data-markdown><script type="text/template">' | |||
|
79 | right = '</script></section>' | |||
|
80 | slides = [list(x[1]) for x in itertools.groupby(text, lambda x: x==u'new_section = True') if not x[0]] | |||
|
81 | for slide in slides: | |||
|
82 | slide.insert(0,left) | |||
|
83 | slide.append(right) | |||
|
84 | if slide[1] == u'new_subsection = True': | |||
|
85 | slide.pop(1) | |||
|
86 | slide.insert(0,'<section>') | |||
|
87 | slide.append('</section>') | |||
|
88 | for i,j in enumerate(slide): | |||
|
89 | if j == u'new_subsection = True': | |||
|
90 | slide[i] = right + left | |||
|
91 | for i,j in enumerate(slide): | |||
|
92 | if j == u'new_fragment = True': | |||
|
93 | slide[i] = '<p class="fragment">' | |||
|
94 | slide[i + 2] = '</p>' | |||
|
95 | return list(itertools.chain(*slides)) | |||
|
96 | ||||
63 | def save(self, outfile=None, encoding=None): |
|
97 | def save(self, outfile=None, encoding=None): | |
64 | "read and parse notebook into self.nb" |
|
98 | "read and parse notebook into self.nb" | |
65 | if outfile is None: |
|
99 | if outfile is None: | |
@@ -92,3 +126,4 b' class ConverterReveal(ConverterMarkdown):' | |||||
92 | def optional_footer(self): |
|
126 | def optional_footer(self): | |
93 | optional_footer_body = self.template_split() |
|
127 | optional_footer_body = self.template_split() | |
94 | return optional_footer_body[1] |
|
128 | return optional_footer_body[1] | |
|
129 |
General Comments 0
You need to be logged in to leave comments.
Login now