From f222b6a39024f44aed3e12969d062afa053d2b3a Mon Sep 17 00:00:00 2001 From: Joscha Date: Tue, 14 May 2019 16:10:21 +0000 Subject: [PATCH] Add split_by and str-like join --- cheuph/markup.py | 38 ++++++++++++++++++++++++++++++++++++++ 1 file changed, 38 insertions(+) diff --git a/cheuph/markup.py b/cheuph/markup.py index b4b145f..8ed55d8 100644 --- a/cheuph/markup.py +++ b/cheuph/markup.py @@ -261,6 +261,44 @@ class AttributedText: return blocks + def split_by(self, attribute_name: str) -> List[Tuple[Any, "AttributedText"]]: + blocks = [] + + chunks: List[Chunk] = [] + attribute: Any = None + + for chunk in self._chunks: + chunk_attr = chunk.attributes.get(attribute_name) + + if chunks: + if attribute == chunk_attr: + chunks.append(chunk) + else: + blocks.append((self.from_chunks(chunks), attribute)) + + chunks = [chunk] + attribute = chunk_attr + else: + chunks.append(chunk) + attribute = chunk_attr + + if chunks: + blocks.append((self.from_chunks(chunks), attribute)) + + return blocks + + def join(self, segments: Iterable["AttributedText"]) -> "AttributedText": + interspersed = segments[:1] + for segment in segments[1:]: + interspersed.append(self) + interspersed.append(segment) + + chunks = [] + for segment in interspersed: + chunks.extend(segment.chunks) + + return self.from_chunks(chunks) + def set(self, name: str, value: Any,