Create basic lektor plugin
This commit is contained in:
parent
74d1052127
commit
fd3ccbae4d
2 changed files with 28 additions and 8 deletions
|
@ -1,6 +1,7 @@
|
|||
# lektor_render_template
|
||||
|
||||
This is where a description of your plugin goes.
|
||||
Provide usage instructions here.
|
||||
Here will be a lektor plugin that generates files based on templates of your lektor project with filters as input string.
|
||||
|
||||
## Work in progress!
|
||||
## Open Issues:
|
||||
+ Currently the plugin does not support any config variable.
|
||||
+ Currently the plugin generates the files in the assets dir.
|
||||
|
|
|
@ -1,12 +1,31 @@
|
|||
# -*- coding: utf-8 -*-
|
||||
""" Import lektor plugin stuff """
|
||||
from lektor.pluginsystem import Plugin
|
||||
from lektor.project import Project
|
||||
from lektor.context import get_ctx
|
||||
|
||||
# pylint: disable=unused-argument
|
||||
def generate_from_template(inputname, **options):
|
||||
""" Generate a File based on a Jinja2 Templated """
|
||||
# Setup Lektor Env
|
||||
project = Project.discover()
|
||||
env = project.make_env()
|
||||
# Setup CTX
|
||||
ctx = get_ctx()
|
||||
if ctx is not None:
|
||||
pad = ctx.pad
|
||||
else:
|
||||
pad = env.new_pad()
|
||||
# Create File
|
||||
file = f"assets/svg/{inputname}.svg"
|
||||
with open(file, 'w', encoding='UTF-8') as outputfile:
|
||||
outputfile.write(env.render_template('galaxy.svg', pad=pad, this={'title': 'Demo Object'}))
|
||||
|
||||
class RenderTemplatePlugin(Plugin):
|
||||
""" Create Plugin and listen to Filter Keyword """
|
||||
name = 'lektor_render_template'
|
||||
description = u'Add your description here.'
|
||||
description = 'Lektor plugin to generate files form filters.'
|
||||
|
||||
def on_process_template_context(self, context, **extra):
|
||||
def test_function():
|
||||
return 'Value from plugin %s' % self.name
|
||||
context['test_function'] = test_function
|
||||
def on_setup_env(self, **extra):
|
||||
""" Create Listener for Filter """
|
||||
self.env.jinja_env.filters['svg'] = generate_from_template
|
||||
|
|
Loading…
Reference in a new issue