From 9914d4ef0b3dcd36adc027f3d15dd8492ab46eb5 Mon Sep 17 00:00:00 2001 From: Fabio Manganiello Date: Sun, 13 Oct 2019 12:29:39 +0200 Subject: [PATCH] Handling CSS and fonts properly in RSSUpdates digests --- .../backend/http/request/rss/__init__.py | 33 ++++++++++++++++--- 1 file changed, 28 insertions(+), 5 deletions(-) diff --git a/platypush/backend/http/request/rss/__init__.py b/platypush/backend/http/request/rss/__init__.py index d20dc35f7..d8a881a83 100644 --- a/platypush/backend/http/request/rss/__init__.py +++ b/platypush/backend/http/request/rss/__init__.py @@ -99,12 +99,19 @@ class RssUpdates(HttpRequest): self.title = feed.feed['title'] source_record.title = self.title - digest = u''' + content = u'''

{}

Feeds digest generated on {}

'''.format(self.title, datetime.datetime.now().strftime('%d %B %Y, %H:%M')) + style = ''' + body { + font-size: 22px; + font-family: 'Merriweather', Georgia, 'Times New Roman', Times, serif; + } + ''' + self.logger.info('Parsed {:d} items from RSS feed <{}>' .format(len(feed.entries), self.url)) @@ -127,8 +134,8 @@ class RssUpdates(HttpRequest): else: entry.content = None - digest += '

{}

{}' \ - .format(entry.title, entry.content) + content += u'''

{}

+
{}
'''.format(entry.title, entry.content) e = { 'entry_id': entry.id, @@ -164,11 +171,27 @@ class RssUpdates(HttpRequest): os.makedirs(os.path.dirname(digest_filename), exist_ok=True) if self.digest_format == 'html': + content = ''' + + + {title} + + + {{content}} + + '''.format(title=self.title, style=style, content=content) + with open(digest_filename, 'w', encoding='utf-8') as f: - f.write(digest) + f.write(content) elif self.digest_format == 'pdf': import weasyprint - weasyprint.HTML(string=digest).write_pdf(digest_filename) + from weasyprint.fonts import FontConfiguration + + font_config = FontConfiguration() + css = [weasyprint.CSS('https://fonts.googleapis.com/css?family=Merriweather'), + weasyprint.CSS(string=style, font_config=font_config)] + + weasyprint.HTML(string=content).write_pdf(digest_filename, stylesheets=css) else: raise RuntimeError('Unsupported format: {}. Supported formats: ' + 'html or pdf'.format(self.digest_format))