mcms-components 0.1.0__py3-none-any.whl
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- mcms_components/__init__.py +13 -0
- mcms_components/apps.py +20 -0
- mcms_components/builtin.py +174 -0
- mcms_components/component.py +302 -0
- mcms_components/components/content/__init__.py +57 -0
- mcms_components/components/content/abbreviation.py +33 -0
- mcms_components/components/content/blockquote.py +27 -0
- mcms_components/components/content/break_component.py +24 -0
- mcms_components/components/content/code.py +27 -0
- mcms_components/components/content/deleted.py +27 -0
- mcms_components/components/content/emphasis.py +27 -0
- mcms_components/components/content/figcaption.py +27 -0
- mcms_components/components/content/figure.py +24 -0
- mcms_components/components/content/heading.py +55 -0
- mcms_components/components/content/horizontal_rule.py +25 -0
- mcms_components/components/content/inserted.py +27 -0
- mcms_components/components/content/link.py +34 -0
- mcms_components/components/content/list.py +52 -0
- mcms_components/components/content/list_item.py +33 -0
- mcms_components/components/content/mark.py +27 -0
- mcms_components/components/content/paragraph.py +27 -0
- mcms_components/components/content/preformatted.py +27 -0
- mcms_components/components/content/small.py +27 -0
- mcms_components/components/content/span.py +27 -0
- mcms_components/components/content/strong.py +26 -0
- mcms_components/components/content/subscript.py +27 -0
- mcms_components/components/content/superscript.py +27 -0
- mcms_components/components/content/text.py +30 -0
- mcms_components/components/content/time.py +31 -0
- mcms_components/components/content/tooltip.py +28 -0
- mcms_components/components/form/__init__.py +31 -0
- mcms_components/components/form/button.py +125 -0
- mcms_components/components/form/data_list.py +25 -0
- mcms_components/components/form/field_set.py +46 -0
- mcms_components/components/form/form.py +98 -0
- mcms_components/components/form/input.py +242 -0
- mcms_components/components/form/label.py +33 -0
- mcms_components/components/form/legend.py +29 -0
- mcms_components/components/form/meter.py +62 -0
- mcms_components/components/form/option.py +53 -0
- mcms_components/components/form/output.py +41 -0
- mcms_components/components/form/select.py +80 -0
- mcms_components/components/form/text_area.py +140 -0
- mcms_components/components/layout/__init__.py +25 -0
- mcms_components/components/layout/cluster.py +12 -0
- mcms_components/components/layout/column.py +24 -0
- mcms_components/components/layout/container.py +24 -0
- mcms_components/components/layout/content.py +12 -0
- mcms_components/components/layout/empty.py +31 -0
- mcms_components/components/layout/grid.py +24 -0
- mcms_components/components/layout/primary.py +19 -0
- mcms_components/components/layout/row.py +24 -0
- mcms_components/components/layout/stack.py +12 -0
- mcms_components/components/media/__init__.py +13 -0
- mcms_components/components/media/audio.py +50 -0
- mcms_components/components/media/image.py +35 -0
- mcms_components/components/media/video.py +55 -0
- mcms_components/components/navigation/__init__.py +11 -0
- mcms_components/components/navigation/current_page.py +24 -0
- mcms_components/components/navigation/navigation_button.py +70 -0
- mcms_components/components/popup/__init__.py +11 -0
- mcms_components/components/popup/alert.py +22 -0
- mcms_components/components/popup/dialog.py +19 -0
- mcms_components/components/popup/modal.py +22 -0
- mcms_components/components/popup/toast.py +22 -0
- mcms_components/components/structure/__init__.py +42 -0
- mcms_components/components/structure/article.py +22 -0
- mcms_components/components/structure/avatar.py +46 -0
- mcms_components/components/structure/badge.py +21 -0
- mcms_components/components/structure/caption.py +27 -0
- mcms_components/components/structure/footer.py +21 -0
- mcms_components/components/structure/header.py +22 -0
- mcms_components/components/structure/main.py +21 -0
- mcms_components/components/structure/navigation.py +22 -0
- mcms_components/components/structure/section.py +22 -0
- mcms_components/components/structure/sidebar.py +24 -0
- mcms_components/components/structure/table.py +23 -0
- mcms_components/components/structure/table_body.py +22 -0
- mcms_components/components/structure/table_cell.py +31 -0
- mcms_components/components/structure/table_foot.py +22 -0
- mcms_components/components/structure/table_head.py +22 -0
- mcms_components/components/structure/table_header.py +30 -0
- mcms_components/components/structure/table_row.py +22 -0
- mcms_components/enums.py +6 -0
- mcms_components/exceptions.py +24 -0
- mcms_components/global_fields.py +25 -0
- mcms_components/html.py +82 -0
- mcms_components/metadata.py +66 -0
- mcms_components/models/__init__.py +0 -0
- mcms_components/py.typed +0 -0
- mcms_components/registry.py +69 -0
- mcms_components/settings.py +0 -0
- mcms_components/values.py +12 -0
- mcms_components-0.1.0.dist-info/METADATA +107 -0
- mcms_components-0.1.0.dist-info/RECORD +96 -0
- mcms_components-0.1.0.dist-info/WHEEL +4 -0
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
"""Public API for mcms_components."""
|
|
2
|
+
|
|
3
|
+
__version__: str = "0.1.0"
|
|
4
|
+
|
|
5
|
+
from mcms_components.component import Component
|
|
6
|
+
from mcms_components.metadata import ComponentMetadata
|
|
7
|
+
from mcms_components.registry import ComponentRegistry
|
|
8
|
+
|
|
9
|
+
__all__ = [
|
|
10
|
+
"Component",
|
|
11
|
+
"ComponentMetadata",
|
|
12
|
+
"ComponentRegistry",
|
|
13
|
+
]
|
mcms_components/apps.py
ADDED
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
"""Django application configuration for mcms_components."""
|
|
2
|
+
|
|
3
|
+
from django.apps import AppConfig
|
|
4
|
+
from mcms_components.builtin import register_builtin_components
|
|
5
|
+
from mcms_components.registry import ComponentRegistry
|
|
6
|
+
|
|
7
|
+
|
|
8
|
+
class MCMSComponentsConfig(AppConfig):
|
|
9
|
+
"""
|
|
10
|
+
Django application configuration for the MCMS component system.
|
|
11
|
+
"""
|
|
12
|
+
|
|
13
|
+
default_auto_field = "django.db.models.BigAutoField"
|
|
14
|
+
name = "mcms_components"
|
|
15
|
+
label = "mcms_components"
|
|
16
|
+
verbose_name = "MCMS Components"
|
|
17
|
+
component_registry: ComponentRegistry = ComponentRegistry()
|
|
18
|
+
|
|
19
|
+
def ready(self) -> None:
|
|
20
|
+
register_builtin_components(self.component_registry)
|
|
@@ -0,0 +1,174 @@
|
|
|
1
|
+
from mcms_components.components.content import (
|
|
2
|
+
LINK,
|
|
3
|
+
HEADING,
|
|
4
|
+
PARAGRAPH,
|
|
5
|
+
LIST,
|
|
6
|
+
LIST_ITEM,
|
|
7
|
+
BLOCKQUOTE,
|
|
8
|
+
CODE,
|
|
9
|
+
PREFORMATTED,
|
|
10
|
+
SMALL,
|
|
11
|
+
EMPHASIS,
|
|
12
|
+
STRONG,
|
|
13
|
+
MARK,
|
|
14
|
+
HORIZONTAL_RULE,
|
|
15
|
+
BREAK,
|
|
16
|
+
FIGURE,
|
|
17
|
+
FIGCAPTION,
|
|
18
|
+
SUBSCRIPT,
|
|
19
|
+
SUPERSCRIPT,
|
|
20
|
+
DELETED,
|
|
21
|
+
INSERTED,
|
|
22
|
+
ABBREVIATION,
|
|
23
|
+
SPAN,
|
|
24
|
+
TIME,
|
|
25
|
+
TOOLTIP,
|
|
26
|
+
)
|
|
27
|
+
from mcms_components.components.media import IMAGE, VIDEO, AUDIO
|
|
28
|
+
from mcms_components.components.navigation import (
|
|
29
|
+
NAVIGATION_BUTTON,
|
|
30
|
+
CURRENT_PAGE,
|
|
31
|
+
)
|
|
32
|
+
from mcms_components.components.form import (
|
|
33
|
+
BUTTON,
|
|
34
|
+
FORM,
|
|
35
|
+
LABEL,
|
|
36
|
+
INPUT,
|
|
37
|
+
TEXT_AREA,
|
|
38
|
+
SELECT,
|
|
39
|
+
OPTION,
|
|
40
|
+
FIELD_SET,
|
|
41
|
+
LEGEND,
|
|
42
|
+
DATA_LIST,
|
|
43
|
+
METER,
|
|
44
|
+
OUTPUT,
|
|
45
|
+
)
|
|
46
|
+
|
|
47
|
+
from mcms_components.components.layout import (
|
|
48
|
+
CONTAINER,
|
|
49
|
+
GRID,
|
|
50
|
+
ROW,
|
|
51
|
+
COLUMN,
|
|
52
|
+
EMPTY,
|
|
53
|
+
CLUSTER,
|
|
54
|
+
STACK,
|
|
55
|
+
CONTENT,
|
|
56
|
+
PRIMARY,
|
|
57
|
+
)
|
|
58
|
+
|
|
59
|
+
from mcms_components.components.popup import ALERT, TOAST, DIALOG, MODAL
|
|
60
|
+
|
|
61
|
+
from mcms_components.components.structure import (
|
|
62
|
+
SECTION,
|
|
63
|
+
ARTICLE,
|
|
64
|
+
SIDEBAR,
|
|
65
|
+
HEADER,
|
|
66
|
+
FOOTER,
|
|
67
|
+
NAVIGATION,
|
|
68
|
+
MAIN,
|
|
69
|
+
TABLE,
|
|
70
|
+
TABLE_HEAD,
|
|
71
|
+
TABLE_BODY,
|
|
72
|
+
TABLE_FOOT,
|
|
73
|
+
TABLE_ROW,
|
|
74
|
+
TABLE_HEADER,
|
|
75
|
+
TABLE_CELL,
|
|
76
|
+
CAPTION,
|
|
77
|
+
BADGE,
|
|
78
|
+
AVATAR,
|
|
79
|
+
)
|
|
80
|
+
|
|
81
|
+
|
|
82
|
+
from mcms_components.registry import ComponentRegistry
|
|
83
|
+
|
|
84
|
+
|
|
85
|
+
def register_builtin_components(registry: ComponentRegistry) -> None:
|
|
86
|
+
"""
|
|
87
|
+
Register the built-in components provided by the mcms-components package.
|
|
88
|
+
"""
|
|
89
|
+
|
|
90
|
+
# Content components
|
|
91
|
+
# registry.register_component(TEXT) # Removing Text so that Empty is the only non-renderable component
|
|
92
|
+
registry.register_component(LINK)
|
|
93
|
+
registry.register_component(HEADING)
|
|
94
|
+
registry.register_component(PARAGRAPH)
|
|
95
|
+
registry.register_component(LIST)
|
|
96
|
+
registry.register_component(LIST_ITEM)
|
|
97
|
+
registry.register_component(BLOCKQUOTE)
|
|
98
|
+
registry.register_component(CODE)
|
|
99
|
+
registry.register_component(PREFORMATTED)
|
|
100
|
+
registry.register_component(SMALL)
|
|
101
|
+
registry.register_component(EMPHASIS)
|
|
102
|
+
registry.register_component(STRONG)
|
|
103
|
+
registry.register_component(MARK)
|
|
104
|
+
registry.register_component(HORIZONTAL_RULE)
|
|
105
|
+
registry.register_component(BREAK)
|
|
106
|
+
registry.register_component(FIGURE)
|
|
107
|
+
registry.register_component(FIGCAPTION)
|
|
108
|
+
registry.register_component(SUBSCRIPT)
|
|
109
|
+
registry.register_component(SUPERSCRIPT)
|
|
110
|
+
registry.register_component(DELETED)
|
|
111
|
+
registry.register_component(INSERTED)
|
|
112
|
+
registry.register_component(ABBREVIATION)
|
|
113
|
+
registry.register_component(SPAN)
|
|
114
|
+
registry.register_component(TIME)
|
|
115
|
+
registry.register_component(TOOLTIP)
|
|
116
|
+
|
|
117
|
+
# Media components
|
|
118
|
+
registry.register_component(IMAGE)
|
|
119
|
+
registry.register_component(VIDEO)
|
|
120
|
+
registry.register_component(AUDIO)
|
|
121
|
+
|
|
122
|
+
# Navigation components
|
|
123
|
+
registry.register_component(NAVIGATION_BUTTON)
|
|
124
|
+
registry.register_component(CURRENT_PAGE)
|
|
125
|
+
|
|
126
|
+
# Form components
|
|
127
|
+
registry.register_component(BUTTON)
|
|
128
|
+
registry.register_component(FORM)
|
|
129
|
+
registry.register_component(LABEL)
|
|
130
|
+
registry.register_component(INPUT)
|
|
131
|
+
registry.register_component(TEXT_AREA)
|
|
132
|
+
registry.register_component(SELECT)
|
|
133
|
+
registry.register_component(OPTION)
|
|
134
|
+
registry.register_component(FIELD_SET)
|
|
135
|
+
registry.register_component(LEGEND)
|
|
136
|
+
registry.register_component(DATA_LIST)
|
|
137
|
+
registry.register_component(METER)
|
|
138
|
+
registry.register_component(OUTPUT)
|
|
139
|
+
|
|
140
|
+
# Layout components
|
|
141
|
+
registry.register_component(CONTAINER)
|
|
142
|
+
registry.register_component(GRID)
|
|
143
|
+
registry.register_component(ROW)
|
|
144
|
+
registry.register_component(COLUMN)
|
|
145
|
+
registry.register_component(EMPTY)
|
|
146
|
+
registry.register_component(CLUSTER)
|
|
147
|
+
registry.register_component(STACK)
|
|
148
|
+
registry.register_component(CONTENT)
|
|
149
|
+
registry.register_component(PRIMARY)
|
|
150
|
+
|
|
151
|
+
# Structure components
|
|
152
|
+
registry.register_component(SECTION)
|
|
153
|
+
registry.register_component(ARTICLE)
|
|
154
|
+
registry.register_component(SIDEBAR)
|
|
155
|
+
registry.register_component(HEADER)
|
|
156
|
+
registry.register_component(FOOTER)
|
|
157
|
+
registry.register_component(NAVIGATION)
|
|
158
|
+
registry.register_component(MAIN)
|
|
159
|
+
registry.register_component(TABLE)
|
|
160
|
+
registry.register_component(TABLE_HEAD)
|
|
161
|
+
registry.register_component(TABLE_BODY)
|
|
162
|
+
registry.register_component(TABLE_FOOT)
|
|
163
|
+
registry.register_component(TABLE_ROW)
|
|
164
|
+
registry.register_component(TABLE_HEADER)
|
|
165
|
+
registry.register_component(TABLE_CELL)
|
|
166
|
+
registry.register_component(CAPTION)
|
|
167
|
+
registry.register_component(BADGE)
|
|
168
|
+
registry.register_component(AVATAR)
|
|
169
|
+
|
|
170
|
+
# Popup components
|
|
171
|
+
registry.register_component(ALERT)
|
|
172
|
+
registry.register_component(TOAST)
|
|
173
|
+
registry.register_component(DIALOG)
|
|
174
|
+
registry.register_component(MODAL)
|
|
@@ -0,0 +1,302 @@
|
|
|
1
|
+
"""
|
|
2
|
+
Core component implementation for django-mcms.
|
|
3
|
+
|
|
4
|
+
A Component represents a reusable visual element that
|
|
5
|
+
can be rendered as part of a RenderTree.
|
|
6
|
+
"""
|
|
7
|
+
|
|
8
|
+
from dataclasses import dataclass, field
|
|
9
|
+
from types import MappingProxyType
|
|
10
|
+
from typing import Any, Mapping
|
|
11
|
+
|
|
12
|
+
from mcms_content.exceptions import FieldValidationError
|
|
13
|
+
from mcms_components.exceptions import ComponentValidationError
|
|
14
|
+
from mcms_content.fields.base import Field
|
|
15
|
+
|
|
16
|
+
from mcms_components.metadata import ComponentMetadata
|
|
17
|
+
from mcms_components.html import HTMLTagMap
|
|
18
|
+
from mcms_components.global_fields import get_global_fields, get_global_attribute_fields
|
|
19
|
+
|
|
20
|
+
|
|
21
|
+
@dataclass(
|
|
22
|
+
frozen=True,
|
|
23
|
+
slots=True,
|
|
24
|
+
)
|
|
25
|
+
class Component:
|
|
26
|
+
"""
|
|
27
|
+
Immutable definition of an MCMS component.
|
|
28
|
+
|
|
29
|
+
A Component represents both a reusable visual component and a node
|
|
30
|
+
in the MCMS render tree. Components may be leaf components, such as
|
|
31
|
+
Text, Image and Button, or composite components containing other
|
|
32
|
+
components, such as Container, Grid, Header and Hero.
|
|
33
|
+
|
|
34
|
+
A component describes the information required by the renderer to
|
|
35
|
+
produce its HTML representation:
|
|
36
|
+
|
|
37
|
+
``html_tag``
|
|
38
|
+
Defines the HTML element produced by the component. For example,
|
|
39
|
+
a Button may use ``"button"``, a Link may use ``"a"``, and a
|
|
40
|
+
Container may use ``"div"``.
|
|
41
|
+
|
|
42
|
+
``fields``
|
|
43
|
+
Defines the content values accepted by the component. Fields are
|
|
44
|
+
instances of ``mcms_content.Field`` and are responsible for
|
|
45
|
+
describing, validating, serializing and deserializing their
|
|
46
|
+
respective values. The component does not redefine the type or
|
|
47
|
+
validation rules of these values.
|
|
48
|
+
|
|
49
|
+
``defaults``
|
|
50
|
+
Provides the default values for the component's fields. Defaults
|
|
51
|
+
belong to the component definition and are used when no runtime
|
|
52
|
+
value has been supplied.
|
|
53
|
+
|
|
54
|
+
``attribute_fields``
|
|
55
|
+
Maps HTML attribute names to component field names. This tells the
|
|
56
|
+
renderer where a field's value should appear in the generated
|
|
57
|
+
HTML. For example, ``{"href": "href"}`` means that the value of
|
|
58
|
+
the component's ``href`` field should be rendered as the HTML
|
|
59
|
+
``href`` attribute.
|
|
60
|
+
|
|
61
|
+
``content_field``
|
|
62
|
+
Optionally identifies a field whose value should be rendered as
|
|
63
|
+
the element's direct textual content. For example, a Button may
|
|
64
|
+
define ``content_field="label"`` so that the value of its
|
|
65
|
+
``label`` field becomes the content of the ``<button>`` element.
|
|
66
|
+
|
|
67
|
+
``style``
|
|
68
|
+
Defines the component's default design styling using the
|
|
69
|
+
``mcms_design.Style`` abstraction. Styling is part of the
|
|
70
|
+
component definition but is resolved by the rendering/design
|
|
71
|
+
system.
|
|
72
|
+
|
|
73
|
+
The Component does not render itself. It describes the structure and
|
|
74
|
+
semantics of a renderable node, while ``mcms_render`` is responsible
|
|
75
|
+
for recursively interpreting the component tree and producing HTML.
|
|
76
|
+
|
|
77
|
+
For example, a Link component may conceptually be defined as:
|
|
78
|
+
|
|
79
|
+
Link
|
|
80
|
+
├── html_tag = "a"
|
|
81
|
+
├── fields
|
|
82
|
+
│ ├── href
|
|
83
|
+
│ └── label
|
|
84
|
+
├── attribute_fields
|
|
85
|
+
│ └── href → href
|
|
86
|
+
└── content_field = "label"
|
|
87
|
+
|
|
88
|
+
This allows the renderer to produce:
|
|
89
|
+
|
|
90
|
+
<a href="...">...</a>
|
|
91
|
+
|
|
92
|
+
without requiring the renderer to contain component-specific logic
|
|
93
|
+
such as ``if component.name == "link"``.
|
|
94
|
+
"""
|
|
95
|
+
|
|
96
|
+
metadata: ComponentMetadata
|
|
97
|
+
|
|
98
|
+
html_tag: str | HTMLTagMap | None = "div"
|
|
99
|
+
|
|
100
|
+
fields: Mapping[str, Field] = field(
|
|
101
|
+
default_factory=dict,
|
|
102
|
+
)
|
|
103
|
+
|
|
104
|
+
defaults: Mapping[str, Any] = field(
|
|
105
|
+
default_factory=dict,
|
|
106
|
+
)
|
|
107
|
+
|
|
108
|
+
attribute_fields: Mapping[str, str] = field(
|
|
109
|
+
default_factory=dict,
|
|
110
|
+
)
|
|
111
|
+
|
|
112
|
+
content_field: str | None = None
|
|
113
|
+
|
|
114
|
+
def serialize(self) -> dict[str, Any]:
|
|
115
|
+
|
|
116
|
+
fields = {key: val.defn_serialize() for key, val in self.fields.items()}
|
|
117
|
+
return {
|
|
118
|
+
"metadata": self.metadata.serialize(),
|
|
119
|
+
"html_tag": self.html_tag.serialize()
|
|
120
|
+
if type(self.html_tag) is HTMLTagMap
|
|
121
|
+
else self.html_tag,
|
|
122
|
+
"fields": fields,
|
|
123
|
+
# "defaults": dict(self.defaults),
|
|
124
|
+
"attribute_fields": dict(self.attribute_fields),
|
|
125
|
+
"content_field": self.content_field,
|
|
126
|
+
}
|
|
127
|
+
|
|
128
|
+
def __post_init__(self) -> None:
|
|
129
|
+
"""
|
|
130
|
+
Normalize mutable input collections.
|
|
131
|
+
|
|
132
|
+
The Component itself is frozen, but dictionaries passed into the
|
|
133
|
+
constructor would otherwise remain mutable.
|
|
134
|
+
|
|
135
|
+
We therefore:
|
|
136
|
+
|
|
137
|
+
* copy ``fields`` and ``defaults``;
|
|
138
|
+
* expose them through read-only mapping proxies;
|
|
139
|
+
|
|
140
|
+
The Field objects themselves remain the domain objects owned by
|
|
141
|
+
mcms_content. This class does not attempt to duplicate or wrap
|
|
142
|
+
their behavior.
|
|
143
|
+
"""
|
|
144
|
+
global_fields = get_global_fields()
|
|
145
|
+
object.__setattr__(
|
|
146
|
+
self,
|
|
147
|
+
"fields",
|
|
148
|
+
MappingProxyType(
|
|
149
|
+
dict({**global_fields, **self.fields}),
|
|
150
|
+
),
|
|
151
|
+
)
|
|
152
|
+
|
|
153
|
+
global_attribute_fields = get_global_attribute_fields()
|
|
154
|
+
|
|
155
|
+
object.__setattr__(
|
|
156
|
+
self,
|
|
157
|
+
"attribute_fields",
|
|
158
|
+
MappingProxyType(
|
|
159
|
+
dict({**global_attribute_fields, **self.attribute_fields}),
|
|
160
|
+
),
|
|
161
|
+
)
|
|
162
|
+
|
|
163
|
+
object.__setattr__(
|
|
164
|
+
self,
|
|
165
|
+
"defaults",
|
|
166
|
+
MappingProxyType(
|
|
167
|
+
dict(self.defaults),
|
|
168
|
+
),
|
|
169
|
+
)
|
|
170
|
+
|
|
171
|
+
@property
|
|
172
|
+
def label(self) -> str:
|
|
173
|
+
"""
|
|
174
|
+
Return the human-readable component label.
|
|
175
|
+
"""
|
|
176
|
+
return self.metadata.label
|
|
177
|
+
|
|
178
|
+
@property
|
|
179
|
+
def name(self) -> str:
|
|
180
|
+
"""
|
|
181
|
+
Return the stable component identifier.
|
|
182
|
+
"""
|
|
183
|
+
return self.metadata.identifier
|
|
184
|
+
|
|
185
|
+
def field(self, name: str) -> Field:
|
|
186
|
+
"""
|
|
187
|
+
Return a configured field by name.
|
|
188
|
+
|
|
189
|
+
Parameters
|
|
190
|
+
----------
|
|
191
|
+
name:
|
|
192
|
+
Name of the field to retrieve.
|
|
193
|
+
|
|
194
|
+
Returns
|
|
195
|
+
-------
|
|
196
|
+
Field
|
|
197
|
+
The configured MCMS field.
|
|
198
|
+
|
|
199
|
+
Raises
|
|
200
|
+
------
|
|
201
|
+
KeyError
|
|
202
|
+
If the component does not define the requested field.
|
|
203
|
+
"""
|
|
204
|
+
return self.fields[name]
|
|
205
|
+
|
|
206
|
+
def has_field(self, name: str) -> bool:
|
|
207
|
+
"""
|
|
208
|
+
Return whether the component defines a field with the given name.
|
|
209
|
+
"""
|
|
210
|
+
return name in self.fields
|
|
211
|
+
|
|
212
|
+
def resolve_html_tag(
|
|
213
|
+
self,
|
|
214
|
+
values: dict[str, Any],
|
|
215
|
+
) -> str | None:
|
|
216
|
+
"""
|
|
217
|
+
Resolve the HTML tag for a component instance.
|
|
218
|
+
"""
|
|
219
|
+
if self.html_tag is None:
|
|
220
|
+
return None
|
|
221
|
+
|
|
222
|
+
if isinstance(self.html_tag, str):
|
|
223
|
+
return self.html_tag
|
|
224
|
+
|
|
225
|
+
value = values[self.html_tag.field]
|
|
226
|
+
|
|
227
|
+
return self.html_tag.resolve(value)
|
|
228
|
+
|
|
229
|
+
def validate_values(
|
|
230
|
+
self,
|
|
231
|
+
values: Mapping[str, Any],
|
|
232
|
+
) -> None:
|
|
233
|
+
"""
|
|
234
|
+
Validate values supplied to this component.
|
|
235
|
+
|
|
236
|
+
Component fields and attribute fields together define the complete
|
|
237
|
+
set of values accepted by a component.
|
|
238
|
+
|
|
239
|
+
Validation is delegated to the individual Field instances, allowing
|
|
240
|
+
each field to enforce its own type and semantic constraints.
|
|
241
|
+
|
|
242
|
+
Parameters
|
|
243
|
+
----------
|
|
244
|
+
values:
|
|
245
|
+
Mapping containing values supplied to the component.
|
|
246
|
+
|
|
247
|
+
Raises
|
|
248
|
+
------
|
|
249
|
+
ComponentValidationError
|
|
250
|
+
If a supplied value fails validation or a required field is
|
|
251
|
+
missing.
|
|
252
|
+
|
|
253
|
+
ValueError
|
|
254
|
+
If a value is supplied for a field that is not defined by the
|
|
255
|
+
component.
|
|
256
|
+
"""
|
|
257
|
+
expected_fields = self.fields
|
|
258
|
+
expected_attribute_fields = self.attribute_fields
|
|
259
|
+
|
|
260
|
+
expected_values = {
|
|
261
|
+
*expected_fields.keys(),
|
|
262
|
+
*expected_attribute_fields.keys(),
|
|
263
|
+
}
|
|
264
|
+
|
|
265
|
+
supplied_values = set(values.keys())
|
|
266
|
+
|
|
267
|
+
unknown_values = supplied_values - expected_values
|
|
268
|
+
|
|
269
|
+
if unknown_values:
|
|
270
|
+
unknown = ", ".join(
|
|
271
|
+
sorted(unknown_values),
|
|
272
|
+
)
|
|
273
|
+
|
|
274
|
+
raise ComponentValidationError(
|
|
275
|
+
f"Component '{self.metadata.identifier}' received "
|
|
276
|
+
f"undefined value(s): {unknown}."
|
|
277
|
+
)
|
|
278
|
+
|
|
279
|
+
for name, field_class in expected_fields.items():
|
|
280
|
+
if name not in values:
|
|
281
|
+
if field_class.required:
|
|
282
|
+
try:
|
|
283
|
+
field_class.validate(None)
|
|
284
|
+
except FieldValidationError:
|
|
285
|
+
raise ComponentValidationError(
|
|
286
|
+
f"'{field_class.name} is required for Component '{self.name}'"
|
|
287
|
+
)
|
|
288
|
+
|
|
289
|
+
continue
|
|
290
|
+
|
|
291
|
+
field_class.validate(values[name])
|
|
292
|
+
|
|
293
|
+
for name, field_name in expected_attribute_fields.items():
|
|
294
|
+
field = expected_fields[field_name]
|
|
295
|
+
|
|
296
|
+
if name not in values:
|
|
297
|
+
if field.required:
|
|
298
|
+
field.validate(None)
|
|
299
|
+
|
|
300
|
+
continue
|
|
301
|
+
|
|
302
|
+
field.validate(values[name])
|
|
@@ -0,0 +1,57 @@
|
|
|
1
|
+
"""
|
|
2
|
+
Built-in content components.
|
|
3
|
+
"""
|
|
4
|
+
|
|
5
|
+
from mcms_components.components.content.text import TEXT
|
|
6
|
+
from mcms_components.components.content.link import LINK
|
|
7
|
+
from mcms_components.components.content.heading import HEADING
|
|
8
|
+
from mcms_components.components.content.paragraph import PARAGRAPH
|
|
9
|
+
from mcms_components.components.content.list import LIST
|
|
10
|
+
from mcms_components.components.content.list_item import LIST_ITEM
|
|
11
|
+
from mcms_components.components.content.blockquote import BLOCKQUOTE
|
|
12
|
+
from mcms_components.components.content.code import CODE
|
|
13
|
+
from mcms_components.components.content.preformatted import PREFORMATTED
|
|
14
|
+
from mcms_components.components.content.small import SMALL
|
|
15
|
+
from mcms_components.components.content.emphasis import EMPHASIS
|
|
16
|
+
from mcms_components.components.content.strong import STRONG
|
|
17
|
+
from mcms_components.components.content.mark import MARK
|
|
18
|
+
from mcms_components.components.content.horizontal_rule import HORIZONTAL_RULE
|
|
19
|
+
from mcms_components.components.content.break_component import BREAK
|
|
20
|
+
from mcms_components.components.content.figure import FIGURE
|
|
21
|
+
from mcms_components.components.content.figcaption import FIGCAPTION
|
|
22
|
+
from mcms_components.components.content.subscript import SUBSCRIPT
|
|
23
|
+
from mcms_components.components.content.superscript import SUPERSCRIPT
|
|
24
|
+
from mcms_components.components.content.deleted import DELETED
|
|
25
|
+
from mcms_components.components.content.inserted import INSERTED
|
|
26
|
+
from mcms_components.components.content.abbreviation import ABBREVIATION
|
|
27
|
+
from mcms_components.components.content.time import TIME
|
|
28
|
+
from mcms_components.components.content.span import SPAN
|
|
29
|
+
from mcms_components.components.content.tooltip import TOOLTIP
|
|
30
|
+
|
|
31
|
+
__all__ = [
|
|
32
|
+
"TEXT",
|
|
33
|
+
"LINK",
|
|
34
|
+
"HEADING",
|
|
35
|
+
"PARAGRAPH",
|
|
36
|
+
"LIST",
|
|
37
|
+
"LIST_ITEM",
|
|
38
|
+
"BLOCKQUOTE",
|
|
39
|
+
"CODE",
|
|
40
|
+
"PREFORMATTED",
|
|
41
|
+
"SMALL",
|
|
42
|
+
"EMPHASIS",
|
|
43
|
+
"STRONG",
|
|
44
|
+
"MARK",
|
|
45
|
+
"HORIZONTAL_RULE",
|
|
46
|
+
"BREAK",
|
|
47
|
+
"FIGURE",
|
|
48
|
+
"FIGCAPTION",
|
|
49
|
+
"SUBSCRIPT",
|
|
50
|
+
"SUPERSCRIPT",
|
|
51
|
+
"DELETED",
|
|
52
|
+
"INSERTED",
|
|
53
|
+
"ABBREVIATION",
|
|
54
|
+
"TIME",
|
|
55
|
+
"SPAN",
|
|
56
|
+
"TOOLTIP",
|
|
57
|
+
]
|
|
@@ -0,0 +1,33 @@
|
|
|
1
|
+
"""
|
|
2
|
+
Abbreviation component.
|
|
3
|
+
|
|
4
|
+
This module defines the built-in MCMS Abbreviation component.
|
|
5
|
+
|
|
6
|
+
The Abbreviation component represents an abbreviated form of a word
|
|
7
|
+
or phrase.
|
|
8
|
+
"""
|
|
9
|
+
|
|
10
|
+
from mcms_components.component import Component
|
|
11
|
+
from mcms_components.metadata import ComponentMetadata
|
|
12
|
+
from mcms_content.fields import TextField
|
|
13
|
+
|
|
14
|
+
|
|
15
|
+
ABBREVIATION = Component(
|
|
16
|
+
metadata=ComponentMetadata(
|
|
17
|
+
identifier="abbreviation",
|
|
18
|
+
label="Abbreviation",
|
|
19
|
+
description="An abbreviated form of a word or phrase.",
|
|
20
|
+
category="content",
|
|
21
|
+
),
|
|
22
|
+
html_tag="abbr",
|
|
23
|
+
fields={
|
|
24
|
+
"title": TextField(
|
|
25
|
+
name="content",
|
|
26
|
+
),
|
|
27
|
+
"content": TextField(name="content", required=True, default="Abbreviation"),
|
|
28
|
+
},
|
|
29
|
+
attribute_fields={
|
|
30
|
+
"title": "title",
|
|
31
|
+
},
|
|
32
|
+
content_field="content",
|
|
33
|
+
)
|
|
@@ -0,0 +1,27 @@
|
|
|
1
|
+
"""
|
|
2
|
+
Blockquote component.
|
|
3
|
+
|
|
4
|
+
This module defines the built-in MCMS Blockquote component.
|
|
5
|
+
|
|
6
|
+
The Blockquote component represents a quotation from another source.
|
|
7
|
+
Its ``value`` field provides the textual content of the quotation.
|
|
8
|
+
"""
|
|
9
|
+
|
|
10
|
+
from mcms_components.component import Component
|
|
11
|
+
from mcms_components.metadata import ComponentMetadata
|
|
12
|
+
from mcms_content.fields import LongTextField
|
|
13
|
+
|
|
14
|
+
|
|
15
|
+
BLOCKQUOTE = Component(
|
|
16
|
+
metadata=ComponentMetadata(
|
|
17
|
+
identifier="blockquote",
|
|
18
|
+
label="Blockquote",
|
|
19
|
+
description="A quotation from another source.",
|
|
20
|
+
category="content",
|
|
21
|
+
),
|
|
22
|
+
html_tag="blockquote",
|
|
23
|
+
fields={
|
|
24
|
+
"content": LongTextField(name="content", required=True, default="Blockquote"),
|
|
25
|
+
},
|
|
26
|
+
content_field="content",
|
|
27
|
+
)
|
|
@@ -0,0 +1,24 @@
|
|
|
1
|
+
"""
|
|
2
|
+
Break component.
|
|
3
|
+
|
|
4
|
+
This module defines the built-in MCMS Break component.
|
|
5
|
+
|
|
6
|
+
The Break component represents a line break in textual content.
|
|
7
|
+
|
|
8
|
+
The HTML ``br`` element is a void element and therefore has no
|
|
9
|
+
content field or child content.
|
|
10
|
+
"""
|
|
11
|
+
|
|
12
|
+
from mcms_components.component import Component
|
|
13
|
+
from mcms_components.metadata import ComponentMetadata
|
|
14
|
+
|
|
15
|
+
|
|
16
|
+
BREAK = Component(
|
|
17
|
+
metadata=ComponentMetadata(
|
|
18
|
+
identifier="break",
|
|
19
|
+
label="Line Break",
|
|
20
|
+
description="A line break in textual content.",
|
|
21
|
+
category="content",
|
|
22
|
+
),
|
|
23
|
+
html_tag="br",
|
|
24
|
+
)
|
|
@@ -0,0 +1,27 @@
|
|
|
1
|
+
"""
|
|
2
|
+
Code component.
|
|
3
|
+
|
|
4
|
+
This module defines the built-in MCMS Code component.
|
|
5
|
+
|
|
6
|
+
The Code component represents an inline fragment of computer code.
|
|
7
|
+
Its ``value`` field provides the code content.
|
|
8
|
+
"""
|
|
9
|
+
|
|
10
|
+
from mcms_components.component import Component
|
|
11
|
+
from mcms_components.metadata import ComponentMetadata
|
|
12
|
+
from mcms_content.fields import CodeField
|
|
13
|
+
|
|
14
|
+
|
|
15
|
+
CODE = Component(
|
|
16
|
+
metadata=ComponentMetadata(
|
|
17
|
+
identifier="code",
|
|
18
|
+
label="Code",
|
|
19
|
+
description="An inline fragment of computer code.",
|
|
20
|
+
category="content",
|
|
21
|
+
),
|
|
22
|
+
html_tag="code",
|
|
23
|
+
fields={
|
|
24
|
+
"content": CodeField(name="content", required=True, default="Code"),
|
|
25
|
+
},
|
|
26
|
+
content_field="content",
|
|
27
|
+
)
|