update and clean up svg creation
This commit is contained in:
parent
e9e82b6512
commit
37e8805243
2 changed files with 65 additions and 95 deletions
|
@ -8,29 +8,28 @@ from scour.scour import scourString
|
|||
from scour.scour import sanitizeOptions as sanitizeScourOptions
|
||||
from scour.scour import parse_args as parseScourArgs
|
||||
|
||||
def optimize_svg(sourcesvg):
|
||||
def optimize_svg(source_svg):
|
||||
""" using scour to optimize and minify svg """
|
||||
scouroptions = parseScourArgs([
|
||||
scour_options = parseScourArgs([
|
||||
"--enable-id-stripping",
|
||||
"--enable-comment-stripping",
|
||||
"--shorten-ids",
|
||||
"--indent=none",
|
||||
"--no-line-breaks"])
|
||||
scouroptions = sanitizeScourOptions(scouroptions)
|
||||
optimizedsvg = scourString(sourcesvg, scouroptions)
|
||||
return optimizedsvg
|
||||
scour_options = sanitizeScourOptions(scour_options)
|
||||
optimized_svg = scourString(source_svg, scour_options)
|
||||
return optimized_svg
|
||||
|
||||
|
||||
# pylint: disable=too-many-arguments
|
||||
def render_template(env, pad, filepath, inputname, inputvalue, color):
|
||||
""" Render my custom Jinja2 Template """
|
||||
|
||||
# Prepare Variables
|
||||
def render_template(env, pad, filepath, label_name, label_value, color):
|
||||
""" 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
|
||||
font_spacing = int(7)
|
||||
rendered_title = f"{inputvalue}: {inputname}"
|
||||
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_title = f"{label_value}: {label_name}"
|
||||
center_position = int(len(label_value))*font_spacing+15
|
||||
right_width = int(len(label_name))*font_spacing
|
||||
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\""
|
||||
accent_multiplier = int((overal_width - 121)/font_spacing)
|
||||
right_box_position = float(int(overal_width - 132)-0.007)
|
||||
|
@ -38,7 +37,8 @@ def render_template(env, pad, filepath, inputname, inputvalue, color):
|
|||
# Create an optimized SVG File
|
||||
with open(f"assets/{filepath}", 'w', encoding='UTF-8') as outputfile:
|
||||
outputfile.write(
|
||||
optimize_svg(env.render_template(
|
||||
optimize_svg(
|
||||
env.render_template(
|
||||
'galaxy.svg', pad=pad, this={
|
||||
'title': str(rendered_title),
|
||||
'font_spacing': str(font_spacing),
|
||||
|
@ -46,54 +46,47 @@ def render_template(env, pad, filepath, inputname, inputvalue, color):
|
|||
'right_width': str(right_width),
|
||||
'right_box_position': str(right_box_position),
|
||||
'latest_accent': str(right_box_position),
|
||||
'name': str(inputname),
|
||||
'name': str(label_name),
|
||||
'accent_multiplier': str(accent_multiplier),
|
||||
"sizes": str(rendered_sizes),
|
||||
"inputvalue": str(inputvalue),
|
||||
"inputvalue": str(label_value),
|
||||
"color_higlight": str(color)
|
||||
})))
|
||||
}
|
||||
)
|
||||
)
|
||||
)
|
||||
|
||||
# pylint: disable=unused-argument
|
||||
def generate_galaxy_svg(inputname, **options):
|
||||
""" Generate a File based on a Jinja2 Templated that will show the ansible galaxy name """
|
||||
# Setup Lektor Env
|
||||
def generate_svg(inputdict, **options):
|
||||
""" Prepare Variables to geneate File based on a Jinja2 Template and returning its filepath """
|
||||
# Setup Lektor Env, CTX and Pad
|
||||
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
|
||||
filepath = f"/svg/{inputname}.svg"
|
||||
# Sorting Input
|
||||
svg_name = str(dict(inputdict)['name'])
|
||||
svg_state = str(dict(inputdict)['state'])
|
||||
svg_label = str(dict(inputdict)['label'])
|
||||
|
||||
# File Path
|
||||
if svg_label == 'ansible-galaxy':
|
||||
filepath = f"/svg/{svg_name}.svg"
|
||||
else:
|
||||
filepath = f"/svg/{svg_name}_{svg_label}.svg"
|
||||
|
||||
# File Color
|
||||
match svg_label:
|
||||
case 'ansible-galaxy':
|
||||
color = '#FF6600'
|
||||
render_template(env, pad, filepath, inputname, 'ansible-galaxy', color)
|
||||
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'])
|
||||
|
||||
# Create File
|
||||
filepath = f"/svg/{inputname}_maintainance.svg"
|
||||
match inputstate:
|
||||
case 'license':
|
||||
color = '#064ccf'
|
||||
case 'maintainance':
|
||||
match svg_state:
|
||||
case 'well':
|
||||
color = '#cd02fa'
|
||||
case 'true':
|
||||
|
@ -104,40 +97,17 @@ def generate_maintainance_svg(inputdict, **options):
|
|||
color = '#220029'
|
||||
case _:
|
||||
color = '#000000'
|
||||
render_template(env, pad, filepath, inputstate, 'maintainance', color)
|
||||
case _:
|
||||
color = '#e74c3c'
|
||||
|
||||
# Render Template and Return Filepath
|
||||
render_template(env, pad, filepath, svg_state, svg_label, color)
|
||||
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):
|
||||
""" Create Plugin and listen to Filter Keyword """
|
||||
name = 'lektor_render_template'
|
||||
description = 'Lektor plugin to generate files form filters.'
|
||||
def on_setup_env(self, **extra):
|
||||
""" Create Listener for Filter """
|
||||
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
|
||||
self.env.jinja_env.filters['svg'] = generate_svg
|
||||
|
|
2
setup.py
2
setup.py
|
@ -27,7 +27,7 @@ setup(
|
|||
packages=find_packages(),
|
||||
py_modules=['lektor_render_template'],
|
||||
# url='https://backwesen.de/l3d/lektor-render-template.git',
|
||||
version='0.3',
|
||||
version='0.4',
|
||||
classifiers=[
|
||||
'Framework :: Lektor',
|
||||
'Environment :: Plugins',
|
||||
|
|
Loading…
Reference in a new issue