##// END OF EJS Templates
Peptified code to get a better visualization
damianavila -
Show More
@@ -5,9 +5,12 b' import os'
5 import itertools
5 import itertools
6
6
7 class ConverterReveal(ConverterMarkdown):
7 class ConverterReveal(ConverterMarkdown):
8 """Convert a notebook to a html slideshow.
8 """
9 Convert a notebook to a html slideshow.
9
10
10 It generates a static html slideshow based in markdown and reveal.js.
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 def __init__(self, infile, highlight_source=False, show_prompts=True,
16 def __init__(self, infile, highlight_source=False, show_prompts=True,
@@ -18,14 +21,14 b' class ConverterReveal(ConverterMarkdown):'
18 self.inline_prompt = inline_prompt
21 self.inline_prompt = inline_prompt
19
22
20 def switch_meta(self, m_list):
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 if len(m_list) > 1:
25 if len(m_list) > 1:
23 if not (len(m_list) == 2 and m_list[1] == [u'new_fragment = True']):
26 if not (len(m_list) == 2 and m_list[1] == [u'new_fragment = True']):
24 m_list[0], m_list[1] = m_list[1], m_list[0]
27 m_list[0], m_list[1] = m_list[1], m_list[0]
25 return m_list
28 return m_list
26
29
27 def meta2str(self, meta):
30 def meta2str(self, meta):
28 "transform metadata dictionary to a string containing delimiters for slides"
31 "transform metadata dict to a string containing delimiters for slides"
29 try:
32 try:
30 meta_tuple = meta[u'slideshow'].items()
33 meta_tuple = meta[u'slideshow'].items()
31 except KeyError as e:
34 except KeyError as e:
@@ -35,14 +38,16 b' class ConverterReveal(ConverterMarkdown):'
35 return u'\n'.join(list(itertools.chain(*meta_list)))
38 return u'\n'.join(list(itertools.chain(*meta_list)))
36
39
37 def render_heading(self, cell):
40 def render_heading(self, cell):
38 return [self.meta2str(cell.metadata), '{0} {1}'.format('#' * cell.level, cell.source), '']
41 return [self.meta2str(cell.metadata),
42 '{0} {1}'.format('#' * cell.level, cell.source), '']
39
43
40 def render_markdown(self, cell):
44 def render_markdown(self, cell):
41 return [self.meta2str(cell.metadata), cell.source, '']
45 return [self.meta2str(cell.metadata), cell.source, '']
42
46
43 def render_raw(self, cell):
47 def render_raw(self, cell):
44 if self.raw_as_verbatim:
48 if self.raw_as_verbatim:
45 return [indent(self.meta2str(cell.metadata)), indent(cell.source), '']
49 return [indent(self.meta2str(cell.metadata)),
50 indent(cell.source), '']
46 else:
51 else:
47 return [self.meta2str(cell.metadata), cell.source, '']
52 return [self.meta2str(cell.metadata), cell.source, '']
48
53
@@ -77,34 +82,37 b' class ConverterReveal(ConverterMarkdown):'
77 def build_slides(self, cell_separator='\n'):
82 def build_slides(self, cell_separator='\n'):
78 "build the slides structure from text list and delimiters"
83 "build the slides structure from text list and delimiters"
79 text = self.main_body(cell_separator)
84 text = self.main_body(cell_separator)
80 delim_false = [u'new_section = False', u'new_subsection = False', u'new_fragment = False']
85 delim_false = [u'new_section = False',
86 u'new_subsection = False',
87 u'new_fragment = False']
81 text = [x for x in text if not x in delim_false]
88 text = [x for x in text if not x in delim_false]
82 left = '<section data-markdown><script type="text/template">'
89 left = '<section data-markdown><script type="text/template">'
83 right = '</script></section>'
90 right = '</script></section>'
84 slides = [list(x[1]) for x in itertools.groupby(text, lambda x: x==u'new_section = True') if not x[0]]
91 slides = [list(x[1]) for x in itertools.groupby(text,
92 lambda x: x == u'new_section = True') if not x[0]]
85 for slide in slides:
93 for slide in slides:
86 slide.insert(0, u'')
94 slide.insert(0, u'')
87 slide.insert(0,left)
95 slide.insert(0, left)
88 slide.append(right)
96 slide.append(right)
89 if slide[2] == u'new_subsection = True':
97 if slide[2] == u'new_subsection = True':
90 slide.pop(2)
98 slide.pop(2)
91 slide.insert(0,'<section>')
99 slide.insert(0, '<section>')
92 slide.append('</section>')
100 slide.append('</section>')
93 for i,j in enumerate(slide):
101 for i, j in enumerate(slide):
94 if j == u'new_subsection = True':
102 if j == u'new_subsection = True':
95 slide[i] = right + left
103 slide[i] = right + left
96 slide.insert(i + 1, u'')
104 slide.insert(i + 1, u'')
97 elif slide[4] == u'new_subsection = True':
105 elif slide[4] == u'new_subsection = True':
98 slide[4] = right
106 slide[4] = right
99 slide.insert(5, u'')
107 slide.insert(5, u'')
100 slide.insert(5,left)
108 slide.insert(5, left)
101 slide.insert(5,'<section>')
109 slide.insert(5, '<section>')
102 slide.append('</section>')
110 slide.append('</section>')
103 for i,j in enumerate(slide):
111 for i, j in enumerate(slide):
104 if j == u'new_subsection = True':
112 if j == u'new_subsection = True':
105 slide[i] = right + left
113 slide[i] = right + left
106 slide.insert(i + 1, u'')
114 slide.insert(i + 1, u'')
107 for i,j in enumerate(slide):
115 for i, j in enumerate(slide):
108 if j == u'new_fragment = True':
116 if j == u'new_fragment = True':
109 slide[i] = '<p class="fragment">'
117 slide[i] = '<p class="fragment">'
110 slide[i + 2] = '</p>'
118 slide[i + 2] = '</p>'
@@ -124,17 +132,19 b' class ConverterReveal(ConverterMarkdown):'
124 def template_read(self):
132 def template_read(self):
125 "read the reveal_template.html"
133 "read the reveal_template.html"
126 here = os.path.split(os.path.realpath(__file__))[0]
134 here = os.path.split(os.path.realpath(__file__))[0]
127 reveal_template = os.path.join(here, '..', 'templates', 'reveal_base.html')
135 reveal_template = os.path.join(here, '..', 'templates',
136 'reveal_base.html')
128 with io.open(reveal_template, 'r', encoding='utf-8') as f:
137 with io.open(reveal_template, 'r', encoding='utf-8') as f:
129 template = f.readlines()
138 template = f.readlines()
130 template = map(lambda s: s.strip(), template) # cosmetic one to get short html files
139 template = [s.strip() for s in template] # cosmetic one
131 return template
140 return template
132
141
133 def template_split(self):
142 def template_split(self):
134 "split the reveal_template.html in header and footer lists"
143 "split the reveal_template.html in header and footer lists"
135 temp = self.template_read()
144 temp = self.template_read()
136 splitted_temp = [list(x[1]) for x in itertools.groupby(temp, lambda x: x==u'%slides%') if not x[0]]
145 splitted_temp = [list(x[1]) for x in itertools.groupby(temp,
137 return splitted_temp
146 lambda x: x == u'%slides%') if not x[0]]
147 return splitted_temp
138
148
139 def optional_header(self):
149 def optional_header(self):
140 optional_header_body = self.template_split()
150 optional_header_body = self.template_split()
General Comments 0
You need to be logged in to leave comments. Login now