Fix #706: use a custom TextWrapper instead of monkeypatching a new wordsep_re into the textwrap one.

This commit is contained in:
Georg Brandl 2011-09-21 09:38:51 +02:00
parent 1b6e2bfa9c
commit 1e05041ea5

View File

@ -18,6 +18,24 @@ from sphinx import addnodes
from sphinx.locale import admonitionlabels, versionlabels, _
class TextWrapper(textwrap.TextWrapper):
"""Custom subclass that uses a different word separator regex."""
wordsep_re = re.compile(
r'(\s+|' # any whitespace
r'(?<=\s)(?::[a-z-]+:)?`\S+|' # interpreted text start
r'[^\s\w]*\w+[a-zA-Z]-(?=\w+[a-zA-Z])|' # hyphenated words
r'(?<=[\w\!\"\'\&\.\,\?])-{2,}(?=\w))') # em-dash
MAXWIDTH = 70
STDINDENT = 3
def my_wrap(text, width=MAXWIDTH, **kwargs):
w = TextWrapper(width=width, **kwargs)
return w.wrap(text)
class TextWriter(writers.Writer):
supported = ('text',)
settings_spec = ('No options here.', '', ())
@ -34,17 +52,6 @@ class TextWriter(writers.Writer):
self.document.walkabout(visitor)
self.output = visitor.body
# monkey-patch...
new_wordsep_re = re.compile(
r'(\s+|' # any whitespace
r'(?<=\s)(?::[a-z-]+:)?`\S+|' # interpreted text start
r'[^\s\w]*\w+[a-zA-Z]-(?=\w+[a-zA-Z])|' # hyphenated words
r'(?<=[\w\!\"\'\&\.\,\?])-{2,}(?=\w))') # em-dash
textwrap.TextWrapper.wordsep_re = new_wordsep_re
MAXWIDTH = 70
STDINDENT = 3
class TextTranslator(nodes.NodeVisitor):
sectionchars = '*=-~"+`'
@ -81,7 +88,7 @@ class TextTranslator(nodes.NodeVisitor):
if not toformat:
return
if wrap:
res = textwrap.wrap(''.join(toformat), width=MAXWIDTH-maxindent)
res = my_wrap(''.join(toformat), width=MAXWIDTH-maxindent)
else:
res = ''.join(toformat).splitlines()
if end:
@ -381,7 +388,7 @@ class TextTranslator(nodes.NodeVisitor):
else:
cells = []
for i, cell in enumerate(line):
par = textwrap.wrap(cell, width=colwidths[i])
par = my_wrap(cell, width=colwidths[i])
if par:
maxwidth = max(map(len, par))
else: