예제 #1
0
파일: io.py 프로젝트: zhanglhz/sphinx
def read_doc(app: "Sphinx", env: BuildEnvironment,
             filename: str) -> nodes.document:
    """Parse a document and convert to doctree."""
    # set up error_handler for the target document
    error_handler = UnicodeDecodeErrorHandler(env.docname)
    codecs.register_error('sphinx', error_handler)  # type: ignore

    reader = SphinxStandaloneReader()
    reader.setup(app)
    filetype = get_filetype(app.config.source_suffix, filename)
    parser = app.registry.create_source_parser(app, filetype)
    if parser.__class__.__name__ == 'CommonMarkParser' and parser.settings_spec == (
    ):
        # a workaround for recommonmark
        #   If recommonmark.AutoStrictify is enabled, the parser invokes reST parser
        #   internally.  But recommonmark-0.4.0 does not provide settings_spec for reST
        #   parser.  As a workaround, this copies settings_spec for RSTParser to the
        #   CommonMarkParser.
        parser.settings_spec = RSTParser.settings_spec

    input_class = app.registry.get_source_input(filetype)
    if input_class:
        # Sphinx-1.8 style
        source = input_class(
            app,
            env,
            source=None,
            source_path=filename,  # type: ignore
            encoding=env.config.source_encoding)
        pub = Publisher(
            reader=reader,
            parser=parser,
            writer=SphinxDummyWriter(),
            source_class=SphinxDummySourceClass,  # type: ignore
            destination=NullOutput())
        pub.process_programmatic_settings(None, env.settings, None)
        pub.set_source(source, filename)
    else:
        # Sphinx-2.0 style
        pub = Publisher(reader=reader,
                        parser=parser,
                        writer=SphinxDummyWriter(),
                        source_class=SphinxFileInput,
                        destination=NullOutput())
        pub.process_programmatic_settings(None, env.settings, None)
        pub.set_source(source_path=filename)

    pub.publish()
    return pub.document
예제 #2
0
def publish_cmdline(reader=None,
                    reader_name='standalone',
                    parser=None,
                    parser_name='restructuredtext',
                    writer=None,
                    writer_name='pseudoxml',
                    settings=None,
                    settings_spec=None,
                    settings_overrides=None,
                    config_section=None,
                    enable_exit_status=1,
                    argv=None,
                    usage=default_usage,
                    description=default_description):
    """
    See docutils.core.publish_cmdline.

    We just modified this function to return the parsed destination file.
    """
    pub = Publisher(reader, parser, writer, settings=settings)
    pub.set_components(reader_name, parser_name, writer_name)
    output = pub.publish(argv,
                         usage,
                         description,
                         settings_spec,
                         settings_overrides,
                         config_section=config_section,
                         enable_exit_status=enable_exit_status)
    return output, pub.settings._source, pub.settings._destination
예제 #3
0
 def __init__(self,
              settings_spec=None,
              settings_overrides=dict(report_level=5, halt_level=5),
              config_section='general'):
     self.pub = Publisher(reader=None,
                          parser=None,
                          writer=None,
                          settings=None,
                          source_class=io.StringInput,
                          destination_class=io.StringOutput)
     self.pub.set_components(reader_name='standalone',
                             parser_name='restructuredtext',
                             writer_name='html')
     # hack: JEP-0071 does not allow HTML char entities, so we hack our way
     # out of it.
     # — == u"\u2014"
     # a setting to only emit charater entities in the writer would be nice
     # FIXME: several   are emitted, and they are explicitly forbidden
     # in the JEP
     #   ==  u"\u00a0"
     self.pub.writer.translator_class.attribution_formats['dash'] = (
         '\u2014', '')
     self.pub.process_programmatic_settings(settings_spec,
                                            settings_overrides,
                                            config_section)
