2023-02-05 01:10:09 +01:00
|
|
|
# -*- coding: utf-8 -*-
|
2023-03-04 03:17:09 +01:00
|
|
|
""" Import lektor plugin stuff """
|
2023-02-05 01:10:09 +01:00
|
|
|
from lektor.pluginsystem import Plugin
|
2023-03-04 03:17:09 +01:00
|
|
|
from lektor.project import Project
|
|
|
|
from lektor.context import get_ctx
|
2023-02-05 01:10:09 +01:00
|
|
|
|
2023-03-04 03:17:09 +01:00
|
|
|
# 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'}))
|
2023-02-05 01:10:09 +01:00
|
|
|
|
|
|
|
class RenderTemplatePlugin(Plugin):
|
2023-03-04 03:17:09 +01:00
|
|
|
""" Create Plugin and listen to Filter Keyword """
|
2023-02-05 01:10:09 +01:00
|
|
|
name = 'lektor_render_template'
|
2023-03-04 03:17:09 +01:00
|
|
|
description = 'Lektor plugin to generate files form filters.'
|
2023-02-05 01:10:09 +01:00
|
|
|
|
2023-03-04 03:17:09 +01:00
|
|
|
def on_setup_env(self, **extra):
|
|
|
|
""" Create Listener for Filter """
|
|
|
|
self.env.jinja_env.filters['svg'] = generate_from_template
|