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

Move markup code out #108

Merged
merged 15 commits into from
Mar 19, 2023
2 changes: 2 additions & 0 deletions changelogs/fragments/108-markup.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
minor_change:
- Internal refactoring of markup code (https://github.com/ansible-community/antsibull-docs/pull/108).
5 changes: 3 additions & 2 deletions src/antsibull_docs/jinja2/environment.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@

from jinja2 import BaseLoader, Environment, FileSystemLoader, PackageLoader

from ..markup.rstify import rst_code, rst_escape
from ..utils.collection_name_transformer import CollectionNameTransformer
from .filters import (
do_max,
Expand All @@ -22,9 +23,9 @@
rst_xline,
to_ini_value,
to_json,
html_ify,
rst_ify,
)
from .htmlify import html_ify
from .rstify import rst_code, rst_escape, rst_ify
from .tests import still_relevant, test_list

# kludge_ns gives us a kludgey way to set variables inside of loops that need to be visible outside
Expand Down
42 changes: 42 additions & 0 deletions src/antsibull_docs/jinja2/filters.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,10 @@

from antsibull_core.logging import log
from jinja2.runtime import Context, Undefined
from jinja2.utils import pass_context

from ..markup.rstify import rst_ify as rst_ify_impl
from ..markup.htmlify import html_ify as html_ify_impl

mlog = log.fields(mod=__name__)

Expand Down Expand Up @@ -127,3 +131,41 @@ def to_ini_value(data: t.Any) -> str:
return 'MAPPINGS ARE NOT SUPPORTED'
# Handle other values (booleans, integers, floats) as JSON
return json.dumps(data)


@pass_context
def rst_ify(context: Context, text: str,
*,
plugin_fqcn: t.Optional[str] = None,
plugin_type: t.Optional[str] = None) -> str:
''' convert symbols like I(this is in italics) to valid restructured text '''
flog = mlog.fields(func='rst_ify')
flog.fields(text=text).debug('Enter')

plugin_fqcn, plugin_type = extract_plugin_data(
context, plugin_fqcn=plugin_fqcn, plugin_type=plugin_type)

text, counts = rst_ify_impl(text, plugin_fqcn=plugin_fqcn, plugin_type=plugin_type)

flog.fields(counts=counts).info('Number of macros converted to rst equivalents')
flog.debug('Leave')
return text


@pass_context
def html_ify(context: Context, text: str,
*,
plugin_fqcn: t.Optional[str] = None,
plugin_type: t.Optional[str] = None) -> str:
''' convert symbols like I(this is in italics) to valid HTML '''
flog = mlog.fields(func='html_ify')
flog.fields(text=text).debug('Enter')

plugin_fqcn, plugin_type = extract_plugin_data(
context, plugin_fqcn=plugin_fqcn, plugin_type=plugin_type)

text, counts = html_ify_impl(text, plugin_fqcn=plugin_fqcn, plugin_type=plugin_type)

flog.fields(counts=counts).info('Number of macros converted to html equivalents')
flog.debug('Leave')
return text
Loading