예제 #4
0
def publish_cmdline_to_binary(reader=None, reader_name='standalone',
                    parser=None, parser_name='restructuredtext',
                    writer=None, writer_name='pseudoxml',
                    settings=None, settings_spec=None,
                    settings_overrides=None, config_section=None,
                    enable_exit_status=1, argv=None,
                    usage=default_usage, description=default_description,
                    destination=None, destination_class=BinaryFileOutput
                    ):
    """
    Set up & run a `Publisher` for command-line-based file I/O (input and
    output file paths taken automatically from the command line).  Return the
    encoded string output also.

    This is just like publish_cmdline, except that it uses
    io.BinaryFileOutput instead of io.FileOutput.

    Parameters: see `publish_programmatically` for the remainder.

    - `argv`: Command-line argument list to use instead of ``sys.argv[1:]``.
    - `usage`: Usage string, output if there's a problem parsing the command
      line.
    - `description`: Program description, output for the "--help" option
      (along with command-line option descriptions).
    """
    pub = Publisher(reader, parser, writer, settings=settings,
        destination_class=destination_class)
    pub.set_components(reader_name, parser_name, writer_name)
    output = pub.publish(
        argv, usage, description, settings_spec, settings_overrides,
        config_section=config_section, enable_exit_status=enable_exit_status)
    return output
예제 #5
0
파일: build.py 프로젝트: scanlime/picogui
def publish(writer_name):

    reader=None
    reader_name='standalone'
    parser=None
    parser_name='restructuredtext'
    writer=None
    settings=None
    settings_spec=None
    settings_overrides=options[writer_name]
    config_section=None
    enable_exit=1
    argv=[]
    usage=default_usage

    pub = Publisher(reader, parser, writer, settings=settings)
    pub.set_components(reader_name, parser_name, writer_name)
    settings = pub.get_settings(settings_spec=settings_spec,
                                config_section=config_section)
    if settings_overrides:
        settings._update(settings_overrides, 'loose')
    source = file(source_path)
    pub.set_source(source, source_path)
    destination_path = 'pg1.' + extensions[writer_name]
    destination = file(destination_path, 'w')
    pub.set_destination(destination, destination_path)
    pub.publish(argv, usage, description, settings_spec, settings_overrides,
                config_section=config_section, enable_exit=enable_exit)
예제 #6
0
파일: util.py 프로젝트: carlkibler/rested
def _docutils_rest_to(rest, writer):
    """ Uses docutils to convert a ReST string to HTML. Returns a tuple
        containg the HTML string and the list of warning nodes that were
        removed from the HTML.
    """
    # Make sure any Sphinx polution of docutils has been removed.
    if Sphinx is not None:
        for key, value in docutils_roles.items():
            if value.__module__.startswith('sphinx'):
                docutils_roles.pop(key)

    pub = Publisher(source_class=docutils.io.StringInput,
                    destination_class=docutils.io.StringOutput)
    pub.set_reader('standalone', None, 'restructuredtext')
    pub.set_writer(writer)
    pub.writer.default_stylesheet_path=''
    pub.get_settings() # Get the default settings
    pub.settings.halt_level = 6 # Don't halt on errors
    pub.settings.warning_stream = StringIO()

    pub.set_source(rest)
    pub.set_destination()
    pub.document = pub.reader.read(pub.source, pub.parser, pub.settings)
    pub.apply_transforms()

    # Walk the node structure of a docutils document and remove 'problematic'
    # and 'system_message' nodes. Save the system_message nodes.
    warning_nodes = []
    for node in pub.document.traverse(docutils.nodes.problematic):
        node.parent.replace(node, node.children[0])
    for node in pub.document.traverse(docutils.nodes.system_message):
        warning_nodes.append(node)
        node.parent.remove(node)

    return pub.writer.write(pub.document, pub.destination), warning_nodes
예제 #7
0
def convertWWW(src, language, options=None):
    if language == 'el':
        encoding = 'iso-8859-7'
    elif language == 'pl':
        encoding = 'iso-8859-2'
    elif language == 'ru':
        encoding = 'windows-1251'
    elif language == 'cs':
        encoding = 'iso-8859-2'
    else:
        encoding = 'iso-8859-15'

    arguments = [
        '--no-generator', '--language=' + language, '--no-source-link',
        '--no-datestamp', '--input-encoding=' + encoding,
        '--output-encoding=' + encoding, '--target-suffix=' + 'php', src, ''
    ]

    if options:
        for option in options:
            arguments.insert(0, option)

    publisher = Publisher(destination_class=NullOutput)
    publisher.set_reader('standalone', None, 'restructuredtext')
    publisher.set_writer('html')
    publisher.publish(argv=arguments)

    return ''.join(publisher.writer.body_pre_docinfo +
                   publisher.writer.body).encode(encoding)
