forked from google/WebFundamentals
-
Notifications
You must be signed in to change notification settings - Fork 0
/
devsiteIndex.py
193 lines (175 loc) · 6.8 KB
/
devsiteIndex.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
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
import os
import yaml
import logging
import devsitePage
import devsiteHelper
from google.appengine.ext.webapp.template import render
SOURCE_PATH = os.path.join(os.path.dirname(__file__), 'src/content')
def getPage(requestPath, lang):
response = None
fileLocations = [
os.path.join(SOURCE_PATH, lang, requestPath, '_index.yaml'),
os.path.join(SOURCE_PATH, 'en', requestPath, '_index.yaml'),
os.path.join(SOURCE_PATH, lang, requestPath, 'index.md'),
os.path.join(SOURCE_PATH, 'en', requestPath, 'index.md')
]
for fileLocation in fileLocations:
if os.path.isfile(fileLocation):
fileContent = open(fileLocation, 'r').read()
fileContent = fileContent.decode('utf8')
if fileLocation.endswith('_index.yaml'):
response = generateYaml(lang, requestPath, fileContent)
break
if fileLocation.endswith('index.md'):
# index.md are treated like other devsite pages, so just use the
# devsitePage rendering template.
requestPath = os.path.join(requestPath, 'index')
response = devsitePage.getPage(requestPath, lang)
break
if response is None and os.environ['SERVER_SOFTWARE'].startswith('Dev'):
if (requestPath.startswith('showcase/') or
requestPath.startswith('shows/') or requestPath.startswith('updates/')):
content = ''
content += '<h1>Generated Listing Page</h1>\n'
content += '<aside class="warning" markdown="1"><strong>Oops</strong> '
content += '<span>Looks like you forgot to build the index files. Try '
content += 'running <code>gulp build</code> from the command line.'
content += '</span></aside>'
fileList = os.listdir(os.path.join(SOURCE_PATH, 'en', requestPath))
for f in fileList:
if not f.startswith('_'):
p = '/web/' + requestPath + f.replace('.md', '')
line = '<li>'
line += '<a href="' + p + '">' + p + '</a>'
line += '</li>'
content += line
response = render('gae/article.tpl', {
'title': 'Whoops! Missing index page!',
'requestPath': requestPath,
'leftNav': '',
'content': content,
'toc': '',
'dateUpdated': 'now',
'lang': 'en'}
)
return response
def parseIndexYamlItems(yamlItems):
result = ''
for yamlItem in yamlItems:
item = '<div class="[[ITEM_CLASSES]]">'
itemClasses = ['devsite-landing-row-item']
descriptionClasses = ['devsite-landing-row-item-description']
link = None
if 'path' in yamlItem:
link = '<a href="' + yamlItem['path'] + '">'
if 'icon' in yamlItem:
if link:
item += link
if 'icon_name' in yamlItem['icon']:
item += '<div class="devsite-landing-row-item-icon material-icons">'
item += yamlItem['icon']['icon_name']
item += '</div>'
descriptionClasses.append('devsite-landing-row-item-icon-description')
if 'path' in yamlItem['icon']:
item += '<img class="devsite-landing-row-item-icon" src="'
item += yamlItem['icon']['path']
item += '">'
descriptionClasses.append('devsite-landing-row-item-icon-description')
if link:
item += '</a>'
if 'image_path' in yamlItem:
item += '<img src="' + yamlItem['image_path'] + '" '
item += 'class="devsite-landing-row-item-image">'
elif not 'youtube_id' in yamlItem:
itemClasses.append('devsite-landing-row-item-no-image')
if 'description' in yamlItem:
item += '<div class="[[DESCRIPTION_CLASSES]]">'
if 'heading' in yamlItem:
if link:
item += link
item += '<h3 id="' + devsiteHelper.slugify(yamlItem['heading']) +'">'
item += yamlItem['heading'] + '</h3>'
# item += '<h3>' + yamlItem['heading'] + '</h3>'
if link:
item += '</a>'
item += yamlItem['description']
if 'buttons' in yamlItem:
item += '<div class="devsite-landing-row-item-buttons">'
for button in yamlItem['buttons']:
item += '<a href="' + button['path'] + '"'
if 'classname' in button:
item += ' class="' + button['classname'] + '"'
else:
item += ' class="button button-white"'
item += '>' + button['label'] + '</a>'
item += '</div>'
item += '</div>'
if 'custom_html' in yamlItem:
item += devsiteHelper.renderDevSiteContent(yamlItem['custom_html'])
if 'youtube_id' in yamlItem:
item += '<div class="devsite-landing-row-item-youtube">'
item += '<iframe class="devsite-embedded-youtube-video" '
item += 'frameborder="0" allowfullscreen '
item += 'src="//www.youtube.com/embed/' + yamlItem['youtube_id']
item += '?autohide=1&showinfo=0&enablejsapi=1">'
item += '</iframe>'
item += '</div>'
item += '</div>'
item = item.replace('[[ITEM_CLASSES]]', ' '.join(itemClasses))
item = item.replace('[[DESCRIPTION_CLASSES]]', ' '.join(descriptionClasses))
result += item
return result
def generateYaml(lang, requestPath, rawYaml):
content = ''
parsedYaml = yaml.load(rawYaml)
page = parsedYaml['landing_page']
rows = page['rows']
title = 'Web'
banner = devsiteHelper.getAnnouncementBanner(lang)
header = 'Generic Page Header Here'
customCss = ''
if 'custom_css_path' in page:
customCss = '<link rel="stylesheet" href="'
customCss += page['custom_css_path']
customCss += '">'
if 'header' in page:
if 'description' in page['header']:
header = page['header']['description']
if 'name' in page['header']:
title = page['header']['name']
for row in rows:
sectionClass = ['devsite-landing-row']
section = '<section class="[[SECTION_CLASSES]]">'
if 'classname' in row:
sectionClass.append(row['classname'])
numItems = None
if 'columns' in row:
numItems = len(row['columns'])
elif 'items' in row:
numItems = len(row['items'])
if numItems:
sectionClass.append('devsite-landing-row-' + str(numItems) + '-up')
if 'heading' in row:
section += '<h2 id="' + devsiteHelper.slugify(row['heading']) +'">'
section += row['heading'] + '</h2>'
if 'items' in row:
section += parseIndexYamlItems(row['items'])
if 'columns' in row:
for column in row['columns']:
section += '<div class="devsite-landing-row-column">'
if 'items' in column:
section += parseIndexYamlItems(column['items'])
section += '</div>'
section += '</section>'
section = section.replace('[[SECTION_CLASSES]]', ' '.join(sectionClass))
content += section
text = render('gae/home.tpl', {
'title': title,
'announcementBanner': banner,
'requestPath': requestPath,
'customcss': customCss,
'header': header,
'content': content,
'lang': lang}
)
return text