4
4
import six
5
5
6
6
7
- convert_heading_re = re .compile (r'convert_h(\d+)' )
8
- line_with_content_re = re .compile (r'^(.*)' , flags = re .MULTILINE )
9
- whitespace_re = re .compile (r'[\t ]+' )
10
- all_whitespace_re = re .compile (r'[\t \r\n]+' )
11
- newline_whitespace_re = re .compile (r'[\t \r\n]*[\r\n][\t \r\n]*' )
12
- html_heading_re = re .compile (r'h[1-6]' )
7
+ re_convert_heading = re .compile (r'convert_h(\d+)' )
8
+ re_line_with_content = re .compile (r'^(.*)' , flags = re .MULTILINE )
9
+ re_whitespace = re .compile (r'[\t ]+' )
10
+ re_all_whitespace = re .compile (r'[\t \r\n]+' )
11
+ re_newline_whitespace = re .compile (r'[\t \r\n]*[\r\n][\t \r\n]*' )
12
+ re_html_heading = re .compile (r'h[1-6]' )
13
13
14
14
# extract (leading_nl, content, trailing_nl) from a string
15
15
# (functionally equivalent to r'^(\n*)(.*?)(\n*)$', but greedy is faster than reluctant here)
16
- extract_newlines_re = re .compile (r'^(\n*)((?:.*[^\n])?)(\n*)$' , flags = re .DOTALL )
16
+ re_extract_newlines = re .compile (r'^(\n*)((?:.*[^\n])?)(\n*)$' , flags = re .DOTALL )
17
17
18
18
19
19
# Heading styles
@@ -80,7 +80,7 @@ def should_remove_whitespace_inside(el):
80
80
"""Return to remove whitespace immediately inside a block-level element."""
81
81
if not el or not el .name :
82
82
return False
83
- if html_heading_re .match (el .name ) is not None :
83
+ if re_html_heading .match (el .name ) is not None :
84
84
return True
85
85
return el .name in ('p' , 'blockquote' ,
86
86
'article' , 'div' , 'section' ,
@@ -221,7 +221,7 @@ def _can_ignore(el):
221
221
222
222
# if this tag is a heading or table cell, add an '_inline' parent pseudo-tag
223
223
if (
224
- html_heading_re .match (node .name ) is not None # headings
224
+ re_html_heading .match (node .name ) is not None # headings
225
225
or node .name in {'td' , 'th' } # table cells
226
226
):
227
227
parent_tags_for_children .add ('_inline' )
@@ -248,7 +248,7 @@ def _can_ignore(el):
248
248
updated_child_strings = ['' ] # so the first lookback works
249
249
for child_string in child_strings :
250
250
# Separate the leading/trailing newlines from the content.
251
- leading_nl , content , trailing_nl = extract_newlines_re .match (child_string ).groups ()
251
+ leading_nl , content , trailing_nl = re_extract_newlines .match (child_string ).groups ()
252
252
253
253
# If the last child had trailing newlines and this child has leading newlines,
254
254
# use the larger newline count, limited to 2.
@@ -298,10 +298,10 @@ def process_text(self, el, parent_tags=None):
298
298
# normalize whitespace if we're not inside a preformatted element
299
299
if 'pre' not in parent_tags :
300
300
if self .options ['wrap' ]:
301
- text = all_whitespace_re .sub (' ' , text )
301
+ text = re_all_whitespace .sub (' ' , text )
302
302
else :
303
- text = newline_whitespace_re .sub ('\n ' , text )
304
- text = whitespace_re .sub (' ' , text )
303
+ text = re_newline_whitespace .sub ('\n ' , text )
304
+ text = re_whitespace .sub (' ' , text )
305
305
306
306
# escape special characters if we're not inside a preformatted or code element
307
307
if '_noformat' not in parent_tags :
@@ -323,7 +323,7 @@ def process_text(self, el, parent_tags=None):
323
323
324
324
def __getattr__ (self , attr ):
325
325
# Handle headings
326
- m = convert_heading_re .match (attr )
326
+ m = re_convert_heading .match (attr )
327
327
if m :
328
328
n = int (m .group (1 ))
329
329
@@ -409,7 +409,7 @@ def convert_blockquote(self, el, text, parent_tags):
409
409
def _indent_for_blockquote (match ):
410
410
line_content = match .group (1 )
411
411
return '> ' + line_content if line_content else '>'
412
- text = line_with_content_re .sub (_indent_for_blockquote , text )
412
+ text = re_line_with_content .sub (_indent_for_blockquote , text )
413
413
414
414
return '\n ' + text + '\n \n '
415
415
@@ -455,7 +455,7 @@ def convert_dd(self, el, text, parent_tags):
455
455
def _indent_for_dd (match ):
456
456
line_content = match .group (1 )
457
457
return ' ' + line_content if line_content else ''
458
- text = line_with_content_re .sub (_indent_for_dd , text )
458
+ text = re_line_with_content .sub (_indent_for_dd , text )
459
459
460
460
# insert definition marker into first-line indent whitespace
461
461
text = ':' + text [1 :]
@@ -465,7 +465,7 @@ def _indent_for_dd(match):
465
465
def convert_dt (self , el , text , parent_tags ):
466
466
# remove newlines from term text
467
467
text = (text or '' ).strip ()
468
- text = all_whitespace_re .sub (' ' , text )
468
+ text = re_all_whitespace .sub (' ' , text )
469
469
if '_inline' in parent_tags :
470
470
return ' ' + text + ' '
471
471
if not text :
@@ -489,7 +489,7 @@ def _convert_hn(self, n, el, text, parent_tags):
489
489
if style == UNDERLINED and n <= 2 :
490
490
line = '=' if n == 1 else '-'
491
491
return self .underline (text , line )
492
- text = all_whitespace_re .sub (' ' , text )
492
+ text = re_all_whitespace .sub (' ' , text )
493
493
hashes = '#' * n
494
494
if style == ATX_CLOSED :
495
495
return '\n \n %s %s %s\n \n ' % (hashes , text , hashes )
@@ -558,7 +558,7 @@ def convert_li(self, el, text, parent_tags):
558
558
def _indent_for_li (match ):
559
559
line_content = match .group (1 )
560
560
return bullet_indent + line_content if line_content else ''
561
- text = line_with_content_re .sub (_indent_for_li , text )
561
+ text = re_line_with_content .sub (_indent_for_li , text )
562
562
563
563
# insert bullet into first-line indent whitespace
564
564
text = bullet + text [bullet_width :]
0 commit comments