77SCRIPT_DIR=" $( cd " $( dirname " ${BASH_SOURCE[0]} " ) " && pwd) "
88PROJECT_ROOT=" $( dirname " $SCRIPT_DIR " ) " # Assuming the project root is one level up from scripts
99
10- # Define the template and output files
11- TEMPLATE_FILE=" template_default.html"
12- OUTPUT_FILE=" index.html" # Output file set to index.html
10+ # Get the project directory passed as an argument, default to current directory if not provided
11+ PROJECT_DIR=" ${1:- $(pwd)} "
12+
13+ # Define the template and output files within the project directory
14+ TEMPLATE_FILE=" $PROJECT_DIR /template_default.html"
15+ OUTPUT_FILE=" $PROJECT_DIR /index.html" # Output file set to index.html
1316
1417# Default theme if not provided (optional)
15- THEME_NAME=" "
18+ THEME_NAME=" $2 "
1619
1720# Check if a theme name was passed as an argument
18- if [[ ! -z " $1 " ]]; then
19- THEME_NAME=" $1 "
21+ if [[ ! -z " $THEME_NAME " ]]; then
2022 echo " Using theme: $THEME_NAME "
2123else
22- echo " No theme provided, proceeding without data-theme attribute."
24+ THEME_NAME=" default"
25+ echo " No theme provided, proceeding with default theme."
2326fi
2427
25- # Check if the template file exists
28+ # Check if the template file exists in the project directory
2629if [[ ! -f " $TEMPLATE_FILE " ]]; then
27- echo " Error: Template file '$TEMPLATE_FILE ' not found. Use './run_add.sh template_default.html' to add the default template file."
30+ echo " Error: Template file '$TEMPLATE_FILE ' not found in project directory . Use './run_add.sh template_default.html' to add the default template file."
2831 exit 1
2932fi
3033
31- # Prompt for the page title if a theme is provided
32- if [[ ! -z " $THEME_NAME " ]]; then
33- read -p " Enter the title for your page (leave empty to use default): " PAGE_TITLE
34- fi
35-
36- # Function to ensure required components exist
34+ # Function to ensure required components exist in the project directory
3735ensure_components () {
3836 local missing=()
3937 for component in sidebar_default content_default footer_default; do
40- local component_file=" . /${component} .html"
38+ local component_file=" $PROJECT_DIR /${component} .html"
4139 if [[ ! -f " $component_file " ]]; then
4240 missing+=(" $component " )
4341 fi
@@ -53,7 +51,7 @@ ensure_components() {
5351 fi
5452}
5553
56- # Ensure all required components are present
54+ # Ensure all required components are present in the project directory
5755ensure_components
5856
5957# Create or overwrite the output file
@@ -72,48 +70,26 @@ while IFS= read -r line || [[ -n "$line" ]]; do
7270
7371 # Replace the <body> tag with the data-theme attribute if a theme is specified
7472 if [[ " $line " =~ \< body ]]; then
75- if [[ ! -z " $THEME_NAME " ]]; then
76- echo " Adding data-theme attribute with theme: $THEME_NAME "
77- line=" <body class=\" flex flex-col min-h-screen\" data-theme=\" $THEME_NAME \" >"
78- else
79- line=" <body class=\" flex flex-col min-h-screen\" >"
80- fi
73+ echo " Adding data-theme attribute with theme: $THEME_NAME "
74+ line=" <body class=\" flex flex-col min-h-screen\" data-theme=\" $THEME_NAME \" >"
8175 fi
8276
83- # Use regex to find all placeholders in the format {{component_name}}
77+ # Process components
8478 while [[ " $line " =~ \{\{ ([a-zA-Z0-9_]+)\}\} ]]; do
8579 COMPONENT_ID=" ${BASH_REMATCH[1]} "
86- COMPONENT_FILE=" ${COMPONENT_ID} .html"
80+ COMPONENT_FILE=" $PROJECT_DIR / $ {COMPONENT_ID} .html"
8781
88- # Check if the component file exists
8982 if [[ -f " $COMPONENT_FILE " ]]; then
90- echo " Processing component: $COMPONENT_ID "
9183 COMPONENT_CONTENT=$( < " $COMPONENT_FILE " )
92-
93- # Safely escape special characters like &
9484 COMPONENT_CONTENT=$( echo " $COMPONENT_CONTENT " | sed ' s/&/\\&/g' )
95-
96- # Replace the placeholder with the component content
9785 line=" ${line/ \{\{ $COMPONENT_ID \}\} / $COMPONENT_CONTENT } "
98- echo " Replaced {{${COMPONENT_ID} }} successfully."
9986 else
100- echo " Warning: Component file '$COMPONENT_ID .html' not found in '$components_dir /default/'. Leaving placeholder unchanged."
101- missing_components+=(" $COMPONENT_ID " )
102- # Optionally, keep the placeholder or replace it with an empty string
103- line=" ${line/ \{\{ $COMPONENT_ID \}\} / {{${COMPONENT_ID} } }}"
87+ line=" ${line/ \{\{ $COMPONENT_ID \}\} / } "
10488 fi
10589 done
10690
10791 # Write the processed line to the output file
10892 echo " $line " >> " $OUTPUT_FILE "
10993done < " $TEMPLATE_FILE "
11094
111- # Check for any missing components
112- if [[ ${# missing_components[@]} -gt 0 ]]; then
113- echo " The following components were not found and left unchanged:"
114- for missing in " ${missing_components[@]} " ; do
115- echo " - {{${missing} }}"
116- done
117- fi
118-
11995echo " Template processing complete. Check '$OUTPUT_FILE '."
0 commit comments