Skip to content

Commit

Permalink
Move markup code out (#108)
Browse files Browse the repository at this point in the history
* Began moving markup code out.

* Forgot __init__.py.

* Fix imports.

* Move parser impl code out.

* Began with new-style parser.

* Fix typing for Python 3.6 and 3.7.

* Prepare formatters.

* Continue.

* Replace current markup code with new one.

* Make more compatible to TS code, add same test vectors.

* Cleanup.

* Add more tests.

* Add changelog fragment.

* Add plain HTML output.

* Rename default RST formatter.
  • Loading branch information
felixfontein authored Mar 19, 2023
1 parent f47a9e9 commit 12469fc
Show file tree
Hide file tree
Showing 35 changed files with 3,350 additions and 1,009 deletions.
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

0 comments on commit 12469fc

Please sign in to comment.