Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use antsibull-docs-parser to render semantic markup #88

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .config/dictionary.txt
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ nthhost
ospfv
slaac
# Python packages
antsibull
argcomplete
redbaron
ruamel
Expand Down
1 change: 1 addition & 0 deletions .pre-commit-config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,7 @@ repos:
- id: pylint
additional_dependencies:
- ansible-core
- antsibull-docs-parser
- pyyaml
- redbaron
- ruamel.yaml
60 changes: 33 additions & 27 deletions collection_prep/jinja_utils.py
Original file line number Diff line number Diff line change
@@ -1,23 +1,19 @@
"""Utilities for jinja2."""
import re

from html import escape as html_escape

from ansible.module_utils._text import to_text
from ansible.module_utils.six import string_types
from antsibull_docs_parser import dom
from antsibull_docs_parser.html import to_html_plain
from antsibull_docs_parser.parser import Context
from antsibull_docs_parser.parser import parse
from antsibull_docs_parser.rst import to_rst_plain
from jinja2.runtime import Undefined
from jinja2.utils import pass_context


NS_MAP = {}

_ITALIC = re.compile(r"I\(([^)]+)\)")
_BOLD = re.compile(r"B\(([^)]+)\)")
_MODULE = re.compile(r"M\(([^)]+)\)")
_URL = re.compile(r"U\(([^)]+)\)")
_LINK = re.compile(r"L\(([^)]+), *([^)]+)\)")
_CONST = re.compile(r"C\(([^)]+)\)")
_RULER = re.compile(r"HORIZONTALLINE")


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


def html_ify(text):
def get_context(j2_context):
"""Create parser context from Jinja2 context.
:param j2_context: The Jinja2 context
:return: A parser context
"""
params = {}
plugin_fqcn = j2_context.get("module")
plugin_type = j2_context.get("plugin_type")
if plugin_fqcn is not None and plugin_type is not None:
params["current_plugin"] = dom.PluginIdentifier(fqcn=plugin_fqcn, type=plugin_type)
return Context(**params)


@pass_context
def html_ify(j2_context, text):
"""Convert symbols like I(this is in italics) to valid HTML.
:param j2_context: The Jinja2 context
:param text: The text to transform
:return: An HTML string of the formatted text
"""
if not isinstance(text, string_types):
text = to_text(text)

text = html_escape(text)
text = _ITALIC.sub(r"<em>\1</em>", text)
text = _BOLD.sub(r"<b>\1</b>", text)
text = _MODULE.sub(r"<span class='module'>\1</span>", text)
text = _URL.sub(r"<a href='\1'>\1</a>", text)
text = _LINK.sub(r"<a href='\2'>\1</a>", text)
text = _CONST.sub(r"<code>\1</code>", text)
text = _RULER.sub(r"<hr/>", text)
paragraphs = parse(text, get_context(j2_context))
text = to_html_plain(paragraphs, par_start="", par_end="")

return text.strip()


def rst_ify(text):
@pass_context
def rst_ify(j2_context, text):
"""Convert symbols like I(this is in italics) to valid restructured text.
:param j2_context: The Jinja2 context
:param text: The text to transform
:return: An RST string of the formatted text
"""
text = _ITALIC.sub(r"*\1*", text)
text = _BOLD.sub(r"**\1**", text)
text = _MODULE.sub(r":ref:`\1 <\1_module>`", text)
text = _LINK.sub(r"`\1 <\2>`_", text)
text = _URL.sub(r"\1", text)
text = _CONST.sub(r"``\1``", text)
text = _RULER.sub(r"------------", text)
if not isinstance(text, string_types):
text = to_text(text)

paragraphs = parse(text, get_context(j2_context))
text = to_rst_plain(paragraphs)

return text

Expand Down
1 change: 1 addition & 0 deletions requirements.txt
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
ansible-core
redbaron
ruamel.yaml
antsibull-docs-parser >= 1.0.0, < 2.0.0