Split rich text in to lines, preserving styles. Args: separator (str, optional): String to split on. Defaults to "\\\\n". include_separator (bool, optional): Include the separator in the lines. Defaults to False. allow_blank (bool, optional): Return a bla
(
self,
separator: str = "\n",
*,
include_separator: bool = False,
allow_blank: bool = False,
)
| 1060 | self._spans.extend(text._spans) |
| 1061 | |
| 1062 | def split( |
| 1063 | self, |
| 1064 | separator: str = class="st">"\n", |
| 1065 | *, |
| 1066 | include_separator: bool = False, |
| 1067 | allow_blank: bool = False, |
| 1068 | ) -> Lines: |
| 1069 | class="st">"""Split rich text in to lines, preserving styles. |
| 1070 | |
| 1071 | Args: |
| 1072 | separator (str, optional): String to split on. Defaults to class="st">"\\\\n". |
| 1073 | include_separator (bool, optional): Include the separator in the lines. Defaults to False. |
| 1074 | allow_blank (bool, optional): Return a blank line if the text ends with a separator. Defaults to False. |
| 1075 | |
| 1076 | Returns: |
| 1077 | List[RichText]: A list of rich text, one per line of the original. |
| 1078 | class="st">""" |
| 1079 | assert separator, class="st">"separator must not be empty" |
| 1080 | |
| 1081 | text = self.plain |
| 1082 | if separator not in text: |
| 1083 | return Lines([self.copy()]) |
| 1084 | |
| 1085 | if include_separator: |
| 1086 | lines = self.divide( |
| 1087 | match.end() for match in re.finditer(re.escape(separator), text) |
| 1088 | ) |
| 1089 | else: |
| 1090 | |
| 1091 | def flatten_spans() -> Iterable[int]: |
| 1092 | for match in re.finditer(re.escape(separator), text): |
| 1093 | start, end = match.span() |
| 1094 | yield start |
| 1095 | yield end |
| 1096 | |
| 1097 | lines = Lines( |
| 1098 | line for line in self.divide(flatten_spans()) if line.plain != separator |
| 1099 | ) |
| 1100 | |
| 1101 | if not allow_blank and text.endswith(separator): |
| 1102 | lines.pop() |
| 1103 | |
| 1104 | return lines |
| 1105 | |
| 1106 | def divide(self, offsets: Iterable[int]) -> Lines: |
| 1107 | class="st">"""Divide text into a number of lines at given offsets. |