예제 #8
0
def processHTML(src, depth):
    src = os.path.normpath(src)

    prefix = os.path.splitext(src)[0]
    suffix = os.path.splitext(src)[1][1:]
    if suffix != DEFAULTLANG:
        return

    dst = prefix + '.html'  #.' + suffix
    dst_abs = os.path.normpath(os.path.join(TRGROOT, dst))
    src_abs = os.path.normpath(os.path.join(SRCROOT, src))
    dst_dir = os.path.dirname(dst_abs)

    makedir(dst_dir)

    if newer([src_abs], dst_abs):
        reportBuilding(src)
        arguments = [
            '--no-generator', '--language=' + suffix, '--no-source-link',
            '--no-datestamp', '--output-encoding=iso-8859-15',
            '--target-suffix=html',
            '--stylesheet=' + '../' * depth + 'aros.css', '--link-stylesheet',
            src_abs, dst_abs
        ]

        publisher = Publisher()
        publisher.set_reader('standalone', None, 'restructuredtext')
        publisher.set_writer('html')
        publisher.publish(argv=arguments)
    else:
        reportSkipping(dst)
예제 #9
0
def read_doc(app, env, filename):
    # type: (Sphinx, BuildEnvironment, unicode) -> nodes.document
    """Parse a document and convert to doctree."""
    filetype = get_filetype(app.config.source_suffix, filename)
    input_class = app.registry.get_source_input(filetype)
    reader = SphinxStandaloneReader(app)
    source = input_class(app, env, source=None, source_path=filename,
                         encoding=env.config.source_encoding)
    parser = app.registry.create_source_parser(app, filetype)
    if parser.__class__.__name__ == 'CommonMarkParser' and parser.settings_spec == ():
        # a workaround for recommonmark
        #   If recommonmark.AutoStrictify is enabled, the parser invokes reST parser
        #   internally.  But recommonmark-0.4.0 does not provide settings_spec for reST
        #   parser.  As a workaround, this copies settings_spec for RSTParser to the
        #   CommonMarkParser.
        parser.settings_spec = RSTParser.settings_spec

    pub = Publisher(reader=reader,
                    parser=parser,
                    writer=SphinxDummyWriter(),
                    source_class=SphinxDummySourceClass,
                    destination=NullOutput())
    pub.set_components(None, 'restructuredtext', None)
    pub.process_programmatic_settings(None, env.settings, None)
    pub.set_source(source, filename)
    pub.publish()
    return pub.document
예제 #10
0
def parse_docstring(doc):
    p = Publisher(source=doc, source_class=io.StringInput)
    p.set_reader('standalone', p.parser, 'restructuredtext')
    p.writer = Writer()
    p.process_programmatic_settings(None, None, None)
    p.set_source(doc, None)
    return p.publish()
예제 #11
0
def main(prog_args):
    argv = None
    reader = standalone.Reader()
    reader_name = 'standalone'
    writer = Writer()
    writer_name = 'pseudoxml'
    parser = None
    parser_name = 'restructuredtext'
    settings = None
    settings_spec = None
    settings_overrides = None
    config_section = None
    enable_exit_status = 1
    usage = default_usage
    publisher = Publisher(reader,
                          parser,
                          writer,
                          settings,
                          destination_class=BinaryFileOutput)
    publisher.set_components(reader_name, parser_name, writer_name)
    description = ('Generates OpenDocument/OpenOffice/ODF slides from '
                   'standalone reStructuredText sources.  ' +
                   default_description)

    output = publisher.publish(argv,
                               usage,
                               description,
                               settings_spec,
                               settings_overrides,
                               config_section=config_section,
                               enable_exit_status=enable_exit_status)
예제 #12
0
def main(args):
    argv = None
    reader = standalone.Reader()
    reader_name = 'standalone'
    writer = EpubWriter()
    writer_name = 'epub2'
    parser = Parser()
    parser_name = 'restructuredtext'
    settings = None
    settings_spec = None
    settings_overrides = None
    config_section = None
    enable_exit_status = 1
    usage = default_usage
    publisher = Publisher(reader,
                          parser,
                          writer,
                          settings,
                          destination_class=EpubFileOutput)
    publisher.set_components(reader_name, parser_name, writer_name)
    description = ('Generates epub books from reStructuredText sources.  ' +
                   default_description)

    output = publisher.publish(argv,
                               usage,
                               description,
                               settings_spec,
                               settings_overrides,
                               config_section=config_section,
                               enable_exit_status=enable_exit_status)
