-
Notifications
You must be signed in to change notification settings - Fork 32
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add custom ExcerptCreator and use it within the API reponses
- Loading branch information
1 parent
e9cf59a
commit e0fa2d3
Showing
4 changed files
with
44 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
""" | ||
This module includes some utility classes and functions used within the API module. | ||
""" | ||
import logging | ||
|
||
from html.parser import HTMLParser | ||
|
||
logger = logging.getLogger(__name__) | ||
|
||
|
||
class ExcerptGenerator(HTMLParser): | ||
""" | ||
Class that can be used to convert a string containing HTML into a pure text string. | ||
""" | ||
|
||
text = "" | ||
|
||
def handle_data(self, data): | ||
""" | ||
Function that overwrites the HTMLParser function and returns a a pure text representations. | ||
:param data: The original HTML content. | ||
:type data: str | ||
""" | ||
self.text += data | ||
|
||
def error(self, message): | ||
logger.warning( | ||
"The ExcerptGenerator couldn't create a pure text for %s and returns the following error message: %s", | ||
self.text, | ||
message, | ||
) |