This repository has been archived by the owner on Oct 25, 2023. It is now read-only.
forked from google/WebFundamentals
-
Notifications
You must be signed in to change notification settings - Fork 0
/
devsiteIndex.py
46 lines (42 loc) · 1.84 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
import os
import logging
import devsiteHelper
import devsiteParseMD
import devsiteParseHTML
import devsiteParseYAML
SOURCE_PATH = os.path.join(os.path.dirname(__file__), 'src/content')
def getPage(requestPath, lang):
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'),
os.path.join(SOURCE_PATH, 'lang', requestPath, 'index.html'),
os.path.join(SOURCE_PATH, 'en', requestPath, 'index.html')
]
for fileLocation in fileLocations:
if os.path.isfile(fileLocation):
content = open(fileLocation, 'r').read()
content = content.decode('utf8')
if fileLocation.endswith('_index.yaml'):
return devsiteParseYAML.parse(requestPath, fileLocation, content, lang)
if fileLocation.endswith('index.md'):
return devsiteParseMD.parse(requestPath, fileLocation, content, lang)
if fileLocation.endswith('index.html'):
return devsiteParseHTML.parse(requestPath, fileLocation, content, lang)
return None
def getDirIndex(requestPath):
result = None
if os.environ['SERVER_SOFTWARE'].startswith('Dev'):
result = ''
result += '<h1>Generated Listing Page</h1>\n'
result += '<aside class="warning" markdown="1"><strong>Oops</strong> '
result += '<span>Looks like you forgot to build the index files. Try '
result += 'running <code>gulp build</code> from the command line.'
result += '</span></aside>'
fileList = os.listdir(os.path.join(SOURCE_PATH, 'en', requestPath))
for f in fileList:
if not f.startswith('_'):
link = os.path.join('/web', requestPath, os.path.splitext(f)[0])
result += '<li><a href="' + link + '">' + link + '</a></li>'
return result