Skip to content

Commit

Permalink
Add semantic markup support.
Browse files Browse the repository at this point in the history
  • Loading branch information
felixfontein committed Dec 7, 2021
1 parent db95dc7 commit 4231efa
Show file tree
Hide file tree
Showing 9 changed files with 52 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,9 @@
:orphan:
{% endif %}

.. |equalsign| unicode:: 0x3D .. equal sign
:trim:

.. _list_of_collections_@{ namespace }@:

{% set title = 'Collections in the ' ~ (namespace | title) ~ ' Namespace' | rst_ify -%}
Expand Down
3 changes: 3 additions & 0 deletions src/antsibull/data/docsite/list_of_plugins.rst.j2
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
:orphan:

.. |equalsign| unicode:: 0x3D .. equal sign
:trim:

.. _list_of_@{ plugin_type }@_plugins:

{% if plugin_type == 'module' %}
Expand Down
3 changes: 3 additions & 0 deletions src/antsibull/data/docsite/plugin-deprecation.rst.j2
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,9 @@
{# avoids rST "isn't included in any toctree" errors for module docs #}
:orphan:

.. |equalsign| unicode:: 0x3D .. equal sign
:trim:

.. _@{ module }@_@{ plugin_type }@_alias_@{ alias }@:

{% if short_description %}
Expand Down
3 changes: 3 additions & 0 deletions src/antsibull/data/docsite/plugin-redirect.rst.j2
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,9 @@

:orphan:

.. |equalsign| unicode:: 0x3D .. equal sign
:trim:

.. Anchors

.. _ansible_collections.@{plugin_name}@_@{plugin_type}@:
Expand Down
3 changes: 3 additions & 0 deletions src/antsibull/data/docsite/plugin-tombstone.rst.j2
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,9 @@

:orphan:

.. |equalsign| unicode:: 0x3D .. equal sign
:trim:

.. Anchors

.. _ansible_collections.@{plugin_name}@_@{plugin_type}@:
Expand Down
3 changes: 3 additions & 0 deletions src/antsibull/data/docsite/plugin.rst.j2
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,9 @@
.. |antsibull-internal-nbsp| unicode:: 0xA0
:trim:

.. |equalsign| unicode:: 0x3D .. equal sign
:trim:

.. role:: ansible-attribute-support-label
.. role:: ansible-attribute-support-property
.. role:: ansible-attribute-support-full
Expand Down
3 changes: 3 additions & 0 deletions src/antsibull/data/docsite/plugins_by_collection.rst.j2
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,9 @@
:orphan:
{% endif %}

.. |equalsign| unicode:: 0x3D .. equal sign
:trim:

{% macro list_plugins(plugin_type) %}
{% for name, desc in plugin_maps[plugin_type].items() | sort %}
* :ref:`@{ name }@ <ansible_collections.@{ collection_name }@.@{ name }@_@{ plugin_type }@>` -- @{ desc | rst_ify | indent(width=2) }@
Expand Down
3 changes: 3 additions & 0 deletions src/antsibull/data/docsite/role.rst.j2
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,9 @@

:orphan:

.. |equalsign| unicode:: 0x3D .. equal sign
:trim:

.. role:: ansible-option-type
.. role:: ansible-option-elements
.. role:: ansible-option-required
Expand Down
28 changes: 28 additions & 0 deletions src/antsibull/jinja2/filters.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,13 +19,24 @@
_ITALIC = re.compile(r"\bI\(([^)]+)\)")
_BOLD = re.compile(r"\bB\(([^)]+)\)")
_MODULE = re.compile(r"\bM\(([^).]+)\.([^).]+)\.([^)]+)\)")
_PLUGIN = re.compile(r"\bP\(([^).]+)\.([^).]+)\.([^)]+)#([a-z]+)\)")
_URL = re.compile(r"\bU\(([^)]+)\)")
_LINK = re.compile(r"\bL\(([^)]+), *([^)]+)\)")
_REF = re.compile(r"\bR\(([^)]+), *([^)]+)\)")
_CONST = re.compile(r"\bC\(([^)]+)\)")
_SEM_OPTION_NAME = re.compile(r"\bO\(([^)]+)\)")
_SEM_OPTION_VALUE = re.compile(r"\bV\(([^)]+)\)")
_SEM_ENV_VARIABLE = re.compile(r"\bE\(([^)]+)\)")
_RULER = re.compile(r"\bHORIZONTALLINE\b")


def _option_name_html(matcher):
parts = matcher.group(1).split('=', 1)
if len(parts) == 1:
return f'<em>{parts[0]}</em>'
return f'<em>{parts[0]}</em>=<code>{parts[1]}</code>'


def html_ify(text):
''' convert symbols like I(this is in italics) to valid HTML '''

Expand All @@ -38,10 +49,15 @@ def html_ify(text):
text, _counts['bold'] = _BOLD.subn(r"<b>\1</b>", text)
text, _counts['module'] = _MODULE.subn(
r"<a href='../../\1/\2/\3_module.html' class='module'>\1.\2.\3</a>", text)
text, _counts['plugin'] = _PLUGIN.subn(
r"<a href='../../\1/\2/\3_\4.html' class='module plugin-\4'>\1.\2.\3</span>", text)
text, _counts['url'] = _URL.subn(r"<a href='\1'>\1</a>", text)
text, _counts['ref'] = _REF.subn(r"<span class='module'>\1</span>", text)
text, _counts['link'] = _LINK.subn(r"<a href='\2'>\1</a>", text)
text, _counts['const'] = _CONST.subn(r"<code>\1</code>", text)
text, _counts['option-name'] = _SEM_OPTION_NAME.subn(_option_name_html, text)
text, _counts['option-value'] = _SEM_OPTION_VALUE.subn(r"<code>\1</code>", text)
text, _counts['environment-var'] = _SEM_ENV_VARIABLE.subn(r"<code>\1</code>", text)
text, _counts['ruler'] = _RULER.subn(r"<hr/>", text)

text = text.strip()
Expand Down Expand Up @@ -71,6 +87,13 @@ def do_max(seq):
return max(seq)


def _option_name_rst(matcher):
parts = matcher.group(1).split('=', 1)
if len(parts) == 1:
return f'*{parts[0]}*'
return f'*{parts[0]}* |equalsign| ``{parts[1]}``'


def rst_ify(text):
''' convert symbols like I(this is in italics) to valid restructured text '''

Expand All @@ -82,10 +105,15 @@ def rst_ify(text):
text, _counts['bold'] = _BOLD.subn(r"**\1**", text)
text, _counts['module'] = _MODULE.subn(
r":ref:`\1.\2.\3 <ansible_collections.\1.\2.\3_module>`", text)
text, _counts['plugin'] = _PLUGIN.subn(
r":ref:`\1.\2.\3 <ansible_collections.\1.\2.\3_\4>`", text)
text, _counts['url'] = _LINK.subn(r"`\1 <\2>`__", text)
text, _counts['ref'] = _URL.subn(r"\1", text)
text, _counts['link'] = _REF.subn(r":ref:`\1 <\2>`", text)
text, _counts['const'] = _CONST.subn(r"``\1``", text)
text, _counts['option-name'] = _SEM_OPTION_NAME.subn(_option_name_rst, text)
text, _counts['option-value'] = _SEM_OPTION_VALUE.subn(r"``\1``", text)
text, _counts['environment-var'] = _SEM_ENV_VARIABLE.subn(r"``\1``", text)
text, _counts['ruler'] = _RULER.subn('\n\n.. raw:: html\n\n <hr>\n\n', text)

flog.fields(counts=_counts).info('Number of macros converted to rst equivalents')
Expand Down

0 comments on commit 4231efa

Please sign in to comment.