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 modules to beginning of plugin index #336

Merged
merged 4 commits into from
Nov 15, 2021
Merged
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
25 changes: 11 additions & 14 deletions antsibull/data/docsite/plugins_by_collection.rst.j2
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,12 @@
:orphan:
{% endif %}

{% macro list_plugins(plugin_type) %}
{% for name, desc in plugin_maps[plugin_type].items() | sort %}
* :ref:`@{ name }@ <ansible_collections.@{ collection_name }@.@{ name }@_@{ plugin_type }@>` -- @{ desc | rst_ify | indent(width=2) }@
{% endfor %}
Comment on lines +6 to +8
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I know this was sort of moved from another place so it's just doing what was already there, but what is the extra spacing for inside {% on some elements? Does that control indentation somehow?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's basically a visual help for readers. You can see that this is part of another block. (Indenting the {% itself is not a good idea since that results in extra whitespace in the output.)

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

As an alternative to whitespace inside the brackets, it's also possible to use {%- and -%} to suppress the extra whitespace.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@tremble true, but the - quickly eats too much whitespace (like newlines as well).

{% endmacro %}

.. _plugins_in_@{collection_name}@:

@{collection_name.title()}@
Expand Down Expand Up @@ -33,42 +39,33 @@ Plugin Index
------------

{% if plugin_maps | reject('eq', 'role') | list %}
These are the plugins in the @{collection_name}@ collection
These are the plugins in the @{collection_name}@ collection:
{% else %}
There are no plugins in the @{collection_name}@ collection with automatically generated documentation.
{% endif %}

{% for category, plugins in plugin_maps.items() | sort | rejectattr('0', 'eq', 'role') %}
{% for category in plugin_maps | reject('eq', 'role') | sort | move_first('module') %}

{% if category == 'module' %}
Modules
~~~~~~~
{% elif category == 'role' %}
Roles
~~~~~
{% else %}
@{ category | capitalize }@ Plugins
@{ '~' * ((category | length) + 8) }@
{% endif %}

{% for name, desc in plugins.items() | sort %}
* :ref:`@{ name }@ <ansible_collections.@{ collection_name }@.@{ name }@_@{ category }@>` -- @{ desc | rst_ify | indent(width=2) }@
{% endfor %}
@{ list_plugins(category) }@
{% endfor %}

{% if 'role' in plugin_maps %}
Role Index
----------

These are the roles in the @{collection_name}@ collection

{% for name, desc in plugin_maps['role'].items() | sort %}
* :ref:`@{ name }@ <ansible_collections.@{ collection_name }@.@{ name }@_role>` -- @{ desc | rst_ify | indent(width=2) }@
{% endfor %}
These are the roles in the @{collection_name}@ collection:

@{ list_plugins('role') }@
{% endif %}


.. seealso::

List of :ref:`collections <list_of_collections>` with docs hosted here.
Expand Down
5 changes: 4 additions & 1 deletion antsibull/jinja2/environment.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,9 @@

from jinja2 import Environment, FileSystemLoader, PackageLoader

from .filters import do_max, documented_type, html_ify, rst_ify, rst_fmt, rst_xline
from .filters import (
do_max, documented_type, html_ify, rst_ify, rst_fmt, rst_xline, move_first,
)
from .tests import still_relevant, test_list


Expand Down Expand Up @@ -64,6 +66,7 @@ def doc_environment(template_location):
env.filters['fmt'] = rst_fmt
env.filters['xline'] = rst_xline
env.filters['documented_type'] = documented_type
env.filters['move_first'] = move_first
env.tests['list'] = test_list
env.tests['still_relevant'] = still_relevant

Expand Down
17 changes: 17 additions & 0 deletions antsibull/jinja2/filters.py
Original file line number Diff line number Diff line change
Expand Up @@ -103,3 +103,20 @@ def rst_xline(width, char="="):
''' return a restructured text line of a given length '''

return char * width


def move_first(sequence, *move_to_beginning):
''' return a copy of sequence where the elements which are in move_to_beginning are
moved to its beginning if they appear in the list '''

remaining = list(sequence)
beginning = []
for elt in move_to_beginning:
try:
remaining.remove(elt)
beginning.append(elt)
except ValueError:
# elt not found in remaining
pass

return beginning + remaining
17 changes: 16 additions & 1 deletion tests/units/test_jinja2.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import pytest

from antsibull.jinja2.filters import rst_ify
from antsibull.jinja2.filters import rst_ify, move_first


RST_IFY_DATA = {
Expand Down Expand Up @@ -31,3 +31,18 @@
@pytest.mark.parametrize('text, expected', RST_IFY_DATA.items())
def test_rst_ify(text, expected):
assert rst_ify(text) == expected


MOVE_FIRST_DATA = [
([], [], []),
(['a', 'b', 'c'], ['d'], ['a', 'b', 'c']),
(['a', 'b', 'c'], ['b'], ['b', 'a', 'c']),
(['a', 'b', 'b', 'c'], ['b'], ['b', 'a', 'b', 'c']),
(['a', 'b', 'c'], ['b', 'c'], ['b', 'c', 'a']),
(['a', 'b', 'c'], ['c', 'b'], ['c', 'b', 'a']),
]


@pytest.mark.parametrize('input, move_to_beginning, expected', MOVE_FIRST_DATA)
def test_move_first(input, move_to_beginning, expected):
assert move_first(input, *move_to_beginning) == expected