update and clean up svg creation

This commit is contained in:
L3D 2023-03-11 22:43:17 +01:00
parent e9e82b6512
commit 37e8805243
Signed by: l3d
GPG key ID: CD08445BFF4313D1
2 changed files with 65 additions and 95 deletions

View file

@ -8,29 +8,28 @@ from scour.scour import scourString
from scour.scour import sanitizeOptions as sanitizeScourOptions from scour.scour import sanitizeOptions as sanitizeScourOptions
from scour.scour import parse_args as parseScourArgs from scour.scour import parse_args as parseScourArgs
def optimize_svg(sourcesvg): def optimize_svg(source_svg):
""" using scour to optimize and minify svg """ """ using scour to optimize and minify svg """
scouroptions = parseScourArgs([ scour_options = parseScourArgs([
"--enable-id-stripping", "--enable-id-stripping",
"--enable-comment-stripping", "--enable-comment-stripping",
"--shorten-ids", "--shorten-ids",
"--indent=none", "--indent=none",
"--no-line-breaks"]) "--no-line-breaks"])
scouroptions = sanitizeScourOptions(scouroptions) scour_options = sanitizeScourOptions(scour_options)
optimizedsvg = scourString(sourcesvg, scouroptions) optimized_svg = scourString(source_svg, scour_options)
return optimizedsvg return optimized_svg
# pylint: disable=too-many-arguments # pylint: disable=too-many-arguments
def render_template(env, pad, filepath, inputname, inputvalue, color): def render_template(env, pad, filepath, label_name, label_value, color):
""" Render my custom Jinja2 Template """ """ Render my custom Jinja2 Template 'galaxy.svg' with the given variables as input and pass it to a svg optimizer """
# Prepare Final Variables for galaxy.svg
# Prepare Variables
font_spacing = int(7) font_spacing = int(7)
rendered_title = f"{inputvalue}: {inputname}" rendered_title = f"{label_value}: {label_name}"
center_position = int(len(inputvalue))*font_spacing+15 center_position = int(len(label_value))*font_spacing+15
right_width = int(len(inputname))*font_spacing right_width = int(len(label_name))*font_spacing
overal_width = int((len(inputvalue)+len(inputname))*font_spacing)+int(31) overal_width = int((len(label_value)+len(label_name))*font_spacing)+int(31)
rendered_sizes = f"width=\"{overal_width}\" height=\"20\" viewBox=\"0 0 {overal_width} 20\"" rendered_sizes = f"width=\"{overal_width}\" height=\"20\" viewBox=\"0 0 {overal_width} 20\""
accent_multiplier = int((overal_width - 121)/font_spacing) accent_multiplier = int((overal_width - 121)/font_spacing)
right_box_position = float(int(overal_width - 132)-0.007) right_box_position = float(int(overal_width - 132)-0.007)
@ -38,106 +37,77 @@ def render_template(env, pad, filepath, inputname, inputvalue, color):
# Create an optimized SVG File # Create an optimized SVG File
with open(f"assets/{filepath}", 'w', encoding='UTF-8') as outputfile: with open(f"assets/{filepath}", 'w', encoding='UTF-8') as outputfile:
outputfile.write( outputfile.write(
optimize_svg(env.render_template( optimize_svg(
'galaxy.svg', pad=pad, this={ env.render_template(
'title': str(rendered_title), 'galaxy.svg', pad=pad, this={
'font_spacing': str(font_spacing), 'title': str(rendered_title),
'center_position': str(center_position), 'font_spacing': str(font_spacing),
'right_width': str(right_width), 'center_position': str(center_position),
'right_box_position': str(right_box_position), 'right_width': str(right_width),
'latest_accent': str(right_box_position), 'right_box_position': str(right_box_position),
'name': str(inputname), 'latest_accent': str(right_box_position),
'accent_multiplier': str(accent_multiplier), 'name': str(label_name),
"sizes": str(rendered_sizes), 'accent_multiplier': str(accent_multiplier),
"inputvalue": str(inputvalue), "sizes": str(rendered_sizes),
"color_higlight": str(color) "inputvalue": str(label_value),
}))) "color_higlight": str(color)
}
)
)
)
# pylint: disable=unused-argument # pylint: disable=unused-argument
def generate_galaxy_svg(inputname, **options): def generate_svg(inputdict, **options):
""" Generate a File based on a Jinja2 Templated that will show the ansible galaxy name """ """ Prepare Variables to geneate File based on a Jinja2 Template and returning its filepath """
# Setup Lektor Env # Setup Lektor Env, CTX and Pad
project = Project.discover() project = Project.discover()
env = project.make_env() env = project.make_env()
# Setup CTX
ctx = get_ctx() ctx = get_ctx()
if ctx is not None: if ctx is not None:
pad = ctx.pad pad = ctx.pad
else: else:
pad = env.new_pad() pad = env.new_pad()
# Create File # Sorting Input
filepath = f"/svg/{inputname}.svg" svg_name = str(dict(inputdict)['name'])
color = '#FF6600' svg_state = str(dict(inputdict)['state'])
render_template(env, pad, filepath, inputname, 'ansible-galaxy', color) svg_label = str(dict(inputdict)['label'])
return filepath
# pylint: disable=unused-argument # File Path
def generate_maintainance_svg(inputdict, **options): if svg_label == 'ansible-galaxy':
""" Generate a File based on a Jinja2 Templated that will show the maintainance state """ filepath = f"/svg/{svg_name}.svg"
# Setup Lektor Env
project = Project.discover()
env = project.make_env()
# Setup CTX
ctx = get_ctx()
if ctx is not None:
pad = ctx.pad
else: else:
pad = env.new_pad() filepath = f"/svg/{svg_name}_{svg_label}.svg"
# Sort Input # File Color
inputname = str(dict(inputdict)['name']) match svg_label:
inputstate = str(dict(inputdict)['state']) case 'ansible-galaxy':
color = '#FF6600'
# Create File case 'license':
filepath = f"/svg/{inputname}_maintainance.svg" color = '#064ccf'
match inputstate: case 'maintainance':
case 'well': match svg_state:
color = '#cd02fa' case 'well':
case 'true': color = '#cd02fa'
color = '#8a00a6' case 'true':
case 'poor': color = '#8a00a6'
color = '#5d0070' case 'poor':
case 'false': color = '#5d0070'
color = '#220029' case 'false':
color = '#220029'
case _:
color = '#000000'
case _: case _:
color = '#000000' color = '#e74c3c'
render_template(env, pad, filepath, inputstate, 'maintainance', color)
# Render Template and Return Filepath
render_template(env, pad, filepath, svg_state, svg_label, color)
return filepath 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'
render_template(env, pad, filepath, inputstate, 'license', color)
return filepath
class RenderTemplatePlugin(Plugin): class RenderTemplatePlugin(Plugin):
""" Create Plugin and listen to Filter Keyword """ """ Create Plugin and listen to Filter Keyword """
name = 'lektor_render_template' name = 'lektor_render_template'
description = 'Lektor plugin to generate files form filters.' description = 'Lektor plugin to generate files form filters.'
def on_setup_env(self, **extra): def on_setup_env(self, **extra):
""" Create Listener for Filter """ """ Create Listener for Filter """
self.env.jinja_env.filters['galaxy'] = generate_galaxy_svg self.env.jinja_env.filters['svg'] = generate_svg
self.env.jinja_env.filters['maintainance'] = generate_maintainance_svg
self.env.jinja_env.filters['license'] = generate_license_svg

View file

@ -27,7 +27,7 @@ setup(
packages=find_packages(), packages=find_packages(),
py_modules=['lektor_render_template'], py_modules=['lektor_render_template'],
# url='https://backwesen.de/l3d/lektor-render-template.git', # url='https://backwesen.de/l3d/lektor-render-template.git',
version='0.3', version='0.4',
classifiers=[ classifiers=[
'Framework :: Lektor', 'Framework :: Lektor',
'Environment :: Plugins', 'Environment :: Plugins',