1
1
import os
2
2
import re
3
3
import markdown
4
+ import argparse
4
5
from loguru import logger
5
6
7
+ parser = argparse .ArgumentParser (description = "Tweak qmd files for colab." )
8
+
9
+ parser .add_argument (
10
+ "--overwrite" ,
11
+ action = "store_true" , # This makes it a boolean flag that defaults to False
12
+ help = "Enable overwrite mode (default: False)"
13
+ )
14
+ parser .add_argument (
15
+ "--input_file_path" ,
16
+ type = str ,
17
+ default = "./content/getting-started/01_environment.qmd" ,
18
+ help = "Which file should we tweak ?"
19
+ )
20
+
21
+ args = parser .parse_args ()
22
+
6
23
7
24
def create_python_snippet (title , content , callout_type ):
8
25
"""
@@ -30,12 +47,34 @@ def create_python_snippet(title, content, callout_type):
30
47
</style>
31
48
"""
32
49
33
- content = content .replace ("{python}" , "python" )
50
+ # Define a dictionary mapping callout types to their respective icons
51
+ icon_mapping = {
52
+ "tip" : '<i class="fa-solid fa-lightbulb"></i>' ,
53
+ "warning" : '<i class="fa-solid fa-triangle-exclamation"></i>' ,
54
+ "important" : '<i class="fa-solid fa-circle-exclamation"></i>' ,
55
+ "caution" : '<i class="fa-solid fa-circle-exclamation"></i>' ,
56
+ "exercise" : '<i class="fa-solid fa-pen-fancy"></i>' ,
57
+ }
58
+
59
+ # Use the dictionary to get the icon, with a default value if the type is not found
60
+ icon = icon_mapping .get (callout_type , '<i class="fa-solid fa-comment"></i>' )
61
+
62
+ content = (
63
+ "\n "
64
+ .join (line for line in content .splitlines () if not line .strip ().startswith ("#|" ))
65
+ )
66
+
67
+ content = (
68
+ content
69
+ .replace ("{python}" , "python" )
70
+ )
71
+
72
+ content = re .sub (r"```(python)\n(.*?)\n```" , r"~~~\1\n\2\n~~~" , content , flags = re .DOTALL )
34
73
35
74
content_html = f"""
36
75
<div class="callout callout-{ callout_type } ">
37
76
<div class="callout-header-{ callout_type } ">
38
- { title }
77
+ { icon } { title }
39
78
</div>
40
79
<div class="callout-body">
41
80
{ markdown .markdown (content )}
@@ -45,16 +84,18 @@ def create_python_snippet(title, content, callout_type):
45
84
46
85
content_html = content_html .replace ("'" , "\\ '" )
47
86
87
+
48
88
full_html = (
49
89
"\n "
50
90
"```{python}\n "
51
91
"from IPython.display import HTML\n "
52
92
f"style = '''\n { style } \n '''\n "
53
93
f"content_html = '''\n { content_html } \n '''\n "
54
- 'HTML(f"{style}\\ n{content_html}")\n '
94
+ 'HTML(f"<script src="https://kit.fontawesome.com/3c27c932d3.js" crossorigin="anonymous"></script> \\ n {style}\\ n{content_html}")\n '
55
95
"\n ```"
56
96
"\n "
57
97
)
98
+
58
99
return full_html
59
100
60
101
@@ -96,7 +137,10 @@ def replacement(match):
96
137
return regex .sub (replacement , content )
97
138
98
139
99
- def process_file (input_file_path , regex_pattern , output_file_path = None ):
140
+ def process_file (
141
+ input_file_path , regex_pattern , output_file_path = None ,
142
+ overwrite : bool = False
143
+ ):
100
144
"""
101
145
Reads a file, performs snippet substitutions, and writes the updated content to a new file.
102
146
@@ -109,8 +153,15 @@ def process_file(input_file_path, regex_pattern, output_file_path=None):
109
153
None
110
154
"""
111
155
112
- if output_file_path is None :
113
- output_file_path = input_file_path .replace (".qmd" , "_modified.qmd" )
156
+
157
+ if overwrite is True :
158
+ logger .debug ("Since overwrite argument is True, forcing output_file_path value" )
159
+ output_file_path = input_file_path
160
+ else :
161
+ logger .debug ("Overwrite argument is False" )
162
+ if output_file_path is None :
163
+ output_file_path = input_file_path .replace (".qmd" , "_modified.qmd" )
164
+
114
165
115
166
# Check if the input file exists
116
167
if not os .path .exists (input_file_path ):
@@ -142,6 +193,12 @@ def process_file(input_file_path, regex_pattern, output_file_path=None):
142
193
# Example usage
143
194
if __name__ == "__main__" :
144
195
process_file (
145
- input_file_path = "./content/getting-started/01_environment.qmd" ,
196
+ input_file_path = args .input_file_path ,
197
+ regex_pattern = r"::::\s*\{(?:\.note|\.caution|\.warning|\.important|\.tip|\.exercise)\}([\s\S]*?)::::" ,
198
+ overwrite = args .overwrite
199
+ )
200
+ process_file (
201
+ input_file_path = args .input_file_path ,
146
202
regex_pattern = r":::\s*\{(?:\.note|\.caution|\.warning|\.important|\.tip|\.exercise)\}([\s\S]*?):::" ,
203
+ overwrite = args .overwrite
147
204
)
0 commit comments