mirror of
https://github.com/sphinx-doc/sphinx.git
synced 2025-02-25 18:55:22 -06:00
Fix #706: use a custom TextWrapper instead of monkeypatching a new wordsep_re into the textwrap one.
This commit is contained in:
parent
1b6e2bfa9c
commit
1e05041ea5
@ -18,6 +18,24 @@ from sphinx import addnodes
|
|||||||
from sphinx.locale import admonitionlabels, versionlabels, _
|
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):
|
class TextWriter(writers.Writer):
|
||||||
supported = ('text',)
|
supported = ('text',)
|
||||||
settings_spec = ('No options here.', '', ())
|
settings_spec = ('No options here.', '', ())
|
||||||
@ -34,17 +52,6 @@ class TextWriter(writers.Writer):
|
|||||||
self.document.walkabout(visitor)
|
self.document.walkabout(visitor)
|
||||||
self.output = visitor.body
|
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):
|
class TextTranslator(nodes.NodeVisitor):
|
||||||
sectionchars = '*=-~"+`'
|
sectionchars = '*=-~"+`'
|
||||||
@ -81,7 +88,7 @@ class TextTranslator(nodes.NodeVisitor):
|
|||||||
if not toformat:
|
if not toformat:
|
||||||
return
|
return
|
||||||
if wrap:
|
if wrap:
|
||||||
res = textwrap.wrap(''.join(toformat), width=MAXWIDTH-maxindent)
|
res = my_wrap(''.join(toformat), width=MAXWIDTH-maxindent)
|
||||||
else:
|
else:
|
||||||
res = ''.join(toformat).splitlines()
|
res = ''.join(toformat).splitlines()
|
||||||
if end:
|
if end:
|
||||||
@ -381,7 +388,7 @@ class TextTranslator(nodes.NodeVisitor):
|
|||||||
else:
|
else:
|
||||||
cells = []
|
cells = []
|
||||||
for i, cell in enumerate(line):
|
for i, cell in enumerate(line):
|
||||||
par = textwrap.wrap(cell, width=colwidths[i])
|
par = my_wrap(cell, width=colwidths[i])
|
||||||
if par:
|
if par:
|
||||||
maxwidth = max(map(len, par))
|
maxwidth = max(map(len, par))
|
||||||
else:
|
else:
|
||||||
|
Loading…
Reference in New Issue
Block a user