fix: use custom method for annotation split to allow escape chars (#432)

This commit is contained in:
Robert Kaussow 2023-01-22 15:02:09 +01:00 committed by GitHub
parent 457e1fdcf9
commit 878cce1791
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
5 changed files with 39 additions and 5 deletions

View File

@ -8,7 +8,7 @@ from collections import defaultdict
import anyconfig import anyconfig
from ansibledoctor.config import SingleConfig from ansibledoctor.config import SingleConfig
from ansibledoctor.utils import SingleLog from ansibledoctor.utils import SingleLog, _split_string
class AnnotationItem: class AnnotationItem:
@ -98,7 +98,7 @@ class Annotation:
line1 = re.sub(reg1, "", line).strip() line1 = re.sub(reg1, "", line).strip()
# step3 take the main key value from the annotation # step3 take the main key value from the annotation
parts = [part.strip() for part in line1.split(":", 2)] parts = [part.strip() for part in _split_string(line1, ":", "\\", 2)]
key = str(parts[0]) key = str(parts[0])
item.data[key] = {} item.data[key] = {}
multiline_char = [">", "$>"] multiline_char = [">", "$>"]

View File

@ -41,6 +41,40 @@ def _should_do_markup():
return sys.stdout.isatty() and os.environ.get("TERM") != "dumb" return sys.stdout.isatty() and os.environ.get("TERM") != "dumb"
def _split_string(string, delimiter, escape, maxsplit=None):
result = []
current_element = []
iterator = iter(string)
count_split = 0
skip_split = False
for character in iterator:
if maxsplit and count_split >= maxsplit:
skip_split = True
if character == escape and not skip_split:
try:
next_character = next(iterator)
if next_character != delimiter and next_character != escape:
# Do not copy the escape character if it is intended to escape either the
# delimiter or the escape character itself. Copy the escape character
# if it is not used to escape either of these characters.
current_element.append(escape)
current_element.append(next_character)
count_split += 1
except StopIteration:
current_element.append(escape)
elif character == delimiter and not skip_split:
result.append("".join(current_element))
current_element = []
count_split += 1
else:
current_element.append(character)
result.append("".join(current_element))
return result
colorama.init(autoreset=True, strip=not _should_do_markup()) colorama.init(autoreset=True, strip=not _should_do_markup())

View File

@ -33,7 +33,7 @@ option1
# the default description with an annotation. # the default description with an annotation.
# @end # @end
# @meta author: [John Doe](https://blog.example.com) # @meta author:value: [John Doe](https://blog.example.com)
``` ```
### `@var` ### `@var`

View File

@ -178,4 +178,4 @@ MIT
## Author ## Author
John Doe [John Doe](https://blog.example.com)

View File

@ -3,7 +3,7 @@
# Role to demonstrate ansible-doctor. It is also possible to overwrite # Role to demonstrate ansible-doctor. It is also possible to overwrite
# the default description with an annotation. # the default description with an annotation.
# @end # @end
# @meta author: [John Doe](https://blog.example.com) # @meta author: [John Doe](https\://blog.example.com)
galaxy_info: galaxy_info:
description: Role to demonstrate ansible-doctor. description: Role to demonstrate ansible-doctor.
author: John Doe author: John Doe