def gen_indices(self): genindex = IndexEntries(self.env).create_index(self) symbols = [] for key, entries in genindex: for column in entries: symbols.append(column[0]) items = [re.sub(self.brack_re, '', sym) \ for sym in symbols if sym not in self.blacklist] symbols = [] flags = [] for sym in items: if sym[0] == '-': flags.append(sym) else: symbols.append(sym) try: completions = path.join(self.outdir, os_path('completion') \ + self.out_suffix) options = path.join(self.outdir, os_path('options') \ + self.out_suffix) with codecs.open(completions, 'w', 'utf-8') as f: for sym in symbols: f.write('* ' + sym + '\n') with codecs.open(options, 'w', 'utf-8') as f: f.write(" ".join(flags)) except (IOError, OSError) as err: self.warn("error writing file %s: %s" % (outfilename, err))
def handle_page(self, pagename, ctx, templatename='page.html', outfilename=None, event_arg=None): # type: (str, Dict, str, str, Any) -> None ctx['current_page_name'] = pagename self.add_sidebars(pagename, ctx) if not outfilename: outfilename = path.join(self.outdir, os_path(pagename) + self.out_suffix) # we're not taking the return value here, since no template is # actually rendered self.app.emit('html-page-context', pagename, templatename, ctx, event_arg) # make context object serializable for key in list(ctx): if isinstance(ctx[key], types.FunctionType): del ctx[key] ensuredir(path.dirname(outfilename)) self.dump_context(ctx, outfilename) # if there is a source file, copy the source file for the # "show source" link if ctx.get('sourcename'): source_name = path.join(self.outdir, '_sources', os_path(ctx['sourcename'])) ensuredir(path.dirname(source_name)) copyfile(self.env.doc2path(pagename), source_name)
def get_outfilename(self, pagename): if pagename == "index" or pagename.endswith(SEP + "index"): outfilename = path.join(self.outdir, os_path(pagename) + self.out_suffix) else: outfilename = path.join(self.outdir, os_path(pagename), "index" + self.out_suffix) return outfilename
def handle_page(self, pagename, ctx, templatename='page.html', outfilename=None, event_arg=None): ctx['current_page_name'] = pagename self.add_sidebars(pagename, ctx) if not outfilename: outfilename = path.join(self.outdir, os_path(pagename) + self.out_suffix) self.app.emit('html-page-context', pagename, templatename, ctx, event_arg) ensuredir(path.dirname(outfilename)) f = open(outfilename, 'wb') try: self.implementation.dump(ctx, f, 2) finally: f.close() # if there is a source file, copy the source file for the # "show source" link if ctx.get('sourcename'): source_name = path.join(self.outdir, '_sources', os_path(ctx['sourcename'])) ensuredir(path.dirname(source_name)) copyfile(self.env.doc2path(pagename), source_name)
def handle_page(self, pagename, ctx, templatename='page.html', outfilename=None, event_arg=None): ctx['current_page_name'] = pagename self.add_sidebars(pagename, ctx) if not outfilename: outfilename = path.join(self.outdir, os_path(pagename) + self.out_suffix) self.app.emit('html-page-context', pagename, templatename, ctx, event_arg) ensuredir(path.dirname(outfilename)) self.dump_context(ctx, outfilename) # if there is a source file, copy the source file for the # "show source" link if ctx.get('sourcename'): source_name = path.join(self.outdir, '_sources', os_path(ctx['sourcename'])) ensuredir(path.dirname(source_name)) copyfile(self.env.doc2path(pagename), source_name)
def get_outfilename(self, pagename): if pagename == 'index' or pagename.endswith(SEP + 'index'): outfilename = path.join(self.outdir, os_path(pagename) + self.out_suffix) else: outfilename = path.join(self.outdir, os_path(pagename), 'index' + self.out_suffix) return outfilename
def write_doc(self, docname, doctree): # work around multiple string % tuple issues in docutils; # replace tuples in attribute values with lists doctree = doctree.deepcopy() destination = docutils.io.StringOutput(encoding="utf-8") ### print an output for downloading notebooks as well with proper links if variable is set if "jupyter_download_nb" in self.config and self.config[ "jupyter_download_nb"]: outfilename = os.path.join(self.outdir + "/_downloads", os_path(docname) + self.out_suffix) ensuredir(os.path.dirname(outfilename)) self.writer._set_ref_urlpath( self.config["jupyter_download_nb_urlpath"]) self.writer._set_jupyter_download_nb_image_urlpath( (self.config["jupyter_download_nb_image_urlpath"])) self.writer.write(doctree, destination) try: with codecs.open(outfilename, "w", "utf-8") as f: f.write(self.writer.output) except (IOError, OSError) as err: self.warn("error writing file %s: %s" % (outfilename, err)) ### output notebooks for executing self.writer._set_ref_urlpath(None) self.writer._set_jupyter_download_nb_image_urlpath(None) self.writer.write(doctree, destination) ### execute the notebook if (self.config["jupyter_execute_notebooks"]): strDocname = str(docname) if strDocname in self.dependency_lists.keys(): self.delayed_notebooks.update({strDocname: self.writer.output}) else: self._execute_notebook_class.execute_notebook( self, self.writer.output, docname, self.futures) else: #do not execute if (self.config['jupyter_generate_html']): nb = nbformat.reads(self.writer.output, as_version=4) language_info = nb.metadata.kernelspec.language self._convert_class = convertToHtmlWriter(self) self._convert_class.convert(nb, docname, language_info, self.outdir) ### mkdir if the directory does not exist outfilename = os.path.join(self.outdir, os_path(docname) + self.out_suffix) ensuredir(os.path.dirname(outfilename)) try: with codecs.open(outfilename, "w", "utf-8") as f: f.write(self.writer.output) except (IOError, OSError) as err: self.logger.warning("error writing file %s: %s" % (outfilename, err))
def handle_page(self, pagename, addctx, templatename="page.html", outfilename=None, event_arg=None): ctx, doc_ctx = self._render_page(pagename, addctx, templatename, event_arg) if not outfilename: outfilename = path.join(self.outdir, "pickles", os_path(pagename) + self.out_suffix) ensuredir(path.dirname(outfilename)) self.dump_context(doc_ctx, outfilename) # if there is a source file, copy the source file for the # "show source" link if ctx.get("sourcename"): source_name = path.join(self.staticdir, "_sources", os_path(ctx["sourcename"])) ensuredir(path.dirname(source_name)) copyfile(self.env.doc2path(pagename), source_name)
def write_doc(self, docname, doctree): self.current_docname = docname destination = StringOutput(encoding='utf-8') self.writer.write(doctree, destination) outfilename = path.join(self.outdir, os_path(docname) + self.out_suffix) ensuredir(path.dirname(outfilename)) try: with codecs.open(outfilename, 'w', 'utf-8') as f: f.write(self.writer.output) self.out_files.append(os_path(docname) + self.out_suffix) except (IOError, OSError) as err: self.warn("error writing file %s: %s" % (outfilename, err)) self.post_process_images(docname, doctree)
def write_doc(self, docname, doctree): # work around multiple string % tuple issues in docutils; # replace tuples in attribute values with lists doctree = doctree.deepcopy() destination = docutils.io.StringOutput(encoding="utf-8") ### output notebooks for executing for single pdfs, the urlpath should be set to website url self.writer._set_ref_urlpath(self.config["tojupyter_pdf_urlpath"]) self.writer._set_tojupyter_image_urlpath(None) self.writer.write(doctree, destination) # get a NotebookNode object from a string nb = nbformat.reads(self.writer.output, as_version=4) nb = self.update_Metadata(nb) ### execute the notebook - keep it forcefully on strDocname = str(docname) if strDocname in self.execution_vars['dependency_lists'].keys(): self.execution_vars['delayed_notebooks'].update({strDocname: nb}) else: self._execute_notebook_class.execute_notebook(self, nb, docname, self.execution_vars, self.execution_vars['futures']) ### mkdir if the directory does not exist outfilename = os.path.join(self.outdir, os_path(docname) + self.out_suffix) ensuredir(os.path.dirname(outfilename)) try: with codecs.open(outfilename, "w", "utf-8") as f: self.writer.output = nbformat.writes(nb, version=4) f.write(self.writer.output) except (IOError, OSError) as err: self.logger.warning("error writing file %s: %s" % (outfilename, err))
def write_doc(self, docname, doctree): # work around multiple string % tuple issues in docutils; # replace tuples in attribute values with lists doctree = doctree.deepcopy() for node in doctree.traverse(nodes.Element): for att, value in list(node.attributes.items()): if isinstance(value, tuple): node.attributes[att] = list(value) value = node.attributes[att] if isinstance(value, list): for i, val in enumerate(value): if isinstance(val, tuple): value[i] = list(val) destination = StringOutput(encoding='utf-8') self.writer.write(doctree, destination) outfilename = path.join(self.outdir, os_path(docname) + self.out_suffix) ensuredir(path.dirname(outfilename)) try: f = codecs.open(outfilename, 'w', 'utf-8') try: f.write(self.writer.output) finally: f.close() except (IOError, OSError) as err: self.warn("error writing file %s: %s" % (outfilename, err))
def run(self): env = self.state.document.settings.env baseurl = env.config.rss_baseurl assert baseurl, "rss_baseurl must be defined in your config.py" source = self.state_machine.input_lines.source(self.lineno - self.state_machine.input_offset - 1) rss_doc = utils.new_document(b("<rss>"), self.state.document.settings) Parser().parse("\n".join(self.content), rss_doc) rst_suffix = env.config.source_suffix path = os.path.relpath(source, env.srcdir).replace(rst_suffix, ".html") builder = env.app.builder docwriter = HTMLWriter(self) docsettings = OptionParser(defaults=env.settings, components=(docwriter,)).get_default_values() docsettings.compact_lists = bool(env.config.html_compact_lists) dest = os.path.join(env.app.outdir, os_path(env.docname) + ".rss") pageurl = "%s/%s" % (baseurl, path) with open(dest, "w") as rss: title = self.options.get("title", "") description = self.options.get("description", None) rss.write('<?xml version="1.0" encoding="ISO-8859-1" ?>\n') rss.write('<rss version="2.0">\n') rss.write("<channel>\n") rss.write("<title>%s</title>\n" % cgi.escape(title)) rss.write("<link>%s</link>\n" % pageurl) if description: rss.write("<description>%s</description>\n" % cgi.escape(description)) for child in rss_doc.children: if not isinstance(child, nodes.section): continue title_index = child.first_child_matching_class(nodes.title) if title_index is None: continue node = nodes.paragraph() node.extend(child.children[title_index + 1 :]) sec_doc = utils.new_document(b("<rss-section>"), docsettings) sec_doc.append(node) visitor = RssTranslator(builder, sec_doc) sec_doc.walkabout(visitor) title = child.children[title_index].astext() sectionurl = "%s#%s" % (pageurl, child.get("ids")[0]) description = "".join(visitor.body) rss.write("<item>\n") rss.write("<title>%s</title>\n" % cgi.escape(title)) rss.write("<link>%s</link>\n" % sectionurl) rss.write("<description><![CDATA[%s]]></description>\n" % description) rss.write("</item>\n") rss.write("</channel>\n") rss.write("</rss>\n") return []
def write_doc(self, docname, doctree): self.current_docname = docname destination = StringOutput(encoding='utf-8') self.writer.write(doctree, destination) outfilename = path.join(self.outdir, os_path(docname) + self.out_suffix) ensuredir(path.dirname(outfilename)) try: f = codecs.open(outfilename, 'w', 'utf-8') try: f.write("#rst2hooktail_source" + (linesep*2)) for link in self.writer.links: f.write(".. _%s: %s%s" % (link.children[0].astext(), link['refuri'], linesep)) f.write(linesep) f.write(self.writer.output) f.write("@@author:%s@@%s" % (self.config.copyright[6:], linesep)) f.write("@@accept:%s@@%s" % (ustrftime("%Y-%m-%d"), linesep)) relations = self.env.collect_relations().get(docname) if relations and relations[0] and relations[0] != "index": f.write("@@category:%s@@%s" % (self.categories[relations[0]], linesep)) f.write("@@id:%s@@%s" % (docname.split('/')[-1], linesep)) finally: f.close() except (IOError, OSError) as err: self.warn("error writing file %s: %s" % (outfilename, err))
def write_doc(self, docname: str, doctree: Node) -> None: # work around multiple string % tuple issues in docutils; # replace tuples in attribute values with lists doctree = doctree.deepcopy() for domain in self.env.domains.values(): xmlns = "xmlns:" + domain.name doctree[xmlns] = "https://www.sphinx-doc.org/" # type: ignore for node in doctree.findall(nodes.Element): for att, value in node.attributes.items(): if isinstance(value, tuple): node.attributes[att] = list(value) value = node.attributes[att] if isinstance(value, list): for i, val in enumerate(value): if isinstance(val, tuple): value[i] = list(val) destination = StringOutput(encoding='utf-8') self.writer.write(doctree, destination) outfilename = path.join(self.outdir, os_path(docname) + self.out_suffix) ensuredir(path.dirname(outfilename)) try: with open(outfilename, 'w', encoding='utf-8') as f: f.write(self.writer.output) except OSError as err: logger.warning(__("error writing file %s: %s"), outfilename, err)
def write_doc(self, docname, doctree): # work around multiple string % tuple issues in docutils; # replace tuples in attribute values with lists doctree = doctree.deepcopy() for node in doctree.traverse(nodes.Element): for att, value in node.attributes.items(): if isinstance(value, tuple): node.attributes[att] = list(value) value = node.attributes[att] if isinstance(value, list): for i, val in enumerate(value): if isinstance(val, tuple): value[i] = list(val) destination = StringOutput(encoding='utf-8') self.writer.write(doctree, destination) outfilename = path.join(self.outdir, os_path(docname) + self.out_suffix) ensuredir(path.dirname(outfilename)) try: f = codecs.open(outfilename, 'w', 'utf-8') try: f.write(self.writer.output) finally: f.close() except (IOError, OSError) as err: self.warn("error writing file %s: %s" % (outfilename, err))
def get_outfilename(self, pagename): """ Same as super()'s, but adds in the current variation as an intermediate directory. """ return os.path.join(self.outdir, self.current_variation[0], os_path(pagename) + self.out_suffix)
def handle_page(self, pagename, addctx, templatename='page.html', outfilename=None, event_arg=None): ctx = self.globalcontext.copy() # current_page_name is backwards compatibility ctx['pagename'] = ctx['current_page_name'] = pagename default_baseuri = self.get_target_uri(pagename) # in the singlehtml builder, default_baseuri still contains an #anchor # part, which relative_uri doesn't really like... default_baseuri = default_baseuri.rsplit('#', 1)[0] def pathto(otheruri, resource=False, baseuri=default_baseuri): if resource and '://' in otheruri: # allow non-local resources given by scheme return otheruri elif not resource: otheruri = self.get_target_uri(otheruri) uri = relative_uri(baseuri, otheruri) or '#' return uri ctx['pathto'] = pathto ctx['hasdoc'] = lambda name: name in self.env.all_docs if self.name != 'htmlhelp': ctx['encoding'] = encoding = self.config.html_output_encoding else: ctx['encoding'] = encoding = self.encoding ctx['toctree'] = lambda **kw: self._get_local_toctree(pagename, **kw) self.add_sidebars(pagename, ctx) ctx.update(addctx) newtmpl = self.app.emit_firstresult('html-page-context', pagename, templatename, ctx, event_arg) if newtmpl: templatename = newtmpl try: output = self.templates.render(templatename, ctx) except UnicodeError: self.warn("a Unicode error occurred when rendering the page %s. " "Please make sure all config values that contain " "non-ASCII content are Unicode strings." % pagename) return if not outfilename: outfilename = self.get_outfilename(pagename) # outfilename's path is in general different from self.outdir ensuredir(path.dirname(outfilename)) try: f = codecs.open(outfilename, 'w', encoding, 'xmlcharrefreplace') try: f.write(output) finally: f.close() except (IOError, OSError) as err: self.warn("error writing file %s: %s" % (outfilename, err)) if self.copysource and ctx.get('sourcename'): # copy the source file for the "show source" link source_name = path.join(self.outdir, '_sources', os_path(ctx['sourcename'])) ensuredir(path.dirname(source_name)) copyfile(self.env.doc2path(pagename), source_name)
def handle_page(self, pagename, ctx, templatename="page.html", outfilename=None, event_arg=None): ctx["current_page_name"] = pagename self.add_sidebars(pagename, ctx) if not outfilename: outfilename = path.join(self.outdir, os_path(pagename) + self.out_suffix) self.app.emit("html-page-context", pagename, templatename, ctx, event_arg) ensuredir(path.dirname(outfilename)) self.dump_context(ctx, outfilename) # if there is a source file, copy the source file for the # "show source" link if ctx.get("sourcename"): source_name = path.join(self.outdir, "_sources", os_path(ctx["sourcename"])) ensuredir(path.dirname(source_name)) copyfile(self.env.doc2path(pagename), source_name)
def handle_page(self, pagename, addctx, templatename='page.html', outfilename=None, event_arg=None): ctx, doc_ctx = self._render_page(pagename, addctx, templatename, event_arg) if not outfilename: outfilename = path.join(self.outdir, 'pickles', os_path(pagename) + self.out_suffix) ensuredir(path.dirname(outfilename)) self.dump_context(doc_ctx, outfilename) # if there is a source file, copy the source file for the # "show source" link if ctx.get('sourcename'): source_name = path.join(self.staticdir, '_sources', os_path(ctx['sourcename'])) ensuredir(path.dirname(source_name)) copyfile(self.env.doc2path(pagename), source_name)
def write_document(self, document_data): data = copy(document_data) target = data.pop('target') logger.info("processing %s... ", target, nonl=1) rinoh_document = self.construct_rinohtype_document(data) outfilename = path.join(self.outdir, os_path(target)) ensuredir(path.dirname(outfilename)) logger.info("rendering... ") rinoh_document.render(outfilename) logger.info("done")
def write_doc(self, docname, doctree): destination = StringOutput(encoding='utf-8') self.writer.write(doctree, destination) outfilename = path.join( self.outdir, os_path(docname) + self.out_suffix) ensuredir(path.dirname(outfilename)) try: self.writer.save(outfilename) except (IOError, OSError), err: self.warn("error writing file %s: %s" % (outfilename, err))
def handle_page(self, pagename, addctx, templatename='page.html', outfilename=None, event_arg=None): # type: (unicode, Dict, unicode, unicode, unicode) -> None ctx, doc_ctx = self._render_page(pagename, addctx, templatename, event_arg) if not outfilename: outfilename = path.join(self.outdir, 'pickles', os_path(pagename) + self.out_suffix) ensuredir(path.dirname(outfilename)) self.dump_context(doc_ctx, outfilename) # if there is a source file, copy the source file for the # "show source" link if ctx.get('sourcename'): source_name = path.join(self.staticdir, '_sources', os_path(ctx['sourcename'])) ensuredir(path.dirname(source_name)) copyfile(self.env.doc2path(pagename), source_name)
def write_doc(self, docname, doctree, docnames, targetname): rinoh_tree = from_doctree(doctree, sphinx_builder=self) template_cfg = self.template_from_config(logger) rinoh_document = template_cfg.document(rinoh_tree) extra_indices = StaticGroupedFlowables(self.generate_indices(docnames)) # TODO: use out-of-line flowables? rinoh_document.insert('back_matter', extra_indices, 0) self.set_document_metadata(rinoh_document, doctree) outfilename = path.join(self.outdir, os_path(targetname)) ensuredir(path.dirname(outfilename)) rinoh_document.render(outfilename)
def write_doc(self, docname, doctree): self.current_docname = docname destination = StringOutput(encoding='utf-8') self.writer.write(doctree, destination) outfilename = path.join(self.outdir, os_path(docname) + self.out_suffix) ensuredir(path.dirname(outfilename)) try: with codecs.open(outfilename, 'w', 'utf-8') as f: f.write(self.writer.output) except (IOError, OSError) as err: self.warn("error writing file %s: %s" % (outfilename, err))
def write_doc(self, docname, doctree, targetname): os.chdir(self.srcdir) parser = ReStructuredTextParser() rinoh_tree = parser.from_doctree(doctree) rinoh_document = Book(rinoh_tree, backend=pdf, options=BookOptions(**self.config.rinoh_options)) rinoh_document.metadata['title'] = doctree.settings.title rinoh_document.metadata['author'] = doctree.settings.author outfilename = path.join(self.outdir, os_path(targetname)) ensuredir(path.dirname(outfilename)) rinoh_document.render(outfilename)
def write_doc(self, docname, doctree): # type: (unicode, nodes.Node) -> None self.current_docname = docname destination = StringOutput(encoding='utf-8') self.writer.write(doctree, destination) outfilename = path.join(self.outdir, os_path(docname) + self.out_suffix) ensuredir(path.dirname(outfilename)) try: with codecs.open(outfilename, 'w', 'utf-8') as f: # type: ignore f.write(self.writer.output) except (IOError, OSError) as err: logger.warning("error writing file %s: %s", outfilename, err)
def write_doc(self, docname, doctree, targetname): os.chdir(self.srcdir) parser = ReStructuredTextReader() rinoh_tree = parser.from_doctree(doctree) rinoh_document = Book(rinoh_tree, backend=pdf, options=BookOptions(**self.config.rinoh_options)) rinoh_document.metadata['title'] = doctree.settings.title rinoh_document.metadata['author'] = doctree.settings.author outfilename = path.join(self.outdir, os_path(targetname)) ensuredir(path.dirname(outfilename)) rinoh_document.render(outfilename)
def build_answerkey(app, answerkey: AnswerKey, _) -> None: """Callback for mcq-answerkey-created to build and write answerkey file. (But only if mcq_build_answerkey is True) """ if not app.config.mcq_build_answerkey: return outpath = path.join(app.builder.outdir, os_path(app.config.mcq_answerkey_file)) answerkey.write_to_file(outpath)
def write_doc(self, docname: str, doctree: Node) -> None: self.current_docname = docname self.secnumbers = self.env.toc_secnumbers.get(docname, {}) destination = StringOutput(encoding='utf-8') self.writer.write(doctree, destination) outfilename = path.join(self.outdir, os_path(docname) + self.out_suffix) ensuredir(path.dirname(outfilename)) try: with open(outfilename, 'w', encoding='utf-8') as f: f.write(self.writer.output) except OSError as err: logger.warning(__("error writing file %s: %s"), outfilename, err)
def write_doc(self, docname, doctree): self.current_docname = docname self.secnumbers = self.env.toc_secnumbers.get(docname, {}) destination = StringOutput(encoding="utf-8") self.writer.write(doctree, destination) outfilename = path.join(self.outdir, os_path(docname) + self.out_suffix) ensuredir(path.dirname(outfilename)) try: with open(outfilename, "w", encoding="utf-8") as f: # type: ignore f.write(self.writer.output) except (IOError, OSError) as err: logger.warning(__("error writing file %s: %s"), outfilename, err)
def write_doc(self, docname, doctree): # type: (unicode, nodes.Node) -> None start, name = docname self.current_docname = start self.fignumbers = self.env.toc_fignumbers.get(start, {}) destination = StringOutput(encoding='utf-8') self.writer.write(doctree, destination) outfilename = path.join(self.outdir, os_path(name) + self.out_suffix) ensuredir(path.dirname(outfilename)) try: self.writer.save(outfilename) except (IOError, OSError) as err: logger.warning("error writing file %s: %s", outfilename, err)
def write_doc(self, docname, doctree): # type: (str, nodes.Node) -> None self.current_docname = docname self.secnumbers = self.env.toc_secnumbers.get(docname, {}) destination = StringOutput(encoding='utf-8') self.writer.write(doctree, destination) outfilename = path.join(self.outdir, os_path(docname) + self.out_suffix) ensuredir(path.dirname(outfilename)) try: with open(outfilename, 'w', encoding='utf-8') as f: f.write(self.writer.output) except OSError as err: logger.warning(__("error writing file %s: %s"), outfilename, err)
def handle_page(self, pagename, ctx, templatename='page.html', outfilename=None, event_arg=None): ctx['current_page_name'] = pagename self.add_sidebars(pagename, ctx) if not outfilename: outfilename = path.join(self.outdir, os_path(pagename) + self.out_suffix) self.app.emit('html-page-context', pagename, templatename, ctx, event_arg) ensuredir(path.dirname(outfilename)) self.dump_context(ctx, outfilename) # if there is a source file, copy the source file for the # "show source" link if ctx.get('sourcename'): source_name = path.join(self.outdir, self.config.html_source_output_dir, os_path(ctx['sourcename'])) ensuredir(path.dirname(source_name)) copyfile(self.env.doc2path(pagename), source_name)
def write_doc(self, docname: str, doctree: Node) -> None: self.current_docname = docname destination = StringOutput(encoding="utf-8") self.writer.write(doctree, destination) src_folder = self.srcdir.split(self.confdir)[1][1:] outdir = path.join(self.outdir, src_folder) outfilename = path.join(outdir, os_path(docname) + self.out_suffix) ensuredir(path.dirname(outfilename)) try: with open(outfilename, "w", encoding="utf-8") as f: f.write(self.writer.output) except OSError as err: logger.warning(__("error writing file %s: %s"), outfilename, err)
def write_doc(self, docname, doctree): self.docname = docname destination = StringOutput(encoding='utf-8') self.writer.write(doctree, destination) outfilename = path.join(self.outdir, os_path(docname) + self.out_suffix) ensuredir(path.dirname(outfilename)) try: f = codecs.open(outfilename, 'w', 'utf-8') try: f.write(self.writer.output) finally: f.close() except (IOError, OSError), err: self.warn("error writing file %s: %s" % (outfilename, err))
def write_doc(self, docname, doctree, docnames, targetname): config = self.config rinoh_tree = from_doctree(doctree['source'], doctree, source_root=Path(self.srcdir), sphinx_builder=self) template_cfg = template_from_config(config, self.confdir, logger.warning) rinoh_document = template_cfg.document(rinoh_tree) extra_indices = StaticGroupedFlowables(self.generate_indices(docnames)) rinoh_document.insert('back_matter', extra_indices, 0) set_document_metadata(rinoh_document, config, doctree) outfilename = path.join(self.outdir, os_path(targetname)) ensuredir(path.dirname(outfilename)) rinoh_document.render(outfilename)
def run(self) -> List[Node]: if 'file' in self.options and self.options['file'].startswith((SEP, os.sep)): env = self.state.document.settings.env filename = self.options['file'] if path.exists(filename): logger.warning(__('":file:" option for csv-table directive now recognizes ' 'an absolute path as a relative path from source directory. ' 'Please update your document.'), location=(env.docname, self.lineno)) else: abspath = path.join(env.srcdir, os_path(self.options['file'][1:])) docdir = path.dirname(env.doc2path(env.docname)) self.options['file'] = relpath(abspath, docdir) return super().run()
def write_doc(self, docname, doctree): # work around multiple string % tuple issues in docutils; # replace tuples in attribute values with lists doctree = doctree.deepcopy() destination = docutils.io.StringOutput(encoding="utf-8") self.writer.write(doctree, destination) outfilename = os.path.join(self.outdir, os_path(docname) + self.out_suffix) # mkdir if the directory does not exist ensuredir(os.path.dirname(outfilename)) try: with codecs.open(outfilename, "w", "utf-8") as f: f.write(self.writer.output) except (IOError, OSError) as err: self.warn("error writing file %s: %s" % (outfilename, err))
def write(self, *ignored): docnames = self.env.all_docs self.info(bold('preparing documents...'), nonl=True) self.prepare_writing(docnames) self.info('done') self.info(bold('assembling single document... '), nonl=True) doctree = self.assemble_doctree() self.info('done') self.info() self.info(bold('writing... '), nonl=True) self.write_doc(self.config.master_doc, doctree) outfile = path.join(self.outdir, os_path(self.config.master_doc) + self.out_suffix) ensuredir(path.dirname(outfile)) self.write_file(outfile, self.writer.output) self.info('done')
def write_doc(self, docname, doctree, docnames, targetname): config = self.config parser = ReStructuredTextReader() rinoh_tree = parser.from_doctree(doctree['source'], doctree) template_cfg = template_from_config(config, self.confdir, self.warn) rinoh_document = template_cfg.document(rinoh_tree) extra_indices = StaticGroupedFlowables(self.generate_indices(docnames)) rinoh_document.insert('back_matter', extra_indices, 0) rinoh_logo = config.rinoh_logo if rinoh_logo: rinoh_document.metadata['logo'] = rinoh_logo rinoh_document.metadata['title'] = doctree.settings.title rinoh_document.metadata['subtitle'] = ('Release {}' .format(config.release)) rinoh_document.metadata['author'] = doctree.settings.author outfilename = path.join(self.outdir, os_path(targetname)) ensuredir(path.dirname(outfilename)) rinoh_document.render(outfilename)
def handle_page(self, pagename, ctx, templatename='page.html', outfilename=None, event_arg=None): """Serializing builder copies source files even when configuration setting *html_copy_source* is **False**. This overwrites that function to avoid making a copy of the source.""" ctx['current_page_name'] = pagename self.add_sidebars(pagename, ctx) if not outfilename: outfilename = os.path.join(self.outdir, os_path(pagename) + self.out_suffix) self.app.emit('html-page-context', pagename, templatename, ctx, event_arg) ensuredir(os.path.dirname(outfilename)) self.dump_context(ctx, outfilename)
def relfn2path(self, filename: str, docname: str = None) -> Tuple[str, str]: """Return paths to a file referenced from a document, relative to documentation root and absolute. In the input "filename", absolute filenames are taken as relative to the source dir, while relative filenames are relative to the dir of the containing document. """ filename = os_path(filename) if filename.startswith('/') or filename.startswith(os.sep): rel_fn = filename[1:] else: docdir = path.dirname(self.doc2path(docname or self.docname, base=None)) rel_fn = path.join(docdir, filename) return (canon_path(path.normpath(rel_fn)), path.normpath(path.join(self.srcdir, rel_fn)))
def write_doc(self, docname, doctree, docnames, targetname): config = self.config suffix, = config.source_suffix source_path = os.path.join(self.srcdir, docname + suffix) parser = ReStructuredTextReader() rinoh_tree = parser.from_doctree(source_path, doctree) rinoh_document_template = config.rinoh_document_template template = (DocumentTemplate.from_string(rinoh_document_template) if isinstance(rinoh_document_template, str) else rinoh_document_template) if isinstance(self.config.rinoh_stylesheet, str): stylesheet = StyleSheet.from_string(self.config.rinoh_stylesheet) elif self.config.rinoh_stylesheet is None: stylesheet = template.Configuration.stylesheet.default_value else: stylesheet = self.config.rinoh_stylesheet if config.pygments_style is not None: stylesheet = pygments_style_to_stylesheet(config.pygments_style, stylesheet) paper_size = config.rinoh_paper_size base_config = template.Configuration(paper_size=paper_size, stylesheet=stylesheet) if config.rinoh_template_configuration is not None: template_configuration = config.rinoh_template_configuration if template_configuration.base is None: template_configuration.base = base_config else: template_configuration = base_config rinoh_document = template(rinoh_tree, configuration=template_configuration, backend=pdf) extra_indices = StaticGroupedFlowables(self.generate_indices(docnames)) rinoh_document.insert('indices', extra_indices, 0) rinoh_logo = config.rinoh_logo if rinoh_logo: rinoh_document.metadata['logo'] = rinoh_logo rinoh_document.metadata['title'] = doctree.settings.title rinoh_document.metadata['subtitle'] = ('Release {}'.format( config.release)) rinoh_document.metadata['author'] = doctree.settings.author outfilename = path.join(self.outdir, os_path(targetname)) ensuredir(path.dirname(outfilename)) rinoh_document.render(outfilename)
def write_doc(self, docname, doctree): # type: (str, nodes.Node) -> None # work around multiple string % tuple issues in docutils; # replace tuples in attribute values with lists doctree = doctree.deepcopy() for node in doctree.traverse(nodes.Element): for att, value in node.attributes.items(): if isinstance(value, tuple): node.attributes[att] = list(value) value = node.attributes[att] if isinstance(value, list): for i, val in enumerate(value): if isinstance(val, tuple): value[i] = list(val) destination = StringOutput(encoding='utf-8') self.writer.write(doctree, destination) outfilename = path.join(self.outdir, os_path(docname) + self.out_suffix) ensuredir(path.dirname(outfilename)) try: with open(outfilename, 'w', encoding='utf-8') as f: f.write(self.writer.output) except OSError as err: logger.warning(__("error writing file %s: %s"), outfilename, err)
def write(self, *ignored): version = self.config.version libchanges = {} apichanges = [] otherchanges = {} if version not in self.env.versionchanges: self.info(bold('no changes in version %s.' % version)) return self.info(bold('writing summary file...')) for type, docname, lineno, module, descname, content in \ self.env.versionchanges[version]: if isinstance(descname, tuple): descname = descname[0] ttext = self.typemap[type] context = content.replace('\n', ' ') if descname and docname.startswith('c-api'): if not descname: continue if context: entry = '<b>%s</b>: <i>%s:</i> %s' % (descname, ttext, context) else: entry = '<b>%s</b>: <i>%s</i>.' % (descname, ttext) apichanges.append((entry, docname, lineno)) elif descname or module: if not module: module = _('Builtins') if not descname: descname = _('Module level') if context: entry = '<b>%s</b>: <i>%s:</i> %s' % (descname, ttext, context) else: entry = '<b>%s</b>: <i>%s</i>.' % (descname, ttext) libchanges.setdefault(module, []).append((entry, docname, lineno)) else: if not context: continue entry = '<i>%s:</i> %s' % (ttext.capitalize(), context) title = self.env.titles[docname].astext() otherchanges.setdefault((docname, title), []).append( (entry, docname, lineno)) ctx = { 'project': self.config.project, 'version': version, 'docstitle': self.config.html_title, 'shorttitle': self.config.html_short_title, 'libchanges': sorted(iteritems(libchanges)), 'apichanges': sorted(apichanges), 'otherchanges': sorted(iteritems(otherchanges)), 'show_copyright': self.config.html_show_copyright, 'show_sphinx': self.config.html_show_sphinx, } f = codecs.open(path.join(self.outdir, 'index.html'), 'w', 'utf8') try: f.write(self.templates.render('changes/frameset.html', ctx)) finally: f.close() f = codecs.open(path.join(self.outdir, 'changes.html'), 'w', 'utf8') try: f.write(self.templates.render('changes/versionchanges.html', ctx)) finally: f.close() hltext = ['.. versionadded:: %s' % version, '.. versionchanged:: %s' % version, '.. deprecated:: %s' % version] def hl(no, line): line = '<a name="L%s"> </a>' % no + htmlescape(line) for x in hltext: if x in line: line = '<span class="hl">%s</span>' % line break return line self.info(bold('copying source files...')) for docname in self.env.all_docs: f = codecs.open(self.env.doc2path(docname), 'r', self.env.config.source_encoding) try: lines = f.readlines() except UnicodeDecodeError: self.warn('could not read %r for changelog creation' % docname) continue finally: f.close() targetfn = path.join(self.outdir, 'rst', os_path(docname)) + '.html' ensuredir(path.dirname(targetfn)) f = codecs.open(targetfn, 'w', 'utf-8') try: text = ''.join(hl(i+1, line) for (i, line) in enumerate(lines)) ctx = { 'filename': self.env.doc2path(docname, None), 'text': text } f.write(self.templates.render('changes/rstsource.html', ctx)) finally: f.close() themectx = dict(('theme_' + key, val) for (key, val) in iteritems(self.theme.get_options({}))) copy_static_entry(path.join(package_dir, 'themes', 'default', 'static', 'default.css_t'), self.outdir, self, themectx) copy_static_entry(path.join(package_dir, 'themes', 'basic', 'static', 'basic.css'), self.outdir, self)
def run(self): env = self.state.document.settings.env baseurl = env.config.rss_baseurl assert baseurl, 'rss_baseurl must be defined in your config.py' source = self.state_machine.input_lines.source( self.lineno - self.state_machine.input_offset - 1) rss_doc = utils.new_document('<rss>', self.state.document.settings) Parser().parse('\n'.join(self.content), rss_doc) path = os.path.relpath(source, env.srcdir) suffixes = env.config.source_suffix # retain backwards compatibility with sphinx < 1.3 if isinstance(suffixes, basestring): suffixes = [suffixes] for suffix in suffixes: if path.endswith(suffix): path = '%s.html' % path[:-len(suffix)] break builder = env.app.builder docwriter = HTMLWriter(self) docsettings = OptionParser( defaults=env.settings, components=(docwriter,)).get_default_values() docsettings.compact_lists = bool(env.config.html_compact_lists) dest = os.path.join(env.app.outdir, os_path(env.docname) + '.rss') pageurl = '%s/%s' % (baseurl, path) with open(dest, 'w') as rss: title = self.options.get('title', '') description = self.options.get('description', None) rss.write('<?xml version="1.0" encoding="ISO-8859-1" ?>\n') rss.write('<rss version="2.0">\n') rss.write('<channel>\n') rss.write('<title>%s</title>\n' % cgi.escape(title)) rss.write('<link>%s</link>\n' % pageurl) if description: rss.write('<description>%s</description>\n' % cgi.escape(description)) for child in rss_doc.children: if not isinstance(child, nodes.section): continue title_index = child.first_child_matching_class(nodes.title) if title_index is None: continue node = nodes.paragraph() node.extend(child.children[title_index + 1:]) sec_doc = utils.new_document('<rss-section>', docsettings) sec_doc.append(node) visitor = RssTranslator(builder, sec_doc) sec_doc.walkabout(visitor) title = child.children[title_index].astext() sectionurl = '%s#%s' % (pageurl, child.get('ids')[0]) description = ''.join(visitor.body) rss.write('<item>\n') rss.write('<title>%s</title>\n' % cgi.escape(title)) rss.write('<link>%s</link>\n' % sectionurl) rss.write('<description><![CDATA[%s]]></description>\n' % description) rss.write('</item>\n') rss.write('</channel>\n') rss.write('</rss>\n') return []
def write(self, *ignored): version = self.config.version libchanges = {} apichanges = [] otherchanges = {} if version not in self.env.versionchanges: self.info(bold("no changes in version %s." % version)) return self.info(bold("writing summary file...")) for type, docname, lineno, module, descname, content in self.env.versionchanges[version]: if isinstance(descname, tuple): descname = descname[0] ttext = self.typemap[type] context = content.replace("\n", " ") if descname and docname.startswith("c-api"): if not descname: continue if context: entry = "<b>%s</b>: <i>%s:</i> %s" % (descname, ttext, context) else: entry = "<b>%s</b>: <i>%s</i>." % (descname, ttext) apichanges.append((entry, docname, lineno)) elif descname or module: if not module: module = _("Builtins") if not descname: descname = _("Module level") if context: entry = "<b>%s</b>: <i>%s:</i> %s" % (descname, ttext, context) else: entry = "<b>%s</b>: <i>%s</i>." % (descname, ttext) libchanges.setdefault(module, []).append((entry, docname, lineno)) else: if not context: continue entry = "<i>%s:</i> %s" % (ttext.capitalize(), context) title = self.env.titles[docname].astext() otherchanges.setdefault((docname, title), []).append((entry, docname, lineno)) ctx = { "project": self.config.project, "version": version, "docstitle": self.config.html_title, "shorttitle": self.config.html_short_title, "libchanges": sorted(libchanges.iteritems()), "apichanges": sorted(apichanges), "otherchanges": sorted(otherchanges.iteritems()), "show_copyright": self.config.html_show_copyright, "show_sphinx": self.config.html_show_sphinx, } f = codecs.open(path.join(self.outdir, "index.html"), "w", "utf8") try: f.write(self.templates.render("changes/frameset.html", ctx)) finally: f.close() f = codecs.open(path.join(self.outdir, "changes.html"), "w", "utf8") try: f.write(self.templates.render("changes/versionchanges.html", ctx)) finally: f.close() hltext = [".. versionadded:: %s" % version, ".. versionchanged:: %s" % version, ".. deprecated:: %s" % version] def hl(no, line): line = '<a name="L%s"> </a>' % no + htmlescape(line) for x in hltext: if x in line: line = '<span class="hl">%s</span>' % line break return line self.info(bold("copying source files...")) for docname in self.env.all_docs: f = codecs.open(self.env.doc2path(docname), "r", self.env.config.source_encoding) try: lines = f.readlines() finally: f.close() targetfn = path.join(self.outdir, "rst", os_path(docname)) + ".html" ensuredir(path.dirname(targetfn)) f = codecs.open(targetfn, "w", "utf-8") try: text = "".join(hl(i + 1, line) for (i, line) in enumerate(lines)) ctx = {"filename": self.env.doc2path(docname, None), "text": text} f.write(self.templates.render("changes/rstsource.html", ctx)) finally: f.close() themectx = dict(("theme_" + key, val) for (key, val) in self.theme.get_options({}).iteritems()) copy_static_entry( path.join(package_dir, "themes", "default", "static", "default.css_t"), self.outdir, self, themectx ) copy_static_entry(path.join(package_dir, "themes", "basic", "static", "basic.css"), self.outdir, self)
if not outfilename: outfilename = self.get_outfilename(pagename) # outfilename's path is in general different from self.outdir ensuredir(path.dirname(outfilename)) try: f = codecs.open(outfilename, "w", encoding, "xmlcharrefreplace") try: f.write(output) finally: f.close() except (IOError, OSError), err: self.warn("error writing file %s: %s" % (outfilename, err)) if self.copysource and ctx.get("sourcename"): # copy the source file for the "show source" link source_name = path.join(self.outdir, "_sources", os_path(ctx["sourcename"])) ensuredir(path.dirname(source_name)) copyfile(self.env.doc2path(pagename), source_name) def handle_finish(self): self.dump_search_index() self.dump_inventory() def dump_inventory(self): self.info(bold("dumping object inventory... "), nonl=True) f = open(path.join(self.outdir, INVENTORY_FILENAME), "wb") try: f.write( ( u"# Sphinx inventory version 2\n" u"# Project: %s\n"
def write(self, *ignored): # type: (Any) -> None version = self.config.version domain = cast(ChangeSetDomain, self.env.get_domain('changeset')) libchanges = {} # type: Dict[str, List[Tuple[str, str, int]]] apichanges = [] # type: List[Tuple[str, str, int]] otherchanges = {} # type: Dict[Tuple[str, str], List[Tuple[str, str, int]]] changesets = domain.get_changesets_for(version) if not changesets: logger.info(bold(__('no changes in version %s.') % version)) return logger.info(bold(__('writing summary file...'))) for changeset in changesets: if isinstance(changeset.descname, tuple): descname = changeset.descname[0] else: descname = changeset.descname ttext = self.typemap[changeset.type] context = changeset.content.replace('\n', ' ') if descname and changeset.docname.startswith('c-api'): if context: entry = '<b>%s</b>: <i>%s:</i> %s' % (descname, ttext, context) else: entry = '<b>%s</b>: <i>%s</i>.' % (descname, ttext) apichanges.append((entry, changeset.docname, changeset.lineno)) elif descname or changeset.module: module = changeset.module or _('Builtins') if not descname: descname = _('Module level') if context: entry = '<b>%s</b>: <i>%s:</i> %s' % (descname, ttext, context) else: entry = '<b>%s</b>: <i>%s</i>.' % (descname, ttext) libchanges.setdefault(module, []).append((entry, changeset.docname, changeset.lineno)) else: if not context: continue entry = '<i>%s:</i> %s' % (ttext.capitalize(), context) title = self.env.titles[changeset.docname].astext() otherchanges.setdefault((changeset.docname, title), []).append( (entry, changeset.docname, changeset.lineno)) ctx = { 'project': self.config.project, 'version': version, 'docstitle': self.config.html_title, 'shorttitle': self.config.html_short_title, 'libchanges': sorted(libchanges.items()), 'apichanges': sorted(apichanges), 'otherchanges': sorted(otherchanges.items()), 'show_copyright': self.config.html_show_copyright, 'show_sphinx': self.config.html_show_sphinx, } with open(path.join(self.outdir, 'index.html'), 'w', encoding='utf8') as f: f.write(self.templates.render('changes/frameset.html', ctx)) with open(path.join(self.outdir, 'changes.html'), 'w', encoding='utf8') as f: f.write(self.templates.render('changes/versionchanges.html', ctx)) hltext = ['.. versionadded:: %s' % version, '.. versionchanged:: %s' % version, '.. deprecated:: %s' % version] def hl(no, line): # type: (int, str) -> str line = '<a name="L%s"> </a>' % no + html.escape(line) for x in hltext: if x in line: line = '<span class="hl">%s</span>' % line break return line logger.info(bold(__('copying source files...'))) for docname in self.env.all_docs: with open(self.env.doc2path(docname), encoding=self.env.config.source_encoding) as f: try: lines = f.readlines() except UnicodeDecodeError: logger.warning(__('could not read %r for changelog creation'), docname) continue targetfn = path.join(self.outdir, 'rst', os_path(docname)) + '.html' ensuredir(path.dirname(targetfn)) with open(targetfn, 'w', encoding='utf-8') as f: text = ''.join(hl(i + 1, line) for (i, line) in enumerate(lines)) ctx = { 'filename': self.env.doc2path(docname, None), 'text': text } f.write(self.templates.render('changes/rstsource.html', ctx)) themectx = {'theme_' + key: val for (key, val) in self.theme.get_options({}).items()} copy_asset_file(path.join(package_dir, 'themes', 'default', 'static', 'default.css_t'), self.outdir, context=themectx, renderer=self.templates) copy_asset_file(path.join(package_dir, 'themes', 'basic', 'static', 'basic.css'), self.outdir)
def get_outfilename(self, pagename): return path.join(self.outdir, os_path(pagename) + self.out_suffix)
def get_outfilename(self, pagename): if pagename == 'context.xml': return os.path.join(self.outdir, os_path(pagename)) return old_get_outfilename(pagename)
def get_outfilename(self, pagename): if pagename in self.config.html_additional_pages: return os.path.join(self.outdir, os_path(self.config.html_additional_pages[pagename])) return os.path.join(self.outdir, os_path(pagename) + self.out_suffix)
if not outfilename: outfilename = self.get_outfilename(pagename) # outfilename's path is in general different from self.outdir ensuredir(path.dirname(outfilename)) try: f = codecs.open(outfilename, 'w', encoding, 'xmlcharrefreplace') try: f.write(output) finally: f.close() except (IOError, OSError), err: self.warn("error writing file %s: %s" % (outfilename, err)) if self.copysource and ctx.get('sourcename'): # copy the source file for the "show source" link source_name = path.join(self.outdir, '_sources', os_path(ctx['sourcename'])) ensuredir(path.dirname(source_name)) copyfile(self.env.doc2path(pagename), source_name) def handle_finish(self): self.dump_search_index() self.dump_inventory() def dump_inventory(self): self.info(bold('dumping object inventory... '), nonl=True) f = open(path.join(self.outdir, INVENTORY_FILENAME), 'wb') try: f.write((u'# Sphinx inventory version 2\n' u'# Project: %s\n' u'# Version: %s\n' u'# The remainder of this file is compressed using zlib.\n'