예제 #13
0
def get_sphinx():
    sphinx = getattr(local_data, 'sphinx', None)
    if sphinx is None:
        sphinx = Sphinx(tempdir,
                        tempdir,
                        tempdir,
                        tempdir,
                        'json',
                        status=None,
                        warning=None)
        sphinx.builder.translator_class = CustomHTMLTranslator

        sphinx.env.patch_lookup_functions()
        sphinx.env.temp_data['docname'] = 'text'
        sphinx.env.temp_data['default_domain'] = 'py'

        pub = Publisher(reader=None,
                        parser=None,
                        writer=HTMLWriter(sphinx.builder),
                        source_class=io.StringInput,
                        destination_class=io.NullOutput)
        pub.set_components('standalone', 'restructuredtext', None)
        pub.process_programmatic_settings(None, sphinx.env.settings, None)
        pub.set_destination(None, None)

        sphinx.publisher = pub

        local_data.sphinx = sphinx

    return sphinx, sphinx.publisher
예제 #14
0
    def render_partial(self, node):
        """Utility: Render a lone doctree node."""
        if node is None:
            return {'fragment': ''}
        doc = new_document(b'<partial node>')
        doc.append(node)

        if self._publisher is None:
            self._publisher = Publisher(
                source_class=DocTreeInput,
                destination_class=StringOutput)
            self._publisher.set_components('standalone',
                                           'restructuredtext', 'pseudoxml')

        pub = self._publisher

        pub.reader = DoctreeReader()
        pub.writer = MarkdownWriter(self)
        pub.process_programmatic_settings(
            None, {}, None)
        #pub.process_programmatic_settings(
        #    None, {'output_encoding': 'unicode'}, None)
        pub.set_source(doc, None)
        pub.set_destination(None, None)
        pub.publish()
        return pub.writer.parts
예제 #15
0
파일: models.py 프로젝트: scanlime/cia-vc
    def render(self):
        key = 'cia.apps.blog.%d' % self.id
        parts = cache.get(key)
        if not parts:

            # Convert the reST markup to a document tree
            document = publish_doctree(source=self.content)

            visitor = ImageTranslator(document)
            document.walkabout(visitor)

            #
            # Publish that document tree as HTML.  We can't use any of
            # the simpler methods in docutils.core, since we need
            # access to writer.parts
            #
            reader = doctree.Reader(parser_name='null')
            pub = Publisher(reader,
                            None,
                            None,
                            source=DocTreeInput(document),
                            destination_class=StringOutput)
            pub.set_writer('html4css1')
            pub.process_programmatic_settings(None, {
                'cloak_email_addresses': True,
                'initial_header_level': 2,
            }, None)

            pub.publish()
            parts = pub.writer.parts

            cache.set(key, parts)
        return parts
예제 #16
0
파일: io.py 프로젝트: jfbu/sphinx
def create_publisher(app: "Sphinx", filetype: str) -> Publisher:
    reader = SphinxStandaloneReader()
    reader.setup(app)

    parser = app.registry.create_source_parser(app, filetype)
    if parser.__class__.__name__ == 'CommonMarkParser' and parser.settings_spec == (
    ):
        # a workaround for recommonmark
        #   If recommonmark.AutoStrictify is enabled, the parser invokes reST parser
        #   internally.  But recommonmark-0.4.0 does not provide settings_spec for reST
        #   parser.  As a workaround, this copies settings_spec for RSTParser to the
        #   CommonMarkParser.
        from docutils.parsers.rst import Parser as RSTParser

        parser.settings_spec = RSTParser.settings_spec

    pub = Publisher(reader=reader,
                    parser=parser,
                    writer=SphinxDummyWriter(),
                    source_class=SphinxFileInput,
                    destination=NullOutput())
    # Propagate exceptions by default when used programmatically:
    defaults = {"traceback": True, **app.env.settings}
    # Set default settings
    if docutils.__version_info__[:2] >= (0, 19):
        pub.get_settings(**defaults)  # type: ignore[arg-type]
    else:
        pub.settings = pub.setup_option_parser(
            **defaults).get_default_values()  # type: ignore
    return pub
