Refactor html->plaintext conversion (for bulk email) into separate library
This commit is contained in:
23
common/lib/html_to_text.py
Normal file
23
common/lib/html_to_text.py
Normal file
@@ -0,0 +1,23 @@
|
||||
"""Provides a function to convert html to plaintext."""
|
||||
from subprocess import Popen, PIPE
|
||||
|
||||
def html_to_text(html_message):
|
||||
"""
|
||||
Converts an html message to plaintext.
|
||||
Currently uses lynx in a subprocess; should be refactored to
|
||||
use something more pythonic.
|
||||
"""
|
||||
process = Popen(
|
||||
['lynx', '-stdin', '-display_charset=UTF-8', '-assume_charset=UTF-8', '-dump'],
|
||||
stdin=PIPE,
|
||||
stdout=PIPE
|
||||
)
|
||||
# use lynx to get plaintext
|
||||
(plaintext, err_from_stderr) = process.communicate(
|
||||
input=html_message.encode('utf-8')
|
||||
)
|
||||
|
||||
if err_from_stderr:
|
||||
log.info(err_from_stderr)
|
||||
|
||||
return plaintext
|
||||
Reference in New Issue
Block a user