示例#1
0
文件: xist.py 项目: dvorberg/t4
 def paragraph(text_elements):
     return elements.paragraph(words(text_elements))
示例#2
0
    def paragraph(self, source):
        texts = []

        def bits(source):
            """
            Yields bits of text and their corresponding style.
            """
            pieces = self.inline_markup_re.split(source)
            pieces.reverse()

            def splitwords(text, style, whitespace_style):
                for letters, whitespace in self.word_re.findall(text):
                    yield letters, whitespace != u"", style, whitespace_style,
                    
            while pieces:
                not_formated = pieces.pop()

                if not_formated:
                    for tpl in splitwords(not_formated, None, None):
                        yield tpl
                        
                if pieces:
                    formated = pieces.pop()

                    stripped = formated.rstrip()
                    has_outer_whitespace = len(stripped) < len(formated)
                    
                    if formated.startswith("''//"):
                        text = stripped[4:-4]
                        style = self.styles["bold-italic"]
                    else:
                        fmt = stripped[:2]
                        text = stripped[2:-2]
                        
                        if fmt == "''":
                            style = self.styles["bold"]
                        elif fmt == "//":
                            style = self.styles["italic"]
                        elif fmt == "@@":
                            style = self.styles["highlighted"]

                    stripped = text.rstrip()
                    has_inner_whitespace = len(stripped) < len(text)
                    
                    if has_inner_whitespace:
                        # If the formated text ends in white space,
                        # we use the format’s style to render it.
                        whitespace_style = style
                    else:
                        # Otherwise we use the surrounding style to render
                        # it.
                        whitespace_style = None

                    if has_inner_whitespace or has_outer_whitespace:
                        text += " "
                        
                    for tpl in splitwords(text, style, whitespace_style):
                        yield tpl

        def syllable_parts(letters):
            """
            Split syllables at soft hyphen characters.
            """
            return soft_hyphened_syllable_re.findall(letters)
            
                        
        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
            
        bs = bits(source)
        ws = words(bs)
        return elements.paragraph(ws, style=self.style)