예제 #17
0
파일: io.py 프로젝트: jfbu/sphinx
def read_doc(app: "Sphinx", env: BuildEnvironment,
             filename: str) -> nodes.document:
    """Parse a document and convert to doctree."""
    warnings.warn('sphinx.io.read_doc() is deprecated.',
                  RemovedInSphinx70Warning,
                  stacklevel=2)

    # set up error_handler for the target document
    error_handler = UnicodeDecodeErrorHandler(env.docname)
    codecs.register_error('sphinx', error_handler)  # type: ignore

    reader = SphinxStandaloneReader()
    reader.setup(app)
    filetype = get_filetype(app.config.source_suffix, filename)
    parser = app.registry.create_source_parser(app, filetype)
    if parser.__class__.__name__ == 'CommonMarkParser' and parser.settings_spec == (
    ):
        # a workaround for recommonmark
        #   If recommonmark.AutoStrictify is enabled, the parser invokes reST parser
        #   internally.  But recommonmark-0.4.0 does not provide settings_spec for reST
        #   parser.  As a workaround, this copies settings_spec for RSTParser to the
        #   CommonMarkParser.
        parser.settings_spec = RSTParser.settings_spec

    pub = Publisher(reader=reader,
                    parser=parser,
                    writer=SphinxDummyWriter(),
                    source_class=SphinxFileInput,
                    destination=NullOutput())
    pub.process_programmatic_settings(None, env.settings, None)
    pub.set_source(source_path=filename)
    pub.publish()
    return pub.document
예제 #18
0
파일: views.py 프로젝트: zzz-i2p/i2p.www
def render_rst(directory, name, meta_parser, template):
    # check if that file actually exists
    path = safe_join(directory, name + '.rst')
    if not os.path.exists(path):
        abort(404)

    # read file
    with codecs.open(path, encoding='utf-8') as fd:
        content = fd.read()

    if not template:
        # Strip out RST
        content = content.replace('.. meta::\n', '')
        content = content.replace('.. contents::\n\n', '')
        content = content.replace('.. raw:: html\n\n', '')
        content = content.replace('\n.. [', '\n[')
        content = content.replace(']_.', '].')
        content = content.replace(']_,', '],')
        content = content.replace(']_', '] ')
        # Change highlight formatter
        content = content.replace('{% highlight', "{% highlight formatter='textspec'")
        # Metatags
        for (metatag, label) in METATAG_LABELS.items():
            content = content.replace('    :%s' % metatag, label)

    # render the post with Jinja2 to handle URLs etc.
    rendered_content = render_template_string(content)
    rendered_content = rendered_content.replace('</pre></div>', '  </pre></div>')

    if not template:
        # Send response
        r = make_response(rendered_content)
        r.mimetype = 'text/plain'
        return r

    # Render the ToC
    doctree = publish_doctree(source=rendered_content)
    bullet_list = doctree[1][1]
    doctree.clear()
    doctree.append(bullet_list)
    reader = Reader(parser_name='null')
    pub = Publisher(reader, None, None,
                    source=io.DocTreeInput(doctree),
                    destination_class=io.StringOutput)
    pub.set_writer('html')
    pub.publish()
    toc = pub.writer.parts['fragment']

    # Remove the ToC from the main document
    rendered_content = rendered_content.replace('.. contents::\n', '')

    # publish the spec with docutils
    parts = publish_parts(source=rendered_content, source_path=directory, writer_name="html")
    meta = meta_parser(parts['meta'])

    if (directory == PROPOSAL_DIR):
        meta['num'] = int(name[:3])

    return render_template(template, title=parts['title'], toc=toc, body=parts['fragment'], name=name, meta=meta)
예제 #19
0
    def __init__(self):

        self._publisher = Publisher(source_class=StringInput,
                                    destination_class=StringOutput)
        self._publisher.set_components('standalone', 'restructuredtext',
                                       'html')
        self._publisher.writer.translator_class = MyHTMLTranslator
        self._publisher.process_programmatic_settings(None, extra_params, None)
