From fc5059290f8e8dc7cb8732ff26d61c398e3162d6 Mon Sep 17 00:00:00 2001 From: Jonathan Eunice Date: Thu, 15 Jun 2017 15:11:29 -0400 Subject: [PATCH 1/2] support true em-dashes --- Lib/test/test_textwrap.py | 35 +++++++++++++++++++++++++++++++++++ Lib/textwrap.py | 8 ++++++-- 2 files changed, 41 insertions(+), 2 deletions(-) diff --git a/Lib/test/test_textwrap.py b/Lib/test/test_textwrap.py index fe15348aff114bd..c9ccddc03db8ce5 100644 --- a/Lib/test/test_textwrap.py +++ b/Lib/test/test_textwrap.py @@ -269,6 +269,41 @@ def test_em_dash(self): "he", " ", "was", " ", "gone"] self.check_split(text, expect) + def test_unicode_em_dash(self): + # Test text with Unicode em-dashes + text = "Em-dashes should be written \u2014 thus." + self.check_wrap(text, 25, + ["Em-dashes should be", + "written \u2014 thus."]) + + # Probe the boundaries of the em-dash. Parallels ASCII tests + # but widths - 1 since len('\u2014') = len('--') - 1 + self.check_wrap(text, 28, + ["Em-dashes should be written", + "\u2014 thus."]) + expect = ["Em-dashes should be written \u2014", + "thus."] + self.check_wrap(text, 29, expect) + self.check_wrap(text, 34, expect) + self.check_wrap(text, 35, + ["Em-dashes should be written \u2014 thus."]) + + # Tests for adjacent glyphs not needed for Unicode em-dash + # because unlike adjacent hypens, not meaningful or common. + + # All of the above behaviour could be deduced by probing the + # _split() method. Note mixed real and simulated em-dashes. + text = "Here's an \u2014 em-dash and\u2014here's another---and another! And--more!" + expect = ["Here's", " ", "an", " ", "\u2014", " ", "em-", "dash", " ", + "and", "\u2014", "here's", " ", "another", "---", + "and", " ", "another!", " ", "And", "--", "more!"] + + self.check_split(text, expect) + + text = "and then\u2014bam!\u2014he was gone" + expect = ["and", " ", "then", "\u2014", "bam!", "\u2014", + "he", " ", "was", " ", "gone"] + self.check_split(text, expect) def test_unix_options (self): # Test that Unix-style command-line options are wrapped correctly. diff --git a/Lib/textwrap.py b/Lib/textwrap.py index 0c18dc582e17aec..09e5ef5d2c49725 100644 --- a/Lib/textwrap.py +++ b/Lib/textwrap.py @@ -81,8 +81,10 @@ class TextWrapper: wordsep_re = re.compile(r''' ( # any whitespace %(ws)s+ - | # em-dash between words + | # ASCII em-dash between words (?<=%(wp)s) -{2,} (?=\w) + | # Unicode em-dash between words + (?<=%(wp)s) \u2014 (?=\w) | # word, possibly hyphenated %(nws)s+? (?: # hyphenated word @@ -90,8 +92,10 @@ class TextWrapper: (?= %(lt)s -? %(lt)s) | # end of word (?=%(ws)s|\Z) - | # em-dash + | # ASCII em-dash (?<=%(wp)s) (?=-{2,}\w) + | # Unicode em-dash + (?<=%(wp)s) (?=\u2014\w) ) )''' % {'wp': word_punct, 'lt': letter, 'ws': whitespace, 'nws': nowhitespace}, From 8194f4edfd5583327615ddadddeccc175976ea9c Mon Sep 17 00:00:00 2001 From: Oleg Iarygin Date: Sat, 1 Apr 2023 08:56:01 +0400 Subject: [PATCH 2/2] Add a news entry --- .../next/Library/2023-04-01-08-55-31.gh-issue-74865.hqKRyU.rst | 2 ++ 1 file changed, 2 insertions(+) create mode 100644 Misc/NEWS.d/next/Library/2023-04-01-08-55-31.gh-issue-74865.hqKRyU.rst diff --git a/Misc/NEWS.d/next/Library/2023-04-01-08-55-31.gh-issue-74865.hqKRyU.rst b/Misc/NEWS.d/next/Library/2023-04-01-08-55-31.gh-issue-74865.hqKRyU.rst new file mode 100644 index 000000000000000..2ab5420efac0c97 --- /dev/null +++ b/Misc/NEWS.d/next/Library/2023-04-01-08-55-31.gh-issue-74865.hqKRyU.rst @@ -0,0 +1,2 @@ +:mod:`textwrap` now treats real, Unicode em-dashes like the simulated ones +(two or more consecutive hyphens). Patch by Jonathan Eunice.