forked from google/WebFundamentals
-
Notifications
You must be signed in to change notification settings - Fork 0
/
devsiteParseYAML.py
168 lines (153 loc) · 6.09 KB
/
devsiteParseYAML.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
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 parse(requestPath, fileLocation, rawYaml, lang='en'):
body = ''
parsedYaml = yaml.load(rawYaml)
bookPath = parsedYaml['book_path']
bookYaml = devsiteHelper.parseBookYaml(bookPath, lang)
projectPath = parsedYaml['project_path']
page = parsedYaml['landing_page']
rows = page['rows']
title = 'Web'
banner = devsiteHelper.getAnnouncementBanner(projectPath, lang)
header = 'Generic Page Header Here'
customCss = ''
lowerTabs = devsiteHelper.getLowerTabs(bookYaml)
if 'custom_css_path' in page:
customCss = '<link rel="stylesheet" href="'
customCss += page['custom_css_path']
customCss += '">'
if 'header' in page:
header = '<div class="devsite-collapsible-section">'
header += '<div class="devsite-header-background devsite-full-site-width">'
header += '<div class="devsite-product-id-row devsite-full-site-width">'
if 'description' in page['header']:
header += '<div class="devsite-product-description-row">'
header += '<div class="devsite-product-description">'
header += page['header']['description']
header += '</div></div>'
if 'buttons' in page['header']:
header += '<div class="devsite-product-button-row">'
for button in page['header']['buttons']:
header += '<a class="button" href="'
header += button['path'] + '">' + button['label'] + '</a>'
header += '</div>'
header += '</div></div></div>'
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 += '<div class="devsite-landing-row-group">'
section += parseIndexYamlItems(row['items'])
section += '</div>'
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))
body += section
body = devsiteHelper.renderDevSiteContent(body, lang)
return render('gae/home.tpl', {
'title': title,
'announcementBanner': banner,
'requestPath': requestPath,
'lowerTabs': lowerTabs,
'customcss': customCss,
'header': header,
'content': body,
'lang': lang,
'footerPromo': devsiteHelper.getFooterPromo(),
'footerLinks': devsiteHelper.getFooterLinkBox()
}
)
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:
imgClass = 'devsite-landing-row-item-image'
if 'image_left' in yamlItem:
imgClass += ' devsite-landing-row-item-image-left'
item += '<img src="' + yamlItem['image_path'] + '" '
item += 'class="' + imgClass + '">'
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