예제 #20
0
def process_description(source, output_encoding='unicode'):
    """Given an source string, returns an HTML fragment as a string.

    The return value is the contents of the <body> tag.

    Parameters:

    - `source`: A multi-line text string; required.
    - `output_encoding`: The desired encoding of the output.  If a Unicode
      string is desired, use the default value of "unicode" .
    """
    # Dedent all lines of `source`.
    source = trim_docstring(source)

    settings_overrides = {
        'raw_enabled': 0,  # no raw HTML code
        'file_insertion_enabled': 0,  # no file/URL access
        'halt_level': 2,  # at warnings or errors, raise an exception
        'report_level': 5,  # never report problems with the reST code
    }

    parts = None

    # Convert reStructuredText to HTML using Docutils.
    document = publish_doctree(source=source,
                               settings_overrides=settings_overrides)

    for node in document.traverse():
        if node.tagname == '#text':
            continue
        if node.hasattr('refuri'):
            uri = node['refuri']
        elif node.hasattr('uri'):
            uri = node['uri']
        else:
            continue
        o = urlparse(uri)
        if o.scheme not in ALLOWED_SCHEMES:
            raise TransformError('link scheme not allowed: {0}'.format(uri))

    # now turn the transformed document into HTML
    reader = readers.doctree.Reader(parser_name='null')
    pub = Publisher(reader,
                    source=io.DocTreeInput(document),
                    destination_class=io.StringOutput)
    pub.set_writer('html')
    pub.process_programmatic_settings(None, settings_overrides, None)
    pub.set_destination(None, None)
    pub.publish()
    parts = pub.writer.parts

    output = parts['body']

    if output_encoding != 'unicode':
        output = output.encode(output_encoding)

    return output
예제 #21
0
파일: build.py 프로젝트: wyanez/tribus
 def run(self):
     log.debug("[%s.%s] Compiling manual from RST sources." %
               (__name__, self.__class__.__name__))
     pub = Publisher(writer=manpage.Writer())
     pub.set_components(reader_name='standalone',
                        parser_name='restructuredtext',
                        writer_name='pseudoxml')
     pub.publish(argv=[
         u'%s' % get_path([DOCDIR, 'man', 'tribus.rst']),
         u'%s' % get_path([DOCDIR, 'man', 'tribus.1'])
     ])
예제 #22
0
    def get_html(self, body_only=True, content_only=False, noclasses=False):
        import sys
        import pygments_rest
        from docutils.core import Publisher
        from docutils.io import StringInput, StringOutput
        from cStringIO import StringIO

        settings = {
            'doctitle_xform': 1,
            'pep_references': 1,
            'rfc_references': 1,
            'footnote_references': 'superscript',
            'output_encoding': 'unicode',
            'report_level':
            2,  # 2=show warnings, 3=show only errors, 5=off (docutils.utils
        }

        if content_only:
            post_rst = self.get_rst(noclasses=noclasses)
        else:
            post_rst = render_to('post_single.rst',
                                 post=self,
                                 noclasses=noclasses)

        pub = Publisher(reader=None,
                        parser=None,
                        writer=None,
                        settings=None,
                        source_class=StringInput,
                        destination_class=StringOutput)

        pub.set_components(reader_name='standalone',
                           parser_name='restructuredtext',
                           writer_name='html')
        pub.process_programmatic_settings(settings_spec=None,
                                          settings_overrides=settings,
                                          config_section=None)
        pub.set_source(post_rst, source_path=self.module_path)
        pub.set_destination(None, None)

        errors_io = StringIO()
        real_stderr = sys.stderr
        sys.stderr = errors_io
        try:
            html_full = pub.publish(enable_exit_status=False)
            html_body = ''.join(pub.writer.html_body)
        finally:
            sys.stderr = real_stderr
        errors = errors_io.getvalue()
        self._process_rest_errors(errors)

        errors_io.close()

        return html_body if body_only else html_full
예제 #23
0
def inspect(
    filename,
    source_path=None,
):
    "returns the document object before any transforms)"
    from docutils.core import Publisher
    pub = Publisher(source_class=io.FileInput, )
    pub.set_reader('standalone', None, "restructuredtext")
    pub.process_programmatic_settings(None, None, None)
    pub.set_source(source_path=source_path)
    pub.set_io()
    return pub.reader.read(pub.source, pub.parser, pub.settings)
