def run(self): # Load the template template_filename = self.options.get('file') if template_filename: with open(template_filename) as fp: template = fp.read() else: template = '\n'.join(self.content) # Render the template context = self.app.config.jinja_context keys = self.options.get('key', []) for key in keys: context = context[key] context['_jinja_key'] = keys template = Template(template) rst = template.render(**context, header_char=self.options.get('header_char', '=')) if 'debug' in self.options: print(rst) # Parse the generated rst node = nodes.Element() rst = StringList(rst.splitlines()) sphinx.util.nested_parse_with_titles(self.state, rst, node) return node.children
def run(self): node = nodes.Element() node.document = self.state.document env = self.state.document.settings.env docname = env.docname template_filename = self.options.get("file") debug_template = self.options.get("debug") cxt = (self.app.config.jinja_contexts[self.arguments[0]].copy() if self.arguments else {}) cxt["options"] = { "header_char": self.options.get("header_char") } if template_filename: if debug_template is not None: print('') print('********** Begin Jinja Debug Output: Template Before Processing **********') print('********** From {} **********'.format(docname)) reference_uri = directives.uri(os.path.join('source', template_filename)) template_path = urllib.url2pathname(reference_uri) encoded_path = template_path.encode(sys.getfilesystemencoding()) imagerealpath = os.path.abspath(encoded_path) with codecs.open(imagerealpath, encoding='utf-8') as f: print(f.read()) print('********** End Jinja Debug Output: Template Before Processing **********') print('') tpl = Environment( loader=FileSystemLoader( self.app.config.jinja_base, followlinks=True) ).get_template(template_filename) else: if debug_template is not None: print('') print('********** Begin Jinja Debug Output: Template Before Processing **********') print('********** From {} **********'.format(docname)) print('\n'.join(self.content)) print('********** End Jinja Debug Output: Template Before Processing **********') print('') tpl = Environment( loader=FileSystemLoader( self.app.config.jinja_base, followlinks=True) ).from_string('\n'.join(self.content)) new_content = tpl.render(**cxt) if debug_template is not None: print('') print('********** Begin Jinja Debug Output: Template After Processing **********') print(new_content) print('********** End Jinja Debug Output: Template After Processing **********') print('') new_content = StringList(new_content.splitlines(), source='') sphinx.util.nested_parse_with_titles( self.state, new_content, node) return node.children
def run(self) -> Node: """Directive to list all SCM contributors""" contributors = self.get_contibutors() contributors_str = (",\n ".join(contributors) if contributors else "<no SCM contributors found>") new_content = textwrap.dedent("""\ .. sectionauthor:: {contributors}\ """).format(contributors=contributors_str) new_content = StringList(new_content.splitlines(), source="") node = nodes.Element() sphinx.util.nested_parse_with_titles(self.state, new_content, node) return node.children
def run(self): node = nodes.Element() node.document = self.state.document docname = self.state.document.settings.env.docname conf = self.app.config template_filename = self.options.get("file") debug_template = self.options.get("debug") cxt = (conf.jinja_contexts[self.arguments[0]].copy() if self.arguments else {}) cxt["options"] = { "header_char": self.options.get("header_char"), } env = Environment(loader=FileSystemLoader(conf.jinja_base, followlinks=True), **conf.jinja_env_kwargs) env.filters.update(conf.jinja_filters) env.tests.update(conf.jinja_tests) env.globals.update(conf.jinja_globals) env.policies.update(conf.jinja_policies) if template_filename: if debug_template is not None: reference_uri = directives.uri(template_filename) template_path = os.path.join( os.path.abspath(conf.jinja_base), url2pathname(reference_uri), ) encoded_path = template_path.encode( sys.getfilesystemencoding()) with codecs.open(encoded_path, encoding='utf-8') as f: debug_print( 'Template Before Processing', '******* From {} *******\n{}'.format( docname, f.read()), ) tpl = env.get_template(template_filename) else: content = '\n'.join(self.content) if debug_template is not None: debug_print('Template Before Processing', content) tpl = env.from_string(content) new_content = tpl.render(**cxt) if debug_template is not None: debug_print('Template After Processing', new_content) new_content = StringList(new_content.splitlines(), source='') sphinx.util.nested_parse_with_titles(self.state, new_content, node) return node.children
def run(self): tags_arg = self.options.get("tags") tags = {t.strip() for t in tags_arg.split(",")} if tags_arg else None header_separator = self.options.get('header-separator') new_content = self.render_content(tags=tags, header_separator=header_separator) with switch_source_input(self.state, self.content): new_content = StringList(new_content.splitlines(), source='') node = nodes.section() # type: Element # necessary so that the child nodes get the right source/line set node.document = self.state.document nested_parse_with_titles(self.state, new_content, node) # record all filenames as dependencies -- this will at least # partially make automatic invalidation possible for filepath in get_provider_yaml_paths(): self.state.document.settings.record_dependencies.add(filepath) return node.children