diff options
| author | Georg Brandl <georg@python.org> | 2011-09-21 09:38:51 +0200 |
|---|---|---|
| committer | Georg Brandl <georg@python.org> | 2011-09-21 09:38:51 +0200 |
| commit | 928422869d83a8fbed187c84803f2d8d7746b965 (patch) | |
| tree | 74d6e0b7b006110f31060c0262d2c5c33cc33eb4 | |
| parent | a66ffbfa1f0e8e5280148805f0ad57b112252966 (diff) | |
| download | sphinx-928422869d83a8fbed187c84803f2d8d7746b965.tar.gz | |
Fix #706: use a custom TextWrapper instead of monkeypatching a new wordsep_re into the textwrap one.
| -rw-r--r-- | sphinx/writers/text.py | 33 |
1 files changed, 20 insertions, 13 deletions
diff --git a/sphinx/writers/text.py b/sphinx/writers/text.py index 1d6ad648..b82bfcdb 100644 --- a/sphinx/writers/text.py +++ b/sphinx/writers/text.py @@ -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 = '*=-~"+`' @@ -73,7 +80,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: @@ -373,7 +380,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: |
