From a13c81e42ac2e832116ff6ccd398b6ad02a261e6 Mon Sep 17 00:00:00 2001 From: j Date: Mon, 24 Sep 2018 18:32:38 +0200 Subject: [PATCH] add css selector to render_page_as_text, add render_page_as_html --- CHANGES | 4 ++++ content_plugins/shortcuts.py | 18 +++++++++++++++++- 2 files changed, 21 insertions(+), 1 deletion(-) diff --git a/CHANGES b/CHANGES index e5e3180..5cf0c59 100644 --- a/CHANGES +++ b/CHANGES @@ -1,3 +1,7 @@ +0.3.10 2018-09-24 +- add render_as_html shortcut +- render_as_text can take css selector (requires lxml) + 0.3.9 2018-09-20 - Slugify style slug in StyleMixin diff --git a/content_plugins/shortcuts.py b/content_plugins/shortcuts.py index 72be31e..9abe93c 100644 --- a/content_plugins/shortcuts.py +++ b/content_plugins/shortcuts.py @@ -1,3 +1,4 @@ +import re from django.contrib.auth.models import AnonymousUser from django.http import HttpRequest from django.template.loader import render_to_string @@ -7,9 +8,24 @@ from content_editor.contents import contents_for_item from shared.utils.text import html_entities_to_unicode -def render_page_as_text(page, template, context_data): +def render_page_as_html(page, template, context_data, css_selector=None): request = HttpRequest() request.user = AnonymousUser() html = render_to_string(template, context_data, request=request) + + if css_selector: + import lxml.html + doc = lxml.html.fromstring(html) + html = [] + for part in doc.cssselect(css_selector): + html.append(lxml.html.tostring(part).decode().strip()) + html = '\n'.join(html) + return html + +def render_page_as_text(page, template, context_data, css_selector=None): + html = render_page_as_html(page, template, context_data, css_selector) text = html_entities_to_unicode(strip_tags(html)).strip() + text = re.sub('[\t ]+', ' ', text) + text = re.sub(re.compile('\n +', re.DOTALL), '\n', text) + text = re.sub(re.compile('\n+', re.DOTALL), '\n', text) return text