Replace XML codeblock spaces with tabs

This commit is contained in:
kobewi
2025-05-17 20:00:17 +02:00
committed by Rémi Verschelde
parent 5dd76968d8
commit 13f642d959
122 changed files with 2407 additions and 2432 deletions

View File

@@ -1840,41 +1840,10 @@ def format_text_block(
context: DefinitionBase,
state: State,
) -> str:
# Linebreak + tabs in the XML should become two line breaks unless in a "codeblock"
pos = 0
while True:
pos = text.find("\n", pos)
if pos == -1:
break
pre_text = text[:pos]
indent_level = 0
while pos + 1 < len(text) and text[pos + 1] == "\t":
pos += 1
indent_level += 1
post_text = text[pos + 1 :]
# Handle codeblocks
if (
post_text.startswith("[codeblock]")
or post_text.startswith("[codeblock ")
or post_text.startswith("[gdscript]")
or post_text.startswith("[gdscript ")
or post_text.startswith("[csharp]")
or post_text.startswith("[csharp ")
):
tag_text = post_text[1:].split("]", 1)[0]
tag_state = get_tag_and_args(tag_text)
result = format_codeblock(tag_state, post_text, indent_level, state)
if result is None:
return ""
text = f"{pre_text}{result[0]}"
pos += result[1] - indent_level
# Handle normal text
else:
text = f"{pre_text}\n\n{post_text}"
pos += 2 - indent_level
result = preformat_text_block(text, state)
if result is None:
return ""
text = result
next_brac_pos = text.find("[")
text = escape_rst(text, next_brac_pos)
@@ -2426,6 +2395,56 @@ def format_text_block(
return text
def preformat_text_block(text: str, state: State) -> Optional[str]:
result = ""
codeblock_tag = ""
indent_level = 0
for line in text.splitlines():
stripped_line = line.lstrip("\t")
tab_count = len(line) - len(stripped_line)
if codeblock_tag:
if line == "":
result += "\n"
continue
if tab_count < indent_level:
print_error(f"{state.current_class}.xml: Invalid indentation.", state)
return None
if stripped_line.startswith("[/" + codeblock_tag):
result += stripped_line
codeblock_tag = ""
else:
# Remove extraneous tabs and replace remaining tabs with spaces.
result += "\n" + " " * (tab_count - indent_level + 1) + stripped_line
else:
if (
stripped_line.startswith("[codeblock]")
or stripped_line.startswith("[codeblock ")
or stripped_line.startswith("[gdscript]")
or stripped_line.startswith("[gdscript ")
or stripped_line.startswith("[csharp]")
or stripped_line.startswith("[csharp ")
):
if result:
result += "\n"
result += stripped_line
tag_text = stripped_line[1:].split("]", 1)[0]
tag_state = get_tag_and_args(tag_text)
codeblock_tag = tag_state.name
indent_level = tab_count
else:
# A line break in XML should become two line breaks (unless in a code block).
if result:
result += "\n\n"
result += stripped_line
return result
def format_context_name(context: Union[DefinitionBase, None]) -> str:
context_name: str = "unknown context"
if context is not None:
@@ -2468,50 +2487,6 @@ def escape_rst(text: str, until_pos: int = -1) -> str:
return text
def format_codeblock(
tag_state: TagState, post_text: str, indent_level: int, state: State
) -> Union[Tuple[str, int], None]:
end_pos = post_text.find("[/" + tag_state.name + "]")
if end_pos == -1:
print_error(
f"{state.current_class}.xml: Tag depth mismatch for [{tag_state.name}]: no closing [/{tag_state.name}].",
state,
)
return None
opening_formatted = tag_state.name
if len(tag_state.arguments) > 0:
opening_formatted += " " + tag_state.arguments
code_text = post_text[len(f"[{opening_formatted}]") : end_pos]
post_text = post_text[end_pos:]
# Remove extraneous tabs
code_pos = 0
while True:
code_pos = code_text.find("\n", code_pos)
if code_pos == -1:
break
to_skip = 0
while code_pos + to_skip + 1 < len(code_text) and code_text[code_pos + to_skip + 1] == "\t":
to_skip += 1
if to_skip > indent_level:
print_error(
f"{state.current_class}.xml: Four spaces should be used for indentation within [{tag_state.name}].",
state,
)
if len(code_text[code_pos + to_skip + 1 :]) == 0:
code_text = f"{code_text[:code_pos]}\n"
code_pos += 1
else:
code_text = f"{code_text[:code_pos]}\n {code_text[code_pos + to_skip + 1 :]}"
code_pos += 5 - to_skip
return (f"\n[{opening_formatted}]{code_text}{post_text}", len(f"\n[{opening_formatted}]{code_text}"))
def format_table(f: TextIO, data: List[Tuple[Optional[str], ...]], remove_empty_columns: bool = False) -> None:
if len(data) == 0:
return