Add the :exp_years: text role

It will render the number of years since i started to work in IT.
This commit is contained in:
Gergely Polonkai 2021-02-15 15:50:54 +01:00
parent 434a0062f3
commit 6b39cecf7a
No known key found for this signature in database
GPG Key ID: 2D2885533B869ED4
1 changed files with 13 additions and 0 deletions

View File

@ -1,3 +1,5 @@
from datetime import datetime
from docutils import nodes
from docutils.parsers import rst
@ -14,10 +16,21 @@ def del_role(name, rawtext, text, lineno, inliner, options=None, content=None):
return [nodes.raw('', f'<del>{text}</del>', format='html')], []
def exp_years_role(*args, **kwargs):
now = datetime.utcnow()
exp_range = now - datetime(1993, 9, 1)
exp_years = int(exp_range.total_seconds() / 31536000)
html_text = f'<span class="definition" title="As of {now.year}">{exp_years}</span>'
return [nodes.raw('', html_text, format='html')], []
def register_roles():
rst.roles.register_local_role('kbd', keyboard_role)
rst.roles.register_local_role('sup', superscript_role)
rst.roles.register_local_role('del', del_role)
rst.roles.register_local_role('exp_years', exp_years_role)
def register():