Adding Scour to optimize svg

This commit is contained in:
L3D 2023-03-10 04:19:29 +01:00
parent 3825f3932e
commit e9e82b6512
Signed by: l3d
GPG key ID: CD08445BFF4313D1
2 changed files with 43 additions and 32 deletions

View file

@ -4,44 +4,54 @@ from lektor.pluginsystem import Plugin
from lektor.project import Project from lektor.project import Project
from lektor.context import get_ctx from lektor.context import get_ctx
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
# pylint: disable=too-many-arguments # pylint: disable=too-many-arguments
def render_template(env, pad, filepath, inputname, inputvalue, color, tiny): def render_template(env, pad, filepath, inputname, inputvalue, color):
""" Render my custom Jinja2 Template """ """ Render my custom Jinja2 Template """
# Prepare Variables # Prepare Variables
font_spacing = int(7)
rendered_title = f"{inputvalue}: {inputname}" rendered_title = f"{inputvalue}: {inputname}"
if not tiny: center_position = int(len(inputvalue))*font_spacing+15
rendered_sizes = f"width=\"{int(132.5+(int(len(inputname)/2)*10))}\" height=\"20\"" right_width = int(len(inputname))*font_spacing
orange_box_width = str(int(5+len(inputname)*5)) overal_width = int((len(inputvalue)+len(inputname))*font_spacing)+int(31)
orange_position = str(int(50+(len(inputname)*5))) rendered_sizes = f"width=\"{overal_width}\" height=\"20\" viewBox=\"0 0 {overal_width} 20\""
latest_element_x = str(int(1+(int(len(inputname)/2)*10)-1)-0.007) accent_multiplier = int((overal_width - 121)/font_spacing)
accent_02_multiplier = str(1+int(len(inputname)/2)) right_box_position = float(int(overal_width - 132)-0.007)
name_position = str(105)
else:
rendered_sizes = f"width=\"{int(102.5+(int(len(inputname)/2)*10))}\" height=\"20\""
orange_box_width = str(int(5+len(inputname)*5)+25)
orange_position = str(int(50+(len(inputname)*5)))
latest_element_x = str(int(1+(int(len(inputname)/2)*10)-1)-30.007)
accent_02_multiplier = str(1+int(len(inputname)/2)-3)
name_position = str(75)
# Create an optimized SVG File
# Create 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(env.render_template( outputfile.write(
'galaxy.svg', pad=pad, this={ optimize_svg(env.render_template(
'galaxy.svg', pad=pad, this={
'title': str(rendered_title), 'title': str(rendered_title),
'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),
'name': str(inputname), 'name': str(inputname),
'name_position': str(name_position), 'accent_multiplier': str(accent_multiplier),
"sizes": str(rendered_sizes), "sizes": str(rendered_sizes),
"orange_box_width": str(orange_box_width),
"orange_position": str(orange_position),
"accent03_x": str(latest_element_x),
"bg_orange_x": str(latest_element_x),
"accent_02_multiplier": str(accent_02_multiplier),
"inputvalue": str(inputvalue), "inputvalue": str(inputvalue),
"color_orange": str(color) "color_higlight": str(color)
})) })))
# pylint: disable=unused-argument # pylint: disable=unused-argument
def generate_galaxy_svg(inputname, **options): def generate_galaxy_svg(inputname, **options):
@ -60,7 +70,7 @@ def generate_galaxy_svg(inputname, **options):
# Create File # Create File
filepath = f"/svg/{inputname}.svg" filepath = f"/svg/{inputname}.svg"
color = '#FF6600' color = '#FF6600'
render_template(env, pad, filepath, inputname, 'ansible-galaxy', color, False) render_template(env, pad, filepath, inputname, 'ansible-galaxy', color)
return filepath return filepath
# pylint: disable=unused-argument # pylint: disable=unused-argument
@ -94,7 +104,7 @@ def generate_maintainance_svg(inputdict, **options):
color = '#220029' color = '#220029'
case _: case _:
color = '#000000' color = '#000000'
render_template(env, pad, filepath, inputstate, 'maintainance', color, False) render_template(env, pad, filepath, inputstate, 'maintainance', color)
return filepath return filepath
# pylint: disable=unused-argument # pylint: disable=unused-argument
@ -118,7 +128,7 @@ def generate_license_svg(inputdict, **options):
# Create File # Create File
filepath = f"/svg/{inputname}_license.svg" filepath = f"/svg/{inputname}_license.svg"
color = '#064ccf' color = '#064ccf'
render_template(env, pad, filepath, inputstate, 'license', color, True) render_template(env, pad, filepath, inputstate, 'license', color)
return filepath return filepath

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.2', version='0.3',
classifiers=[ classifiers=[
'Framework :: Lektor', 'Framework :: Lektor',
'Environment :: Plugins', 'Environment :: Plugins',
@ -36,5 +36,6 @@ setup(
'lektor.plugins': [ 'lektor.plugins': [
'render-template = lektor_render_template:RenderTemplatePlugin', 'render-template = lektor_render_template:RenderTemplatePlugin',
] ]
} },
install_requires=['scour']
) )