Skip to content

Commit 40463f4

Browse files
committed
Use antsibull-docs-parser to render semantic markup.
1 parent 6be1a58 commit 40463f4

File tree

4 files changed

+36
-27
lines changed

4 files changed

+36
-27
lines changed

.config/dictionary.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ nthhost
2222
ospfv
2323
slaac
2424
# Python packages
25+
antsibull
2526
argcomplete
2627
redbaron
2728
ruamel

.pre-commit-config.yaml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,7 @@ repos:
6767
- id: pylint
6868
additional_dependencies:
6969
- ansible-core
70+
- antsibull-docs-parser
7071
- pyyaml
7172
- redbaron
7273
- ruamel.yaml

collection_prep/jinja_utils.py

Lines changed: 33 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,19 @@
11
"""Utilities for jinja2."""
2-
import re
32

4-
from html import escape as html_escape
53

64
from ansible.module_utils._text import to_text
75
from ansible.module_utils.six import string_types
6+
from antsibull_docs_parser import dom
7+
from antsibull_docs_parser.html import to_html_plain
8+
from antsibull_docs_parser.parser import Context
9+
from antsibull_docs_parser.parser import parse
10+
from antsibull_docs_parser.rst import to_rst_plain
811
from jinja2.runtime import Undefined
12+
from jinja2.utils import pass_context
913

1014

1115
NS_MAP = {}
1216

13-
_ITALIC = re.compile(r"I\(([^)]+)\)")
14-
_BOLD = re.compile(r"B\(([^)]+)\)")
15-
_MODULE = re.compile(r"M\(([^)]+)\)")
16-
_URL = re.compile(r"U\(([^)]+)\)")
17-
_LINK = re.compile(r"L\(([^)]+), *([^)]+)\)")
18-
_CONST = re.compile(r"C\(([^)]+)\)")
19-
_RULER = re.compile(r"HORIZONTALLINE")
20-
2117

2218
def to_kludge_ns(key, value):
2319
"""Save a value for later use.
@@ -39,40 +35,50 @@ def from_kludge_ns(key):
3935
return NS_MAP[key]
4036

4137

42-
def html_ify(text):
38+
def get_context(j2_context):
39+
"""Create parser context from Jinja2 context.
40+
41+
:param j2_context: The Jinja2 context
42+
:return: A parser context
43+
"""
44+
params = {}
45+
plugin_fqcn = j2_context.get("module")
46+
plugin_type = j2_context.get("plugin_type")
47+
if plugin_fqcn is not None and plugin_type is not None:
48+
params["current_plugin"] = dom.PluginIdentifier(fqcn=plugin_fqcn, type=plugin_type)
49+
return Context(**params)
50+
51+
52+
@pass_context
53+
def html_ify(j2_context, text):
4354
"""Convert symbols like I(this is in italics) to valid HTML.
4455
56+
:param j2_context: The Jinja2 context
4557
:param text: The text to transform
4658
:return: An HTML string of the formatted text
4759
"""
4860
if not isinstance(text, string_types):
4961
text = to_text(text)
5062

51-
text = html_escape(text)
52-
text = _ITALIC.sub(r"<em>\1</em>", text)
53-
text = _BOLD.sub(r"<b>\1</b>", text)
54-
text = _MODULE.sub(r"<span class='module'>\1</span>", text)
55-
text = _URL.sub(r"<a href='\1'>\1</a>", text)
56-
text = _LINK.sub(r"<a href='\2'>\1</a>", text)
57-
text = _CONST.sub(r"<code>\1</code>", text)
58-
text = _RULER.sub(r"<hr/>", text)
63+
paragraphs = parse(text, get_context(j2_context))
64+
text = to_html_plain(paragraphs, par_start="", par_end="")
5965

6066
return text.strip()
6167

6268

63-
def rst_ify(text):
69+
@pass_context
70+
def rst_ify(j2_context, text):
6471
"""Convert symbols like I(this is in italics) to valid restructured text.
6572
73+
:param j2_context: The Jinja2 context
6674
:param text: The text to transform
6775
:return: An RST string of the formatted text
6876
"""
69-
text = _ITALIC.sub(r"*\1*", text)
70-
text = _BOLD.sub(r"**\1**", text)
71-
text = _MODULE.sub(r":ref:`\1 <\1_module>`", text)
72-
text = _LINK.sub(r"`\1 <\2>`_", text)
73-
text = _URL.sub(r"\1", text)
74-
text = _CONST.sub(r"``\1``", text)
75-
text = _RULER.sub(r"------------", text)
77+
if not isinstance(text, string_types):
78+
text = to_text(text)
79+
80+
paragraphs = parse(text, get_context(j2_context))
81+
text = to_rst_plain(paragraphs)
7682

7783
return text
7884

requirements.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
11
ansible-core
22
redbaron
33
ruamel.yaml
4+
antsibull-docs-parser >= 1.0.0, < 2.0.0

0 commit comments

Comments
 (0)