Edit File by line
/home/barbar84/public_h.../wp-conte.../plugins/sujqvwi/ExeBy/smexe_ro.../lib64/python2....
File: textwrap.py
"""Text wrapping and filling.
[0] Fix | Delete
"""
[1] Fix | Delete
[2] Fix | Delete
# Copyright (C) 1999-2001 Gregory P. Ward.
[3] Fix | Delete
# Copyright (C) 2002, 2003 Python Software Foundation.
[4] Fix | Delete
# Written by Greg Ward <gward@python.net>
[5] Fix | Delete
[6] Fix | Delete
__revision__ = "$Id$"
[7] Fix | Delete
[8] Fix | Delete
import string, re
[9] Fix | Delete
[10] Fix | Delete
try:
[11] Fix | Delete
_unicode = unicode
[12] Fix | Delete
except NameError:
[13] Fix | Delete
# If Python is built without Unicode support, the unicode type
[14] Fix | Delete
# will not exist. Fake one.
[15] Fix | Delete
class _unicode(object):
[16] Fix | Delete
pass
[17] Fix | Delete
[18] Fix | Delete
# Do the right thing with boolean values for all known Python versions
[19] Fix | Delete
# (so this module can be copied to projects that don't depend on Python
[20] Fix | Delete
# 2.3, e.g. Optik and Docutils) by uncommenting the block of code below.
[21] Fix | Delete
#try:
[22] Fix | Delete
# True, False
[23] Fix | Delete
#except NameError:
[24] Fix | Delete
# (True, False) = (1, 0)
[25] Fix | Delete
[26] Fix | Delete
__all__ = ['TextWrapper', 'wrap', 'fill', 'dedent']
[27] Fix | Delete
[28] Fix | Delete
# Hardcode the recognized whitespace characters to the US-ASCII
[29] Fix | Delete
# whitespace characters. The main reason for doing this is that in
[30] Fix | Delete
# ISO-8859-1, 0xa0 is non-breaking whitespace, so in certain locales
[31] Fix | Delete
# that character winds up in string.whitespace. Respecting
[32] Fix | Delete
# string.whitespace in those cases would 1) make textwrap treat 0xa0 the
[33] Fix | Delete
# same as any other whitespace char, which is clearly wrong (it's a
[34] Fix | Delete
# *non-breaking* space), 2) possibly cause problems with Unicode,
[35] Fix | Delete
# since 0xa0 is not in range(128).
[36] Fix | Delete
_whitespace = '\t\n\x0b\x0c\r '
[37] Fix | Delete
[38] Fix | Delete
class TextWrapper:
[39] Fix | Delete
"""
[40] Fix | Delete
Object for wrapping/filling text. The public interface consists of
[41] Fix | Delete
the wrap() and fill() methods; the other methods are just there for
[42] Fix | Delete
subclasses to override in order to tweak the default behaviour.
[43] Fix | Delete
If you want to completely replace the main wrapping algorithm,
[44] Fix | Delete
you'll probably have to override _wrap_chunks().
[45] Fix | Delete
[46] Fix | Delete
Several instance attributes control various aspects of wrapping:
[47] Fix | Delete
width (default: 70)
[48] Fix | Delete
the maximum width of wrapped lines (unless break_long_words
[49] Fix | Delete
is false)
[50] Fix | Delete
initial_indent (default: "")
[51] Fix | Delete
string that will be prepended to the first line of wrapped
[52] Fix | Delete
output. Counts towards the line's width.
[53] Fix | Delete
subsequent_indent (default: "")
[54] Fix | Delete
string that will be prepended to all lines save the first
[55] Fix | Delete
of wrapped output; also counts towards each line's width.
[56] Fix | Delete
expand_tabs (default: true)
[57] Fix | Delete
Expand tabs in input text to spaces before further processing.
[58] Fix | Delete
Each tab will become 1 .. 8 spaces, depending on its position in
[59] Fix | Delete
its line. If false, each tab is treated as a single character.
[60] Fix | Delete
replace_whitespace (default: true)
[61] Fix | Delete
Replace all whitespace characters in the input text by spaces
[62] Fix | Delete
after tab expansion. Note that if expand_tabs is false and
[63] Fix | Delete
replace_whitespace is true, every tab will be converted to a
[64] Fix | Delete
single space!
[65] Fix | Delete
fix_sentence_endings (default: false)
[66] Fix | Delete
Ensure that sentence-ending punctuation is always followed
[67] Fix | Delete
by two spaces. Off by default because the algorithm is
[68] Fix | Delete
(unavoidably) imperfect.
[69] Fix | Delete
break_long_words (default: true)
[70] Fix | Delete
Break words longer than 'width'. If false, those words will not
[71] Fix | Delete
be broken, and some lines might be longer than 'width'.
[72] Fix | Delete
break_on_hyphens (default: true)
[73] Fix | Delete
Allow breaking hyphenated words. If true, wrapping will occur
[74] Fix | Delete
preferably on whitespaces and right after hyphens part of
[75] Fix | Delete
compound words.
[76] Fix | Delete
drop_whitespace (default: true)
[77] Fix | Delete
Drop leading and trailing whitespace from lines.
[78] Fix | Delete
"""
[79] Fix | Delete
[80] Fix | Delete
whitespace_trans = string.maketrans(_whitespace, ' ' * len(_whitespace))
[81] Fix | Delete
[82] Fix | Delete
unicode_whitespace_trans = {}
[83] Fix | Delete
uspace = ord(u' ')
[84] Fix | Delete
for x in map(ord, _whitespace):
[85] Fix | Delete
unicode_whitespace_trans[x] = uspace
[86] Fix | Delete
[87] Fix | Delete
# This funky little regex is just the trick for splitting
[88] Fix | Delete
# text up into word-wrappable chunks. E.g.
[89] Fix | Delete
# "Hello there -- you goof-ball, use the -b option!"
[90] Fix | Delete
# splits into
[91] Fix | Delete
# Hello/ /there/ /--/ /you/ /goof-/ball,/ /use/ /the/ /-b/ /option!
[92] Fix | Delete
# (after stripping out empty strings).
[93] Fix | Delete
wordsep_re = re.compile(
[94] Fix | Delete
r'(\s+|' # any whitespace
[95] Fix | Delete
r'[^\s\w]*\w+[^0-9\W]-(?=\w+[^0-9\W])|' # hyphenated words
[96] Fix | Delete
r'(?<=[\w\!\"\'\&\.\,\?])-{2,}(?=\w))') # em-dash
[97] Fix | Delete
[98] Fix | Delete
# This less funky little regex just split on recognized spaces. E.g.
[99] Fix | Delete
# "Hello there -- you goof-ball, use the -b option!"
[100] Fix | Delete
# splits into
[101] Fix | Delete
# Hello/ /there/ /--/ /you/ /goof-ball,/ /use/ /the/ /-b/ /option!/
[102] Fix | Delete
wordsep_simple_re = re.compile(r'(\s+)')
[103] Fix | Delete
[104] Fix | Delete
# XXX this is not locale- or charset-aware -- string.lowercase
[105] Fix | Delete
# is US-ASCII only (and therefore English-only)
[106] Fix | Delete
sentence_end_re = re.compile(r'[%s]' # lowercase letter
[107] Fix | Delete
r'[\.\!\?]' # sentence-ending punct.
[108] Fix | Delete
r'[\"\']?' # optional end-of-quote
[109] Fix | Delete
r'\Z' # end of chunk
[110] Fix | Delete
% string.lowercase)
[111] Fix | Delete
[112] Fix | Delete
[113] Fix | Delete
def __init__(self,
[114] Fix | Delete
width=70,
[115] Fix | Delete
initial_indent="",
[116] Fix | Delete
subsequent_indent="",
[117] Fix | Delete
expand_tabs=True,
[118] Fix | Delete
replace_whitespace=True,
[119] Fix | Delete
fix_sentence_endings=False,
[120] Fix | Delete
break_long_words=True,
[121] Fix | Delete
drop_whitespace=True,
[122] Fix | Delete
break_on_hyphens=True):
[123] Fix | Delete
self.width = width
[124] Fix | Delete
self.initial_indent = initial_indent
[125] Fix | Delete
self.subsequent_indent = subsequent_indent
[126] Fix | Delete
self.expand_tabs = expand_tabs
[127] Fix | Delete
self.replace_whitespace = replace_whitespace
[128] Fix | Delete
self.fix_sentence_endings = fix_sentence_endings
[129] Fix | Delete
self.break_long_words = break_long_words
[130] Fix | Delete
self.drop_whitespace = drop_whitespace
[131] Fix | Delete
self.break_on_hyphens = break_on_hyphens
[132] Fix | Delete
[133] Fix | Delete
# recompile the regexes for Unicode mode -- done in this clumsy way for
[134] Fix | Delete
# backwards compatibility because it's rather common to monkey-patch
[135] Fix | Delete
# the TextWrapper class' wordsep_re attribute.
[136] Fix | Delete
self.wordsep_re_uni = re.compile(self.wordsep_re.pattern, re.U)
[137] Fix | Delete
self.wordsep_simple_re_uni = re.compile(
[138] Fix | Delete
self.wordsep_simple_re.pattern, re.U)
[139] Fix | Delete
[140] Fix | Delete
[141] Fix | Delete
# -- Private methods -----------------------------------------------
[142] Fix | Delete
# (possibly useful for subclasses to override)
[143] Fix | Delete
[144] Fix | Delete
def _munge_whitespace(self, text):
[145] Fix | Delete
"""_munge_whitespace(text : string) -> string
[146] Fix | Delete
[147] Fix | Delete
Munge whitespace in text: expand tabs and convert all other
[148] Fix | Delete
whitespace characters to spaces. Eg. " foo\\tbar\\n\\nbaz"
[149] Fix | Delete
becomes " foo bar baz".
[150] Fix | Delete
"""
[151] Fix | Delete
if self.expand_tabs:
[152] Fix | Delete
text = text.expandtabs()
[153] Fix | Delete
if self.replace_whitespace:
[154] Fix | Delete
if isinstance(text, str):
[155] Fix | Delete
text = text.translate(self.whitespace_trans)
[156] Fix | Delete
elif isinstance(text, _unicode):
[157] Fix | Delete
text = text.translate(self.unicode_whitespace_trans)
[158] Fix | Delete
return text
[159] Fix | Delete
[160] Fix | Delete
[161] Fix | Delete
def _split(self, text):
[162] Fix | Delete
"""_split(text : string) -> [string]
[163] Fix | Delete
[164] Fix | Delete
Split the text to wrap into indivisible chunks. Chunks are
[165] Fix | Delete
not quite the same as words; see _wrap_chunks() for full
[166] Fix | Delete
details. As an example, the text
[167] Fix | Delete
Look, goof-ball -- use the -b option!
[168] Fix | Delete
breaks into the following chunks:
[169] Fix | Delete
'Look,', ' ', 'goof-', 'ball', ' ', '--', ' ',
[170] Fix | Delete
'use', ' ', 'the', ' ', '-b', ' ', 'option!'
[171] Fix | Delete
if break_on_hyphens is True, or in:
[172] Fix | Delete
'Look,', ' ', 'goof-ball', ' ', '--', ' ',
[173] Fix | Delete
'use', ' ', 'the', ' ', '-b', ' ', option!'
[174] Fix | Delete
otherwise.
[175] Fix | Delete
"""
[176] Fix | Delete
if isinstance(text, _unicode):
[177] Fix | Delete
if self.break_on_hyphens:
[178] Fix | Delete
pat = self.wordsep_re_uni
[179] Fix | Delete
else:
[180] Fix | Delete
pat = self.wordsep_simple_re_uni
[181] Fix | Delete
else:
[182] Fix | Delete
if self.break_on_hyphens:
[183] Fix | Delete
pat = self.wordsep_re
[184] Fix | Delete
else:
[185] Fix | Delete
pat = self.wordsep_simple_re
[186] Fix | Delete
chunks = pat.split(text)
[187] Fix | Delete
chunks = filter(None, chunks) # remove empty chunks
[188] Fix | Delete
return chunks
[189] Fix | Delete
[190] Fix | Delete
def _fix_sentence_endings(self, chunks):
[191] Fix | Delete
"""_fix_sentence_endings(chunks : [string])
[192] Fix | Delete
[193] Fix | Delete
Correct for sentence endings buried in 'chunks'. Eg. when the
[194] Fix | Delete
original text contains "... foo.\\nBar ...", munge_whitespace()
[195] Fix | Delete
and split() will convert that to [..., "foo.", " ", "Bar", ...]
[196] Fix | Delete
which has one too few spaces; this method simply changes the one
[197] Fix | Delete
space to two.
[198] Fix | Delete
"""
[199] Fix | Delete
i = 0
[200] Fix | Delete
patsearch = self.sentence_end_re.search
[201] Fix | Delete
while i < len(chunks)-1:
[202] Fix | Delete
if chunks[i+1] == " " and patsearch(chunks[i]):
[203] Fix | Delete
chunks[i+1] = " "
[204] Fix | Delete
i += 2
[205] Fix | Delete
else:
[206] Fix | Delete
i += 1
[207] Fix | Delete
[208] Fix | Delete
def _handle_long_word(self, reversed_chunks, cur_line, cur_len, width):
[209] Fix | Delete
"""_handle_long_word(chunks : [string],
[210] Fix | Delete
cur_line : [string],
[211] Fix | Delete
cur_len : int, width : int)
[212] Fix | Delete
[213] Fix | Delete
Handle a chunk of text (most likely a word, not whitespace) that
[214] Fix | Delete
is too long to fit in any line.
[215] Fix | Delete
"""
[216] Fix | Delete
# Figure out when indent is larger than the specified width, and make
[217] Fix | Delete
# sure at least one character is stripped off on every pass
[218] Fix | Delete
if width < 1:
[219] Fix | Delete
space_left = 1
[220] Fix | Delete
else:
[221] Fix | Delete
space_left = width - cur_len
[222] Fix | Delete
[223] Fix | Delete
# If we're allowed to break long words, then do so: put as much
[224] Fix | Delete
# of the next chunk onto the current line as will fit.
[225] Fix | Delete
if self.break_long_words:
[226] Fix | Delete
cur_line.append(reversed_chunks[-1][:space_left])
[227] Fix | Delete
reversed_chunks[-1] = reversed_chunks[-1][space_left:]
[228] Fix | Delete
[229] Fix | Delete
# Otherwise, we have to preserve the long word intact. Only add
[230] Fix | Delete
# it to the current line if there's nothing already there --
[231] Fix | Delete
# that minimizes how much we violate the width constraint.
[232] Fix | Delete
elif not cur_line:
[233] Fix | Delete
cur_line.append(reversed_chunks.pop())
[234] Fix | Delete
[235] Fix | Delete
# If we're not allowed to break long words, and there's already
[236] Fix | Delete
# text on the current line, do nothing. Next time through the
[237] Fix | Delete
# main loop of _wrap_chunks(), we'll wind up here again, but
[238] Fix | Delete
# cur_len will be zero, so the next line will be entirely
[239] Fix | Delete
# devoted to the long word that we can't handle right now.
[240] Fix | Delete
[241] Fix | Delete
def _wrap_chunks(self, chunks):
[242] Fix | Delete
"""_wrap_chunks(chunks : [string]) -> [string]
[243] Fix | Delete
[244] Fix | Delete
Wrap a sequence of text chunks and return a list of lines of
[245] Fix | Delete
length 'self.width' or less. (If 'break_long_words' is false,
[246] Fix | Delete
some lines may be longer than this.) Chunks correspond roughly
[247] Fix | Delete
to words and the whitespace between them: each chunk is
[248] Fix | Delete
indivisible (modulo 'break_long_words'), but a line break can
[249] Fix | Delete
come between any two chunks. Chunks should not have internal
[250] Fix | Delete
whitespace; ie. a chunk is either all whitespace or a "word".
[251] Fix | Delete
Whitespace chunks will be removed from the beginning and end of
[252] Fix | Delete
lines, but apart from that whitespace is preserved.
[253] Fix | Delete
"""
[254] Fix | Delete
lines = []
[255] Fix | Delete
if self.width <= 0:
[256] Fix | Delete
raise ValueError("invalid width %r (must be > 0)" % self.width)
[257] Fix | Delete
[258] Fix | Delete
# Arrange in reverse order so items can be efficiently popped
[259] Fix | Delete
# from a stack of chucks.
[260] Fix | Delete
chunks.reverse()
[261] Fix | Delete
[262] Fix | Delete
while chunks:
[263] Fix | Delete
[264] Fix | Delete
# Start the list of chunks that will make up the current line.
[265] Fix | Delete
# cur_len is just the length of all the chunks in cur_line.
[266] Fix | Delete
cur_line = []
[267] Fix | Delete
cur_len = 0
[268] Fix | Delete
[269] Fix | Delete
# Figure out which static string will prefix this line.
[270] Fix | Delete
if lines:
[271] Fix | Delete
indent = self.subsequent_indent
[272] Fix | Delete
else:
[273] Fix | Delete
indent = self.initial_indent
[274] Fix | Delete
[275] Fix | Delete
# Maximum width for this line.
[276] Fix | Delete
width = self.width - len(indent)
[277] Fix | Delete
[278] Fix | Delete
# First chunk on line is whitespace -- drop it, unless this
[279] Fix | Delete
# is the very beginning of the text (ie. no lines started yet).
[280] Fix | Delete
if self.drop_whitespace and chunks[-1].strip() == '' and lines:
[281] Fix | Delete
del chunks[-1]
[282] Fix | Delete
[283] Fix | Delete
while chunks:
[284] Fix | Delete
l = len(chunks[-1])
[285] Fix | Delete
[286] Fix | Delete
# Can at least squeeze this chunk onto the current line.
[287] Fix | Delete
if cur_len + l <= width:
[288] Fix | Delete
cur_line.append(chunks.pop())
[289] Fix | Delete
cur_len += l
[290] Fix | Delete
[291] Fix | Delete
# Nope, this line is full.
[292] Fix | Delete
else:
[293] Fix | Delete
break
[294] Fix | Delete
[295] Fix | Delete
# The current line is full, and the next chunk is too big to
[296] Fix | Delete
# fit on *any* line (not just this one).
[297] Fix | Delete
if chunks and len(chunks[-1]) > width:
[298] Fix | Delete
self._handle_long_word(chunks, cur_line, cur_len, width)
[299] Fix | Delete
[300] Fix | Delete
# If the last chunk on this line is all whitespace, drop it.
[301] Fix | Delete
if self.drop_whitespace and cur_line and cur_line[-1].strip() == '':
[302] Fix | Delete
del cur_line[-1]
[303] Fix | Delete
[304] Fix | Delete
# Convert current line back to a string and store it in list
[305] Fix | Delete
# of all lines (return value).
[306] Fix | Delete
if cur_line:
[307] Fix | Delete
lines.append(indent + ''.join(cur_line))
[308] Fix | Delete
[309] Fix | Delete
return lines
[310] Fix | Delete
[311] Fix | Delete
[312] Fix | Delete
# -- Public interface ----------------------------------------------
[313] Fix | Delete
[314] Fix | Delete
def wrap(self, text):
[315] Fix | Delete
"""wrap(text : string) -> [string]
[316] Fix | Delete
[317] Fix | Delete
Reformat the single paragraph in 'text' so it fits in lines of
[318] Fix | Delete
no more than 'self.width' columns, and return a list of wrapped
[319] Fix | Delete
lines. Tabs in 'text' are expanded with string.expandtabs(),
[320] Fix | Delete
and all other whitespace characters (including newline) are
[321] Fix | Delete
converted to space.
[322] Fix | Delete
"""
[323] Fix | Delete
text = self._munge_whitespace(text)
[324] Fix | Delete
chunks = self._split(text)
[325] Fix | Delete
if self.fix_sentence_endings:
[326] Fix | Delete
self._fix_sentence_endings(chunks)
[327] Fix | Delete
return self._wrap_chunks(chunks)
[328] Fix | Delete
[329] Fix | Delete
def fill(self, text):
[330] Fix | Delete
"""fill(text : string) -> string
[331] Fix | Delete
[332] Fix | Delete
Reformat the single paragraph in 'text' to fit in lines of no
[333] Fix | Delete
more than 'self.width' columns, and return a new string
[334] Fix | Delete
containing the entire wrapped paragraph.
[335] Fix | Delete
"""
[336] Fix | Delete
return "\n".join(self.wrap(text))
[337] Fix | Delete
[338] Fix | Delete
[339] Fix | Delete
# -- Convenience interface ---------------------------------------------
[340] Fix | Delete
[341] Fix | Delete
def wrap(text, width=70, **kwargs):
[342] Fix | Delete
"""Wrap a single paragraph of text, returning a list of wrapped lines.
[343] Fix | Delete
[344] Fix | Delete
Reformat the single paragraph in 'text' so it fits in lines of no
[345] Fix | Delete
more than 'width' columns, and return a list of wrapped lines. By
[346] Fix | Delete
default, tabs in 'text' are expanded with string.expandtabs(), and
[347] Fix | Delete
all other whitespace characters (including newline) are converted to
[348] Fix | Delete
space. See TextWrapper class for available keyword args to customize
[349] Fix | Delete
wrapping behaviour.
[350] Fix | Delete
"""
[351] Fix | Delete
w = TextWrapper(width=width, **kwargs)
[352] Fix | Delete
return w.wrap(text)
[353] Fix | Delete
[354] Fix | Delete
def fill(text, width=70, **kwargs):
[355] Fix | Delete
"""Fill a single paragraph of text, returning a new string.
[356] Fix | Delete
[357] Fix | Delete
Reformat the single paragraph in 'text' to fit in lines of no more
[358] Fix | Delete
than 'width' columns, and return a new string containing the entire
[359] Fix | Delete
wrapped paragraph. As with wrap(), tabs are expanded and other
[360] Fix | Delete
whitespace characters converted to space. See TextWrapper class for
[361] Fix | Delete
available keyword args to customize wrapping behaviour.
[362] Fix | Delete
"""
[363] Fix | Delete
w = TextWrapper(width=width, **kwargs)
[364] Fix | Delete
return w.fill(text)
[365] Fix | Delete
[366] Fix | Delete
[367] Fix | Delete
# -- Loosely related functionality -------------------------------------
[368] Fix | Delete
[369] Fix | Delete
_whitespace_only_re = re.compile('^[ \t]+$', re.MULTILINE)
[370] Fix | Delete
_leading_whitespace_re = re.compile('(^[ \t]*)(?:[^ \t\n])', re.MULTILINE)
[371] Fix | Delete
[372] Fix | Delete
def dedent(text):
[373] Fix | Delete
"""Remove any common leading whitespace from every line in `text`.
[374] Fix | Delete
[375] Fix | Delete
This can be used to make triple-quoted strings line up with the left
[376] Fix | Delete
edge of the display, while still presenting them in the source code
[377] Fix | Delete
in indented form.
[378] Fix | Delete
[379] Fix | Delete
Note that tabs and spaces are both treated as whitespace, but they
[380] Fix | Delete
are not equal: the lines " hello" and "\\thello" are
[381] Fix | Delete
considered to have no common leading whitespace. (This behaviour is
[382] Fix | Delete
new in Python 2.5; older versions of this module incorrectly
[383] Fix | Delete
expanded tabs before searching for common leading whitespace.)
[384] Fix | Delete
[385] Fix | Delete
Entirely blank lines are normalized to a newline character.
[386] Fix | Delete
"""
[387] Fix | Delete
# Look for the longest leading string of spaces and tabs common to
[388] Fix | Delete
# all lines.
[389] Fix | Delete
margin = None
[390] Fix | Delete
text = _whitespace_only_re.sub('', text)
[391] Fix | Delete
indents = _leading_whitespace_re.findall(text)
[392] Fix | Delete
for indent in indents:
[393] Fix | Delete
if margin is None:
[394] Fix | Delete
margin = indent
[395] Fix | Delete
[396] Fix | Delete
# Current line more deeply indented than previous winner:
[397] Fix | Delete
# no change (previous winner is still on top).
[398] Fix | Delete
elif indent.startswith(margin):
[399] Fix | Delete
pass
[400] Fix | Delete
[401] Fix | Delete
# Current line consistent with and no deeper than previous winner:
[402] Fix | Delete
# it's the new winner.
[403] Fix | Delete
elif margin.startswith(indent):
[404] Fix | Delete
margin = indent
[405] Fix | Delete
[406] Fix | Delete
# Find the largest common whitespace between current line and previous
[407] Fix | Delete
# winner.
[408] Fix | Delete
else:
[409] Fix | Delete
for i, (x, y) in enumerate(zip(margin, indent)):
[410] Fix | Delete
if x != y:
[411] Fix | Delete
margin = margin[:i]
[412] Fix | Delete
break
[413] Fix | Delete
else:
[414] Fix | Delete
margin = margin[:len(indent)]
[415] Fix | Delete
[416] Fix | Delete
# sanity check (testing/debugging only)
[417] Fix | Delete
if 0 and margin:
[418] Fix | Delete
for line in text.split("\n"):
[419] Fix | Delete
assert not line or line.startswith(margin), \
[420] Fix | Delete
"line = %r, margin = %r" % (line, margin)
[421] Fix | Delete
[422] Fix | Delete
if margin:
[423] Fix | Delete
text = re.sub(r'(?m)^' + margin, '', text)
[424] Fix | Delete
return text
[425] Fix | Delete
[426] Fix | Delete
if __name__ == "__main__":
[427] Fix | Delete
#print dedent("\tfoo\n\tbar")
[428] Fix | Delete
#print dedent(" \thello there\n \t how are you?")
[429] Fix | Delete
print dedent("Hello there.\n This is indented.")
[430] Fix | Delete
[431] Fix | Delete
It is recommended that you Edit text format, this type of Fix handles quite a lot in one request
Function