1
1
"""Utilities for jinja2."""
2
- import re
3
2
4
- from html import escape as html_escape
5
3
6
4
from ansible .module_utils ._text import to_text
7
5
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
8
11
from jinja2 .runtime import Undefined
12
+ from jinja2 .utils import pass_context
9
13
10
14
11
15
NS_MAP = {}
12
16
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
-
21
17
22
18
def to_kludge_ns (key , value ):
23
19
"""Save a value for later use.
@@ -39,40 +35,50 @@ def from_kludge_ns(key):
39
35
return NS_MAP [key ]
40
36
41
37
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 ):
43
54
"""Convert symbols like I(this is in italics) to valid HTML.
44
55
56
+ :param j2_context: The Jinja2 context
45
57
:param text: The text to transform
46
58
:return: An HTML string of the formatted text
47
59
"""
48
60
if not isinstance (text , string_types ):
49
61
text = to_text (text )
50
62
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 = "" )
59
65
60
66
return text .strip ()
61
67
62
68
63
- def rst_ify (text ):
69
+ @pass_context
70
+ def rst_ify (j2_context , text ):
64
71
"""Convert symbols like I(this is in italics) to valid restructured text.
65
72
73
+ :param j2_context: The Jinja2 context
66
74
:param text: The text to transform
67
75
:return: An RST string of the formatted text
68
76
"""
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 )
76
82
77
83
return text
78
84
0 commit comments