Georg Brandl avatar Georg Brandl committed 258b136

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

Comments (0)

Files changed (1)

sphinx/writers/text.py

 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.', '', ())
         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 = '*=-~"+`'
             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:
             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:
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for ProjectModifiedEvent.java.
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.