From 36444bf833881ea916e586b3fdf5cade11f51142 Mon Sep 17 00:00:00 2001 From: L3D Date: Wed, 22 Mar 2023 00:52:11 +0100 Subject: [PATCH] Update render Template with aditional parts --- README.md | 4 ++- lektor_render_template.py | 71 ++++++++++++++++++++++++--------------- setup.py | 2 +- 3 files changed, 47 insertions(+), 30 deletions(-) diff --git a/README.md b/README.md index 6c65c08..ff64c64 100644 --- a/README.md +++ b/README.md @@ -6,9 +6,11 @@ In this case it is expecting the [galaxy.svg](https://backwesen.de/l3d/ansible.l ## Example Usage: ```json -{'name': this.filename, 'state': this.colored_label, 'label': this.label_name} | svg +{'name': this.filename, 'state': this.colored_label, 'label': this.label_name, 'color': '#FF0000', 'suffix': 'fileend'} | svg ``` +Note that ``name``, ``state`` and ``label`` are required keys, ``color`` and ``suffix`` are optional keys. + You can see a real life example of this plugin at [ansible.l3d.space](https://ansible.l3d.space/). ## Open Issues: diff --git a/lektor_render_template.py b/lektor_render_template.py index 4372595..301eabb 100644 --- a/lektor_render_template.py +++ b/lektor_render_template.py @@ -62,9 +62,42 @@ def render_template(env, pad, filepath, label_name, label_value, color): ) ) +def create_filepath(svg_label, svg_name, svg_suffix): + """ Generate File path """ + if svg_label == 'ansible-galaxy': + 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" + +def create_color(svg_label, svg_state, svg_color): + """ Create Color based on Input or predefined patterns """ + label_color_map = { + 'ansible-galaxy': '#FF6600', + 'license': '#064ccf', + 'maintainance': { + 'well': '#cd02fa', + 'true': '#8a00a6', + 'poor': '#5d0070', + 'false': '#220029', + 'default': '#000000' + }, + 'default': '#e74c3c' + } + if bool(svg_color): + return svg_color + if svg_label in label_color_map: + label_item = label_color_map[svg_label] + if isinstance(label_item, dict): + 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 returning its filepath """ + """ 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() @@ -78,35 +111,17 @@ def generate_svg(inputdict, **options): 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" + if 'suffix' in dict(inputdict).keys(): + svg_suffix = str(dict(inputdict)['suffix']) else: - filepath = f"/svg/{svg_name}_{svg_label}.svg" - - # File Color - label_color_map = { - 'ansible-galaxy': '#FF6600', - 'license': '#064ccf', - 'maintainance': { - 'well': '#cd02fa', - 'true': '#8a00a6', - 'poor': '#5d0070', - 'false': '#220029', - 'default': '#000000' - }, - 'default': '#e74c3c' - } - - if svg_label in label_color_map: - label_item = label_color_map[svg_label] - if isinstance(label_item, dict): - color = label_item.get(svg_state, label_item.get('default', '#000000')) - else: - color = label_item + svg_suffix = False + if 'color' in dict(inputdict).keys(): + svg_color = str(dict(inputdict)['color']) else: - color = label_color_map['default'] + svg_color = False + + filepath = create_filepath(svg_label, svg_name, svg_suffix) + color = create_color(svg_label, svg_state, svg_color) # Render Template and Return Filepath render_template(env, pad, filepath, svg_state, svg_label, color) diff --git a/setup.py b/setup.py index 06ba660..5e2a171 100644 --- a/setup.py +++ b/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.6', + version='0.7', classifiers=[ 'Framework :: Lektor', 'Environment :: Plugins',