Răsfoiți Sursa

AMP renderer for rich text block (#134)

Fixes issue #119
Cory Sutyak 6 ani în urmă
părinte
comite
eed9c4b148

+ 1 - 1
coderedcms/blocks/__init__.py

@@ -16,7 +16,7 @@ from .layout_blocks import * #noqa
 # Collections of blocks commonly used together.
 
 HTML_STREAMBLOCKS = [
-    ('text', blocks.RichTextBlock(icon='fa-file-text-o')),
+    ('text', RichTextBlock(icon='fa-file-text-o')),
     ('button', ButtonBlock()),
     ('image', ImageBlock()),
     ('image_link', ImageLinkBlock()),

+ 4 - 0
coderedcms/blocks/content_blocks.py

@@ -325,3 +325,7 @@ class ReusableContentBlock(BaseBlock):
         icon = 'fa-recycle'
         label = _('Reusable Content')
         template = 'coderedcms/blocks/reusable_content_block.html'
+
+class RichTextBlock(blocks.RichTextBlock):
+    class Meta:
+        template = 'coderedcms/blocks/rich_text_block.html'

Fișier diff suprimat deoarece este prea mare
+ 23 - 0
coderedcms/migrations/0010_auto_20190320_1231.py


+ 14 - 0
coderedcms/migrations/0012_merge_20190322_1324.py

@@ -0,0 +1,14 @@
+# Generated by Django 2.1.7 on 2019-03-22 17:24
+
+from django.db import migrations
+
+
+class Migration(migrations.Migration):
+
+    dependencies = [
+        ('coderedcms', '0011_merge_20190322_1309'),
+        ('coderedcms', '0010_auto_20190320_1231'),
+    ]
+
+    operations = [
+    ]

+ 12 - 0
coderedcms/templates/coderedcms/blocks/rich_text_block.html

@@ -0,0 +1,12 @@
+{% extends 'coderedcms/blocks/base_block.html' %}
+{% load wagtailcore_tags coderedcms_tags %}
+
+{% block block_render %}
+
+{% if format == 'amp' %}
+{{ self|richtext_amp }}
+{% else %}
+{{ self|richtext }}
+{% endif %}
+
+{% endblock %}

+ 13 - 0
coderedcms/templatetags/coderedcms_tags.py

@@ -8,6 +8,8 @@ from django.utils import timezone
 from django.utils.html import mark_safe
 from django.utils.formats import localize
 from wagtail.core.models import Collection
+from wagtail.core.rich_text import RichText
+from wagtail.core.templatetags.wagtailcore_tags import richtext
 from wagtail.images.models import Image
 
 from coderedcms import utils, __version__
@@ -124,3 +126,14 @@ def structured_data_datetime(dt):
     if dt.time():
         return datetime.strftime(dt, "%Y-%m-%dT%H:%M")
     return datetime.strftime(dt, "%Y-%m-%d")
+
+@register.filter
+def richtext_amp(value):
+
+    if isinstance(value, RichText):
+        value = richtext(value.source)
+    else:
+        value = richtext(value)
+    
+    value = utils.convert_to_amp(value)
+    return mark_safe(value)

+ 17 - 1
coderedcms/utils.py

@@ -1,4 +1,4 @@
-
+from bs4 import BeautifulSoup
 from django.core.validators import URLValidator
 from django.core.exceptions import ValidationError
 from django.utils.html import mark_safe
@@ -37,3 +37,19 @@ def fix_ical_datetime_format(dt_str):
         dt_str = dt_str[:-3] + dt_str[-2:]
         return dt_str
     return dt_str
+
+def convert_to_amp(value):
+    """
+    Function that converts non-amp compliant html to valid amp html.
+    value must be a string
+    """
+    soup = BeautifulSoup(value)
+
+    #Replace img tags with amp-img
+    try:
+        img_tags = soup.find('img')
+        img_tags.name = 'amp-img'
+    except AttributeError:
+        pass
+
+    return soup.prettify()

+ 1 - 0
setup.py

@@ -40,6 +40,7 @@ setup(
         'Topic :: Internet :: WWW/HTTP :: Site Management',
     ],
     install_requires=[
+        "beautifulsoup4>=4.5.1,<4.6.1",
         'django-eventtools==0.9.*',
         'django-bootstrap4',
         'django>=2.0,<2.2',

Unele fișiere nu au fost afișate deoarece prea multe fișiere au fost modificate în acest diff