-
Notifications
You must be signed in to change notification settings - Fork 17
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Remove superfluous whitespace or escaped spaces from templates (#313)
* Remove superfluous whitespace or escaped spaces from templates. * Remove trialing whitespaces from author cleanup tests.
- Loading branch information
1 parent
8789af6
commit 22c422d
Showing
112 changed files
with
300 additions
and
279 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
minor_changes: | ||
- "Remove superfluous whitespace or escaped spaces from templates (https://github.com/ansible-community/antsibull-docs/pull/313)." |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
# GNU General Public License v3.0+ (see LICENSES/GPL-3.0-or-later.txt or | ||
# https://www.gnu.org/licenses/gpl-3.0.txt) | ||
# SPDX-License-Identifier: GPL-3.0-or-later | ||
# SPDX-FileCopyrightText: 2024, Ansible Project | ||
""" | ||
Text utils. | ||
""" | ||
|
||
import typing as t | ||
|
||
|
||
def count_leading_whitespace( | ||
line: str, *, max_count: t.Optional[int] = None | ||
) -> t.Optional[int]: | ||
""" | ||
Count the number of leading whitespace for the given line. | ||
If ``max_count`` is given, will not return numbers greater than ``max_count``. | ||
If the line completely consists out of whitespace, ``None`` is returned. | ||
""" | ||
length = len(line) | ||
to_check = length | ||
if max_count is not None and max_count < to_check: | ||
to_check = max_count | ||
count = 0 | ||
while count < to_check and line[count] in " \t": | ||
count += 1 | ||
if count == length: | ||
return None | ||
return count | ||
|
||
|
||
def sanitize_whitespace(content: str) -> str: | ||
# Split into lines and remove trailing whitespace | ||
lines = [line.rstrip(" \t") for line in content.splitlines()] | ||
|
||
# Remove starting and trailing empty lines | ||
start = 0 | ||
end = len(lines) | ||
while start < end and lines[start] == "": | ||
start += 1 | ||
while start < end and lines[end - 1] == "": | ||
end -= 1 | ||
lines = lines[start:end] | ||
|
||
# Remove common leading whitespace | ||
common_whitespace = None | ||
for line in lines: | ||
whitespace = count_leading_whitespace(line, max_count=common_whitespace) | ||
if whitespace is not None: | ||
if common_whitespace is None or common_whitespace > whitespace: | ||
common_whitespace = whitespace | ||
if common_whitespace == 0: | ||
break | ||
if common_whitespace is not None and common_whitespace > 0: | ||
lines = [line[common_whitespace:] for line in lines] | ||
|
||
# Re-combine the result | ||
return "\n".join(lines) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -394,12 +394,10 @@ Examples | |
|
||
.. code-block:: yaml+jinja | ||
|
||
|
||
This is not YAML. | ||
|
||
|
||
|
||
|
||
.. Facts | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -323,12 +323,10 @@ Examples | |
|
||
.. code-block:: yaml+jinja | ||
|
||
|
||
{'a': 1} | ns2.col.bar({'b': 2}, baz='cde') | ||
|
||
|
||
|
||
|
||
.. Facts | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -142,7 +142,6 @@ Examples | |
|
||
|
||
|
||
|
||
.. Facts | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -316,14 +316,12 @@ Examples | |
|
||
.. code-block:: yaml+jinja | ||
|
||
|
||
- name: Do some foo | ||
ns2.col.foo2: | ||
bar: foo | ||
|
||
|
||
|
||
|
||
.. Facts | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -352,7 +352,7 @@ Status | |
Authors | ||
~~~~~~~ | ||
|
||
- Nobody | ||
- Nobody | ||
|
||
|
||
.. hint:: | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -91,7 +91,7 @@ Synopsis | |
Authors | ||
~~~~~~~ | ||
|
||
- Felix Fontein (@felixfontein) | ||
- Felix Fontein (@felixfontein) | ||
|
||
|
||
.. hint:: | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -231,12 +231,10 @@ Examples | |
|
||
.. code-block:: yaml+jinja | ||
|
||
|
||
some_var: "{{ 'foo' | ns2.col.foo }}" | ||
|
||
|
||
|
||
|
||
.. Facts | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -134,13 +134,11 @@ Examples | |
|
||
.. code-block:: yaml+jinja | ||
|
||
|
||
foo: | ||
bar! | ||
|
||
|
||
|
||
|
||
.. Facts | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.