Commit 760c3ef5 authored by Roland Denis's avatar Roland Denis

Some cleaning and adding Jinja2 filter to display list of names

Also fixing display of list of names with only one name
parent 4e4bb4fb
Pipeline #6570 passed with stages
in 2 minutes and 28 seconds
......@@ -13,16 +13,42 @@ import os
from . import json
###############################################################################
# Tools and filters
def csv_converter(s):
""" Comma separated values as a list. """
return [value.strip() for value in s.split(',')]
def names_list(names):
""" Format a list of names separated by a comma and a final 'et' """
if len(names) > 1:
return ', '.join(names[:-1]) + ' et ' + names[-1]
elif len(names) == 1:
return names[-1]
else:
return ''
###############################################################################
# Nodes
class region(nodes.General, nodes.Element):
""" a region """
class meso_list(nodes.General, nodes.Element):
""" mesocenters list """
class meso(nodes.General, nodes.Element):
""" mesocenter description """
###############################################################################
# Directives
class Region(Directive):
required_arguments = 0
optional_arguments = 1
......@@ -33,7 +59,7 @@ class Region(Directive):
def run(self):
node = region()
node['name'] = self.arguments[0]
self.state.nested_parse(self.content, self.content_offset, node)
return [node]
......@@ -66,11 +92,6 @@ class MesoList(Directive):
return [ml]
def csv_converter(s):
""" Comma separated values as a list. """
return [value.strip() for value in s.split(',')]
class Meso(Directive):
required_arguments = 0
optional_arguments = 10
......@@ -104,14 +125,23 @@ class Meso(Directive):
return [node]
def register():
directives.register_directive('region', Region)
directives.register_directive('meso_list', MesoList)
directives.register_directive('meso', Meso)
###############################################################################
# HTML Translator
# Extend default translator
class myHTMLTranslator(PelicanHTMLTranslator):
def __init__(self, *args, **kwargs):
# Pre-loading Jinja2 templates
# Pre-loading Jinja2 templates and filters
env = Environment(
loader=FileSystemLoader(os.path.dirname(__file__)+'/templates/'),
)
env.filters['names_list'] = names_list
self.meso_template = env.get_template('meso.tpl')
super().__init__(*args, *kwargs)
......@@ -140,8 +170,3 @@ class myHTMLTranslator(PelicanHTMLTranslator):
# Overwrite default translator so that extensions can be chained
readers.PelicanHTMLTranslator = myHTMLTranslator
def register():
directives.register_directive('region', Region)
directives.register_directive('meso_list', MesoList)
directives.register_directive('meso', Meso)
......@@ -34,16 +34,12 @@
{% endif %}
{% if meso['institutesname'] %}
<p><img src="../theme/img/employer.png" alt="Institutions" title="Institutions" />
{% for name in meso['institutesname'] -%}
{{ ' et ' if loop.last }}{{ ', ' if not loop.first and not loop.last }}{{ name }}
{%- endfor %}
{{ meso['institutesname'] | names_list }}
</p>
{% endif %}
{% if meso['financersname'] %}
<p class="collapse"><img src="../theme/img/piggy_bank.png" alt="Financeurs" title="Financeurs" />
{% for name in meso['financersname'] -%}
{{ ' et ' if loop.last }}{{ ', ' if not loop.first and not loop.last }}{{ name }}
{%- endfor %}
{{ meso['financersname'] | names_list }}
</p>
{% endif %}
{% if meso['accesspolicy'] %}
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment