forked from facebookresearch/ParlAI
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgenerate.py
147 lines (115 loc) · 4.75 KB
/
generate.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
#!/usr/bin/env python3
# Copyright (c) Facebook, Inc. and its affiliates.
# This source code is licensed under the MIT license found in the
# LICENSE file in the root directory of this source tree.
"""
Builds the ParlAI website.
"""
import os
import git
import markdown
import shutil
from mdx_gfm import PartialGithubFlavoredMarkdownExtension
GIT_ROOT_LEVEL = git.Git().rev_parse('--show-toplevel')
WEBSITE_ROOT = os.path.join(GIT_ROOT_LEVEL, 'website')
TEMPLATES = os.path.join(WEBSITE_ROOT, 'templates')
OUT_DIR = os.path.join(WEBSITE_ROOT, 'build')
STATIC_FILE_EXTS = {'.css', '.jpg', '.jpeg', '.png', '.json', '.jsonl', '.html', '.md'}
def ghmarkdown(source):
return markdown.markdown(
source, extensions=[PartialGithubFlavoredMarkdownExtension()]
)
def _read_file(filename):
with open(filename) as f:
return f.read()
def _mkdirp(directory):
"""
Equivalent to mkdir -p.
"""
if not os.path.exists(directory):
os.makedirs(directory)
def _write_file(partial_filename, content):
filename = os.path.join(OUT_DIR, partial_filename)
_mkdirp(os.path.dirname(filename))
print("writing {} bytes to {}".format(len(content), filename))
with open(filename, 'w') as f:
f.write(content)
def wrap_base(content, title):
template = _read_file(os.path.join(TEMPLATES, 'base.html'))
template = template.replace('{{{CONTENT}}}', content)
template = template.replace('{{{TITLE}}}', title)
return template
def make_errorpage():
content = _read_file(os.path.join(TEMPLATES, 'error.html'))
html = wrap_base(content, "Error")
_write_file('error.html', html)
def make_aboutpage():
template = _read_file(os.path.join(TEMPLATES, 'about.html'))
readme = _read_file(os.path.join(GIT_ROOT_LEVEL, 'README.md'))
# filter out the circleci badge from the about page
readme = "\n".join(
[l for l in readme.split("\n") if not l.startswith("[![CircleCI]")]
)
readme_html = ghmarkdown(readme)
readme_html = readme_html.replace("docs/source/\\", "/docs/")
content = template.replace('{{{CONTENT}}}', readme_html)
html = wrap_base(content, "About | ParlAI")
_write_file('about/index.html', html)
def make_homepage():
template = _read_file(os.path.join(TEMPLATES, 'home.html'))
html = wrap_base(template, "ParlAI")
_write_file('index.html', html)
def make_projects_landing():
template = _read_file(os.path.join(TEMPLATES, 'project.html'))
landing = _read_file(os.path.join(GIT_ROOT_LEVEL, 'projects/README.md'))
landing = landing.replace(
'This directory also contains subfolders for some of the projects which are '
'housed in the ParlAI repo, others are maintained via external websites. '
'Please also refer',
'See the [ParlAI projects](https://github.com/facebookresearch/ParlAI/'
'tree/main/projects) page on GitHub for more information. Refer',
)
landing_html = template.replace('{{{CONTENT}}}', ghmarkdown(landing))
html = wrap_base(landing_html, "Projects | ParlAI")
_write_file('projects/index.html', html)
def make_projects_individual():
template = _read_file(os.path.join(TEMPLATES, 'project.html'))
projects_dir = os.path.join(GIT_ROOT_LEVEL, 'projects')
possible_projects = os.listdir(projects_dir)
projects = [
pp
for pp in possible_projects
if os.path.exists(os.path.join(projects_dir, pp, 'README.md'))
]
for p in projects:
project_dir = os.path.join(projects_dir, p)
content = _read_file(os.path.join(project_dir, 'README.md'))
content_html = template.replace('{{{CONTENT}}}', ghmarkdown(content))
content_html = content_html.replace(
'src="',
'src="https://raw.githubusercontent.com/facebookresearch/'
'ParlAI/main/projects/{}'.format(p + '/' if p else ''),
)
title = p.title().replace("_", " ")
html = wrap_base(content_html, title)
_write_file(os.path.join('projects', p, 'index.html'), html)
# if there are any static files in the project folder, copy them over
# to the website
files = os.listdir(project_dir)
for fn in files:
_, ext = os.path.splitext(fn.lower())
if ext in STATIC_FILE_EXTS:
src = os.path.join(project_dir, fn)
nice_src = os.path.relpath(src, GIT_ROOT_LEVEL)
dest = os.path.join(OUT_DIR, 'projects', p, fn)
nice_dest = os.path.relpath(dest, GIT_ROOT_LEVEL)
print(f"Copy {nice_src} -> {nice_dest}")
shutil.copyfile(src, dest)
def main():
make_errorpage()
make_homepage()
make_aboutpage()
make_projects_landing()
make_projects_individual()
if __name__ == '__main__':
main()