def syllables(element, span_style=None): if isinstance(element, xsc.Text): u = element.__unicode__() match = starts_word_re.search(u) starts_word = (match is not None) for letters, whitespace in word_re.findall(u): ends_word = ( whitespace != u"" ) if ends_word: whitespace_style = span_style else: whitespace_style = None yield ( elements.syllable(letters, span_style, whitespace_style), starts_word, ends_word, ) starts_word = False else: mystyle = style(element) assert mystyle.display == "inline", ValueError( "Inline elements may not contain block elements.") for child in element: for syllable in syllables(child, mystyle): yield syllable
def split_syllable_by_style(syllable): ret = [] current = None for letter, old_syllable in syllable: if old_syllable != current: current = old_syllable ret.append((current, [],)) ret[-1][1].append(letter) r = [] for old_syllable, letters in ret[:-1]: r.append(elements.syllable( u"".join(letters), old_syllable._style, old_syllable._whitespace_style )) r.append(elements.syllable(u"".join(ret[-1][1]), ret[-1][0]._style, ret[-1][0]._whitespace_style, soft_hyphen=True)) return r
def words(bits): """ Yields elements.word instances for each of the white-space separated words in bits. """ word = elements.word() for letters, ends_in_whitespace, style, whitespace_style in bits: for part in syllable_parts(letters): word.append(elements.syllable(part, style, whitespace_style)) if ends_in_whitespace: yield word word = elements.word() if len(word) > 0: yield word
def syllables(self, word, style=None): parts = split(word, elements.syllable.soft_hyphen_character) for part in parts[:-1]: yield elements.syllable(word, style, True) yield elements.syllable(parts[-1], style, False)