comparison hgext/releasenotes.py @ 40243:96e50dfd8c94

releasenotes: use stringutil.wrap() instead of handcrafted TextWrapper wrapper It's silly to splitlines() a joined string, but we don't care the performance here.
author Yuya Nishihara <yuya@tcha.org>
date Sat, 13 Oct 2018 12:29:43 +0200
parents a7cdd81f191b
children c303d65d2e34
comparison
equal deleted inserted replaced
40242:19ed212de2d1 40243:96e50dfd8c94
14 from __future__ import absolute_import 14 from __future__ import absolute_import
15 15
16 import difflib 16 import difflib
17 import errno 17 import errno
18 import re 18 import re
19 import textwrap
20 19
21 from mercurial.i18n import _ 20 from mercurial.i18n import _
22 from mercurial import ( 21 from mercurial import (
23 config, 22 config,
24 error, 23 error,
27 pycompat, 26 pycompat,
28 registrar, 27 registrar,
29 scmutil, 28 scmutil,
30 util, 29 util,
31 ) 30 )
31 from mercurial.utils import (
32 stringutil,
33 )
32 34
33 cmdtable = {} 35 cmdtable = {}
34 command = registrar.command(cmdtable) 36 command = registrar.command(cmdtable)
35 37
36 try: 38 try:
55 57
56 RE_DIRECTIVE = re.compile('^\.\. ([a-zA-Z0-9_]+)::\s*([^$]+)?$') 58 RE_DIRECTIVE = re.compile('^\.\. ([a-zA-Z0-9_]+)::\s*([^$]+)?$')
57 RE_ISSUE = br'\bissue ?[0-9]{4,6}(?![0-9])\b' 59 RE_ISSUE = br'\bissue ?[0-9]{4,6}(?![0-9])\b'
58 60
59 BULLET_SECTION = _('Other Changes') 61 BULLET_SECTION = _('Other Changes')
60
61 if pycompat.ispy3:
62 class byteswrapper(object):
63 def __init__(self, **kwargs):
64 for k in kwargs:
65 v = kwargs[k]
66 if not isinstance(v, str) and isinstance(v, bytes):
67 kwargs[k] = v.decode('utf8')
68 self._tw = textwrap.TextWrapper(**kwargs)
69 def wrap(self, data):
70 return [
71 l.encode('utf8') for l in self._tw.wrap(data.decode('utf8'))]
72 else:
73 byteswrapper = textwrap.TextWrapper
74 62
75 class parsedreleasenotes(object): 63 class parsedreleasenotes(object):
76 def __init__(self): 64 def __init__(self):
77 self.sections = {} 65 self.sections = {}
78 66
455 for title, paragraphs in notes.titledforsection(sectionname): 443 for title, paragraphs in notes.titledforsection(sectionname):
456 lines.append(title) 444 lines.append(title)
457 lines.append('-' * len(title)) 445 lines.append('-' * len(title))
458 lines.append('') 446 lines.append('')
459 447
460 wrapper = byteswrapper(width=78)
461 for i, para in enumerate(paragraphs): 448 for i, para in enumerate(paragraphs):
462 if i: 449 if i:
463 lines.append('') 450 lines.append('')
464 lines.extend(wrapper.wrap(' '.join(para))) 451 lines.extend(stringutil.wrap(' '.join(para),
452 width=78).splitlines())
465 453
466 lines.append('') 454 lines.append('')
467 455
468 # Second pass to emit bullet list items. 456 # Second pass to emit bullet list items.
469 457
477 lines.append(BULLET_SECTION) 465 lines.append(BULLET_SECTION)
478 lines.append('-' * len(BULLET_SECTION)) 466 lines.append('-' * len(BULLET_SECTION))
479 lines.append('') 467 lines.append('')
480 468
481 for paragraphs in nontitled: 469 for paragraphs in nontitled:
482 wrapper = byteswrapper(initial_indent='* ', 470 lines.extend(stringutil.wrap(' '.join(paragraphs[0]),
483 subsequent_indent=' ', 471 width=78,
484 width=78) 472 initindent='* ',
485 lines.extend(wrapper.wrap(' '.join(paragraphs[0]))) 473 hangindent=' ').splitlines())
486 474
487 wrapper = byteswrapper(initial_indent=' ',
488 subsequent_indent=' ',
489 width=78)
490 for para in paragraphs[1:]: 475 for para in paragraphs[1:]:
491 lines.append('') 476 lines.append('')
492 lines.extend(wrapper.wrap(' '.join(para))) 477 lines.extend(stringutil.wrap(' '.join(para),
478 width=78,
479 initindent=' ',
480 hangindent=' ').splitlines())
493 481
494 lines.append('') 482 lines.append('')
495 483
496 if lines and lines[-1]: 484 if lines and lines[-1]:
497 lines.append('') 485 lines.append('')