-
Notifications
You must be signed in to change notification settings - Fork 51
/
path_format.py
175 lines (137 loc) · 5.43 KB
/
path_format.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
169
170
171
172
173
174
175
"""Path formatter for formatting grow paths."""
import datetime
import re
import string
from grow.common import structures
from grow.common import utils
HTML_EXTENSIONS = ('.html', '.htm')
VALID_DOC_EXTENSIONS = ('.html', '.htm', '.xml', '.svg')
INDEX_BASE_ENDINGS = ('/{base}', '/{base}/')
class PathFormat:
"""Format url paths using the information from the pod."""
PARAM_CURLY_REGEX_SECTION = re.compile(r'/{([^}]*)}/')
PARAM_CURLY_REGEX_END = re.compile(r'/{([^}]*)}$')
def __init__(self, pod):
self.pod = pod
self.formatter = string.Formatter()
@staticmethod
def parameterize(path):
"""Replace stubs with routes params."""
path = re.sub(PathFormat.PARAM_CURLY_REGEX_SECTION, r'/:\1/', path)
return re.sub(PathFormat.PARAM_CURLY_REGEX_END, r'/:\1', path)
@staticmethod
def strip_double_slash(path):
"""Remove double slashes from the path."""
while '//' in path:
path = path.replace('//', '/')
return path
@staticmethod
def trailing_slash(doc, path):
"""Adds trailing slash when appropriate."""
if not doc.view.endswith(HTML_EXTENSIONS):
return path
if path.endswith(VALID_DOC_EXTENSIONS):
return path
if path and not path.endswith('/'):
return '{}/'.format(path)
return path
@staticmethod
def _locale_or_alias(locale):
if not locale:
return ''
if not isinstance(locale, str) and locale.alias is not None:
return locale.alias
return str(locale)
def format_doc(self, doc, path, locale=None, parameterize=False):
"""Format a URL path using the doc information."""
path = '' if path is None else path
# Most params should always be replaced.
params = self.params_pod()
params.update(self.params_doc(path, doc))
params = self.params_lower(path, params)
path = utils.safe_format(path, **params)
if parameterize:
path = self.parameterize(path)
params = {}
if locale is None:
locale = doc.locale
params['locale'] = self._locale_or_alias(locale)
path = utils.safe_format(path, **params)
path = self.strip_double_slash(path)
return self.trailing_slash(doc, path)
def format_pod(self, path, parameterize=False):
"""Format a URL path using the pod information."""
path = '' if path is None else path
params = self.params_pod()
path = utils.safe_format(path, **params)
if parameterize:
path = self.parameterize(path)
return self.strip_double_slash(path)
def format_static(self, path, locale=None, parameterize=False,
fingerprint=None):
"""Format a static document url."""
params = self.params_pod()
path = utils.safe_format(path, **params)
if parameterize:
path = self.parameterize(path)
params = {}
if fingerprint:
params['fingerprint'] = fingerprint
params['locale'] = self._locale_or_alias(locale)
if params:
path = utils.safe_format(path, **params)
return self.strip_double_slash(path)
def format_view(self, doc, path, parameterize=False):
"""Format a URL path using the doc information for views."""
path = '' if path is None else path
# Most params should always be replaced.
params = self.params_pod()
params.update(self.params_doc(path, doc))
params = self.params_lower(path, params)
path = utils.safe_format(path, **params)
if parameterize:
path = self.parameterize(path)
return self.strip_double_slash(path)
def params_doc(self, path, doc):
"""Selective access to the document properties depending on path."""
params = {}
# Remove the base when the path ends with the base and is index.
if doc.base == 'index' and path.endswith(INDEX_BASE_ENDINGS):
params['base'] = ''
else:
params['base'] = doc.base
params['collection'] = structures.AttributeDict(
base_path=doc.collection_base_path,
sub_path=doc.collection_sub_path,
path=doc.collection.collection_path,
dirname=doc.collection.dirname,
basename=doc.collection.basename,
root=doc.collection.root)
if '{category}' in path:
params['category'] = doc.category
if '{parent}' in path:
params['parent'] = doc.parent if doc.parent else utils.DummyDict()
if '{slug}' in path:
params['slug'] = doc.slug
if '{date}' in path:
if isinstance(doc.date, datetime.datetime):
params['date'] = doc.date.date()
else:
params['date'] = doc.date
return params
def params_lower(self, path, params):
"""Update to support lowercase when in the path."""
if '|lower' in path:
for key, value in params.items():
if isinstance(value, str):
params['{}|lower'.format(key)] = value.lower()
return params
def params_pod(self):
params = {}
podspec = self.pod.podspec.get_config()
if 'root' in podspec:
params['root'] = podspec['root']
else:
params['root'] = ''
params['env'] = self.pod.env
return params