Show More
@@ -1,146 +1,156 b'' | |||
|
1 | 1 | from converters.markdown import ConverterMarkdown |
|
2 | 2 | from IPython.utils.text import indent |
|
3 | 3 | import io |
|
4 | 4 | import os |
|
5 | 5 | import itertools |
|
6 | 6 | |
|
7 | 7 | class ConverterReveal(ConverterMarkdown): |
|
8 | """Convert a notebook to a html slideshow. | |
|
8 | """ | |
|
9 | Convert a notebook to a html slideshow. | |
|
9 | 10 | |
|
10 | 11 | It generates a static html slideshow based in markdown and reveal.js. |
|
12 | The delimiters for each slide, subslide, and fragment are retrieved | |
|
13 | from the 'slideshow' metadata. | |
|
11 | 14 | """ |
|
12 | 15 | |
|
13 | 16 | def __init__(self, infile, highlight_source=False, show_prompts=True, |
|
14 | 17 | inline_prompt=True): |
|
15 | 18 | super(ConverterMarkdown, self).__init__(infile) |
|
16 | 19 | self.highlight_source = highlight_source |
|
17 | 20 | self.show_prompts = show_prompts |
|
18 | 21 | self.inline_prompt = inline_prompt |
|
19 | 22 | |
|
20 | 23 | def switch_meta(self, m_list): |
|
21 |
"switch metadata delimiters order to build properly the slides" |
|
|
24 | "switch metadata delimiters order to build properly the slides" | |
|
22 | 25 | if len(m_list) > 1: |
|
23 | 26 | if not (len(m_list) == 2 and m_list[1] == [u'new_fragment = True']): |
|
24 | 27 | m_list[0], m_list[1] = m_list[1], m_list[0] |
|
25 | 28 | return m_list |
|
26 | 29 | |
|
27 | 30 | def meta2str(self, meta): |
|
28 |
"transform metadata dict |
|
|
31 | "transform metadata dict to a string containing delimiters for slides" | |
|
29 | 32 | try: |
|
30 | 33 | meta_tuple = meta[u'slideshow'].items() |
|
31 | 34 | except KeyError as e: |
|
32 | 35 | meta_tuple = () |
|
33 | 36 | meta_list = [[x + ' = ' + unicode(y)] for x, y in meta_tuple] |
|
34 | 37 | meta_list = self.switch_meta(meta_list) |
|
35 | 38 | return u'\n'.join(list(itertools.chain(*meta_list))) |
|
36 | 39 | |
|
37 | 40 | def render_heading(self, cell): |
|
38 |
return [self.meta2str(cell.metadata), |
|
|
41 | return [self.meta2str(cell.metadata), | |
|
42 | '{0} {1}'.format('#' * cell.level, cell.source), ''] | |
|
39 | 43 | |
|
40 | 44 | def render_markdown(self, cell): |
|
41 | 45 | return [self.meta2str(cell.metadata), cell.source, ''] |
|
42 | 46 | |
|
43 | 47 | def render_raw(self, cell): |
|
44 | 48 | if self.raw_as_verbatim: |
|
45 |
return [indent(self.meta2str(cell.metadata)), |
|
|
49 | return [indent(self.meta2str(cell.metadata)), | |
|
50 | indent(cell.source), ''] | |
|
46 | 51 | else: |
|
47 | 52 | return [self.meta2str(cell.metadata), cell.source, ''] |
|
48 | 53 | |
|
49 | 54 | def convert(self, cell_separator='\n'): |
|
50 | 55 | """ |
|
51 | 56 | Generic method to converts notebook to a string representation. |
|
52 | 57 | |
|
53 | 58 | This is accomplished by dispatching on the cell_type, so subclasses of |
|
54 | 59 | Convereter class do not need to re-implement this method, but just |
|
55 | 60 | need implementation for the methods that will be dispatched. |
|
56 | 61 | |
|
57 | 62 | Parameters |
|
58 | 63 | ---------- |
|
59 | 64 | cell_separator : string |
|
60 | 65 | Character or string to join cells with. Default is "\n" |
|
61 | 66 | |
|
62 | 67 | Returns |
|
63 | 68 | ------- |
|
64 | 69 | out : string |
|
65 | 70 | """ |
|
66 | 71 | lines = [] |
|
67 | 72 | lines.extend(self.optional_header()) |
|
68 | 73 | begin = ['<div class="reveal"><div class="slides">'] |
|
69 | 74 | lines.extend(begin) |
|
70 | 75 | slides_list = self.build_slides(cell_separator) |
|
71 | 76 | lines.extend(slides_list) |
|
72 | 77 | end = ['</div></div>'] |
|
73 | 78 | lines.extend(end) |
|
74 | 79 | lines.extend(self.optional_footer()) |
|
75 | 80 | return u'\n'.join(lines) |
|
76 | 81 | |
|
77 | 82 | def build_slides(self, cell_separator='\n'): |
|
78 | 83 | "build the slides structure from text list and delimiters" |
|
79 | 84 | text = self.main_body(cell_separator) |
|
80 |
delim_false = [u'new_section = False', |
|
|
85 | delim_false = [u'new_section = False', | |
|
86 | u'new_subsection = False', | |
|
87 | u'new_fragment = False'] | |
|
81 | 88 | text = [x for x in text if not x in delim_false] |
|
82 | 89 | left = '<section data-markdown><script type="text/template">' |
|
83 | 90 | right = '</script></section>' |
|
84 |
slides = [list(x[1]) for x in itertools.groupby(text, |
|
|
91 | slides = [list(x[1]) for x in itertools.groupby(text, | |
|
92 | lambda x: x == u'new_section = True') if not x[0]] | |
|
85 | 93 | for slide in slides: |
|
86 | 94 | slide.insert(0, u'') |
|
87 | slide.insert(0,left) | |
|
95 | slide.insert(0, left) | |
|
88 | 96 | slide.append(right) |
|
89 | 97 | if slide[2] == u'new_subsection = True': |
|
90 | 98 | slide.pop(2) |
|
91 | slide.insert(0,'<section>') | |
|
99 | slide.insert(0, '<section>') | |
|
92 | 100 | slide.append('</section>') |
|
93 | for i,j in enumerate(slide): | |
|
101 | for i, j in enumerate(slide): | |
|
94 | 102 | if j == u'new_subsection = True': |
|
95 | 103 | slide[i] = right + left |
|
96 | 104 | slide.insert(i + 1, u'') |
|
97 | 105 | elif slide[4] == u'new_subsection = True': |
|
98 | 106 | slide[4] = right |
|
99 | 107 | slide.insert(5, u'') |
|
100 |
slide.insert(5,left) |
|
|
101 | slide.insert(5,'<section>') | |
|
108 | slide.insert(5, left) | |
|
109 | slide.insert(5, '<section>') | |
|
102 | 110 | slide.append('</section>') |
|
103 | for i,j in enumerate(slide): | |
|
111 | for i, j in enumerate(slide): | |
|
104 | 112 | if j == u'new_subsection = True': |
|
105 | 113 | slide[i] = right + left |
|
106 | 114 | slide.insert(i + 1, u'') |
|
107 | for i,j in enumerate(slide): | |
|
115 | for i, j in enumerate(slide): | |
|
108 | 116 | if j == u'new_fragment = True': |
|
109 | 117 | slide[i] = '<p class="fragment">' |
|
110 | 118 | slide[i + 2] = '</p>' |
|
111 | 119 | slide.insert(i + 3, u'') |
|
112 | 120 | return list(itertools.chain(*slides)) |
|
113 | 121 | |
|
114 | 122 | def save(self, outfile=None, encoding=None): |
|
115 | 123 | "read and parse notebook into self.nb" |
|
116 | 124 | if outfile is None: |
|
117 | 125 | outfile = self.outbase + '_slides.' + 'html' |
|
118 | 126 | if encoding is None: |
|
119 | 127 | encoding = self.default_encoding |
|
120 | 128 | with io.open(outfile, 'w', encoding=encoding) as f: |
|
121 | 129 | f.write(self.output) |
|
122 | 130 | return os.path.abspath(outfile) |
|
123 | 131 | |
|
124 | 132 | def template_read(self): |
|
125 | 133 | "read the reveal_template.html" |
|
126 | 134 | here = os.path.split(os.path.realpath(__file__))[0] |
|
127 |
reveal_template = os.path.join(here, '..', 'templates', |
|
|
135 | reveal_template = os.path.join(here, '..', 'templates', | |
|
136 | 'reveal_base.html') | |
|
128 | 137 | with io.open(reveal_template, 'r', encoding='utf-8') as f: |
|
129 | 138 | template = f.readlines() |
|
130 |
template = |
|
|
139 | template = [s.strip() for s in template] # cosmetic one | |
|
131 | 140 | return template |
|
132 | 141 | |
|
133 | 142 | def template_split(self): |
|
134 | 143 | "split the reveal_template.html in header and footer lists" |
|
135 | 144 | temp = self.template_read() |
|
136 |
splitted_temp = [list(x[1]) for x in itertools.groupby(temp, |
|
|
137 | return splitted_temp | |
|
145 | splitted_temp = [list(x[1]) for x in itertools.groupby(temp, | |
|
146 | lambda x: x == u'%slides%') if not x[0]] | |
|
147 | return splitted_temp | |
|
138 | 148 | |
|
139 | 149 | def optional_header(self): |
|
140 | 150 | optional_header_body = self.template_split() |
|
141 | 151 | return optional_header_body[0] |
|
142 | 152 | |
|
143 | 153 | def optional_footer(self): |
|
144 | 154 | optional_footer_body = self.template_split() |
|
145 | 155 | return optional_footer_body[1] |
|
146 | 156 |
General Comments 0
You need to be logged in to leave comments.
Login now