-
Notifications
You must be signed in to change notification settings - Fork 0
/
generateTeamPortfolio.py
96 lines (72 loc) · 2.9 KB
/
generateTeamPortfolio.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
import urllib3
import yaml
from pprint import pprint
from urllib import parse
from jinja2 import Environment, FileSystemLoader
# Set the directories
team_file = './team.yaml'
template_folder = './templates'
output_file_html = './team.html'
output_file_md = './team.md'
team_template = 'team.jinja2'
header_template = 'header.jinja2'
image_prefix = "https://raw.githubusercontent.com/RoboticsBrno/Our-team/main/docs/"
# Custom function to remove http:// or https:// from URLs
def nice_url(url):
url = url.replace('http://', '').replace('https://', '')
if url.startswith("www."):
url = url[4:]
if url.startswith("github.com/"):
url = url[11:]
if url.startswith("github.io/"):
url = url[10:]
if url.endswith("/"):
url = url[:-1]
if url.startswith("linkedin.com/"):
url = parse.unquote(url[13:])
return url
def image_absolute_path(image):
return parse.urljoin(image_prefix, image)
def remove_blank_lines(text):
lines = text.split('\n')
non_empty_lines = [line for line in lines if line.strip() != ""]
return "\n".join(non_empty_lines)
def load_yaml(filename: str) -> dict:
try :
with open(filename, 'r') as file:
# Load the YAML data
return yaml.safe_load(file)
except FileNotFoundError:
print(f"File {filename} not found")
exit(1)
def load_team_data(team_filename: str) -> dict:
return load_yaml(team_filename)
def save_team_portfolio(filename: str, team_portfolio: str) -> None:
with open(filename, 'w') as file:
file.write(team_portfolio)
def generate_team_portfolio(team_data: dict, template_folder: str, team_template: str, header_template: str) -> str:
# Set the template environment
env = Environment(loader=FileSystemLoader(template_folder))
# Add the custom function to the template environment
env.filters['remove_http_s'] = nice_url
env.filters['image_absolute_path'] = image_absolute_path
portfolio_string = env.get_template(header_template).render()
# Loop through the team members
for index, team_member_data in enumerate(team_data):
team_member_data["index"] = index
# Load the Jinja template
template = env.get_template(team_template)
# Render the template with the YAML data
rendered_text = template.render(team_member_data)
# Remove blank lines from the rendered text
cleaned_text = remove_blank_lines(rendered_text)
# Write the cleaned text to the output file
portfolio_string += cleaned_text
return portfolio_string
if __name__ == "__main__":
team_data = load_team_data(team_file)
# pprint(team_data)
portfolio = generate_team_portfolio(team_data, template_folder, team_template, header_template)
save_team_portfolio(output_file_html, portfolio)
save_team_portfolio(output_file_md, portfolio)
print("Team portfolio generated successfully.")