Improved collapsing of adjacent whitespace and removal of empty elements in search plugin

This commit is contained in:
squidfunk 2023-01-08 10:23:28 +01:00
parent 7484b19e9a
commit 68257348b8
2 changed files with 38 additions and 2 deletions

View File

@ -444,7 +444,20 @@ class Parser(HTMLParser):
if self.section.el in self.context: if self.section.el in self.context:
data = self.section.title data = self.section.title
# Search for corresponding opening tag
index = data.index(f"<{tag}>")
for i in range(index + 1, len(data)):
if not data[i].isspace():
index = len(data)
break
# Remove element if empty (or only whitespace)
if len(data) > index:
while len(data) > index:
data.pop()
# Append to section title or text # Append to section title or text
else:
data.append(f"</{tag}>") data.append(f"</{tag}>")
# Called for the text contents of each tag # Called for the text contents of each tag
@ -477,6 +490,11 @@ class Parser(HTMLParser):
escape(data, quote = False) escape(data, quote = False)
) )
# Collapse adjacent whitespace
elif data.isspace():
if not self.section.text or not self.section.text[-1].isspace():
self.section.text.append(data)
# Handle everything else # Handle everything else
else: else:
self.section.text.append( self.section.text.append(

View File

@ -444,7 +444,20 @@ class Parser(HTMLParser):
if self.section.el in self.context: if self.section.el in self.context:
data = self.section.title data = self.section.title
# Search for corresponding opening tag
index = data.index(f"<{tag}>")
for i in range(index + 1, len(data)):
if not data[i].isspace():
index = len(data)
break
# Remove element if empty (or only whitespace)
if len(data) > index:
while len(data) > index:
data.pop()
# Append to section title or text # Append to section title or text
else:
data.append(f"</{tag}>") data.append(f"</{tag}>")
# Called for the text contents of each tag # Called for the text contents of each tag
@ -477,6 +490,11 @@ class Parser(HTMLParser):
escape(data, quote = False) escape(data, quote = False)
) )
# Collapse adjacent whitespace
elif data.isspace():
if not self.section.text or not self.section.text[-1].isspace():
self.section.text.append(data)
# Handle everything else # Handle everything else
else: else:
self.section.text.append( self.section.text.append(