File tree 2 files changed +38
-2
lines changed
2 files changed +38
-2
lines changed Original file line number Diff line number Diff line change @@ -444,8 +444,21 @@ def handle_endtag(self, tag):
444
444
if self .section .el in self .context :
445
445
data = self .section .title
446
446
447
+ # Search for corresponding opening tag
448
+ index = data .index (f"<{ tag } >" )
449
+ for i in range (index + 1 , len (data )):
450
+ if not data [i ].isspace ():
451
+ index = len (data )
452
+ break
453
+
454
+ # Remove element if empty (or only whitespace)
455
+ if len (data ) > index :
456
+ while len (data ) > index :
457
+ data .pop ()
458
+
447
459
# Append to section title or text
448
- data .append (f"</{ tag } >" )
460
+ else :
461
+ data .append (f"</{ tag } >" )
449
462
450
463
# Called for the text contents of each tag
451
464
def handle_data (self , data ):
@@ -477,6 +490,11 @@ def handle_data(self, data):
477
490
escape (data , quote = False )
478
491
)
479
492
493
+ # Collapse adjacent whitespace
494
+ elif data .isspace ():
495
+ if not self .section .text or not self .section .text [- 1 ].isspace ():
496
+ self .section .text .append (data )
497
+
480
498
# Handle everything else
481
499
else :
482
500
self .section .text .append (
Original file line number Diff line number Diff line change @@ -444,8 +444,21 @@ def handle_endtag(self, tag):
444
444
if self .section .el in self .context :
445
445
data = self .section .title
446
446
447
+ # Search for corresponding opening tag
448
+ index = data .index (f"<{ tag } >" )
449
+ for i in range (index + 1 , len (data )):
450
+ if not data [i ].isspace ():
451
+ index = len (data )
452
+ break
453
+
454
+ # Remove element if empty (or only whitespace)
455
+ if len (data ) > index :
456
+ while len (data ) > index :
457
+ data .pop ()
458
+
447
459
# Append to section title or text
448
- data .append (f"</{ tag } >" )
460
+ else :
461
+ data .append (f"</{ tag } >" )
449
462
450
463
# Called for the text contents of each tag
451
464
def handle_data (self , data ):
@@ -477,6 +490,11 @@ def handle_data(self, data):
477
490
escape (data , quote = False )
478
491
)
479
492
493
+ # Collapse adjacent whitespace
494
+ elif data .isspace ():
495
+ if not self .section .text or not self .section .text [- 1 ].isspace ():
496
+ self .section .text .append (data )
497
+
480
498
# Handle everything else
481
499
else :
482
500
self .section .text .append (
You can’t perform that action at this time.
0 commit comments