from converters.markdown import ConverterMarkdown from IPython.utils.text import indent import io import os import itertools class ConverterReveal(ConverterMarkdown): """ Convert a notebook to a html slideshow. It generates a static html slideshow based in markdown and reveal.js. The delimiters for each slide, subslide, and fragment are retrieved from the 'slideshow' metadata. """ def __init__(self, infile, highlight_source=False, show_prompts=True, inline_prompt=True): super(ConverterMarkdown, self).__init__(infile) self.highlight_source = highlight_source self.show_prompts = show_prompts self.inline_prompt = inline_prompt def switch_meta(self, m_list): "switch metadata delimiters order to build properly the slides" if len(m_list) > 1: if not (len(m_list) == 2 and m_list[1] == [u'new_fragment = True']): m_list[0], m_list[1] = m_list[1], m_list[0] return m_list def meta2str(self, meta): "transform metadata dict to a string containing delimiters for slides" try: meta_tuple = meta[u'slideshow'].items() except KeyError as e: meta_tuple = () meta_list = [[x + ' = ' + unicode(y)] for x, y in meta_tuple] meta_list = self.switch_meta(meta_list) return u'\n'.join(list(itertools.chain(*meta_list))) def render_heading(self, cell): return [self.meta2str(cell.metadata), '{0} {1}'.format('#' * cell.level, cell.source), ''] def render_markdown(self, cell): return [self.meta2str(cell.metadata), cell.source, ''] def render_raw(self, cell): if self.raw_as_verbatim: return [indent(self.meta2str(cell.metadata)), indent(cell.source), ''] else: return [self.meta2str(cell.metadata), cell.source, ''] def convert(self, cell_separator='\n'): """ Generic method to converts notebook to a string representation. This is accomplished by dispatching on the cell_type, so subclasses of Convereter class do not need to re-implement this method, but just need implementation for the methods that will be dispatched. Parameters ---------- cell_separator : string Character or string to join cells with. Default is "\n" Returns ------- out : string """ lines = [] lines.extend(self.optional_header()) begin = ['
' slide[i + 2] = '
' slide.insert(i + 3, u'') return list(itertools.chain(*slides)) def save(self, outfile=None, encoding=None): "read and parse notebook into self.nb" if outfile is None: outfile = self.outbase + '_slides.' + 'html' if encoding is None: encoding = self.default_encoding with io.open(outfile, 'w', encoding=encoding) as f: f.write(self.output) return os.path.abspath(outfile) def template_read(self): "read the reveal_template.html" here = os.path.split(os.path.realpath(__file__))[0] reveal_template = os.path.join(here, '..', 'templates', 'reveal_base.html') with io.open(reveal_template, 'r', encoding='utf-8') as f: template = f.readlines() template = [s.strip() for s in template] # cosmetic one return template def template_split(self): "split the reveal_template.html in header and footer lists" temp = self.template_read() splitted_temp = [list(x[1]) for x in itertools.groupby(temp, lambda x: x == u'%slides%') if not x[0]] return splitted_temp def optional_header(self): optional_header_body = self.template_split() return optional_header_body[0] def optional_footer(self): optional_footer_body = self.template_split() return optional_footer_body[1]