-
-
Notifications
You must be signed in to change notification settings - Fork 4k
/
Copy path__init__.py
51 lines (36 loc) · 1.53 KB
/
__init__.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
from django.utils.safestring import mark_safe
from wagtail.core.rich_text.feature_registry import FeatureRegistry
from wagtail.core.rich_text.rewriters import EmbedRewriter, LinkRewriter, MultiRuleRewriter
features = FeatureRegistry()
# Rewriter function to be built up on first call to expand_db_html, using the utility classes
# from wagtail.core.rich_text.rewriters along with the embed handlers / link handlers registered
# with the feature registry
FRONTEND_REWRITER = None
def expand_db_html(html):
"""
Expand database-representation HTML into proper HTML usable on front-end templates
"""
global FRONTEND_REWRITER
if FRONTEND_REWRITER is None:
embed_rules = features.get_embed_types()
link_rules = features.get_link_types()
FRONTEND_REWRITER = MultiRuleRewriter([
LinkRewriter(link_rules), EmbedRewriter(embed_rules)
])
return FRONTEND_REWRITER(html)
class RichText:
"""
A custom object used to represent a renderable rich text value.
Provides a 'source' property to access the original source code,
and renders to the front-end HTML rendering.
Used as the native value of a wagtailcore.blocks.field_block.RichTextBlock.
"""
def __init__(self, source):
self.source = (source or '')
def __html__(self):
return '<div class="rich-text">' + expand_db_html(self.source) + '</div>'
def __str__(self):
return mark_safe(self.__html__())
def __bool__(self):
return bool(self.source)
__nonzero__ = __bool__