mirror of
https://github.com/3b1b/manim.git
synced 2025-09-01 00:48:45 +00:00
Refactor LabelledString
This commit is contained in:
parent
82c972b946
commit
7e8b3a4c6b
3 changed files with 424 additions and 302 deletions
|
@ -153,7 +153,7 @@ class TransformMatchingTex(TransformMatchingParts):
|
||||||
return mobject.get_tex()
|
return mobject.get_tex()
|
||||||
|
|
||||||
|
|
||||||
class TransformMatchingString(AnimationGroup):
|
class TransformMatchingStrings(AnimationGroup):
|
||||||
CONFIG = {
|
CONFIG = {
|
||||||
"key_map": dict(),
|
"key_map": dict(),
|
||||||
"transform_mismatches_class": None,
|
"transform_mismatches_class": None,
|
||||||
|
|
|
@ -50,6 +50,7 @@ class LabelledString(_StringSVG):
|
||||||
CONFIG = {
|
CONFIG = {
|
||||||
"base_color": WHITE,
|
"base_color": WHITE,
|
||||||
"use_plain_file": False,
|
"use_plain_file": False,
|
||||||
|
"isolate": [],
|
||||||
}
|
}
|
||||||
|
|
||||||
def __init__(self, string: str, **kwargs):
|
def __init__(self, string: str, **kwargs):
|
||||||
|
@ -57,10 +58,11 @@ class LabelledString(_StringSVG):
|
||||||
super().__init__(**kwargs)
|
super().__init__(**kwargs)
|
||||||
|
|
||||||
def get_file_path(self, use_plain_file: bool = False) -> str:
|
def get_file_path(self, use_plain_file: bool = False) -> str:
|
||||||
if use_plain_file:
|
#if use_plain_file:
|
||||||
content = self.plain_string
|
# content = self.plain_string
|
||||||
else:
|
#else:
|
||||||
content = self.labelled_string
|
# content = self.labelled_string
|
||||||
|
content = self.get_decorated_string(use_plain_file=use_plain_file)
|
||||||
return self.get_file_path_by_content(content)
|
return self.get_file_path_by_content(content)
|
||||||
|
|
||||||
@abstractmethod
|
@abstractmethod
|
||||||
|
@ -87,6 +89,7 @@ class LabelledString(_StringSVG):
|
||||||
glyphs = self.submobjects
|
glyphs = self.submobjects
|
||||||
self.set_fill(self.base_color)
|
self.set_fill(self.base_color)
|
||||||
|
|
||||||
|
# TODO
|
||||||
# Simply pack together adjacent mobjects with the same label.
|
# Simply pack together adjacent mobjects with the same label.
|
||||||
submob_labels, glyphs_lists = self.group_neighbours(
|
submob_labels, glyphs_lists = self.group_neighbours(
|
||||||
glyph_labels, glyphs
|
glyph_labels, glyphs
|
||||||
|
@ -105,14 +108,12 @@ class LabelledString(_StringSVG):
|
||||||
|
|
||||||
# Toolkits
|
# Toolkits
|
||||||
|
|
||||||
@staticmethod
|
def find_spans(self, *patterns: str) -> list[Span]:
|
||||||
def color_to_label(color: ManimColor) -> int:
|
return [
|
||||||
r, g, b = color_to_int_rgb(color)
|
match_obj.span()
|
||||||
rg = r * 256 + g
|
for pattern in patterns
|
||||||
rgb = rg * 256 + b
|
for match_obj in re.finditer(pattern, self.string)
|
||||||
if rgb == 16777215: # white
|
]
|
||||||
return -1
|
|
||||||
return rgb
|
|
||||||
|
|
||||||
@staticmethod
|
@staticmethod
|
||||||
def get_neighbouring_pairs(iterable: Iterable) -> list:
|
def get_neighbouring_pairs(iterable: Iterable) -> list:
|
||||||
|
@ -211,109 +212,233 @@ class LabelledString(_StringSVG):
|
||||||
result.update(other_repl_items)
|
result.update(other_repl_items)
|
||||||
return result
|
return result
|
||||||
|
|
||||||
@property
|
#@property
|
||||||
def skipped_spans(self) -> list[Span]:
|
#def skipped_spans(self) -> list[Span]:
|
||||||
return []
|
# return [
|
||||||
|
# match_obj.span()
|
||||||
|
# for match_obj in re.finditer(r"\s+", self.string)
|
||||||
|
# ]
|
||||||
|
|
||||||
def lstrip(self, index: int) -> int:
|
#def lstrip(self, index: int) -> int:
|
||||||
index_seq = list(it.chain(*self.skipped_spans))
|
# index_seq = list(it.chain(*self.skipped_spans))
|
||||||
region_index = self.find_region_index(index, index_seq)
|
# region_index = self.find_region_index(index, index_seq)
|
||||||
if region_index % 2 == 0:
|
# if region_index % 2 == 0:
|
||||||
return index_seq[region_index + 1]
|
# return index_seq[region_index + 1]
|
||||||
return index
|
# return index
|
||||||
|
|
||||||
def rstrip(self, index: int) -> int:
|
#def rstrip(self, index: int) -> int:
|
||||||
index_seq = list(it.chain(*self.skipped_spans))
|
# index_seq = list(it.chain(*self.skipped_spans))
|
||||||
region_index = self.find_region_index(index - 1, index_seq)
|
# region_index = self.find_region_index(index - 1, index_seq)
|
||||||
if region_index % 2 == 0:
|
# if region_index % 2 == 0:
|
||||||
return index_seq[region_index]
|
# return index_seq[region_index]
|
||||||
return index
|
# return index
|
||||||
|
|
||||||
def strip(self, span: Span) -> Span | None:
|
#def strip(self, span: Span) -> Span | None:
|
||||||
result = (
|
# result = (
|
||||||
self.lstrip(span[0]),
|
# self.lstrip(span[0]),
|
||||||
self.rstrip(span[1])
|
# self.rstrip(span[1])
|
||||||
)
|
# )
|
||||||
if result[0] >= result[1]:
|
# if result[0] >= result[1]:
|
||||||
return None
|
# return None
|
||||||
return result
|
# return result
|
||||||
|
|
||||||
@staticmethod
|
@staticmethod
|
||||||
def lslide(index: int, slid_spans: list[Span]) -> int:
|
def lstrip(index: int, skipped: list[Span]) -> int:
|
||||||
slide_dict = dict(sorted(slid_spans))
|
transfer_dict = dict(sorted(skipped))
|
||||||
while index in slide_dict.keys():
|
while index in transfer_dict.keys():
|
||||||
index = slide_dict[index]
|
index = transfer_dict[index]
|
||||||
return index
|
return index
|
||||||
|
|
||||||
@staticmethod
|
@staticmethod
|
||||||
def rslide(index: int, slid_spans: list[Span]) -> int:
|
def rstrip(index: int, skipped: list[Span]) -> int:
|
||||||
slide_dict = dict(sorted([
|
transfer_dict = dict(sorted([
|
||||||
slide_span[::-1] for slide_span in slid_spans
|
skipped_span[::-1] for skipped_span in skipped
|
||||||
], reverse=True))
|
], reverse=True))
|
||||||
while index in slide_dict.keys():
|
while index in transfer_dict.keys():
|
||||||
index = slide_dict[index]
|
index = transfer_dict[index]
|
||||||
return index
|
return index
|
||||||
|
|
||||||
@staticmethod
|
@staticmethod
|
||||||
def slide(span: Span, slid_spans: list[Span]) -> Span | None:
|
def strip(span: Span, skipped: list[Span]) -> Span | None:
|
||||||
result = (
|
result = (
|
||||||
LabelledString.lslide(span[0], slid_spans),
|
LabelledString.lstrip(span[0], skipped),
|
||||||
LabelledString.rslide(span[1], slid_spans)
|
LabelledString.rstrip(span[1], skipped)
|
||||||
)
|
)
|
||||||
if result[0] >= result[1]:
|
if result[0] >= result[1]:
|
||||||
return None
|
return None
|
||||||
return result
|
return result
|
||||||
|
|
||||||
|
@abstractmethod
|
||||||
|
def get_begin_color_command_str(r: int, g: int, b: int) -> str:
|
||||||
|
return ""
|
||||||
|
|
||||||
|
@abstractmethod
|
||||||
|
def get_end_color_command_str() -> str:
|
||||||
|
return ""
|
||||||
|
|
||||||
|
@staticmethod
|
||||||
|
def color_to_label(color: ManimColor) -> int:
|
||||||
|
r, g, b = color_to_int_rgb(color)
|
||||||
|
rg = r * 256 + g
|
||||||
|
rgb = rg * 256 + b
|
||||||
|
if rgb == 16777215: # white
|
||||||
|
return -1
|
||||||
|
return rgb
|
||||||
|
|
||||||
# Parser
|
# Parser
|
||||||
|
|
||||||
@property
|
@property
|
||||||
def full_span(self) -> Span:
|
def full_span(self) -> Span:
|
||||||
return (0, len(self.string))
|
return (0, len(self.string))
|
||||||
|
|
||||||
def get_substrs_to_isolate(self, substrs: list[str]) -> list[str]:
|
@property
|
||||||
result = list(filter(
|
def space_spans(self) -> list[Span]:
|
||||||
lambda s: s in self.string,
|
return self.find_spans(r"\s+")
|
||||||
remove_list_redundancies(substrs)
|
|
||||||
))
|
@abstractmethod
|
||||||
if "" in result:
|
def internal_specified_spans(self) -> list[Span]:
|
||||||
result.remove("")
|
return []
|
||||||
return result
|
|
||||||
|
|
||||||
@property
|
@property
|
||||||
|
def external_specified_spans(self) -> list[Span]:
|
||||||
|
substrs = remove_list_redundancies(self.isolate)
|
||||||
|
if "" in substrs:
|
||||||
|
substrs.remove("")
|
||||||
|
return self.find_spans(*[
|
||||||
|
re.escape(substr.strip()) for substr in substrs
|
||||||
|
])
|
||||||
|
|
||||||
|
@property
|
||||||
|
def specified_spans(self) -> list[Span]:
|
||||||
|
return remove_list_redundancies([
|
||||||
|
self.full_span,
|
||||||
|
*self.internal_specified_spans,
|
||||||
|
*self.external_specified_spans
|
||||||
|
])
|
||||||
|
|
||||||
|
def get_specified_substrings(self) -> list[str]:
|
||||||
|
return remove_list_redundancies([
|
||||||
|
self.string[slice(*span)]
|
||||||
|
for span in self.specified_spans
|
||||||
|
])
|
||||||
|
|
||||||
|
@abstractmethod
|
||||||
def label_span_list(self) -> list[Span]:
|
def label_span_list(self) -> list[Span]:
|
||||||
return []
|
return []
|
||||||
|
|
||||||
@property
|
@abstractmethod
|
||||||
def inserted_string_pairs(self) -> list[tuple[Span, tuple[str, str]]]:
|
def get_inserted_string_pairs(
|
||||||
|
self, use_plain_file: bool
|
||||||
|
) -> list[tuple[Span, tuple[str, str]]]:
|
||||||
|
return []
|
||||||
|
|
||||||
|
@abstractmethod
|
||||||
|
def command_repl_items(self) -> list[tuple[Span, str]]:
|
||||||
return []
|
return []
|
||||||
|
|
||||||
@property
|
@property
|
||||||
def command_repl_items(self) -> list[tuple[Span, str]]:
|
def command_spans(self) -> list[Span]:
|
||||||
return []
|
return [cmd_span for cmd_span, _ in self.command_repl_items]
|
||||||
|
|
||||||
|
@abstractmethod
|
||||||
|
def remove_commands_in_plain_file(self) -> bool:
|
||||||
|
return True
|
||||||
|
|
||||||
|
#@abstractmethod
|
||||||
|
#def get_command_repl_items(
|
||||||
|
# self, use_plain_file: bool
|
||||||
|
#) -> list[tuple[Span, str]]:
|
||||||
|
# return []
|
||||||
|
|
||||||
|
def get_decorated_string(self, use_plain_file: bool) -> str:
|
||||||
|
if use_plain_file and self.remove_commands_in_plain_file:
|
||||||
|
other_repl_items = []
|
||||||
|
else:
|
||||||
|
other_repl_items = self.command_repl_items
|
||||||
|
span_repl_dict = self.get_span_replacement_dict(
|
||||||
|
self.get_inserted_string_pairs(use_plain_file),
|
||||||
|
other_repl_items
|
||||||
|
)
|
||||||
|
result = self.replace_str_by_spans(self.string, span_repl_dict)
|
||||||
|
|
||||||
|
if not use_plain_file:
|
||||||
|
return result
|
||||||
|
return "".join([
|
||||||
|
self.get_begin_color_command_str(
|
||||||
|
*color_to_int_rgb(self.base_color)
|
||||||
|
),
|
||||||
|
result,
|
||||||
|
self.get_end_color_command_str()
|
||||||
|
])
|
||||||
|
|
||||||
@abstractmethod
|
@abstractmethod
|
||||||
def has_predefined_colors(self) -> bool:
|
def has_predefined_colors(self) -> bool:
|
||||||
return False
|
return False
|
||||||
|
|
||||||
@property
|
#@property
|
||||||
def plain_string(self) -> str:
|
#def plain_string(self) -> str:
|
||||||
return self.string
|
# return self.string
|
||||||
|
|
||||||
|
#@property
|
||||||
|
#def labelled_string(self) -> str:
|
||||||
|
# return self.replace_str_by_spans(
|
||||||
|
# self.string, self.get_span_replacement_dict(
|
||||||
|
# self.inserted_string_pairs,
|
||||||
|
# self.command_repl_items
|
||||||
|
# )
|
||||||
|
# )
|
||||||
|
|
||||||
@property
|
@property
|
||||||
def labelled_string(self) -> str:
|
def additionally_ignored_indices(self) -> list[int]:
|
||||||
return self.replace_str_by_spans(
|
|
||||||
self.string, self.get_span_replacement_dict(
|
|
||||||
self.inserted_string_pairs,
|
|
||||||
self.command_repl_items
|
|
||||||
)
|
|
||||||
)
|
|
||||||
|
|
||||||
@property
|
|
||||||
def ignored_indices_for_submob_strings(self) -> list[int]:
|
|
||||||
return []
|
return []
|
||||||
|
|
||||||
def handle_submob_string(self, substr: str, string_span: Span) -> str:
|
@property
|
||||||
return substr
|
def skipped_spans(self) -> list[Span]:
|
||||||
|
return list(it.chain(
|
||||||
|
self.space_spans,
|
||||||
|
self.command_spans,
|
||||||
|
[
|
||||||
|
(index, index + 1)
|
||||||
|
for index in self.additionally_ignored_indices
|
||||||
|
]
|
||||||
|
))
|
||||||
|
|
||||||
|
@property
|
||||||
|
def containing_labels_dict(self) -> dict[Span, list[int]]:
|
||||||
|
label_span_list = self.label_span_list
|
||||||
|
result = {
|
||||||
|
span: []
|
||||||
|
for span in label_span_list
|
||||||
|
}
|
||||||
|
for span_0 in label_span_list:
|
||||||
|
for span_index, span_1 in enumerate(label_span_list):
|
||||||
|
if self.span_contains(span_0, span_1):
|
||||||
|
result[span_0].append(span_index)
|
||||||
|
elif span_0[0] < span_1[0] < span_0[1] < span_1[1]:
|
||||||
|
string_0, string_1 = [
|
||||||
|
self.string[slice(*span)]
|
||||||
|
for span in [span_0, span_1]
|
||||||
|
]
|
||||||
|
raise ValueError(
|
||||||
|
"Partially overlapping substrings detected: "
|
||||||
|
f"'{string_0}' and '{string_1}'"
|
||||||
|
)
|
||||||
|
result[self.full_span] = list(range(-1, len(label_span_list)))
|
||||||
|
return result
|
||||||
|
|
||||||
|
def get_cleaned_substr(self, string_span: Span) -> str:
|
||||||
|
span = self.strip(string_span, self.skipped_spans)
|
||||||
|
if span is None:
|
||||||
|
return ""
|
||||||
|
|
||||||
|
span_repl_dict = {
|
||||||
|
tuple([index - span[0] for index in cmd_span]): ""
|
||||||
|
for cmd_span in self.command_spans
|
||||||
|
if self.span_contains(span, cmd_span)
|
||||||
|
}
|
||||||
|
return self.replace_str_by_spans(
|
||||||
|
self.string[slice(*span)], span_repl_dict
|
||||||
|
)
|
||||||
|
|
||||||
def get_submob_strings(self, submob_labels: list[int]) -> list[str]:
|
def get_submob_strings(self, submob_labels: list[int]) -> list[str]:
|
||||||
ordered_spans = [
|
ordered_spans = [
|
||||||
|
@ -343,62 +468,17 @@ class LabelledString(_StringSVG):
|
||||||
(ordered_span_begins[0], *string_span_begins),
|
(ordered_span_begins[0], *string_span_begins),
|
||||||
(*string_span_ends, ordered_span_ends[-1])
|
(*string_span_ends, ordered_span_ends[-1])
|
||||||
))
|
))
|
||||||
|
return [
|
||||||
command_spans = [span for span, _ in self.command_repl_items]
|
self.get_cleaned_substr(string_span)
|
||||||
slid_spans = list(it.chain(
|
for string_span in string_spans
|
||||||
self.skipped_spans,
|
]
|
||||||
command_spans,
|
|
||||||
[
|
|
||||||
(index, index + 1)
|
|
||||||
for index in self.ignored_indices_for_submob_strings
|
|
||||||
]
|
|
||||||
))
|
|
||||||
result = []
|
|
||||||
for string_span in string_spans:
|
|
||||||
string_span = self.slide(string_span, slid_spans)
|
|
||||||
if string_span is None:
|
|
||||||
result.append("")
|
|
||||||
continue
|
|
||||||
|
|
||||||
span_repl_dict = {
|
|
||||||
tuple([index - string_span[0] for index in cmd_span]): ""
|
|
||||||
for cmd_span in command_spans
|
|
||||||
if self.span_contains(string_span, cmd_span)
|
|
||||||
}
|
|
||||||
substr = self.string[slice(*string_span)]
|
|
||||||
substr = self.replace_str_by_spans(substr, span_repl_dict)
|
|
||||||
substr = self.handle_submob_string(substr, string_span)
|
|
||||||
result.append(substr)
|
|
||||||
return result
|
|
||||||
|
|
||||||
# Selector
|
# Selector
|
||||||
|
|
||||||
@property
|
|
||||||
def containing_labels_dict(self) -> dict[Span, list[int]]:
|
|
||||||
label_span_list = self.label_span_list
|
|
||||||
result = {
|
|
||||||
span: []
|
|
||||||
for span in label_span_list
|
|
||||||
}
|
|
||||||
for span_0 in label_span_list:
|
|
||||||
for span_index, span_1 in enumerate(label_span_list):
|
|
||||||
if self.span_contains(span_0, span_1):
|
|
||||||
result[span_0].append(span_index)
|
|
||||||
elif span_0[0] < span_1[0] < span_0[1] < span_1[1]:
|
|
||||||
string_0, string_1 = [
|
|
||||||
self.string[slice(*span)]
|
|
||||||
for span in [span_0, span_1]
|
|
||||||
]
|
|
||||||
raise ValueError(
|
|
||||||
"Partially overlapping substrings detected: "
|
|
||||||
f"'{string_0}' and '{string_1}'"
|
|
||||||
)
|
|
||||||
result[self.full_span] = list(range(-1, len(label_span_list)))
|
|
||||||
return result
|
|
||||||
|
|
||||||
def find_span_components_of_custom_span(
|
def find_span_components_of_custom_span(
|
||||||
self, custom_span: Span
|
self, custom_span: Span
|
||||||
) -> list[Span]:
|
) -> list[Span]:
|
||||||
|
skipped_spans = self.skipped_spans
|
||||||
span_choices = sorted(filter(
|
span_choices = sorted(filter(
|
||||||
lambda span: self.span_contains(custom_span, span),
|
lambda span: self.span_contains(custom_span, span),
|
||||||
self.label_span_list
|
self.label_span_list
|
||||||
|
@ -408,10 +488,10 @@ class LabelledString(_StringSVG):
|
||||||
|
|
||||||
result = []
|
result = []
|
||||||
span_begin, span_end = custom_span
|
span_begin, span_end = custom_span
|
||||||
span_begin = self.rstrip(span_begin)
|
span_begin = self.rstrip(span_begin, skipped_spans)
|
||||||
span_end = self.rstrip(span_end)
|
span_end = self.rstrip(span_end, skipped_spans)
|
||||||
while span_begin != span_end:
|
while span_begin != span_end:
|
||||||
span_begin = self.lstrip(span_begin)
|
span_begin = self.lstrip(span_begin, skipped_spans)
|
||||||
if span_begin not in span_choices_dict.keys():
|
if span_begin not in span_choices_dict.keys():
|
||||||
return []
|
return []
|
||||||
next_begin = span_choices_dict[span_begin]
|
next_begin = span_choices_dict[span_begin]
|
||||||
|
@ -432,8 +512,8 @@ class LabelledString(_StringSVG):
|
||||||
|
|
||||||
def get_parts_by_string(self, substr: str) -> VGroup:
|
def get_parts_by_string(self, substr: str) -> VGroup:
|
||||||
return VGroup(*[
|
return VGroup(*[
|
||||||
self.get_part_by_custom_span(match_obj.span())
|
self.get_part_by_custom_span(span)
|
||||||
for match_obj in re.finditer(re.escape(substr), self.string)
|
for span in self.find_spans(re.escape(substr.strip()))
|
||||||
])
|
])
|
||||||
|
|
||||||
def get_part_by_string(self, substr: str, index: int = 0) -> VMobject:
|
def get_part_by_string(self, substr: str, index: int = 0) -> VMobject:
|
||||||
|
@ -466,23 +546,6 @@ class LabelledString(_StringSVG):
|
||||||
part = self.get_part_by_string(substr, index=index)
|
part = self.get_part_by_string(substr, index=index)
|
||||||
return self.indices_of_part(part)
|
return self.indices_of_part(part)
|
||||||
|
|
||||||
@property
|
|
||||||
def specified_substrings(self) -> list[str]:
|
|
||||||
return []
|
|
||||||
|
|
||||||
def get_specified_substrings(self) -> list[str]:
|
|
||||||
return self.specified_substrings
|
|
||||||
|
|
||||||
@property
|
|
||||||
def isolated_substrings(self) -> list[str]:
|
|
||||||
return remove_list_redundancies([
|
|
||||||
self.string[slice(*span)]
|
|
||||||
for span in self.label_span_list
|
|
||||||
])
|
|
||||||
|
|
||||||
def get_isolated_substrings(self) -> list[str]:
|
|
||||||
return self.isolated_substrings
|
|
||||||
|
|
||||||
def get_string(self) -> str:
|
def get_string(self) -> str:
|
||||||
return self.string
|
return self.string
|
||||||
|
|
||||||
|
@ -492,16 +555,17 @@ class MTex(LabelledString):
|
||||||
"font_size": 48,
|
"font_size": 48,
|
||||||
"alignment": "\\centering",
|
"alignment": "\\centering",
|
||||||
"tex_environment": "align*",
|
"tex_environment": "align*",
|
||||||
"isolate": [],
|
|
||||||
"tex_to_color_map": {},
|
"tex_to_color_map": {},
|
||||||
}
|
}
|
||||||
|
|
||||||
def __init__(self, tex_string: str, **kwargs):
|
def __init__(self, tex_string: str, **kwargs):
|
||||||
|
digest_config(self, kwargs)
|
||||||
tex_string = tex_string.strip()
|
tex_string = tex_string.strip()
|
||||||
# Prevent from passing an empty string.
|
# Prevent from passing an empty string.
|
||||||
if not tex_string:
|
if not tex_string:
|
||||||
tex_string = "\\quad"
|
tex_string = "\\quad"
|
||||||
self.tex_string = tex_string
|
self.tex_string = tex_string
|
||||||
|
self.isolate.extend(self.tex_to_color_map.keys())
|
||||||
super().__init__(tex_string, **kwargs)
|
super().__init__(tex_string, **kwargs)
|
||||||
|
|
||||||
self.set_color_by_tex_to_color_map(self.tex_to_color_map)
|
self.set_color_by_tex_to_color_map(self.tex_to_color_map)
|
||||||
|
@ -515,10 +579,10 @@ class MTex(LabelledString):
|
||||||
self.path_string_config,
|
self.path_string_config,
|
||||||
self.base_color,
|
self.base_color,
|
||||||
self.use_plain_file,
|
self.use_plain_file,
|
||||||
|
self.isolate,
|
||||||
self.tex_string,
|
self.tex_string,
|
||||||
self.alignment,
|
self.alignment,
|
||||||
self.tex_environment,
|
self.tex_environment,
|
||||||
self.isolate,
|
|
||||||
self.tex_to_color_map
|
self.tex_to_color_map
|
||||||
)
|
)
|
||||||
|
|
||||||
|
@ -548,6 +612,28 @@ class MTex(LabelledString):
|
||||||
def tex_to_svg_file_path(tex_file_content: str) -> str:
|
def tex_to_svg_file_path(tex_file_content: str) -> str:
|
||||||
return tex_to_svg_file(tex_file_content)
|
return tex_to_svg_file(tex_file_content)
|
||||||
|
|
||||||
|
# Toolkits
|
||||||
|
|
||||||
|
#@property
|
||||||
|
#def skipped_spans(self) -> list[Span]:
|
||||||
|
# return super().skipped_spans + self.indices_to_spans(
|
||||||
|
# self.script_char_indices
|
||||||
|
# )
|
||||||
|
|
||||||
|
@staticmethod
|
||||||
|
def get_begin_color_command_str(r: int, g: int, b: int) -> str:
|
||||||
|
return "".join([
|
||||||
|
"{{",
|
||||||
|
"\\color[RGB]",
|
||||||
|
"{",
|
||||||
|
",".join(map(str, (r, g, b))),
|
||||||
|
"}"
|
||||||
|
])
|
||||||
|
|
||||||
|
@staticmethod
|
||||||
|
def get_end_color_command_str() -> str:
|
||||||
|
return "}}"
|
||||||
|
|
||||||
# Parser
|
# Parser
|
||||||
|
|
||||||
@property
|
@property
|
||||||
|
@ -559,15 +645,20 @@ class MTex(LabelledString):
|
||||||
if len(match_obj.group()) % 2 == 1
|
if len(match_obj.group()) % 2 == 1
|
||||||
]
|
]
|
||||||
|
|
||||||
def get_brace_indices_lists(self) -> tuple[list[Span], list[Span]]:
|
@staticmethod
|
||||||
string = self.string
|
def get_unescaped_char_indices(*chars: str):
|
||||||
indices = list(filter(
|
return list(filter(
|
||||||
lambda index: index - 1 not in self.backslash_indices,
|
lambda index: index - 1 not in self.backslash_indices,
|
||||||
[
|
[
|
||||||
match_obj.start()
|
match_obj.start()
|
||||||
for match_obj in re.finditer(r"[{}]", string)
|
for char in chars
|
||||||
|
for match_obj in re.finditer(re.escape(char), string)
|
||||||
]
|
]
|
||||||
))
|
))
|
||||||
|
|
||||||
|
def get_brace_indices_lists(self) -> tuple[list[Span], list[Span]]:
|
||||||
|
string = self.string
|
||||||
|
indices = self.get_unescaped_char_indices("{", "}")
|
||||||
left_brace_indices = []
|
left_brace_indices = []
|
||||||
right_brace_indices = []
|
right_brace_indices = []
|
||||||
left_brace_indices_stack = []
|
left_brace_indices_stack = []
|
||||||
|
@ -594,20 +685,8 @@ class MTex(LabelledString):
|
||||||
return self.get_brace_indices_lists()[1]
|
return self.get_brace_indices_lists()[1]
|
||||||
|
|
||||||
@property
|
@property
|
||||||
def skipped_spans(self) -> list[Span]:
|
def script_char_indices(self) -> list[Span]:
|
||||||
return [
|
return self.get_unescaped_char_indices("_", "^")
|
||||||
match_obj.span()
|
|
||||||
for match_obj in re.finditer(r"\s*([_^])\s*|(\s+)", self.string)
|
|
||||||
if match_obj.group(2) is not None
|
|
||||||
or match_obj.start(1) - 1 not in self.backslash_indices
|
|
||||||
]
|
|
||||||
|
|
||||||
@property
|
|
||||||
def script_char_spans(self) -> list[Span]:
|
|
||||||
return list(filter(
|
|
||||||
lambda span: self.string[slice(*span)].strip(),
|
|
||||||
self.skipped_spans
|
|
||||||
))
|
|
||||||
|
|
||||||
@property
|
@property
|
||||||
def script_content_spans(self) -> list[Span]:
|
def script_content_spans(self) -> list[Span]:
|
||||||
|
@ -615,7 +694,8 @@ class MTex(LabelledString):
|
||||||
brace_indices_dict = dict(zip(
|
brace_indices_dict = dict(zip(
|
||||||
self.left_brace_indices, self.right_brace_indices
|
self.left_brace_indices, self.right_brace_indices
|
||||||
))
|
))
|
||||||
for _, span_begin in self.script_char_spans:
|
for index in self.script_char_indices:
|
||||||
|
span_begin = self.lstrip(index, self.space_spans)
|
||||||
if span_begin in brace_indices_dict.keys():
|
if span_begin in brace_indices_dict.keys():
|
||||||
span_end = brace_indices_dict[span_begin] + 1
|
span_end = brace_indices_dict[span_begin] + 1
|
||||||
else:
|
else:
|
||||||
|
@ -635,7 +715,7 @@ class MTex(LabelledString):
|
||||||
return result
|
return result
|
||||||
|
|
||||||
@property
|
@property
|
||||||
def double_braces_spans(self) -> list[Span]:
|
def internal_specified_spans(self) -> list[Span]:
|
||||||
# Match paired double braces (`{{...}}`).
|
# Match paired double braces (`{{...}}`).
|
||||||
result = []
|
result = []
|
||||||
reversed_brace_indices_dict = dict(zip(
|
reversed_brace_indices_dict = dict(zip(
|
||||||
|
@ -659,28 +739,16 @@ class MTex(LabelledString):
|
||||||
return result
|
return result
|
||||||
|
|
||||||
@property
|
@property
|
||||||
def additional_substrings(self) -> list[str]:
|
def label_span_list(self) -> list[Span]:
|
||||||
return self.get_substrs_to_isolate(list(it.chain(
|
|
||||||
self.tex_to_color_map.keys(),
|
|
||||||
self.isolate
|
|
||||||
)))
|
|
||||||
|
|
||||||
def get_label_span_list(self, extended: bool) -> list[Span]:
|
|
||||||
script_content_spans = self.script_content_spans
|
script_content_spans = self.script_content_spans
|
||||||
script_spans = [
|
script_spans = [
|
||||||
(script_char_span[0], script_content_span[1])
|
(self.rstrip(index, self.space_spans), script_content_span[1])
|
||||||
for script_char_span, script_content_span in zip(
|
for index, script_content_span in zip(
|
||||||
self.script_char_spans, script_content_spans
|
self.script_char_indices, script_content_spans
|
||||||
)
|
)
|
||||||
]
|
]
|
||||||
spans = remove_list_redundancies([
|
spans = remove_list_redundancies([
|
||||||
self.full_span,
|
*self.specified_spans,
|
||||||
*self.double_braces_spans,
|
|
||||||
*filter(lambda stripped_span: stripped_span is not None, [
|
|
||||||
self.strip(match_obj.span())
|
|
||||||
for substr in self.additional_substrings
|
|
||||||
for match_obj in re.finditer(re.escape(substr), self.string)
|
|
||||||
]),
|
|
||||||
*script_content_spans
|
*script_content_spans
|
||||||
])
|
])
|
||||||
result = []
|
result = []
|
||||||
|
@ -688,37 +756,53 @@ class MTex(LabelledString):
|
||||||
if span in script_content_spans:
|
if span in script_content_spans:
|
||||||
continue
|
continue
|
||||||
span_begin, span_end = span
|
span_begin, span_end = span
|
||||||
shrinked_end = self.rslide(span_end, script_spans)
|
shrinked_end = self.rstrip(span_end, script_spans)
|
||||||
if span_begin >= shrinked_end:
|
if span_begin >= shrinked_end:
|
||||||
continue
|
continue
|
||||||
shrinked_span = (span_begin, shrinked_end)
|
result.append((span_begin, self.lstrip(span_end, script_spans)))
|
||||||
if shrinked_span in result:
|
|
||||||
continue
|
|
||||||
result.append(shrinked_span)
|
|
||||||
|
|
||||||
if extended:
|
#if extended:
|
||||||
result = [
|
# result = [
|
||||||
(span_begin, self.lslide(span_end, script_spans))
|
# (span_begin, self.lstrip(span_end, script_spans))
|
||||||
for span_begin, span_end in result
|
# for span_begin, span_end in result
|
||||||
]
|
# ]
|
||||||
return script_content_spans + result
|
return script_content_spans + remove_list_redundancies(result)
|
||||||
|
|
||||||
@property
|
#@property
|
||||||
def label_span_list(self) -> list[Span]:
|
#def label_span_list(self) -> list[Span]:
|
||||||
return self.get_label_span_list(extended=False)
|
# return self.get_label_span_list(extended=False)
|
||||||
|
|
||||||
@property
|
def get_inserted_string_pairs(
|
||||||
def inserted_string_pairs(self) -> list[tuple[Span, tuple[str, str]]]:
|
self, use_plain_file: bool
|
||||||
|
) -> list[tuple[Span, tuple[str, str]]]:
|
||||||
|
if use_plain_file:
|
||||||
|
return []
|
||||||
return [
|
return [
|
||||||
(span, (
|
(span, (
|
||||||
"{{" + self.get_color_command_by_label(label),
|
self.get_begin_color_command_str(
|
||||||
"}}"
|
label // 256 // 256,
|
||||||
|
label // 256 % 256,
|
||||||
|
label % 256
|
||||||
|
),
|
||||||
|
self.get_end_color_command_str()
|
||||||
))
|
))
|
||||||
for label, span in enumerate(
|
for label, span in enumerate(
|
||||||
self.get_label_span_list(extended=True)
|
self.label_span_list
|
||||||
)
|
)
|
||||||
]
|
]
|
||||||
|
|
||||||
|
#@property
|
||||||
|
#def inserted_string_pairs(self) -> list[tuple[Span, tuple[str, str]]]:
|
||||||
|
# return [
|
||||||
|
# (span, (
|
||||||
|
# "{{" + self.get_color_command_by_label(label),
|
||||||
|
# "}}"
|
||||||
|
# ))
|
||||||
|
# for label, span in enumerate(
|
||||||
|
# self.get_label_span_list(extended=True)
|
||||||
|
# )
|
||||||
|
# ]
|
||||||
|
|
||||||
@property
|
@property
|
||||||
def command_repl_items(self) -> list[tuple[Span, str]]:
|
def command_repl_items(self) -> list[tuple[Span, str]]:
|
||||||
color_related_command_dict = {
|
color_related_command_dict = {
|
||||||
|
@ -754,39 +838,44 @@ class MTex(LabelledString):
|
||||||
result.append(((span_begin, span_end), repl_str))
|
result.append(((span_begin, span_end), repl_str))
|
||||||
return result
|
return result
|
||||||
|
|
||||||
|
@property
|
||||||
|
def remove_commands_in_plain_file(self) -> bool:
|
||||||
|
return True
|
||||||
|
|
||||||
@property
|
@property
|
||||||
def has_predefined_colors(self) -> bool:
|
def has_predefined_colors(self) -> bool:
|
||||||
return bool(self.command_repl_items)
|
return bool(self.command_repl_items)
|
||||||
|
|
||||||
@staticmethod
|
#@staticmethod
|
||||||
def get_color_command_by_label(label: int) -> str:
|
#def get_color_command_by_label(label: int) -> str:
|
||||||
if label == -1:
|
# if label == -1:
|
||||||
label = 16777215 # white
|
# label = 16777215 # white
|
||||||
rg, b = divmod(label, 256)
|
# rg, b = divmod(label, 256)
|
||||||
r, g = divmod(rg, 256)
|
# r, g = divmod(rg, 256)
|
||||||
return "".join([
|
# return "".join([
|
||||||
"\\color[RGB]",
|
# "\\color[RGB]",
|
||||||
"{",
|
# "{",
|
||||||
",".join(map(str, (r, g, b))),
|
# ",".join(map(str, (r, g, b))),
|
||||||
"}"
|
# "}"
|
||||||
])
|
# ])
|
||||||
|
|
||||||
|
#@property
|
||||||
|
#def plain_string(self) -> str:
|
||||||
|
# return "".join([
|
||||||
|
# "{{",
|
||||||
|
# self.get_color_command_by_label(
|
||||||
|
# self.color_to_label(self.base_color)
|
||||||
|
# ),
|
||||||
|
# self.string,
|
||||||
|
# "}}"
|
||||||
|
# ])
|
||||||
|
|
||||||
@property
|
@property
|
||||||
def plain_string(self) -> str:
|
def additionally_ignored_indices(self) -> list[int]:
|
||||||
return "".join([
|
|
||||||
"{{",
|
|
||||||
self.get_color_command_by_label(
|
|
||||||
self.color_to_label(self.base_color)
|
|
||||||
),
|
|
||||||
self.string,
|
|
||||||
"}}"
|
|
||||||
])
|
|
||||||
|
|
||||||
@property
|
|
||||||
def ignored_indices_for_submob_strings(self) -> list[int]:
|
|
||||||
return self.left_brace_indices + self.right_brace_indices
|
return self.left_brace_indices + self.right_brace_indices
|
||||||
|
|
||||||
def handle_submob_string(self, substr: str, string_span: Span) -> str:
|
def get_cleaned_substr(self, string_span: Span) -> str:
|
||||||
|
substr = super().get_cleaned_substr(string_span)
|
||||||
unclosed_left_braces = 0
|
unclosed_left_braces = 0
|
||||||
unclosed_right_braces = 0
|
unclosed_right_braces = 0
|
||||||
for index in range(*string_span):
|
for index in range(*string_span):
|
||||||
|
@ -803,13 +892,6 @@ class MTex(LabelledString):
|
||||||
unclosed_left_braces * "}"
|
unclosed_left_braces * "}"
|
||||||
])
|
])
|
||||||
|
|
||||||
@property
|
|
||||||
def specified_substrings(self) -> list[str]:
|
|
||||||
return remove_list_redundancies([
|
|
||||||
self.string[slice(*double_braces_span)]
|
|
||||||
for double_braces_span in self.double_braces_spans
|
|
||||||
] + self.additional_substrings)
|
|
||||||
|
|
||||||
# Method alias
|
# Method alias
|
||||||
|
|
||||||
def get_parts_by_tex(self, substr: str) -> VGroup:
|
def get_parts_by_tex(self, substr: str) -> VGroup:
|
||||||
|
|
|
@ -131,15 +131,16 @@ class MarkupText(LabelledString):
|
||||||
"t2w": {},
|
"t2w": {},
|
||||||
"global_config": {},
|
"global_config": {},
|
||||||
"local_configs": {},
|
"local_configs": {},
|
||||||
"isolate": [],
|
|
||||||
}
|
}
|
||||||
|
|
||||||
def __init__(self, text: str, **kwargs):
|
def __init__(self, text: str, **kwargs):
|
||||||
self.full2short(kwargs)
|
self.full2short(kwargs)
|
||||||
digest_config(self, kwargs)
|
digest_config(self, kwargs)
|
||||||
validate_error = MarkupUtils.validate(text)
|
|
||||||
if validate_error:
|
if self.is_markup:
|
||||||
raise ValueError(validate_error)
|
validate_error = MarkupUtils.validate(text)
|
||||||
|
if validate_error:
|
||||||
|
raise ValueError(validate_error)
|
||||||
|
|
||||||
self.text = text
|
self.text = text
|
||||||
super().__init__(text, **kwargs)
|
super().__init__(text, **kwargs)
|
||||||
|
@ -162,6 +163,7 @@ class MarkupText(LabelledString):
|
||||||
self.path_string_config,
|
self.path_string_config,
|
||||||
self.base_color,
|
self.base_color,
|
||||||
self.use_plain_file,
|
self.use_plain_file,
|
||||||
|
self.isolate,
|
||||||
self.text,
|
self.text,
|
||||||
self.is_markup,
|
self.is_markup,
|
||||||
self.font_size,
|
self.font_size,
|
||||||
|
@ -178,8 +180,7 @@ class MarkupText(LabelledString):
|
||||||
self.t2s,
|
self.t2s,
|
||||||
self.t2w,
|
self.t2w,
|
||||||
self.global_config,
|
self.global_config,
|
||||||
self.local_configs,
|
self.local_configs
|
||||||
self.isolate
|
|
||||||
)
|
)
|
||||||
|
|
||||||
def full2short(self, config: dict) -> None:
|
def full2short(self, config: dict) -> None:
|
||||||
|
@ -288,6 +289,15 @@ class MarkupText(LabelledString):
|
||||||
MarkupText.get_neighbouring_pairs(index_seq), attr_dict_list[:-1]
|
MarkupText.get_neighbouring_pairs(index_seq), attr_dict_list[:-1]
|
||||||
))
|
))
|
||||||
|
|
||||||
|
@staticmethod
|
||||||
|
def get_begin_color_command_str(r: int, g: int, b: int) -> str:
|
||||||
|
color_hex = "#{:02x}{:02x}{:02x}".format(r, g, b).upper()
|
||||||
|
return MarkupText.get_begin_tag_str({"foreground": color_hex})
|
||||||
|
|
||||||
|
@staticmethod
|
||||||
|
def get_end_color_command_str() -> str:
|
||||||
|
return MarkupText.get_end_tag_str()
|
||||||
|
|
||||||
# Parser
|
# Parser
|
||||||
|
|
||||||
@property
|
@property
|
||||||
|
@ -319,17 +329,12 @@ class MarkupText(LabelledString):
|
||||||
if end_match_obj.group(3):
|
if end_match_obj.group(3):
|
||||||
raise ValueError("Attributes shan't exist in ending tags")
|
raise ValueError("Attributes shan't exist in ending tags")
|
||||||
if tag_name == "span":
|
if tag_name == "span":
|
||||||
attr_dict = dict([
|
attr_dict = {
|
||||||
(
|
match.group(1): match.group(2) or match.group(3)
|
||||||
MarkupText.convert_attr_key(match.group(1)),
|
|
||||||
MarkupText.convert_attr_val(
|
|
||||||
match.group(2) or match.group(3)
|
|
||||||
)
|
|
||||||
)
|
|
||||||
for match in re.finditer(
|
for match in re.finditer(
|
||||||
attr_pattern, begin_match_obj.group(3)
|
attr_pattern, begin_match_obj.group(3)
|
||||||
)
|
)
|
||||||
])
|
}
|
||||||
elif tag_name in TAG_TO_ATTR_DICT.keys():
|
elif tag_name in TAG_TO_ATTR_DICT.keys():
|
||||||
if begin_match_obj.group(3):
|
if begin_match_obj.group(3):
|
||||||
raise ValueError(
|
raise ValueError(
|
||||||
|
@ -372,6 +377,19 @@ class MarkupText(LabelledString):
|
||||||
for key, val in result
|
for key, val in result
|
||||||
]
|
]
|
||||||
|
|
||||||
|
@property
|
||||||
|
def local_attr_items_from_markup(self) -> list[tuple[Span, str, str]]:
|
||||||
|
return sorted([
|
||||||
|
(
|
||||||
|
(begin_tag_span[0], end_tag_span[1]),
|
||||||
|
self.convert_attr_key(key),
|
||||||
|
self.convert_attr_val(val)
|
||||||
|
)
|
||||||
|
for begin_tag_span, end_tag_span, attr_dict
|
||||||
|
in self.tag_items_from_markup
|
||||||
|
for key, val in attr_dict.items()
|
||||||
|
])
|
||||||
|
|
||||||
@property
|
@property
|
||||||
def local_attr_items_from_config(self) -> list[tuple[Span, str, str]]:
|
def local_attr_items_from_config(self) -> list[tuple[Span, str, str]]:
|
||||||
result = [
|
result = [
|
||||||
|
@ -383,11 +401,11 @@ class MarkupText(LabelledString):
|
||||||
(self.t2w, "font_weight")
|
(self.t2w, "font_weight")
|
||||||
)
|
)
|
||||||
for word_or_span, val in t2x_dict.items()
|
for word_or_span, val in t2x_dict.items()
|
||||||
for text_span in self.find_spans(word_or_span)
|
for text_span in self.find_spans_by_word_or_span(word_or_span)
|
||||||
] + [
|
] + [
|
||||||
(text_span, key, val)
|
(text_span, key, val)
|
||||||
for word_or_span, local_config in self.local_configs.items()
|
for word_or_span, local_config in self.local_configs.items()
|
||||||
for text_span in self.find_spans(word_or_span)
|
for text_span in self.find_spans_by_word_or_span(word_or_span)
|
||||||
for key, val in local_config.items()
|
for key, val in local_config.items()
|
||||||
]
|
]
|
||||||
return [
|
return [
|
||||||
|
@ -399,45 +417,45 @@ class MarkupText(LabelledString):
|
||||||
for text_span, key, val in result
|
for text_span, key, val in result
|
||||||
]
|
]
|
||||||
|
|
||||||
def find_spans(self, word_or_span: str | Span) -> list[Span]:
|
def find_spans_by_word_or_span(
|
||||||
|
self, word_or_span: str | Span
|
||||||
|
) -> list[Span]:
|
||||||
if isinstance(word_or_span, tuple):
|
if isinstance(word_or_span, tuple):
|
||||||
return [word_or_span]
|
return [word_or_span]
|
||||||
|
|
||||||
return [
|
return self.find_spans(re.escape(word_or_span))
|
||||||
match_obj.span()
|
|
||||||
for match_obj in re.finditer(re.escape(word_or_span), self.string)
|
#@property
|
||||||
]
|
#def skipped_spans(self) -> list[Span]:
|
||||||
|
# return [
|
||||||
|
# match_obj.span()
|
||||||
|
# for match_obj in re.finditer(r"\s+", self.string)
|
||||||
|
# ]
|
||||||
|
|
||||||
|
#@property
|
||||||
|
#def additional_substrings(self) -> list[str]:
|
||||||
|
# return self.get_substrs_to_isolate(self.isolate)
|
||||||
|
|
||||||
@property
|
@property
|
||||||
def skipped_spans(self) -> list[Span]:
|
def internal_specified_spans(self) -> list[Span]:
|
||||||
return [
|
return [
|
||||||
match_obj.span()
|
markup_span
|
||||||
for match_obj in re.finditer(r"\s+", self.string)
|
for markup_span, _, _ in self.local_attr_items_from_markup
|
||||||
]
|
]
|
||||||
|
|
||||||
@property
|
@property
|
||||||
def label_span_list(self) -> list[Span]:
|
def label_span_list(self) -> list[Span]:
|
||||||
breakup_indices = [
|
entity_spans = [span for span, _ in self.command_repl_items]
|
||||||
index
|
if self.is_markup:
|
||||||
for pattern in [
|
entity_spans += self.find_spans(r"&.*?;")
|
||||||
r"\s+",
|
|
||||||
r"\b",
|
|
||||||
*[
|
|
||||||
re.escape(substr)
|
|
||||||
for substr in self.get_substrs_to_isolate(self.isolate)
|
|
||||||
]
|
|
||||||
]
|
|
||||||
for match_obj in re.finditer(pattern, self.string)
|
|
||||||
for index in match_obj.span()
|
|
||||||
]
|
|
||||||
breakup_indices = sorted(filter(
|
breakup_indices = sorted(filter(
|
||||||
lambda index: not any([
|
lambda index: not any([
|
||||||
span[0] < index < span[1]
|
span[0] < index < span[1]
|
||||||
for span, _ in self.command_repl_items
|
for span in entity_spans
|
||||||
]),
|
]),
|
||||||
remove_list_redundancies([
|
remove_list_redundancies(list(it.chain(*(
|
||||||
*self.full_span, *breakup_indices
|
self.specified_spans + self.find_spans(r"\s+", r"\b")
|
||||||
])
|
))))
|
||||||
))
|
))
|
||||||
return list(filter(
|
return list(filter(
|
||||||
lambda span: self.string[slice(*span)].strip(),
|
lambda span: self.string[slice(*span)].strip(),
|
||||||
|
@ -451,20 +469,15 @@ class MarkupText(LabelledString):
|
||||||
(self.full_span, key, val)
|
(self.full_span, key, val)
|
||||||
for key, val in self.global_attr_items_from_config
|
for key, val in self.global_attr_items_from_config
|
||||||
],
|
],
|
||||||
sorted([
|
self.local_attr_items_from_markup,
|
||||||
((begin_tag_span[0], end_tag_span[1]), key, val)
|
|
||||||
for begin_tag_span, end_tag_span, attr_dict
|
|
||||||
in self.tag_items_from_markup
|
|
||||||
for key, val in attr_dict.items()
|
|
||||||
]),
|
|
||||||
self.local_attr_items_from_config
|
self.local_attr_items_from_config
|
||||||
))
|
))
|
||||||
|
|
||||||
def get_inserted_string_pairs(
|
def get_inserted_string_pairs(
|
||||||
self, use_label: bool
|
self, use_plain_file: bool
|
||||||
) -> list[tuple[Span, tuple[str, str]]]:
|
) -> list[tuple[Span, tuple[str, str]]]:
|
||||||
attr_items = self.predefined_items
|
attr_items = self.predefined_items
|
||||||
if use_label:
|
if not use_plain_file:
|
||||||
attr_items = [
|
attr_items = [
|
||||||
(span, key, WHITE if key in COLOR_RELATED_KEYS else val)
|
(span, key, WHITE if key in COLOR_RELATED_KEYS else val)
|
||||||
for span, key, val in attr_items
|
for span, key, val in attr_items
|
||||||
|
@ -480,17 +493,37 @@ class MarkupText(LabelledString):
|
||||||
for span, attr_dict in self.merge_attr_items(attr_items)
|
for span, attr_dict in self.merge_attr_items(attr_items)
|
||||||
]
|
]
|
||||||
|
|
||||||
@property
|
#@property
|
||||||
def inserted_string_pairs(self) -> list[tuple[Span, tuple[str, str]]]:
|
#def inserted_string_pairs(self) -> list[tuple[Span, tuple[str, str]]]:
|
||||||
return self.get_inserted_string_pairs(use_label=True)
|
# return self.get_inserted_string_pairs(use_label=True)
|
||||||
|
|
||||||
@property
|
@property
|
||||||
def command_repl_items(self) -> list[tuple[Span, str]]:
|
def command_repl_items(self) -> list[tuple[Span, str]]:
|
||||||
return [
|
result = [
|
||||||
(tag_span, "")
|
(tag_span, "")
|
||||||
for begin_tag, end_tag, _ in self.tag_items_from_markup
|
for begin_tag, end_tag, _ in self.tag_items_from_markup
|
||||||
for tag_span in (begin_tag, end_tag)
|
for tag_span in (begin_tag, end_tag)
|
||||||
]
|
]
|
||||||
|
if not self.is_markup:
|
||||||
|
result += [
|
||||||
|
(span, escaped)
|
||||||
|
for char, escaped in (
|
||||||
|
("&", "&"),
|
||||||
|
(">", ">"),
|
||||||
|
("<", "<")
|
||||||
|
)
|
||||||
|
for span in self.find_spans(re.escape(char))
|
||||||
|
]
|
||||||
|
return result
|
||||||
|
|
||||||
|
def remove_commands_in_plain_file(self) -> bool:
|
||||||
|
return False
|
||||||
|
|
||||||
|
#@abstractmethod
|
||||||
|
#def get_command_repl_items(
|
||||||
|
# self, use_plain_file: bool
|
||||||
|
#) -> list[tuple[Span, str]]:
|
||||||
|
# return []
|
||||||
|
|
||||||
@property
|
@property
|
||||||
def has_predefined_colors(self) -> bool:
|
def has_predefined_colors(self) -> bool:
|
||||||
|
@ -499,18 +532,25 @@ class MarkupText(LabelledString):
|
||||||
for _, key, _ in self.predefined_items
|
for _, key, _ in self.predefined_items
|
||||||
])
|
])
|
||||||
|
|
||||||
@property
|
#@property
|
||||||
def plain_string(self) -> str:
|
#def plain_string(self) -> str:
|
||||||
return "".join([
|
# return "".join([
|
||||||
self.get_begin_tag_str({"foreground": self.base_color}),
|
# self.get_begin_tag_str({"foreground": self.base_color}),
|
||||||
self.replace_str_by_spans(
|
# self.replace_str_by_spans(
|
||||||
self.string, self.get_span_replacement_dict(
|
# self.string, self.get_span_replacement_dict(
|
||||||
self.get_inserted_string_pairs(use_label=False),
|
# self.get_inserted_string_pairs(use_label=False),
|
||||||
self.command_repl_items
|
# self.command_repl_items
|
||||||
)
|
# )
|
||||||
),
|
# ),
|
||||||
self.get_end_tag_str()
|
# self.get_end_tag_str()
|
||||||
])
|
# ])
|
||||||
|
|
||||||
|
#@property
|
||||||
|
#def specified_substrings(self) -> list[str]: # TODO: clean up and merge
|
||||||
|
# return remove_list_redundancies([
|
||||||
|
# self.get_cleaned_substr(markup_span)
|
||||||
|
# for markup_span, _, _ in self.local_attr_items_from_markup
|
||||||
|
# ] + self.additional_substrings)
|
||||||
|
|
||||||
# Method alias
|
# Method alias
|
||||||
|
|
||||||
|
|
Loading…
Add table
Reference in a new issue