11"""Utilities for jinja2."""
2- import re
32
4- from html import escape as html_escape
53
64from ansible .module_utils ._text import to_text
75from 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
811from jinja2 .runtime import Undefined
12+ from jinja2 .utils import pass_context
913
1014
1115NS_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
2218def 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
0 commit comments