Creates a new PabeBotRun instance and add it to self.runs. >>> from pagebot.toolbox.units import pt >>> style0 = dict(fontSize=pt(12)) >>> style1 = dict(fontSize=pt(12)) >>> style2 = dict(fontSize=pt(18)) >>> from pagebot.contexts import getContext >>
(self, s, style=None)
| 737 | return '$%s$' % s.replace('\n',' ') |
| 738 | |
| 739 | def add(self, s, style=None): |
| 740 | """Creates a new PabeBotRun instance and add it to self.runs. |
| 741 | |
| 742 | >>> from pagebot.toolbox.units import pt |
| 743 | >>> style0 = dict(fontSize=pt(12)) |
| 744 | >>> style1 = dict(fontSize=pt(12)) |
| 745 | >>> style2 = dict(fontSize=pt(18)) |
| 746 | >>> from pagebot.contexts import getContext |
| 747 | >>> context = getContext() |
| 748 | >>> bs = BabelString('AB', style0, context=context) |
| 749 | >>> bs.style |
| 750 | {'fontSize': 12pt} |
| 751 | >>> bs.add('CD') # No style, adds to the last run |
| 752 | >>> bs |
| 753 | $ABCD$ |
| 754 | >>> bs.runs |
| 755 | [<BabelRun "ABCD">] |
| 756 | >>> bs.add('EF', style=style0) # Identical style, adds to the last run |
| 757 | >>> bs.runs |
| 758 | [<BabelRun "ABCDEF">] |
| 759 | >>> bs.add('GH', style=style1) # Similar style, adds to the last run |
| 760 | >>> bs.runs |
| 761 | [<BabelRun "ABCDEFGH">] |
| 762 | >>> bs.add('XYZ', style2) # Different style creates a new run |
| 763 | >>> bs.runs |
| 764 | [<BabelRun "ABCDEFGH">, <BabelRun "XYZ">] |
| 765 | >>> len(bs), len(bs.runs) # Total number of characters and number of runs |
| 766 | (11, 2) |
| 767 | """ |
| 768 | if s: |
| 769 | if self.runs and (style is None or self.style == style): |
| 770 | # If styles are matching, then just add. |
| 771 | self.runs[-1].s += str(s) |
| 772 | else: # With incompatible styles, make a new run. |
| 773 | self.runs.append(BabelRun(s, style)) |
| 774 | self.reset() # As we changed the content, cache needs to recalculate. |
| 775 | |
| 776 | def __len__(self): |
| 777 | """Answers total string length. |
no test coverage detected