Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import os
- from jinja2 import Template
- import yaml
- from docutils.parsers.rst import Directive, directives
- from nikola.plugin_categories import RestExtension
- class TemplatePlugin(RestExtension):
- name = "render_template"
- def set_site(self, site):
- """Set Nikola site."""
- self.site = site
- # Even though listings don't use CodeBlock anymore, I am
- # leaving these to make the code directive work with
- # docutils < 0.9
- TemplateDirective.site = site
- directives.register_directive('template', TemplateDirective)
- Template.folders = 'data' # site.config['DATA']
- return super().set_site(site)
- class TemplateDirective(Directive):
- required_arguments = 1
- option_spec = {'name': directives.unchanged}
- has_content = True
- def run(self):
- """Run render template directive."""
- self.assert_has_content()
- data_path = os.path.join('data', self.arguments.pop(0))
- with open(data_path) as f:
- data = yaml.load(f.read())
- self.state.document.settings.record_dependencies.add(data_path)
- template = Template('\n'.join(self.content))
- self.state_machine.insert_input(template.render(data=data).splitlines(),
- data_path)
- return []
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement