##// END OF EJS Templates
Peptified code to get a better visualization
damianavila -
Show More
@@ -1,146 +1,156 b''
1 from converters.markdown import ConverterMarkdown
1 from converters.markdown import ConverterMarkdown
2 from IPython.utils.text import indent
2 from IPython.utils.text import indent
3 import io
3 import io
4 import os
4 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,
14 inline_prompt=True):
17 inline_prompt=True):
15 super(ConverterMarkdown, self).__init__(infile)
18 super(ConverterMarkdown, self).__init__(infile)
16 self.highlight_source = highlight_source
19 self.highlight_source = highlight_source
17 self.show_prompts = show_prompts
20 self.show_prompts = show_prompts
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:
32 meta_tuple = ()
35 meta_tuple = ()
33 meta_list = [[x + ' = ' + unicode(y)] for x, y in meta_tuple]
36 meta_list = [[x + ' = ' + unicode(y)] for x, y in meta_tuple]
34 meta_list = self.switch_meta(meta_list)
37 meta_list = self.switch_meta(meta_list)
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
49 def convert(self, cell_separator='\n'):
54 def convert(self, cell_separator='\n'):
50 """
55 """
51 Generic method to converts notebook to a string representation.
56 Generic method to converts notebook to a string representation.
52
57
53 This is accomplished by dispatching on the cell_type, so subclasses of
58 This is accomplished by dispatching on the cell_type, so subclasses of
54 Convereter class do not need to re-implement this method, but just
59 Convereter class do not need to re-implement this method, but just
55 need implementation for the methods that will be dispatched.
60 need implementation for the methods that will be dispatched.
56
61
57 Parameters
62 Parameters
58 ----------
63 ----------
59 cell_separator : string
64 cell_separator : string
60 Character or string to join cells with. Default is "\n"
65 Character or string to join cells with. Default is "\n"
61
66
62 Returns
67 Returns
63 -------
68 -------
64 out : string
69 out : string
65 """
70 """
66 lines = []
71 lines = []
67 lines.extend(self.optional_header())
72 lines.extend(self.optional_header())
68 begin = ['<div class="reveal"><div class="slides">']
73 begin = ['<div class="reveal"><div class="slides">']
69 lines.extend(begin)
74 lines.extend(begin)
70 slides_list = self.build_slides(cell_separator)
75 slides_list = self.build_slides(cell_separator)
71 lines.extend(slides_list)
76 lines.extend(slides_list)
72 end = ['</div></div>']
77 end = ['</div></div>']
73 lines.extend(end)
78 lines.extend(end)
74 lines.extend(self.optional_footer())
79 lines.extend(self.optional_footer())
75 return u'\n'.join(lines)
80 return u'\n'.join(lines)
76
81
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>'
111 slide.insert(i + 3, u'')
119 slide.insert(i + 3, u'')
112 return list(itertools.chain(*slides))
120 return list(itertools.chain(*slides))
113
121
114 def save(self, outfile=None, encoding=None):
122 def save(self, outfile=None, encoding=None):
115 "read and parse notebook into self.nb"
123 "read and parse notebook into self.nb"
116 if outfile is None:
124 if outfile is None:
117 outfile = self.outbase + '_slides.' + 'html'
125 outfile = self.outbase + '_slides.' + 'html'
118 if encoding is None:
126 if encoding is None:
119 encoding = self.default_encoding
127 encoding = self.default_encoding
120 with io.open(outfile, 'w', encoding=encoding) as f:
128 with io.open(outfile, 'w', encoding=encoding) as f:
121 f.write(self.output)
129 f.write(self.output)
122 return os.path.abspath(outfile)
130 return os.path.abspath(outfile)
123
131
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,
146 lambda x: x == u'%slides%') if not x[0]]
137 return splitted_temp
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()
141 return optional_header_body[0]
151 return optional_header_body[0]
142
152
143 def optional_footer(self):
153 def optional_footer(self):
144 optional_footer_body = self.template_split()
154 optional_footer_body = self.template_split()
145 return optional_footer_body[1]
155 return optional_footer_body[1]
146
156
General Comments 0
You need to be logged in to leave comments. Login now