Added substrings_to_isolate to TexMobject configuration

This commit is contained in:
Grant Sanderson
2018-05-05 20:16:20 -07:00
parent dc6c47f13c
commit fb5f1517a9
2 changed files with 33 additions and 11 deletions

View File

@ -4,6 +4,7 @@ from svg_mobject import SVGMobject
from svg_mobject import VMobjectFromSVGPathstring from svg_mobject import VMobjectFromSVGPathstring
from mobject.shape_matchers import BackgroundRectangle from mobject.shape_matchers import BackgroundRectangle
from utils.config_ops import digest_config from utils.config_ops import digest_config
from utils.strings import split_string_list_to_isolate_substring
from mobject.types.vectorized_mobject import VGroup from mobject.types.vectorized_mobject import VGroup
from mobject.types.vectorized_mobject import VMobject from mobject.types.vectorized_mobject import VMobject
from mobject.types.vectorized_mobject import VectorizedPoint from mobject.types.vectorized_mobject import VectorizedPoint
@ -15,6 +16,7 @@ import operator as op
TEX_MOB_SCALE_FACTOR = 0.05 TEX_MOB_SCALE_FACTOR = 0.05
class TexSymbol(VMobjectFromSVGPathstring): class TexSymbol(VMobjectFromSVGPathstring):
def pointwise_become_partial(self, mobject, a, b): def pointwise_become_partial(self, mobject, a, b):
# TODO, this assumes a = 0 # TODO, this assumes a = 0
@ -128,10 +130,12 @@ class SingleStringTexMobject(SVGMobject):
class TexMobject(SingleStringTexMobject): class TexMobject(SingleStringTexMobject):
CONFIG = { CONFIG = {
"arg_separator": " ", "arg_separator": " ",
"substrings_to_isolate": [],
} }
def __init__(self, *tex_strings, **kwargs): def __init__(self, *tex_strings, **kwargs):
digest_config(self, kwargs) digest_config(self, kwargs)
tex_strings = self.break_up_tex_strings(tex_strings)
self.tex_strings = tex_strings self.tex_strings = tex_strings
SingleStringTexMobject.__init__( SingleStringTexMobject.__init__(
self, self.arg_separator.join(tex_strings), **kwargs self, self.arg_separator.join(tex_strings), **kwargs
@ -141,6 +145,14 @@ class TexMobject(SingleStringTexMobject):
if self.organize_left_to_right: if self.organize_left_to_right:
self.organize_submobjects_left_to_right() self.organize_submobjects_left_to_right()
def break_up_tex_strings(self, tex_strings):
split_list = split_string_list_to_isolate_substring(
tex_strings, *self.substrings_to_isolate
)
split_list = map(str.strip, split_list)
split_list = filter(lambda s: s != '', split_list)
return split_list
def break_up_by_substrings(self): def break_up_by_substrings(self):
""" """
Reorganize existing submojects one layer Reorganize existing submojects one layer

View File

@ -27,25 +27,35 @@ def complex_string(complex_num):
return filter(lambda c: c not in "()", str(complex_num)) return filter(lambda c: c not in "()", str(complex_num))
def break_up_string_by_terms(full_string, *terms): def split_string_to_isolate_substrings(full_string, *substrings_to_isolate):
""" """
Given a string, and an arbitrary number of possible substrings, returns a list Given a string, and an arbitrary number of possible substrings, returns a list
of strings which would concatenate to make the full string, and in which of strings which would concatenate to make the full string, and in which
these special substrings appear as their own elements. these special substrings appear as their own elements.
For example, break_up_string_by_terms("to be or not to be", "to", "be") would For example, split_string_to_isolate_substrings("to be or not to be", "to", "be") would
return ["to", " ", "be", " or not ", "to", " ", "be"] return ["to", " ", "be", " or not ", "to", " ", "be"]
""" """
if len(terms) == 0: if len(substrings_to_isolate) == 0:
return [full_string] return [full_string]
term = terms[0] substring_to_isolate = substrings_to_isolate[0]
substrings = list(it.chain(*zip( all_substrings = list(it.chain(*zip(
full_string.split(term), full_string.split(substring_to_isolate),
it.repeat(term) it.repeat(substring_to_isolate)
))) )))
substrings.pop(-1) all_substrings.pop(-1)
substrings = filter(lambda s: s != "", substrings) all_substrings = filter(lambda s: s != "", all_substrings)
return split_string_list_to_isolate_substring(
all_substrings, *substrings_to_isolate[1:]
)
def split_string_list_to_isolate_substring(string_list, *substrings_to_isolate):
"""
Similar to split_string_to_isolate_substrings, but the first argument
is a list of strings, thought of as something already broken up a bit.
"""
return list(it.chain(*[ return list(it.chain(*[
break_up_string_by_terms(substring, *terms[1:]) split_string_to_isolate_substrings(s, *substrings_to_isolate)
for substring in substrings for s in string_list
])) ]))