Skip to content

Commit 3ba9028

Browse files
output print added for generate script
1 parent 3022567 commit 3ba9028

File tree

2 files changed

+37
-27
lines changed

2 files changed

+37
-27
lines changed

scripts/generate_site.sh

Lines changed: 29 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -7,34 +7,26 @@ set -e
77
SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
88
PROJECT_ROOT="$(dirname "$SCRIPT_DIR")" # Assuming the project root is one level up from scripts
99

10-
# Get the project directory passed as an argument, default to current directory if not provided
10+
# Get the project directory and theme from arguments
1111
PROJECT_DIR="${1:-$(pwd)}"
12+
THEME_NAME="${2:-default}"
1213

1314
# Define the template and output files within the project directory
1415
TEMPLATE_FILE="$PROJECT_DIR/template_default.html"
1516
OUTPUT_FILE="$PROJECT_DIR/index.html" # Output file set to index.html
1617

17-
# Default theme if not provided (optional)
18-
THEME_NAME="$2"
19-
20-
# Check if a theme name was passed as an argument
21-
if [[ ! -z "$THEME_NAME" ]]; then
22-
echo "Using theme: $THEME_NAME"
23-
else
24-
THEME_NAME="default"
25-
echo "No theme provided, proceeding with default theme."
26-
fi
18+
echo "Adding data-theme attribute with theme: $THEME_NAME"
2719

2820
# Check if the template file exists in the project directory
2921
if [[ ! -f "$TEMPLATE_FILE" ]]; then
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."
22+
echo "Error: Template file '$TEMPLATE_FILE' not found. Use './run_add.sh template_default.html' to add the default template file."
3123
exit 1
3224
fi
3325

3426
# Function to ensure required components exist in the project directory
3527
ensure_components() {
3628
local missing=()
37-
for component in sidebar_default content_default footer_default; do
29+
for component in sidebar_default content_default header_default themecontroller_default hero_default; do
3830
local component_file="$PROJECT_DIR/${component}.html"
3931
if [[ ! -f "$component_file" ]]; then
4032
missing+=("$component")
@@ -63,33 +55,51 @@ missing_components=()
6355
# Read the template file line by line
6456
while IFS= read -r line || [[ -n "$line" ]]; do
6557
# Replace the <title> tag with the user-provided title, if provided
66-
if [[ "$line" =~ \<title\>(.*)\<\/title\> ]] && [[ ! -z "$PAGE_TITLE" ]]; then
67-
echo "Replacing <title> tag with user-provided title."
68-
line="<title>${PAGE_TITLE}</title>"
58+
if [[ "$line" =~ \<title\>(.*)\<\/title\> ]]; then
59+
echo "Replacing <title> tag."
60+
line="<title>✍️ MiniTemplate - Simple Template Engine</title>"
6961
fi
7062

71-
# Replace the <body> tag with the data-theme attribute if a theme is specified
63+
# Replace the <body> tag with the data-theme attribute
7264
if [[ "$line" =~ \<body ]]; then
7365
echo "Adding data-theme attribute with theme: $THEME_NAME"
7466
line="<body class=\"flex flex-col min-h-screen\" data-theme=\"$THEME_NAME\">"
7567
fi
7668

77-
# Process components
69+
# Use regex to find all placeholders in the format {{component_name}}
7870
while [[ "$line" =~ \{\{([a-zA-Z0-9_]+)\}\} ]]; do
7971
COMPONENT_ID="${BASH_REMATCH[1]}"
8072
COMPONENT_FILE="$PROJECT_DIR/${COMPONENT_ID}.html"
8173

74+
# Check if the component file exists in the project directory
8275
if [[ -f "$COMPONENT_FILE" ]]; then
76+
echo "Processing component: $COMPONENT_ID"
8377
COMPONENT_CONTENT=$(<"$COMPONENT_FILE")
78+
79+
# Safely escape special characters like &
8480
COMPONENT_CONTENT=$(echo "$COMPONENT_CONTENT" | sed 's/&/\\&/g')
81+
82+
# Replace the placeholder with the component content
8583
line="${line/\{\{$COMPONENT_ID\}\}/$COMPONENT_CONTENT}"
84+
echo "Replaced {{${COMPONENT_ID}}} successfully."
8685
else
87-
line="${line/\{\{$COMPONENT_ID\}\}/}"
86+
echo "Warning: Component file '$COMPONENT_ID.html' not found in '$PROJECT_DIR'. Leaving placeholder unchanged."
87+
missing_components+=("$COMPONENT_ID")
88+
# Optionally, keep the placeholder or replace it with an empty string
89+
line="${line/\{\{$COMPONENT_ID\}\}/{{${COMPONENT_ID}}}}"
8890
fi
8991
done
9092

9193
# Write the processed line to the output file
9294
echo "$line" >> "$OUTPUT_FILE"
9395
done < "$TEMPLATE_FILE"
9496

97+
# Check for any missing components
98+
if [[ ${#missing_components[@]} -gt 0 ]]; then
99+
echo "The following components were not found and left unchanged:"
100+
for missing in "${missing_components[@]}"; do
101+
echo "- {{${missing}}}"
102+
done
103+
fi
104+
95105
echo "Template processing complete. Check '$OUTPUT_FILE'."

scripts/run_generate.sh

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -18,10 +18,7 @@ find_project_root() {
1818
return 1 # Return error if no root found
1919
}
2020

21-
# Get the current directory where the script is executed
22-
current_dir=$(pwd)
23-
24-
# Find the project root directory (if needed for other purposes)
21+
# Find the project root directory
2522
project_root=$(find_project_root "$(dirname "${BASH_SOURCE[0]}")")
2623

2724
# Check if the project root was found
@@ -33,20 +30,23 @@ fi
3330
# Append the scripts directory to the root path
3431
scripts_dir="$project_root/scripts"
3532

33+
# Print the final scripts directory for verification
34+
echo "Scripts directory path: $scripts_dir"
35+
3636
# Check if the generate_site.sh script exists in the scripts directory
3737
if [[ ! -f "$scripts_dir/generate_site.sh" ]]; then
3838
echo "Error: generate_site.sh not found in $scripts_dir"
3939
exit 1
4040
fi
4141

42-
# Check if a theme was provided as an argument, default to empty string if none
42+
# Check if a theme was provided as an argument, default to "default" if none
4343
if [[ -n "$1" ]]; then
4444
theme="$1"
45-
echo "Theme '$theme' will be used."
45+
echo "Using theme: $theme"
4646
else
4747
theme="default"
4848
echo "No theme provided, proceeding with default theme."
4949
fi
5050

51-
# Run the generate_site.sh script and pass the current directory and theme as parameters
52-
"$scripts_dir/generate_site.sh" "$current_dir" "$theme"
51+
# Run the generate_site.sh script and pass the theme as a parameter
52+
"$scripts_dir/generate_site.sh" "$project_root" "$theme"

0 commit comments

Comments
 (0)