-
Notifications
You must be signed in to change notification settings - Fork 1
/
compile_schedule.py
108 lines (89 loc) · 4.11 KB
/
compile_schedule.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
import pandas as pd
import subprocess
import os
from datetime import datetime
import re
import csv
# Function to convert unicode code points to actual emoji characters
def unicode_to_emoji(unicode_str):
codes = unicode_str.split()
return ''.join(chr(int(code.lstrip('U+'), 16)) for code in codes)
# Function to load emoji mapping from CSV
def load_emoji_mapping(csv_file):
emoji_mapping = {}
with open(csv_file, 'r', encoding='utf-8') as file:
reader = csv.DictReader(file)
for row in reader:
emoji = unicode_to_emoji(row['Emoji'])
emoji_mapping[emoji] = row['Name']
return emoji_mapping
# Load emoji mapping
emoji_mapping = load_emoji_mapping('emoji_mapping.csv')
# Determine the term and year
def get_term_and_year(start_date):
date = datetime.strptime(start_date, "%Y-%m-%d")
year = date.year
if (date.month == 8 and date.day >= 24) or date.month in [9, 10, 11] or (date.month == 12 and date.day <= 31):
term = "Fall"
elif date.month in [1, 2] or (date.month == 3 and date.day <= 15):
term = "Winter"
elif (date.month == 3 and date.day >= 24) or date.month in [4, 5] or (date.month == 6 and date.day <= 7):
term = "Spring"
else:
term = "Summer"
return term, year
def replace_emojis(text):
def replace(match):
emoji = match.group(0)
latex_name = emoji_mapping.get(emoji, '')
return f"\\emoji{{{latex_name}}}" if latex_name else emoji
# Use regex to find emojis
emoji_pattern = re.compile('|'.join(re.escape(key) for key in emoji_mapping.keys()))
return emoji_pattern.sub(replace, text)
def remove_first_and_last_lines(lines, n, m):
non_whitespace_lines = [line for line in lines if line.strip()]
return non_whitespace_lines[n:-m]
# Load events
events_df = pd.read_csv('events.tsv', delimiter='\t')
start_date = events_df.iloc[0]['Start Date']
term, year = get_term_and_year(start_date)
# Generate events calendar in Markdown format
events_markdown = f"# PBS Social Committee Calendar of Events\n{term}, {year}\n\nHere’s our schedule of events this term.\n\n"
for _, row in events_df.iterrows():
content_file = row["Content File"]
if pd.isna(content_file) or not os.path.exists(f'templates/{content_file}'):
print(f"Warning: Content file for '{row['Event Name']}' is missing or incorrect.")
continue
with open(f'templates/{content_file}', 'r') as file:
event_content = file.readlines()
# Extract event title from the second non-whitespace line
non_whitespace_lines = [line.strip() for line in event_content if line.strip()]
event_title = non_whitespace_lines[1] if len(non_whitespace_lines) > 1 else row["Event Name"]
# Remove the first and last two non-whitespace lines
event_content = event_content = remove_first_and_last_lines(event_content, 2, 2)
# Replace placeholders with actual values
event_content = ''.join(event_content).replace('{DATE}', row['Date']).replace('{TIME}', row['Time']).replace('{LOCATION}', row['Location'] + '\n\n')
# Adjust formatting for Date, Time, Location
event_content = event_content.replace('**Date:**', '\n**Date:**')
event_content = event_content.replace('**Time:**', '\n**Time:**')
event_content = event_content.replace('**Location:**', '\n**Location:**')
# Replace emojis
event_title = replace_emojis(event_title)
event_content = replace_emojis(event_content)
events_markdown += f"## {event_title}\n\n"
events_markdown += event_content + "\n\n"
events_markdown += "---\n\n"
# Save the events markdown to a file
with open('events_schedule.md', 'w') as file:
file.write(events_markdown.strip())
# Set TEXINPUTS environment variable to include the latex directory
env = os.environ.copy()
env['TEXINPUTS'] = './/latex//:'
# Convert the Markdown file to PDF using Pandoc with LuaLaTeX and Merriweather font
subprocess.run([
'pandoc', 'events_schedule.md', '-o', 'events_schedule.pdf',
'--pdf-engine=lualatex',
'--include-in-header=latex_header.tex',
'--variable', 'geometry:margin=1in',
'--variable', 'fontsize=12pt'
], env=env)