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 mobject.shape_matchers import BackgroundRectangle
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 VMobject
from mobject.types.vectorized_mobject import VectorizedPoint
@ -15,6 +16,7 @@ import operator as op
TEX_MOB_SCALE_FACTOR = 0.05
class TexSymbol(VMobjectFromSVGPathstring):
def pointwise_become_partial(self, mobject, a, b):
# TODO, this assumes a = 0
@ -128,10 +130,12 @@ class SingleStringTexMobject(SVGMobject):
class TexMobject(SingleStringTexMobject):
CONFIG = {
"arg_separator": " ",
"substrings_to_isolate": [],
}
def __init__(self, *tex_strings, **kwargs):
digest_config(self, kwargs)
tex_strings = self.break_up_tex_strings(tex_strings)
self.tex_strings = tex_strings
SingleStringTexMobject.__init__(
self, self.arg_separator.join(tex_strings), **kwargs
@ -141,6 +145,14 @@ class TexMobject(SingleStringTexMobject):
if self.organize_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):
"""
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))
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
of strings which would concatenate to make the full string, and in which
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"]
"""
if len(terms) == 0:
if len(substrings_to_isolate) == 0:
return [full_string]
term = terms[0]
substrings = list(it.chain(*zip(
full_string.split(term),
it.repeat(term)
substring_to_isolate = substrings_to_isolate[0]
all_substrings = list(it.chain(*zip(
full_string.split(substring_to_isolate),
it.repeat(substring_to_isolate)
)))
substrings.pop(-1)
substrings = filter(lambda s: s != "", substrings)
all_substrings.pop(-1)
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(*[
break_up_string_by_terms(substring, *terms[1:])
for substring in substrings
split_string_to_isolate_substrings(s, *substrings_to_isolate)
for s in string_list
]))