예제 #24
0
파일: mkpydoc.py 프로젝트: viest/ctypes
def convert(infilename, outfilename):
    print "converting %s to %s" % (infilename, outfilename)
    pub = Publisher()
    pub.set_components(
        'standalone',  # reader
        'restructuredtext',  # parser
        'latex')  # writer (arg, will be discarded)
    pub.reader = OptikReader()
    pub.writer = PyLaTeXWriter()
    pub.process_programmatic_settings(None, None, None)
    pub.set_source(source_path=infilename)
    pub.set_destination(destination_path=outfilename)
    pub.publish()
예제 #25
0
def process_labels(site, logger, source, post):
    site.processing_labels = True
    pub = Publisher(reader=Reader(), parser=None, writer=None)
    pub.set_components(None, 'restructuredtext', 'html')
    # Reading the file will generate output/errors that we don't care about
    # at this stage. The report_level = 5 means no output
    pub.process_programmatic_settings(
        settings_spec=None,
        settings_overrides={'report_level': 5},
        config_section=None,
    )
    pub.set_source(None, source)
    pub.publish()
    document = pub.document
    site.processing_labels = False

    # Code based on Sphinx std domain
    for name, is_explicit in document.nametypes.items():
        if not is_explicit:
            continue
        labelid = document.nameids[name]
        if labelid is None:
            continue
        node = document.ids[labelid]
        if node.tagname == 'target' and 'refid' in node:
            node = document.ids.get(node['refid'])
            labelid = node['names'][0]
        if node.tagname == 'footnote' or 'refuri' in node or node.tagname.startswith(
                'desc_'):
            continue
        if name in site.ref_labels:
            logger.warn(
                'Duplicate label {dup}, other instance in {other}'.format(
                    dup=name, other=site.ref_labels[name][0]))
        site.anon_ref_labels[name] = post.permalink(), labelid

        def clean_astext(node):
            """Like node.astext(), but ignore images.
            Taken from sphinx.util.nodes"""
            node = node.deepcopy()
            for img in node.traverse(nodes.image):
                img['alt'] = ''
            for raw in node.traverse(nodes.raw):
                raw.parent.remove(raw)
            return node.astext()

        if node.tagname in ('section', 'rubric'):
            sectname = clean_astext(node[0])
        else:
            continue
        site.ref_labels[name] = post.permalink(), labelid, sectname
예제 #26
0
def render_readme_like_pypi(source, output_encoding='unicode'):
    """
    Render a ReST document just like PyPI does.
    """
    # Dedent all lines of `source`.
    source = trim_docstring(source)

    settings_overrides = {
        'raw_enabled': 0,  # no raw HTML code
        'file_insertion_enabled': 0,  # no file/URL access
        'halt_level': 2,  # at warnings or errors, raise an exception
        'report_level': 5,  # never report problems with the reST code
    }

    parts = None

    # Convert reStructuredText to HTML using Docutils.
    document = publish_doctree(source=source,
                               settings_overrides=settings_overrides)

    for node in document.traverse():
        if node.tagname == '#text':
            continue
        if node.hasattr('refuri'):
            uri = node['refuri']
        elif node.hasattr('uri'):
            uri = node['uri']
        else:
            continue
        o = urlparse(uri)
        if o.scheme not in ALLOWED_SCHEMES:
            raise TransformError('link scheme not allowed: {0}'.format(uri))

    # now turn the transformed document into HTML
    reader = readers.doctree.Reader(parser_name='null')
    pub = Publisher(reader,
                    source=io.DocTreeInput(document),
                    destination_class=io.StringOutput)
    pub.set_writer('html')
    pub.process_programmatic_settings(None, settings_overrides, None)
    pub.set_destination(None, None)
    pub.publish()
    parts = pub.writer.parts

    output = parts['body']

    if output_encoding != 'unicode':
        output = output.encode(output_encoding)

    return output
