This repository was archived by the owner on Aug 29, 2025. It is now read-only.
-
-
Notifications
You must be signed in to change notification settings - Fork 49
Expand file tree
/
Copy path_to_html5.py
More file actions
65 lines (53 loc) · 2.03 KB
/
_to_html5.py
File metadata and controls
65 lines (53 loc) · 2.03 KB
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
52
53
54
55
56
57
58
59
60
61
62
63
64
65
import collections
from dash.development.base_component import Component
import re
_VOID_ELEMENTS = [
'area', 'base', 'br', 'col', 'embed', 'hr', 'img', 'input', 'keygen',
'link', 'meta', 'param', 'source', 'track', 'wbr'
]
_CAMELCASE_REGEX = re.compile('([a-z0-9])([A-Z])')
def _camel_case_to_css_case(name):
return _CAMELCASE_REGEX.sub(r'\1-\2', name).lower()
def _attribute(name, value):
if name == 'className':
return ('class', value,)
elif name == 'style':
# Dash CSS is camel-cased but CSS is hyphenated
# convert e.g. fontColor to font-color
inline_style = '; '.join([
'='.join([_camel_case_to_css_case(k), v])
for (k, v) in value.iteritems()
])
return (name, inline_style,)
else:
return (name, value,)
def _to_html5(self):
def __to_html5(component):
if not isinstance(component, Component):
return str(component)
component_type = component._type.lower()
children = ''
if component_type not in _VOID_ELEMENTS:
children = getattr(component, 'children', '')
if isinstance(children, Component):
children = __to_html5(children)
elif isinstance(children, collections.MutableSequence):
children = '\n'.join([__to_html5(child) for child in children])
else:
children = str(children)
if children != '':
children = '\n' + children
closing = ''
if component_type not in _VOID_ELEMENTS:
closing = '\n</{}>'.format(component_type)
return '<{type}{properties}>{children}{closing}'.format(
type=component_type,
properties=''.join([
' {}="{}"'.format(
_attribute(p, getattr(component, p)))
for p in component._prop_names
if (hasattr(component, p) and p is not 'children')
]),
children=children,
closing=closing)
return __to_html5(self)