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_html(document) -> str: builder = _get_builder() destination = StringOutput(encoding="utf-8") docwriter = HTMLWriter(builder) docsettings = OptionParser(defaults=builder.env.settings, components=(docwriter, ), read_config_files=True).get_default_values() docsettings.compact_lists = True document.settings = docsettings docwriter.write(document, destination) docwriter.assemble_parts() return docwriter.parts["body"]
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 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 []