diff --git a/notebook_sphinxext.py b/notebook_sphinxext.py index 0529122..83d60ba 100644 --- a/notebook_sphinxext.py +++ b/notebook_sphinxext.py @@ -24,11 +24,11 @@ class Notebook(Directive): has_content = False def run(self): - if (not self.state.document.settings.raw_enabled - or (not self.state.document.settings.file_insertion_enabled - and ('file' in self.options - or 'url' in self.options))): + # check if raw html is supported + if not self.state.document.settings.raw_enabled: raise self.warning('"%s" directive disabled.' % self.name) + + # set up encoding attributes = {'format': 'html'} encoding = self.options.get( 'encoding', self.state.document.settings.input_encoding) @@ -37,35 +37,34 @@ class Notebook(Directive): # get path to notebook source_dir = os.path.dirname( os.path.abspath(self.state.document.current_source)) - path = os.path.normpath(os.path.join(source_dir, - self.arguments[0])) - path = utils.relative_path(None, path) + nb_path = os.path.normpath(os.path.join(source_dir, + self.arguments[0])) + nb_path = utils.relative_path(None, nb_path) # convert notebook to html - converter = ConverterHTML(path) - htmlfname = converter.render() - htmlpath = utils.relative_path(None, htmlfname) - - try: - raw_file = io.FileInput(source_path=htmlpath, - encoding=encoding, - error_handler=e_handler) - # TODO: currently, raw input files are recorded as - # dependencies even if not used for the chosen output format. - self.state.document.settings.record_dependencies.add(htmlpath) - except IOError, error: - raise self.severe(u'Problems with "%s" directive path:\n%s.' - % (self.name, ErrorString(error))) - try: - text = raw_file.read() - except UnicodeError, error: - raise self.severe(u'Problem with "%s" directive:\n%s' - % (self.name, ErrorString(error))) - attributes['source'] = htmlpath + converter = ConverterHTML(nb_path) + converter.read() + + # add HTML5 scoped attribute to header style tags + header = map(lambda s: s.replace(''] + lines.extend(header) + lines.extend(converter.main_body()) + lines.append('') + text = '\n'.join(lines) + # add dependency + self.state.document.settings.record_dependencies.add(nb_path) + attributes['source'] = nb_path + + # create notebook node nb_node = notebook('', text, **attributes) - (nb_node.source, - nb_node.line) = self.state_machine.get_source_and_line(self.lineno) + (nb_node.source, nb_node.line) = \ + self.state_machine.get_source_and_line(self.lineno) + return [nb_node]