~pythonregexp2.7/python/issue2636-09-01+10

« back to all changes in this revision

Viewing changes to Lib/textwrap.py

  • Committer: Jeffrey C. "The TimeHorse" Jacobs
  • Date: 2008-05-24 18:56:40 UTC
  • mfrom: (39055.1.22 Regexp-2.6)
  • Revision ID: darklord@timehorse.com-20080524185640-59vz6l1f7qgixgal
Merged in changes from the Single-Loop Engine branch.

Show diffs side-by-side

added added

removed removed

Lines of Context:
63
63
      break_long_words (default: true)
64
64
        Break words longer than 'width'.  If false, those words will not
65
65
        be broken, and some lines might be longer than 'width'.
 
66
      break_on_hyphens (default: true)
 
67
        Allow breaking hyphenated words. If true, wrapping will occur
 
68
        preferably on whitespaces and right after hyphens part of
 
69
        compound words.
66
70
      drop_whitespace (default: true)
67
71
        Drop leading and trailing whitespace from lines.
68
72
    """
85
89
        r'[^\s\w]*\w+[a-zA-Z]-(?=\w+[a-zA-Z])|'   # hyphenated words
86
90
        r'(?<=[\w\!\"\'\&\.\,\?])-{2,}(?=\w))')   # em-dash
87
91
 
 
92
    # This less funky little regex just split on recognized spaces. E.g.
 
93
    #   "Hello there -- you goof-ball, use the -b option!"
 
94
    # splits into
 
95
    #   Hello/ /there/ /--/ /you/ /goof-ball,/ /use/ /the/ /-b/ /option!/
 
96
    wordsep_simple_re = re.compile(r'(\s+)')
 
97
 
88
98
    # XXX this is not locale- or charset-aware -- string.lowercase
89
99
    # is US-ASCII only (and therefore English-only)
90
100
    sentence_end_re = re.compile(r'[%s]'              # lowercase letter
102
112
                 replace_whitespace=True,
103
113
                 fix_sentence_endings=False,
104
114
                 break_long_words=True,
105
 
                 drop_whitespace=True):
 
115
                 drop_whitespace=True,
 
116
                 break_on_hyphens=True):
106
117
        self.width = width
107
118
        self.initial_indent = initial_indent
108
119
        self.subsequent_indent = subsequent_indent
111
122
        self.fix_sentence_endings = fix_sentence_endings
112
123
        self.break_long_words = break_long_words
113
124
        self.drop_whitespace = drop_whitespace
 
125
        self.break_on_hyphens = break_on_hyphens
114
126
 
115
127
 
116
128
    # -- Private methods -----------------------------------------------
143
155
        breaks into the following chunks:
144
156
          'Look,', ' ', 'goof-', 'ball', ' ', '--', ' ',
145
157
          'use', ' ', 'the', ' ', '-b', ' ', 'option!'
 
158
        if break_on_hyphens is True, or in:
 
159
          'Look,', ' ', 'goof-ball', ' ', '--', ' ',
 
160
          'use', ' ', 'the', ' ', '-b', ' ', option!'
 
161
        otherwise.
146
162
        """
147
 
        chunks = self.wordsep_re.split(text)
 
163
        if self.break_on_hyphens is True:
 
164
            chunks = self.wordsep_re.split(text)
 
165
        else:
 
166
            chunks = self.wordsep_simple_re.split(text)
148
167
        chunks = filter(None, chunks)  # remove empty chunks
149
168
        return chunks
150
169