5
5
from jinja2 import Environment , FileSystemLoader
6
6
import re
7
7
8
-
9
8
# TODO: make this work for arbitrary context. ie. implement replace_using_context()
10
9
def replace_placeholder (source_str , variable_name , variable_value ):
11
10
# Escaping any regex special characters in variable_name
@@ -14,49 +13,39 @@ def replace_placeholder(source_str, variable_name, variable_value):
14
13
# Using regular expression to replace ${variable_name} with actual variable_value
15
14
# \s* means any amount of whitespace (including none)
16
15
# pattern = rf'\$\{{\s*\{{\s*{variable_name_escaped}\s*\}}\s*\}}'
17
- pattern = rf" <<\s*{ variable_name_escaped } \s*>>"
16
+ pattern = rf' <<\s*{ variable_name_escaped } \s*>>'
18
17
return re .sub (pattern , variable_value .strip (), source_str )
19
18
20
-
21
19
# Setup command-line argument parsing
22
- parser = argparse .ArgumentParser (
23
- description = "Render a Jinja2 template using a JSON context."
24
- )
25
- parser .add_argument (
26
- "template_file" ,
27
- type = str ,
28
- help = "Path to the Jinja2 template file (with .j2 extension)." ,
29
- )
30
- parser .add_argument (
31
- "json_file" , type = str , help = "Path to the JSON file to use as the rendering context."
32
- )
33
- parser .add_argument ("output_file" , type = str , help = "Path to the output file." )
20
+ parser = argparse .ArgumentParser (description = 'Render a Jinja2 template using a JSON context.' )
21
+ parser .add_argument ('template_file' , type = str , help = 'Path to the Jinja2 template file (with .j2 extension).' )
22
+ parser .add_argument ('json_file' , type = str , help = 'Path to the JSON file to use as the rendering context.' )
23
+ parser .add_argument ('output_file' , type = str , help = 'Path to the output file.' )
34
24
35
25
args = parser .parse_args ()
36
26
37
27
# Load JSON file as the rendering context
38
- with open (args .json_file , "r" ) as file :
28
+ with open (args .json_file , 'r' ) as file :
39
29
context = json .load (file )
40
30
41
31
# Setup Jinja2 environment and load the template
42
32
env = Environment (
43
- loader = FileSystemLoader (searchpath = "./" ),
44
- variable_start_string = "<<" ,
45
- variable_end_string = ">>" ,
46
- block_start_string = "<%" ,
47
- block_end_string = "%>" ,
48
- comment_start_string = "<#" ,
49
- comment_end_string = "#>" ,
50
- )
51
- env .filters ["replace_placeholder" ] = replace_placeholder
33
+ loader = FileSystemLoader (searchpath = './' ),
34
+ variable_start_string = '<<' ,
35
+ variable_end_string = '>>' ,
36
+ block_start_string = '<%' ,
37
+ block_end_string = '%>' ,
38
+ comment_start_string = '<#' ,
39
+ comment_end_string = '#>' )
40
+ env .filters ['replace_placeholder' ] = replace_placeholder
52
41
53
42
template = env .get_template (args .template_file )
54
43
55
44
# Render the template with the context
56
45
rendered_content = template .render (context )
57
46
# print(rendered_content)
58
47
59
- with open (args .output_file , "w" ) as file :
48
+ with open (args .output_file , 'w' ) as file :
60
49
file .write (rendered_content )
61
50
62
- print (f" Template rendered successfully. Output saved to { args .output_file } " )
51
+ print (f' Template rendered successfully. Output saved to { args .output_file } ' )
0 commit comments