MCPcopy Create free account
hub / github.com/PageBot/PageBot / add

Method add

Lib/pagebot/contexts/basecontext/babelstring.py:739–774  ·  view source on GitHub ↗

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)

Source from the content-addressed store, hash-verified

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.

Callers 15

__add__Method · 0.95
addColorsMethod · 0.80
idCommaString2IdSetFunction · 0.80
unique_refMethod · 0.80
slick.min.jsFile · 0.80
slick.jsFile · 0.80
findStemsMethod · 0.80
findBarsMethod · 0.80
blendMethod · 0.80
validateMethod · 0.80
_getXMLMethod · 0.80
getUnicodeRangeBitsFunction · 0.80

Calls 3

resetMethod · 0.95
BabelRunClass · 0.85
appendMethod · 0.45

Tested by

no test coverage detected