-
Notifications
You must be signed in to change notification settings - Fork 10
/
Copy pathfix_meta.py
177 lines (133 loc) · 5.1 KB
/
fix_meta.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
168
169
170
171
172
173
174
175
176
177
## fix up meta info for all files
import os
from pathlib import Path
from requests import post
import yaml
debug = False
def list_all_files(root_dir):
"""List all files in a directory and its subdirectories."""
all_files = []
for root, dirs, files in os.walk(root_dir):
dirs[:] = [d for d in dirs if d not in ['.git', '.venv', "_site", '.quarto']]
for file in files:
all_files.append(os.path.join(root, file))
return all_files
files = os.environ.get('QUARTO_PROJECT_INPUT_FILES', False)
print(files)
if files:
files = files.split('\n')
else:
base = Path("/home/norm/compiler_course_2024fa/")
files = list_all_files(base)
#files = ["test.qmd"]
def compare_dicts(file, dict1, dict2, path=""):
# Check keys in dict1 not in dict2
for key in dict1:
current_path = f"{path}.{key}" if path else key
if key not in dict2:
print(f"{file}:Key {current_path} found in the first dictionary but not in the second.")
elif isinstance(dict1[key], dict) and isinstance(dict2[key], dict):
# If both values are dictionaries, compare recursively
compare_dicts(file, dict1[key], dict2[key], current_path)
elif dict1[key] != dict2[key]:
print(f"{file}: Value for key {current_path} differs: {dict1[key]} vs {dict2[key]}")
# Check keys in dict2 not in dict1
for key in dict2:
current_path = f"{path}.{key}" if path else key
if key not in dict1:
print(f"{file}: Key {current_path} found in the second dictionary but not in the first.")
def fix_mermaid(lines):
i = 0
while (i < len(lines)):
if lines[i] == '```{mermaid}':
if i + 1 == len(lines) or lines[i + 1].strip() != '%%{init: {"flowchart": {"htmlLabels": false}} }%%':
lines.insert(i + 1, '%%{init: {"flowchart": {"htmlLabels": false}} }%%\n')
i += 1
i +=1
return lines
def split_file(contents):
# Find indices of lines that are exactly '---'
separator_indices = [i for i, line in enumerate(contents) if line.strip() == '---']
# If no separators are found, return the whole contents as pre_contents
if not separator_indices:
return contents, "", []
if len(separator_indices) == 1:
return contents, "", []
# Assuming the first '---' starts the YAML block and the second '---' ends it
start, end = separator_indices[0], separator_indices[1]
pre_contents = contents[:start+1]
yaml_contents = "\n".join(contents[start + 1:end])
post_contents = contents[end:]
return pre_contents, yaml_contents, post_contents
yaml_without_slides = """
title: title
format:
html: default
"""
yaml_for_blogs = """
title: title
format:
html: default
execute:
enabled: false
"""
yaml_with_slides = """
title: title
format:
html: default
revealjs:
code-fold: true
echo: true
chalkboard: true
output-file: xxxx
scrollable: true
code-line-numbers: true
slideNumber: "c/t"
mathjax: true
sidebar: false
execute:
echo: true
"""
for file in files:
#print(file)
directory, _, file_name = file.rpartition('/')
yaml_template = yaml.safe_load(yaml_without_slides)
use_revealjs = False
if debug:
print(f"scan {file_name} in {directory}")
# Check if 'blogs' is in the directory path
if 'blogs' in directory:
continue
if directory.endswith('lectures'):
yaml_template = yaml.safe_load(yaml_with_slides)
use_revealjs = True
with open(file, 'r') as f:
try:
contents = f.read().split('\n')
except UnicodeDecodeError:
# print("skip", file)
continue
new_contents = []
pre, yaml_contents, post = split_file(contents)
# Parse the YAML content if any was found
if yaml_contents:
yaml_from_file = yaml.safe_load(yaml_contents)
# preserve some entries
keeps = ['title', 'tbl-colwidths', 'echo', 'author']
for keep in keeps:
if keep in yaml_from_file:
yaml_template[keep] = yaml_from_file[keep]
if use_revealjs:
if 'output-location' in yaml_from_file['format']['revealjs']:
yaml_template['format']['revealjs']['output-location'] = yaml_from_file['format']['revealjs']['output-location']
yaml_template['format']['revealjs']['output-file'] = 'revealjs_' + file_name
yaml_from_file['format']['revealjs']['output-file'] = 'revealjs_' + file_name
yaml_from_file['format']['revealjs']['code-line-numbers'] = True
pre = fix_mermaid(pre)
post = fix_mermaid(post)
compare_dicts(file, yaml_from_file, yaml_template)
#print(file, yaml_template['title'])
new_contents = '\n'.join(pre) + '\n' + yaml.dump(yaml_template) + '\n' + '\n'.join(post)
#break
with open(file, 'w') as f:
f.write(new_contents)