Пример #1
0
def export(nbname, outfilename=None):
    exportRST = RSTExporter()
    # exclude default paths
    exportRST.template_path = ['.','/Users/adam/repos/blog'] 

    (body,resources) = exportRST.from_filename(nbname)

    if outfilename is None:
        outfilename = nbname.replace("ipynb","rst")

    with open(outfilename,'w') as f:
        f.write(body)

    return body,resources
Пример #2
0
def export(nbname, outfilename=None):
    exportRST = RSTExporter()
    # exclude default paths
    # exportRST.template_path = ['.','/Users/adam/repos/blog'] 

    (body,resources) = exportRST.from_filename(nbname)

    if outfilename is None:
        outfilename = nbname.replace("ipynb","rst")

    with open(outfilename,'w') as f:
        f.write(body[1:].lstrip())      # offset because it puts in a blank line at top?

    return body,resources
Пример #3
0
class IPyNBReader(BaseReader):

    enabled = bool(RSTExporter)
    file_extensions = ['ipynb']

    if RSTExporter:
        exporter = RSTExporter()
        exporter.register_preprocessor(extract_blogdata)
        exporter.register_preprocessor(append_new_line_to_cell)

    def __init__(self, *args, **kwargs):
        super(IPyNBReader, self).__init__(*args, **kwargs)

    def _get_publisher(self, contents):
        """
        Stolen from :class:`~Pelican.readers.RstReader` almost
        verbatim, except for the source which now is a file-like object.
        """
        extra_params = {
            'initial_header_level': '2',
            'syntax_highlight': 'short',
            'input_encoding': 'utf-8',
            'exit_status_level': 2,
            'embed_stylesheet': False
        }

        user_params = self.settings.get('DOCUTILS_SETTINGS', None)

        if user_params:
            extra_params.update(user_params)

        pub = docutils.core.Publisher(
            source_class=RstReader.FileInput,
            destination_class=docutils.io.StringOutput)

        pub.set_components('standalone', 'restructuredtext', 'html')
        pub.writer.translator_class = PelicanHTMLTranslator
        pub.process_programmatic_settings(None, extra_params, None)
        pub.set_source(source=contents)
        pub.publish(enable_exit_status=True)
        return pub

    def read(self, filename):
        contents, metadata = self.exporter.from_filename(filename)
        publisher = self._get_publisher(contents=StringIO(contents))
        parts = publisher.writer.parts

        contents = parts.get('body')

        blogdata = metadata.pop('blogdata')
        parsed = {k: self.process_metadata(k, v) for k, v in blogdata.items()}

        return contents, parsed

    @classmethod
    def register_preprocessor(cls, preprocessor, enabled=True):
        '''Helper method to register extra preprocessors onto the RST exporter.
        '''
        return cls.exporter.register_preprocessor(preprocessor, enabled)