2023-02-05 01:10:09 +01:00
|
|
|
# -*- coding: utf-8 -*-
|
2023-03-04 03:17:09 +01:00
|
|
|
""" Import lektor plugin stuff """
|
2023-03-21 19:36:28 +01:00
|
|
|
from os import makedirs
|
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
|
|
|
|
|
2023-03-21 19:36:28 +01:00
|
|
|
|
2023-03-11 22:43:17 +01:00
|
|
|
def optimize_svg(source_svg):
|
2023-03-10 04:19:29 +01:00
|
|
|
""" using scour to optimize and minify svg """
|
2023-03-11 22:43:17 +01:00
|
|
|
scour_options = parseScourArgs([
|
2023-03-10 04:19:29 +01:00
|
|
|
"--enable-id-stripping",
|
|
|
|
"--enable-comment-stripping",
|
|
|
|
"--shorten-ids",
|
|
|
|
"--indent=none",
|
|
|
|
"--no-line-breaks"])
|
2023-03-11 22:43:17 +01:00
|
|
|
scour_options = sanitizeScourOptions(scour_options)
|
|
|
|
optimized_svg = scourString(source_svg, scour_options)
|
|
|
|
return optimized_svg
|
2023-03-10 04:19:29 +01:00
|
|
|
|
|
|
|
|
2023-03-08 02:29:24 +01:00
|
|
|
# pylint: disable=too-many-arguments
|
2023-03-11 22:43:17 +01:00
|
|
|
def render_template(env, pad, filepath, label_name, label_value, color):
|
2023-03-21 19:36:28 +01:00
|
|
|
"""
|
|
|
|
Render my custom Jinja2 Template 'galaxy.svg' with
|
|
|
|
the given variables as input and pass it to a svg optimizer
|
|
|
|
"""
|
2023-03-11 22:43:17 +01:00
|
|
|
# Prepare Final Variables for galaxy.svg
|
2023-03-10 04:19:29 +01:00
|
|
|
font_spacing = int(7)
|
2023-03-11 22:43:17 +01:00
|
|
|
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)
|
2023-03-10 04:19:29 +01:00
|
|
|
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-21 19:36:28 +01:00
|
|
|
makedirs('assets/svg/', exist_ok=True)
|
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(
|
2023-03-11 22:43:17 +01:00
|
|
|
optimize_svg(
|
|
|
|
env.render_template(
|
|
|
|
'galaxy.svg', pad=pad, this={
|
|
|
|
'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(label_name),
|
|
|
|
'accent_multiplier': str(accent_multiplier),
|
|
|
|
"sizes": str(rendered_sizes),
|
|
|
|
"inputvalue": str(label_value),
|
|
|
|
"color_higlight": str(color)
|
|
|
|
}
|
|
|
|
)
|
|
|
|
)
|
|
|
|
)
|
2023-03-08 02:29:24 +01:00
|
|
|
|
2023-03-22 00:52:11 +01:00
|
|
|
def create_filepath(svg_label, svg_name, svg_suffix):
|
|
|
|
""" Generate File path """
|
2023-03-11 22:43:17 +01:00
|
|
|
if svg_label == 'ansible-galaxy':
|
2023-03-22 00:52:11 +01:00
|
|
|
if bool(svg_suffix):
|
|
|
|
return f"/svg/{svg_name}_{svg_suffix}.svg"
|
|
|
|
return f"/svg/{svg_name}.svg"
|
|
|
|
if bool(svg_suffix):
|
|
|
|
return f"/svg/{svg_name}_{svg_label}_{svg_suffix}.svg"
|
|
|
|
return f"/svg/{svg_name}_{svg_label}.svg"
|
2023-03-11 22:43:17 +01:00
|
|
|
|
2023-03-22 00:52:11 +01:00
|
|
|
def create_color(svg_label, svg_state, svg_color):
|
|
|
|
""" Create Color based on Input or predefined patterns """
|
2023-03-21 19:36:28 +01:00
|
|
|
label_color_map = {
|
|
|
|
'ansible-galaxy': '#FF6600',
|
|
|
|
'license': '#064ccf',
|
|
|
|
'maintainance': {
|
|
|
|
'well': '#cd02fa',
|
|
|
|
'true': '#8a00a6',
|
|
|
|
'poor': '#5d0070',
|
|
|
|
'false': '#220029',
|
|
|
|
'default': '#000000'
|
|
|
|
},
|
|
|
|
'default': '#e74c3c'
|
|
|
|
}
|
2023-03-22 00:52:11 +01:00
|
|
|
if bool(svg_color):
|
|
|
|
return svg_color
|
2023-03-21 19:36:28 +01:00
|
|
|
if svg_label in label_color_map:
|
|
|
|
label_item = label_color_map[svg_label]
|
|
|
|
if isinstance(label_item, dict):
|
2023-03-22 00:52:11 +01:00
|
|
|
return label_item.get(svg_state, label_item.get('default', '#000000'))
|
|
|
|
return label_item
|
|
|
|
return label_color_map['default']
|
|
|
|
|
|
|
|
# pylint: disable=unused-argument
|
|
|
|
def generate_svg(inputdict, **options):
|
|
|
|
""" Prepare Variables to geneate File based on a Jinja2 Template and return filepath """
|
|
|
|
# Setup Lektor Env, CTX and Pad
|
|
|
|
project = Project.discover()
|
|
|
|
env = project.make_env()
|
|
|
|
ctx = get_ctx()
|
|
|
|
if ctx is not None:
|
|
|
|
pad = ctx.pad
|
2023-03-21 19:36:28 +01:00
|
|
|
else:
|
2023-03-22 00:52:11 +01:00
|
|
|
pad = env.new_pad()
|
|
|
|
|
|
|
|
# Sorting Input
|
|
|
|
svg_name = str(dict(inputdict)['name'])
|
|
|
|
svg_state = str(dict(inputdict)['state'])
|
|
|
|
svg_label = str(dict(inputdict)['label'])
|
|
|
|
if 'suffix' in dict(inputdict).keys():
|
|
|
|
svg_suffix = str(dict(inputdict)['suffix'])
|
|
|
|
else:
|
|
|
|
svg_suffix = False
|
|
|
|
if 'color' in dict(inputdict).keys():
|
|
|
|
svg_color = str(dict(inputdict)['color'])
|
|
|
|
else:
|
|
|
|
svg_color = False
|
|
|
|
|
|
|
|
filepath = create_filepath(svg_label, svg_name, svg_suffix)
|
|
|
|
color = create_color(svg_label, svg_state, svg_color)
|
2023-03-08 02:29:24 +01:00
|
|
|
|
2023-03-11 22:43:17 +01:00
|
|
|
# Render Template and Return Filepath
|
|
|
|
render_template(env, pad, filepath, svg_state, svg_label, 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-11 22:43:17 +01:00
|
|
|
self.env.jinja_env.filters['svg'] = generate_svg
|