예제 #27
0
def get_doctree(path):
    """
    Obtain a Sphinx doctree from the RST file at ``path``.

    Performs no Releases-specific processing; this code would, ideally, be in
    Sphinx itself, but things there are pretty tightly coupled. So we wrote
    this.

    :param str path: A relative or absolute file path string.

    :returns:
        A two-tuple of the generated ``sphinx.application.Sphinx`` app and the
        doctree (a ``docutils.document`` object).
    """
    root, filename = os.path.split(path)
    docname, _ = os.path.splitext(filename)
    # TODO: this only works for top level changelog files (i.e. ones where
    # their dirname is the project/doc root)
    app = make_app(srcdir=root)
    # Create & init a BuildEnvironment. Mm, tasty side effects.
    app._init_env(freshenv=True)
    env = app.env
    env.update(config=app.config, srcdir=root, doctreedir=app.doctreedir, app=app)
    # Code taken from sphinx.environment.read_doc; easier to manually call
    # it with a working Environment object, instead of doing more random crap
    # to trick the higher up build system into thinking our single changelog
    # document was "updated".
    env.temp_data['docname'] = docname
    env.app = app
    # NOTE: SphinxStandaloneReader API changed in 1.4 :(
    reader_kwargs = {'app': env.app, 'parsers': env.config.source_parsers}
    if sphinx.version_info[:2] < (1, 4):
        del reader_kwargs['app']
    reader = SphinxStandaloneReader(**reader_kwargs)
    pub = Publisher(reader=reader,
                    writer=SphinxDummyWriter(),
                    destination_class=NullOutput)
    pub.set_components(None, 'restructuredtext', None)
    pub.process_programmatic_settings(None, env.settings, None)
    # NOTE: docname derived higher up, from our given path
    src_path = env.doc2path(docname)
    source = SphinxFileInput(app, env, source=None, source_path=src_path,
                             encoding=env.config.source_encoding)
    pub.source = source
    pub.settings._source = src_path
    pub.set_destination(None, None)
    pub.publish()
    return app, pub.document
예제 #28
0
    def _get_publisher(self, source_path):
        extra_params = {
            'initial_header_level': '2',
            'syntax_highlight': 'short',
            'input_encoding': 'utf-8'
        }
        user_params = self.settings.get('DOCUTILS_SETTINGS')
        if user_params:
            extra_params.update(user_params)

        pub = Publisher(destination_class=StringOutput)
        pub.set_components('standalone', 'restructuredtext', 'html')
        pub.writer.translator_class = HTMLTranslator
        pub.process_programmatic_settings(None, extra_params, None)
        pub.set_source(source_path=source_path)
        pub.publish()
        return pub
예제 #29
0
def pypi_rest2html(source, output_encoding="unicode"):
    """
    >>> pypi_rest2html("test!")
    u'<p>test!</p>\n'
    """
    settings_overrides = {
        "raw_enabled": 0,  # no raw HTML code
        "file_insertion_enabled": 0,  # no file/URL access
        "halt_level": 2,  # at warnings or errors, raise an exception
        "report_level": 5,  # never report problems with the reST code
    }

    # Convert reStructuredText to HTML using Docutils.
    document = publish_doctree(source=source,
                               settings_overrides=settings_overrides)

    for node in document.traverse():
        if node.tagname == "#text":
            continue
        if node.hasattr("refuri"):
            uri = node["refuri"]
        elif node.hasattr("uri"):
            uri = node["uri"]
        else:
            continue
        o = urlparse(uri)
        if o.scheme not in ALLOWED_SCHEMES:
            raise TransformError("link scheme not allowed")

    # now turn the transformed document into HTML
    reader = readers.doctree.Reader(parser_name="null")
    pub = Publisher(reader,
                    source=io.DocTreeInput(document),
                    destination_class=io.StringOutput)
    pub.set_writer("html")
    pub.process_programmatic_settings(None, settings_overrides, None)
    pub.set_destination(None, None)
    pub.publish()
    parts = pub.writer.parts

    output = parts["body"]

    if output_encoding != "unicode":
        output = output.encode(output_encoding)

    return output
예제 #30
0
파일: rest.py 프로젝트: leo-dur/doc484
def publish_doctree(source, logger):
    # type: (str, Any) -> Node
    pub = Publisher(reader=None,
                    parser=None,
                    writer=None,
                    settings=None,
                    source_class=io.StringInput,
                    destination_class=io.NullOutput)
    pub.reader = Reader(None, 'restructuredtext')
    pub.reader.doc_logger = logger
    pub.set_writer('null')
    pub.parser = pub.reader.parser
    pub.process_programmatic_settings(None, None, None)
    pub.set_source(source, None)
    pub.set_destination(None, None)
    output = pub.publish(enable_exit_status=False)
    return pub.document