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
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
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)
def init_publisher(): from docutils.core import Publisher from docutils.io import StringOutput p = Publisher(destination_class=StringOutput,writer=g_writer) p.get_settings() p.set_components('standalone', 'restructuredtext', 'html') p.set_destination(None, None) return p
def pypi_render(source): """ Copied (and slightly adapted) from pypi.description_tools """ ALLOWED_SCHEMES = '''file ftp gopher hdl http https imap mailto mms news nntp prospero rsync rtsp rtspu sftp shttp sip sips snews svn svn+ssh telnet wais irc'''.split() 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 } # capture publishing errors, they go to stderr old_stderr = sys.stderr sys.stderr = s = StringIO.StringIO() parts = None try: # 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.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 except: pass sys.stderr = old_stderr # original text if publishing errors occur if parts is None or len(s.getvalue()) > 0: return None else: return parts['body']
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
def __init__(self): # publisher is used for html generation pub = Publisher(None, None, None, settings=None, source_class=StringInput, destination_class=StringOutput) pub.set_components('standalone', 'restructuredtext', 'html') pub.process_programmatic_settings(None, None, None) pub.set_destination(None, None) self.pub = pub
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
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 = StandaloneReader() pub.writer = VerseLaTeXWriter() pub.process_programmatic_settings(None, None, None) pub.set_source(source_path=infilename) pub.set_destination(destination_path=outfilename) pub.publish()
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
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()
class HTMLGenerator: """ Really simple HTMLGenerator starting from publish_parts It reuses the docutils.core.Publisher class, which means it is *not* threadsafe. """ def __init__(self, settings_spec=None, settings_overrides=None, config_section='general'): if settings_overrides is None: settings_overrides = {'report_level': 5, 'halt_level': 5} 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) def create_xhtml(self, text, destination=None, destination_path=None, enable_exit_status=None): """ Create xhtml for a fragment of IM dialog. We can use the source_name to store info about the message """ self.pub.set_source(text, None) self.pub.set_destination(destination, destination_path) output = self.pub.publish(enable_exit_status=enable_exit_status) # kludge until we can get docutils to stop generating (rare) # entities return '\u00a0'.join( self.pub.writer.parts['fragment'].strip().split(' '))
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
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
def check_rst(self, rst): pub = Publisher(reader=None, parser=None, writer=None, settings=None, source_class=io.StringInput, destination_class=io.StringOutput) pub.set_components(reader_name='standalone', parser_name='restructuredtext', writer_name='pseudoxml') pub.process_programmatic_settings( settings_spec=None, settings_overrides={'output_encoding': 'unicode'}, config_section=None, ) pub.set_source(rst, source_path=None) pub.set_destination(destination=None, destination_path=None) output = pub.publish(enable_exit_status=False) self.assertLess(pub.document.reporter.max_level, 0) return output, pub
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
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
def render_map(builder, node): if node: doc = new_document(b('<partial node>')) doc.append(node) publisher = Publisher( source_class = DocTreeInput, destination_class=StringOutput) publisher.set_components('standalone', 'restructuredtext', 'pseudoxml') publisher.reader = DoctreeReader() publisher.writer = DitaMapWriter(builder) publisher.process_programmatic_settings(None, {'output_encoding': 'utf-8'}, None) publisher.set_source(doc, None) publisher.set_destination(None, None) publisher.publish() return publisher.writer.output output = XML_HEAD output += u"<map></map>" return output
class HTMLGenerator: '''Really simple HTMLGenerator starting from publish_parts. It reuses the docutils.core.Publisher class, which means it is *not* threadsafe. ''' 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'] = ( u'\u2014', '') self.pub.process_programmatic_settings(settings_spec, settings_overrides, config_section) def create_xhtml(self, text, destination=None, destination_path=None, enable_exit_status=None): ''' Create xhtml for a fragment of IM dialog. We can use the source_name to store info about the message.''' self.pub.set_source(text, None) self.pub.set_destination(destination, destination_path) output = self.pub.publish(enable_exit_status=enable_exit_status) # kludge until we can get docutils to stop generating (rare) # entities return u'\u00a0'.join(self.pub.writer.parts['fragment'].strip().split( ' '))
def aoeuaoeuaoeu_docs(f): ''' I couldn't figure out how to use the docutils docstring parser, so I wrote my own. Can somebody show me the right way to do this? ''' raise NotImplementedError from docutils.core import Publisher from docutils.io import StringInput pub = Publisher(None, None, None, settings = settings, source_class = StringInput, destination_class = destination_class) pub.set_components('standalone', 'restructuredtext', 'pseudoxml') pub.process_programmatic_settings( settings_spec, settings_overrides, config_section) pub.set_source(f.__doc__, f.__name__) pub.set_destination(None, f.__name__) output = pub.publish(enable_exit_status = False) return output, pub return publish_parts(f.__doc__, source_class = StringInput, source_path = f.__name__)
def parts_from_doctree(document, destination_path=None, writer=Writer(), writer_name='html4css2', settings=None, settings_spec=None, settings_overrides=None, config_section=None, enable_exit_status=None): """ A version of :func:`docutils.core.publish_from_doctree` that supplies the "parts" rather than a regular string representation. """ reader = Reader(parser_name='null') pub = Publisher(reader, None, writer, source=io.DocTreeInput(document), destination_class=io.StringOutput, settings=settings) if not writer and writer_name: pub.set_writer(writer_name) pub.process_programmatic_settings( settings_spec, settings_overrides, config_section) pub.set_destination(None, destination_path) output = pub.publish() return pub.writer.parts
def parts_from_doctree(document, destination_path=None, writer=None, writer_name='pseudoxml', settings=None, settings_spec=None, settings_overrides=None, config_section=None, enable_exit_status=None): """ Set up & run a `Publisher` to render from an existing document tree data structure, for programmatic use with string I/O. Return the encoded string output. Note that document.settings is overridden; if you want to use the settings of the original `document`, pass settings=document.settings. Also, new document.transformer and document.reporter objects are generated. For encoded string output, be sure to set the 'output_encoding' setting to the desired encoding. Set it to 'unicode' for unencoded Unicode string output. Here's one way:: publish_from_doctree( ..., settings_overrides={'output_encoding': 'unicode'}) Parameters: `document` is a `docutils.nodes.document` object, an existing document tree. Other parameters: see `publish_programmatically`. """ reader = docutils.readers.doctree.Reader(parser_name='null') pub = Publisher(reader, None, writer, source=io.DocTreeInput(document), destination_class=io.StringOutput, settings=settings) if not writer and writer_name: pub.set_writer(writer_name) pub.process_programmatic_settings( settings_spec, settings_overrides, config_section) pub.set_destination(None, destination_path) pub.publish(enable_exit_status=enable_exit_status) return pub.writer.parts
def sandbox_partial_builder(doctree, env): env.process_downloads(env.docname, doctree) for domain in env.domains.values(): domain.process_doc(env, env.docname, doctree) env.resolve_references(doctree, env.docname, env.app.builder) if not hasattr(env.app.builder, 'dlpath'): # FIXME: builders.html.StandaloneHTMLBuilder.write_doc で設定される属性のため、この時点では存在しない env.app.builder.dlpath = '_downloads' env.app.builder.fignumbers = env.toc_secnumbers.get(env.docname, {}) env.app.builder.secnumbers = env.toc_secnumbers.get(env.docname, {}) pub = Publisher( reader=SandboxDoctreeReader(), writer=Writer(env.app.builder), source=DocTreeInput(doctree), destination_class=StringOutput, ) pub.set_components(None, 'restructuredtext', None) defaults = env.settings.copy() defaults['output_encoding'] = 'unicode' pub.process_programmatic_settings(None, defaults, None) pub.set_destination(None, None) out = pub.publish(enable_exit_status=False) return pub.writer.parts['fragment']
def write_doc(self, docname, doctree): outfilename = path.join(self.outdir, self.file_transform(docname)) logger.debug(outfilename) reader = docutils.readers.doctree.Reader(parser_name='null') #Note, want to use our OdtWriter class here - but it doesn't work yet writer = Writer() pub = Publisher(reader, None, writer, settings=None, source=io.DocTreeInput(doctree), destination_class=io.BinaryFileOutput) settings_spec = None settings_overrides = {'output_encoding': 'unicode'} config_section = None pub.process_programmatic_settings(settings_spec, settings_overrides, config_section) destination = None destination_path = outfilename pub.set_destination(destination, destination_path) output = pub.publish(enable_exit_status=False)
class _PydocParser: def __init__(self): # Set up the instance we'll be using to render docstrings. self.errors = [] self.writer = _DocumentPseudoWriter() self.publisher = Publisher(_EpydocReader(self.errors), writer=self.writer, source_class=io.StringInput) self.publisher.set_components('standalone', 'restructuredtext', 'pseudoxml') settings_overrides={ 'report_level':10000, 'halt_level':10000, 'warning_stream':None, } self.publisher.process_programmatic_settings(None, settings_overrides, None) self.publisher.set_destination() def parse_docstring(self, docstring, errors): """Parse a docstring for eventual transformation into HTML This function is a replacement for parse_docstring from epydoc.markup.restructuredtext.parse_docstring. This function reuses the Publisher instance while the original did not. Using This function yields significantly faster WADL generation for complex systems. """ # Clear any errors from previous calls. del self.errors[:] self.publisher.set_source(docstring, None) self.publisher.publish() # Move any errors into the caller-provided list. errors[:] = self.errors[:] return ParsedRstDocstring(self.writer.document)
print 'symbol_footnotes', pformat(document.symbol_footnotes) print 'symbol_footnote_refs', pformat(document.symbol_footnote_refs) print 'footnotes', pformat(document.footnotes) print 'citations', pformat(document.citations) print 'autofootnote_start', pformat(document.autofootnote_start) print 'symbol_footnote_start', pformat(document.symbol_footnote_start) print 'id_start', pformat(document.id_start) print 'transform_messages', pformat(document.transform_messages) print 'transformer', pformat(document.transformer) print 'decoration', pformat(document.decoration) sys.exit() #dump(document) ### 2. Render doctree to output format, ### apply completion transforms. pub.source = io.DocTreeInput(document) pub.destination_class = io.FileOutput pub.set_destination() pub.reader = doctree.Reader(parser_name='null') pub.writer = Writer() pub.apply_transforms() output = pub.writer.write(pub.document, pub.destination) #pub.writer.assemble_parts() # ----
def read_doc(self, docname, app=None): # type: (unicode, Sphinx) -> None """Parse a file and add/update inventory entries for the doctree.""" self.temp_data['docname'] = docname # defaults to the global default, but can be re-set in a document self.temp_data['default_domain'] = \ self.domains.get(self.config.primary_domain) self.settings['input_encoding'] = self.config.source_encoding self.settings['trim_footnote_reference_space'] = \ self.config.trim_footnote_reference_space self.settings['gettext_compact'] = self.config.gettext_compact docutilsconf = path.join(self.srcdir, 'docutils.conf') # read docutils.conf from source dir, not from current dir OptionParser.standard_config_files[1] = docutilsconf if path.isfile(docutilsconf): self.note_dependency(docutilsconf) with sphinx_domains(self): if self.config.default_role: role_fn, messages = roles.role(self.config.default_role, english, 0, dummy_reporter) if role_fn: roles._roles[''] = role_fn else: logger.warning('default role %s not found', self.config.default_role, location=docname) codecs.register_error('sphinx', self.warn_and_replace) # type: ignore # publish manually reader = SphinxStandaloneReader(self.app, parsers=self.config.source_parsers) pub = Publisher(reader=reader, writer=SphinxDummyWriter(), destination_class=NullOutput) pub.set_components(None, 'restructuredtext', None) pub.process_programmatic_settings(None, self.settings, None) src_path = self.doc2path(docname) source = SphinxFileInput(app, self, source=None, source_path=src_path, encoding=self.config.source_encoding) pub.source = source pub.settings._source = src_path pub.set_destination(None, None) pub.publish() doctree = pub.document # post-processing for domain in itervalues(self.domains): domain.process_doc(self, docname, doctree) # allow extension-specific post-processing if app: app.emit('doctree-read', doctree) # store time of reading, for outdated files detection # (Some filesystems have coarse timestamp resolution; # therefore time.time() can be older than filesystem's timestamp. # For example, FAT32 has 2sec timestamp resolution.) self.all_docs[docname] = max( time.time(), path.getmtime(self.doc2path(docname))) if self.versioning_condition: old_doctree = None if self.versioning_compare: # get old doctree try: with open(self.doc2path(docname, self.doctreedir, '.doctree'), 'rb') as f: old_doctree = pickle.load(f) except EnvironmentError: pass # add uids for versioning if not self.versioning_compare or old_doctree is None: list(add_uids(doctree, self.versioning_condition)) else: list(merge_doctrees( old_doctree, doctree, self.versioning_condition)) # make it picklable doctree.reporter = None doctree.transformer = None doctree.settings.warning_stream = None doctree.settings.env = None doctree.settings.record_dependencies = None # cleanup self.temp_data.clear() self.ref_context.clear() roles._roles.pop('', None) # if a document has set a local default role # save the parsed doctree doctree_filename = self.doc2path(docname, self.doctreedir, '.doctree') ensuredir(path.dirname(doctree_filename)) with open(doctree_filename, 'wb') as f: pickle.dump(doctree, f, pickle.HIGHEST_PROTOCOL)
def env_updated(app, env): config = app.builder.config doctree = env.get_doctree(config.master_doc) from sphinx import addnodes toctrees = [] for toctreenode in doctree.traverse(addnodes.toctree): toctree = env.resolve_toctree(config.master_doc, app.builder, toctreenode, prune = False, includehidden = True, maxdepth = 0, collapse = False) toctrees.append(toctree) if not toctrees: toc = None else: toc = toctrees[0] for toctree in toctrees[1:]: toc.extend(toctree.children) # toc = env.get_toctree_for(config.master_doc, app.builder, False) node = toc doc = new_document(b('<partial node>')) doc.append(node) pub = Publisher( source_class = DocTreeInput, destination_class=StringOutput) pub.set_components('standalone', 'restructuredtext', 'pseudoxml') pub.reader = DoctreeReader() pub.writer = Writer() pub.writer.format = 'pseudoxml' pub.process_programmatic_settings( None, {'output_encoding': 'unicode'}, None) pub.set_source(doc, None) pub.set_destination(None, None) pub.publish() import xml.etree.cElementTree as ET from cStringIO import StringIO #out = re.sub(r'^<!DOCTYPE[^>]*>\s*', '<?xml version="1.0"?>', pub.writer.output) out = pub.writer.output.encode('utf-8').replace(' encoding="unicode"', ' encoding="utf-8"') #pprint.pprint(out) doctree = ET.fromstring(out) #pprint.pprint(doctree) #pprint.pprint(dir(doctree)) if hasattr(doctree, 'getroot'): doctree = doctree.getroot() #root = doctree.getroot() fuzzy_find_entries = [] docs = {} def indexentries(entry, links, cap = ''): if links: fuzzy_find_entries.append(dict( href = links[0][1], name = entry, path = "index/%s/%s" % (char,cap), info = "INDEX", detail = '', )) for i, (ismain, link) in enumerate(links[1:], start=1): doclink = link.split('#', 1)[0] docname = docs.get(doclink, i) fuzzy_find_entries.append(dict( href = link, name = "%s (%s)" % (entry, docname), path = "index/%s/%s" % (char, cap), info = "INDEX", detail = '', )) return '' else: return entry if app.config.html_findanything_add_topics: if hasattr(doctree, 'iter'): references = doctree.iter('reference') else: references = doctree.getiterator('reference') # fuzzy_find_entries.append(dict( # href = link, # name = entry, # path = "index/%s/" % char, # info = "INDEX", # detail = '', # )) refset = set() for ref in references: refuri = ref.attrib['refuri'] docs[refuri] = ref.text path = "/" if "/" in refuri: path = refuri.rsplit('/', 1)[0]+"/" if '#' in refuri: docname = docs.get(refuri.split('#', 1)[0]) if docname: path += docname + "/" info = 'SECTION' else: info = 'PAGE' e = dict( href = ref.attrib['refuri'], name = ref.text, info = info, path = path, detail = '', ) refid = "%(href)s^%(path)s^%(name)s^%(info)s" % e if refid in refset: continue refset.add(refid) fuzzy_find_entries.append(e) if app.config.html_findanything_add_indexentries: genindex = env.create_index(app.builder) for char,char_list in genindex: for entry, (links, subitems) in char_list: cap = indexentries(entry, links) if subitems: if cap: cap += '/' for subname, sublinks in subitems: indexentries(subname, sublinks, cap=cap) s = json.dumps(fuzzy_find_entries) static_dir = os.path.join(app.builder.outdir, '_static') if not os.path.exists(static_dir): os.makedirs(static_dir) with open(os.path.join(static_dir, 'fuzzyindex.js'), 'wb') as f: f.write("DOCUMENTATION_OPTIONS.FIND_ANYTHING_ENTRIES = %s;" % s); if app.config.html_findanything_use_cached_hits: f.write("DOCUMENTATION_OPTIONS.FIND_ANYTHING_USE_CACHED_HITS = true;") f.write("DOCUMENTATION_OPTIONS.FIND_ANYTHING_WIDTH = '%s';" % app.config.html_findanything_width);
def process_targets(site, logger, source, permalink): """Process the target locations in the reST files.""" site.processing_targets = True reader = Reader() reader.l_settings = {"source": source} with open(source, "r", encoding="utf8") as in_file: data = in_file.read() pub = Publisher( reader=reader, parser=None, writer=None, settings=None, source_class=StringInput, destination_class=StringOutput, ) 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(data, None) pub.set_destination(None, None) pub.publish() document = pub.document site.processing_targets = 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_targets: logger.warn( "Duplicate label {dup}, other instance in {other}".format( dup=name, other=site.ref_targets[name][0])) site.anon_ref_targets[name] = 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_targets[name] = permalink, labelid, sectname
from docutils.core import publish_cmdline, default_description,\ publish_doctree,\ Publisher from dotmpe.du.ext.writer.formresults import Writer as FormResultsWriter #publish_cmdline(reader=FormReader(), writer_name='pseudoxml')#writer=xhtmlform.Writer()) source_path = sys.argv[1] source = open(source_path) #doctree = publish_doctree(source, reader=FormReader())#, writer_name='pseudoxml') #print doctree pub = Publisher(FormReader(), writer=FormResultsWriter(), source_class=io.FileInput, destination_class=io.StringOutput)#, parser, writer, settings=settings, # source_class=source_class, # destination_class=destination_class) pub.set_components(None, 'rst', 'pseudoxml') pub.process_programmatic_settings(None, None, None) # settings_spec, settings_overrides, config_section) pub.set_source(source, source_path) pub.set_destination(None, None)#destination, destination_path) output = pub.publish(enable_exit_status=False) #print pub.document.form_processor.fields #print pub.document.form_processor.messages #print pub.document.form_processor.values print output
def get_doctree(path, **kwargs): """ 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. Any additional kwargs are passed unmodified into an internal `make_app` call. :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). .. versionchanged:: 1.6 Added support for passing kwargs to `make_app`. """ 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, **kwargs) # Create & init a BuildEnvironment. Mm, tasty side effects. app._init_env(freshenv=True) env = app.env # More arity/API changes: Sphinx 1.3/1.4-ish require one to pass in the app # obj in BuildEnvironment.update(); modern Sphinx performs that inside # Application._init_env() (which we just called above) and so that kwarg is # removed from update(). EAFP. kwargs = dict( config=app.config, srcdir=root, doctreedir=app.doctreedir, app=app, ) try: env.update(**kwargs) except TypeError: # Assume newer Sphinx w/o an app= kwarg del kwargs['app'] env.update(**kwargs) # 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': app, 'parsers': env.config.source_parsers, } if sphinx.version_info[:2] < (1, 4): del reader_kwargs['app'] # This monkeypatches (!!!) docutils to 'inject' all registered Sphinx # domains' roles & so forth. Without this, rendering the doctree lacks # almost all Sphinx magic, including things like :ref: and :doc:! with sphinx_domains(env): 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
def get_doctree(path, **kwargs): """ 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. Any additional kwargs are passed unmodified into an internal `make_app` call. :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). .. versionchanged:: 1.6 Added support for passing kwargs to `make_app`. """ 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, **kwargs) # Create & init a BuildEnvironment. Mm, tasty side effects. app._init_env(freshenv=True) env = app.env # More arity/API changes: Sphinx 1.3/1.4-ish require one to pass in the app # obj in BuildEnvironment.update(); modern Sphinx performs that inside # Application._init_env() (which we just called above) and so that kwarg is # removed from update(). EAFP. kwargs = dict( config=app.config, srcdir=root, doctreedir=app.doctreedir, app=app, ) try: env.update(**kwargs) except TypeError: # Assume newer Sphinx w/o an app= kwarg del kwargs["app"] env.update(**kwargs) # 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": app, "parsers": env.config.source_parsers, } if sphinx.version_info[:2] < (1, 4): del reader_kwargs["app"] # This monkeypatches (!!!) docutils to 'inject' all registered Sphinx # domains' roles & so forth. Without this, rendering the doctree lacks # almost all Sphinx magic, including things like :ref: and :doc:! with sphinx_domains(env): try: reader = SphinxStandaloneReader(**reader_kwargs) except TypeError: # If we import from io, this happens automagically, not in API del reader_kwargs["parsers"] 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