lektor-render-template/lektor_render_template.py

144 lines
4.7 KiB
Python
Raw Normal View History

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-10 04:19:29 +01:00
from scour.scour import scourString
from scour.scour import sanitizeOptions as sanitizeScourOptions
from scour.scour import parse_args as parseScourArgs
def optimize_svg(sourcesvg):
""" using scour to optimize and minify svg """
scouroptions = parseScourArgs([
"--enable-id-stripping",
"--enable-comment-stripping",
"--shorten-ids",
"--indent=none",
"--no-line-breaks"])
scouroptions = sanitizeScourOptions(scouroptions)
optimizedsvg = scourString(sourcesvg, scouroptions)
return optimizedsvg
2023-03-08 02:29:24 +01:00
# pylint: disable=too-many-arguments
2023-03-10 04:19:29 +01:00
def render_template(env, pad, filepath, inputname, inputvalue, color):
2023-03-08 02:29:24 +01:00
""" Render my custom Jinja2 Template """
# Prepare Variables
2023-03-10 04:19:29 +01:00
font_spacing = int(7)
2023-03-08 02:29:24 +01:00
rendered_title = f"{inputvalue}: {inputname}"
2023-03-10 04:19:29 +01:00
center_position = int(len(inputvalue))*font_spacing+15
right_width = int(len(inputname))*font_spacing
overal_width = int((len(inputvalue)+len(inputname))*font_spacing)+int(31)
rendered_sizes = f"width=\"{overal_width}\" height=\"20\" viewBox=\"0 0 {overal_width} 20\""
accent_multiplier = int((overal_width - 121)/font_spacing)
right_box_position = float(int(overal_width - 132)-0.007)
# Create an optimized SVG File
2023-03-08 02:29:24 +01:00
with open(f"assets/{filepath}", 'w', encoding='UTF-8') as outputfile:
2023-03-10 04:19:29 +01:00
outputfile.write(
optimize_svg(env.render_template(
'galaxy.svg', pad=pad, this={
2023-03-08 02:29:24 +01:00
'title': str(rendered_title),
2023-03-10 04:19:29 +01:00
'font_spacing': str(font_spacing),
'center_position': str(center_position),
'right_width': str(right_width),
'right_box_position': str(right_box_position),
'latest_accent': str(right_box_position),
2023-03-08 02:29:24 +01:00
'name': str(inputname),
2023-03-10 04:19:29 +01:00
'accent_multiplier': str(accent_multiplier),
2023-03-08 02:29:24 +01:00
"sizes": str(rendered_sizes),
"inputvalue": str(inputvalue),
2023-03-10 04:19:29 +01:00
"color_higlight": str(color)
})))
2023-03-08 02:29:24 +01:00
2023-03-04 03:17:09 +01:00
# pylint: disable=unused-argument
2023-03-08 02:29:24 +01:00
def generate_galaxy_svg(inputname, **options):
""" Generate a File based on a Jinja2 Templated that will show the ansible galaxy name """
2023-03-04 03:17:09 +01:00
# Setup Lektor Env
project = Project.discover()
env = project.make_env()
2023-03-04 03:17:09 +01:00
# Setup CTX
ctx = get_ctx()
if ctx is not None:
pad = ctx.pad
else:
pad = env.new_pad()
2023-03-08 02:29:24 +01:00
# Create File
filepath = f"/svg/{inputname}.svg"
color = '#FF6600'
2023-03-10 04:19:29 +01:00
render_template(env, pad, filepath, inputname, 'ansible-galaxy', color)
2023-03-08 02:29:24 +01:00
return filepath
# pylint: disable=unused-argument
def generate_maintainance_svg(inputdict, **options):
""" Generate a File based on a Jinja2 Templated that will show the maintainance state """
# 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()
# Sort Input
inputname = str(dict(inputdict)['name'])
inputstate = str(dict(inputdict)['state'])
2023-03-04 03:17:09 +01:00
# Create File
2023-03-08 02:29:24 +01:00
filepath = f"/svg/{inputname}_maintainance.svg"
match inputstate:
case 'well':
color = '#cd02fa'
case 'true':
color = '#8a00a6'
case 'poor':
color = '#5d0070'
case 'false':
color = '#220029'
case _:
color = '#000000'
2023-03-10 04:19:29 +01:00
render_template(env, pad, filepath, inputstate, 'maintainance', color)
2023-03-08 02:29:24 +01:00
return filepath
# pylint: disable=unused-argument
def generate_license_svg(inputdict, **options):
""" Generate a File based on a Jinja2 Templated that will show the used License """
# 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()
# Sort Input
inputname = str(dict(inputdict)['name'])
inputstate = str(dict(inputdict)['state'])
# Create File
filepath = f"/svg/{inputname}_license.svg"
color = '#064ccf'
2023-03-10 04:19:29 +01:00
render_template(env, pad, filepath, inputstate, 'license', color)
2023-03-08 02:29:24 +01:00
return filepath
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.'
def on_setup_env(self, **extra):
""" Create Listener for Filter """
2023-03-08 02:29:24 +01:00
self.env.jinja_env.filters['galaxy'] = generate_galaxy_svg
self.env.jinja_env.filters['maintainance'] = generate_maintainance_svg
self.env.jinja_env.filters['license'] = generate_license_svg