This repository has been archived by the owner on Nov 20, 2023. It is now read-only.
forked from pypa/sampleproject
-
Notifications
You must be signed in to change notification settings - Fork 0
/
generators.py
168 lines (137 loc) · 4.5 KB
/
generators.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
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
# -*- coding: utf-8 -*-
"""
smallparts.markup.generators
Markup (HTML, XML) generation
"""
from smallparts import constants
from smallparts.markup import elements
from smallparts.namespaces import Namespace
from smallparts.text import join
#
# Constants
#
SUPPORTED_HTML_DIALECTS = {
elements.LABEL_HTML_5: Namespace(
doctype='<!DOCTYPE html>',
xmlns=None),
elements.LABEL_XHTML_1_0_STRICT: Namespace(
doctype='<!DOCTYPE html PUBLIC'
' "-//W3C//DTD XHTML 1.0 Strict//EN"'
' "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">',
xmlns='http://www.w3.org/1999/xhtml'),
elements.LABEL_XHTML_1_0_TRANSITIONAL: Namespace(
doctype='<!DOCTYPE html PUBLIC'
' "-//W3C//DTD XHTML 1.0 Transitional//EN"'
' "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">',
xmlns='http://www.w3.org/1999/xhtml'),
}
#
# Functions
#
def css_property(property_name, property_value):
"""Generate a CSS property:
property_name: property_value;
"""
return '{0}: {1};'.format(property_name, property_value)
def css_important_property(property_name, property_value):
"""Generate an 'important' CSS property:
property_name: property_value !important;
"""
return css_property(property_name,
'{0} !important'.format(property_value))
def html_document(dialect=elements.LABEL_HTML_5,
lang='en',
title='Untitled page',
head='',
body=''):
"""Generate an HTML document"""
try:
html_dialect = SUPPORTED_HTML_DIALECTS[dialect]
except KeyError:
raise ValueError(
'Unsupported HTML dialect.'
' Please specify one of {0}!'.format(
constants.COMMA_BLANK.join(SUPPORTED_HTML_DIALECTS)))
#
element = elements.Cache(dialect)
head_fragments = ['']
if dialect == elements.LABEL_HTML_5 and \
'<meta charset' not in head.lower():
head_fragments.append(element.meta(charset=constants.UTF_8))
#
if '<title' not in head.lower():
head_fragments.append(element.title(title))
#
head = head.strip()
if head:
head_fragments.append(head)
#
head_fragments.append('')
body = body.strip()
if body:
body = '\n{0}\n'.format(body)
return join.by_newlines(
html_dialect.doctype,
element.html(
join.by_newlines(
'',
element.head(constants.NEWLINE.join(head_fragments)),
element.body(body),
''),
xmlns=html_dialect.xmlns,
lang=lang))
def js_function_call(function_name, arguments):
"""Generate JavaScript code:
function_name(*arguments)
"""
return '{0}({1})'.format(
function_name,
constants.COMMA_BLANK.join(
"{0!r}".format(single_arg)
for single_arg in arguments))
def js_return(function_name, *arguments):
"""Generate JavaScript code:
return function_name(*arguments);
"""
return 'return {0};'.format(
js_function_call(function_name, arguments))
def wrap_cdata(character_data):
"""Wrap character_data in a CDATA section.
If necessary use multiple CDATA sections as suggested in
<https://en.wikipedia.org/wiki/CDATA#Nesting>
"""
return join.directly(
'<![CDATA[',
character_data.replace(']]>', ']]]]><![CDATA[>'),
']]>')
def xml_declaration(version=constants.XML_1_0,
encoding=constants.UTF_8,
standalone=None):
"""Return an XML declaration.
Omit the 'standalone' attribute if not specified.
"""
if standalone is not None:
if standalone:
standalone = constants.YES
else:
standalone = constants.NO
#
#
return '<?xml{0} ?>'.format(
elements.XmlElement.attributes_string(
dict(version=version,
encoding=encoding,
standalone=standalone).items()))
def xml_document(content,
version=constants.XML_1_0,
encoding=constants.UTF_8,
standalone=None):
"""Return a full XML document.
Strip trailing whitespace from the content.
"""
return join.by_newlines(
xml_declaration(version=version,
encoding=encoding,
standalone=standalone),
content.rstrip())
# vim: fileencoding=utf-8 ts=4 sts=4 sw=4 autoindent expandtab syntax=python: