You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
"description": "How to store markup (such as HTML or reStructuredText) and render it with a transformation"
5
+
"property=og:description": "How to store markup (such as HTML or reStructuredText) and render it with a transformation"
6
+
"property=og:title": "How to store markup (such as HTML or reStructuredText) and render it with a transformation"
7
+
"keywords": "Plone, content types, rich text, markup, and transformations"
8
8
---
9
9
10
-
# Rich text, markup and transformations
10
+
# Rich text, markup, and transformations
11
11
12
-
**How to store markup (such as HTML or reStructuredText) and render it with a transformation**
12
+
This chapter describes how to store markup (such as HTML or reStructuredText) and render it with a transformation.
13
13
14
-
Many content items need to allow users to provide rich text in some kind
15
-
of markup, be that HTML (perhaps entered using a WYSIWYG editor),
16
-
reStructuredText, Markdown or some other format. This markup typically
17
-
needs to be transformed into HTML for the view template, but we also
18
-
want to keep track of the original “raw” markup so that it can be edited
19
-
again. Even when the input format is HTML, there is often a need for a
20
-
transformation to tidy up the HTML and strip out tags that are not
21
-
permitted.
14
+
Many content items need to allow users to provide rich text in some kind of markup, be that HTML (perhaps entered using a "What You See Is What You Get" or WYSIWYG editor), reStructuredText, Markdown, or some other format.
15
+
This markup typically needs to be transformed into HTML for the view template, but we also want to keep track of the original raw markup so that it can be edited again.
16
+
Even when the input format is HTML, there is often a need for a transformation to tidy up the HTML and strip out tags that are not permitted.
22
17
23
-
It is possible to store HTML in a standard `Text` field. You can even
24
-
get a WYSIWYG widget, by using a schema such as this:
18
+
It is possible to store HTML in a standard `Text` field.
19
+
You can even get a WYSIWYG widget, by using a schema such as the following.
25
20
26
-
```
21
+
```python
27
22
from plone.autoform import directives as form
28
23
from plone.supermodel import model
29
24
from zope import schema
@@ -37,11 +32,10 @@ class ITestSchema(model.Schema):
37
32
38
33
(richtext-label)=
39
34
40
-
However, this approach does not allow for alternative markups or any
41
-
form of content filtering. For that, we need to use a more powerful
42
-
field: `RichText` from the [plone.app.textfield] package:
35
+
However, this approach does not allow for alternative markups or any form of content filtering.
36
+
For that we need to use a more powerful field, `RichText`, from the [`plone.app.textfield`](https://pypi.org/project/plone.app.textfield/) package.
43
37
44
-
```
38
+
```python
45
39
from plone.app.textfield import RichText
46
40
from plone.supermodel import model
47
41
@@ -50,28 +44,26 @@ class ITestSchema(model.Schema):
50
44
body = RichText(title="Body text")
51
45
```
52
46
53
-
The `RichText` field constructor can take the following arguments in
54
-
addition to the usual arguments for a `Text` field:
47
+
The `RichText` field constructor can take the following arguments, in addition to the usual arguments for a `Text` field.
55
48
56
-
-`default_mime_type`, a string representing the default MIME type of
57
-
the input markup. This defaults to `text/html`.
58
-
-`output_mime_type`, a string representing the default output MIME
59
-
type. This defaults to `text/x-html-safe`, which is a Plone-specific
60
-
MIME type that disallows certain tags. Use the {guilabel}`HTML Filtering`
61
-
control panel in Plone to control the tags.
62
-
-`allowed_mime_types`, a tuple of strings giving a vocabulary of
63
-
allowed input MIME types. If this is `None` (the default), the
64
-
allowable types will be restricted to those set in Plone’s
65
-
{guilabel}`Markup` control panel.
49
+
`default_mime_type`
50
+
: A string representing the default MIME type of the input markup.
51
+
This defaults to `text/html`.
66
52
67
-
Also note: The *default* field can be set to either a unicode object (in
68
-
which case it will be assumed to be a string of the default MIME type)
69
-
or a `RichTextValue` object (see below).
53
+
`output_mime_type`
54
+
: A string representing the default output MIME type.
55
+
This defaults to `text/x-html-safe`, which is a Plone-specific MIME type that disallows certain tags.
56
+
Use the {guilabel}`HTML Filtering` control panel in Plone to control the tags.
70
57
71
-
Below is an example of a field allow StructuredText and
72
-
reStructuredText, transformed to HTML by default:
58
+
`allowed_mime_types`
59
+
: A tuple of strings giving a vocabulary of allowed input MIME types.
60
+
If this is `None` (the default), the allowable types will be restricted to those set in Plone's {guilabel}`Markup` control panel.
73
61
74
-
```
62
+
The *default* field can be set to either a Unicode object (in which case it will be assumed to be a string of the default MIME type) or a `RichTextValue` object (see below).
63
+
64
+
Below is an example of a field that allows StructuredText and reStructuredText, transformed to HTML by default.
65
+
66
+
```python
75
67
from plone.app.textfield import RichText
76
68
from plone.supermodel import model
77
69
@@ -98,130 +90,104 @@ class ITestSchema(model.Schema):
98
90
)
99
91
```
100
92
93
+
101
94
## The RichTextValue
102
95
103
-
The `RichText` field does not store a string. Instead, it stores a
104
-
`RichTextValue` object. This is an immutable object that has the
105
-
following properties:
96
+
The `RichText` field does not store a string.
97
+
Instead, it stores a `RichTextValue`object.
98
+
This is an immutable object that has the following properties.
106
99
107
100
`raw`
108
-
109
-
: a unicode string with the original input markup;
101
+
: A Unicode string with the original input markup.
110
102
111
103
`mimeType`
112
-
113
-
: the MIME type of the original markup, e.g. `text/html` or
114
-
`text/structured`;
104
+
: The MIME type of the original markup, for example `text/html` or `text/structured`.
115
105
116
106
`encoding`
117
-
118
-
: the default character encoding used when transforming the input markup.
119
-
Most likely, this will be UTF-8;
107
+
: The default character encoding used when transforming the input markup.
108
+
Most likely, this will be UTF-8.
120
109
121
110
`raw_encoded`
122
-
123
-
: the raw input encoded in the given encoding;
111
+
: The raw input encoded in the given encoding.
124
112
125
113
`outputMimeType`
126
-
127
-
: the MIME type of the default output, taken from the field at the time of
128
-
instantiation;
114
+
: The MIME type of the default output, taken from the field at the time of instantiation.
129
115
130
116
`output`
117
+
: A Unicode object representing the transformed output.
118
+
If possible, this is cached persistently, until the `RichTextValue` is replaced with a new one (as happens when an edit form is saved, for example).
131
119
132
-
: a unicode object representing the transformed output. If possible, this
133
-
is cached persistently until the `RichTextValue` is replaced with a
134
-
new one (as happens when an edit form is saved, for example).
135
-
136
-
The storage of the `RichTextValue` object is optimised for the case where
137
-
the transformed output will be read frequently (i.e. on the view screen
138
-
of the content object) and the raw value will be read infrequently (i.e.
139
-
on the edit screen). Because the output value is cached indefinitely,
140
-
you will need to replace the `RichTextValue` object with a new one if any
141
-
of the transformation parameters change. However, as we will see below,
142
-
it is possible to apply a different transformation on demand should you
143
-
need to.
144
-
145
-
The code snippet belows shows how a `RichTextValue` object can be
146
-
constructed in code. In this case, we have a raw input string of type
147
-
`text/plain` that will be transformed to a default output of
148
-
`text/html`. (Note that we would normally look up the default output
149
-
type from the field instance.):
120
+
The storage of the `RichTextValue` object is optimized for the case where the transformed output will be read frequently (for example, on the view screen of the content object) and the raw value will be read infrequently (for example, on the edit screen).
121
+
Because the output value is cached indefinitely, you will need to replace the `RichTextValue` object with a new one if any of the transformation parameters change.
122
+
However, as we will see below, it is possible to apply a different transformation on demand, if you need to.
150
123
151
-
```
124
+
The code snippet belows shows how a `RichTextValue` object can be constructed in code.
125
+
In this case, we have a raw input string of type `text/plain` that will be transformed to a default output of `text/html`.
126
+
Note that we would normally look up the default output type from the field instance.
127
+
128
+
```python
152
129
from plone.app.textfield.value import RichTextValue
Of course, the standard widget used for a `RichText` field will
159
-
correctly store this type of object for you, so it is rarely necessary
160
-
to create one yourself.
134
+
Of course, the standard widget used for a `RichText` field will correctly store this type of object for you, so it is rarely necessary to create one yourself.
135
+
161
136
162
137
## Using rich text fields in templates
163
138
164
-
What about using the text field in a template? If you are using a
165
-
`DisplayForm`, the display widget for the `RichText` field will render
166
-
the transformed output markup automatically. If you are writing TAL
167
-
manually, you may try something like this:
139
+
What about using the text field in a template?
140
+
If you use a `DisplayForm`, the display widget for the `RichText` field will render the transformed output markup automatically.
141
+
If you write TAL manually, you may try something like the following.
168
142
169
-
```html
143
+
```xml
170
144
<divtal:content="structure context/body" />
171
145
```
172
146
173
-
This, however, will render a string like:
147
+
This, however, will render a string as follows.
174
148
175
-
```
149
+
```html
176
150
RichTextValue object. (Did you mean <attribute>.raw or <attribute>.output?)
This will render the cached, transformed output. This operation is
186
-
approximately as efficient as rendering a simple `Text` field, since the
187
-
transformation is only applied once, when the value is first saved.
159
+
This will render the cached, transformed output.
160
+
This operation is approximately as efficient as rendering a simple `Text` field, since the transformation is only applied once, when the value is first saved.
161
+
188
162
189
163
## Alternative transformations
190
164
191
-
Sometimes, you may want to invoke alternative transformations. Under the
192
-
hood, the default implementation uses the `portal_transforms` tool to
193
-
calculate a transform chain from the raw value’s input MIME type to the
194
-
desired output MIME type. (Should you need to write your own transforms,
195
-
take a look at [this tutorial].) This is abstracted behind an
196
-
`ITransformer` adapter to allow alternative implementations.
165
+
Sometimes, you may want to invoke alternative transformations.
166
+
Under the hood, the default implementation uses the `portal_transforms` tool to calculate a transform chain from the raw value's input MIME type to the desired output MIME type.
167
+
If you need to write your own transforms, take a look at [this tutorial](https://5.docs.plone.org/develop/plone/misc/portal_transforms.html).
168
+
This is abstracted behind an `ITransformer` adapter to allow alternative implementations.
197
169
198
-
To invoke a transformation in code, you can use the following syntax:
170
+
To invoke a transformation in code, you can use the following syntax.
199
171
200
-
```
172
+
```python
201
173
from plone.app.textfield.interfaces import ITransformer
0 commit comments