|
7 | 7 | SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)" |
8 | 8 | PROJECT_ROOT="$(dirname "$SCRIPT_DIR")" # Assuming the project root is one level up from scripts |
9 | 9 |
|
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 |
11 | 11 | PROJECT_DIR="${1:-$(pwd)}" |
| 12 | +THEME_NAME="${2:-default}" |
12 | 13 |
|
13 | 14 | # Define the template and output files within the project directory |
14 | 15 | TEMPLATE_FILE="$PROJECT_DIR/template_default.html" |
15 | 16 | OUTPUT_FILE="$PROJECT_DIR/index.html" # Output file set to index.html |
16 | 17 |
|
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" |
27 | 19 |
|
28 | 20 | # Check if the template file exists in the project directory |
29 | 21 | 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." |
31 | 23 | exit 1 |
32 | 24 | fi |
33 | 25 |
|
34 | 26 | # Function to ensure required components exist in the project directory |
35 | 27 | ensure_components() { |
36 | 28 | 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 |
38 | 30 | local component_file="$PROJECT_DIR/${component}.html" |
39 | 31 | if [[ ! -f "$component_file" ]]; then |
40 | 32 | missing+=("$component") |
@@ -63,33 +55,51 @@ missing_components=() |
63 | 55 | # Read the template file line by line |
64 | 56 | while IFS= read -r line || [[ -n "$line" ]]; do |
65 | 57 | # 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>" |
69 | 61 | fi |
70 | 62 |
|
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 |
72 | 64 | if [[ "$line" =~ \<body ]]; then |
73 | 65 | echo "Adding data-theme attribute with theme: $THEME_NAME" |
74 | 66 | line="<body class=\"flex flex-col min-h-screen\" data-theme=\"$THEME_NAME\">" |
75 | 67 | fi |
76 | 68 |
|
77 | | - # Process components |
| 69 | + # Use regex to find all placeholders in the format {{component_name}} |
78 | 70 | while [[ "$line" =~ \{\{([a-zA-Z0-9_]+)\}\} ]]; do |
79 | 71 | COMPONENT_ID="${BASH_REMATCH[1]}" |
80 | 72 | COMPONENT_FILE="$PROJECT_DIR/${COMPONENT_ID}.html" |
81 | 73 |
|
| 74 | + # Check if the component file exists in the project directory |
82 | 75 | if [[ -f "$COMPONENT_FILE" ]]; then |
| 76 | + echo "Processing component: $COMPONENT_ID" |
83 | 77 | COMPONENT_CONTENT=$(<"$COMPONENT_FILE") |
| 78 | + |
| 79 | + # Safely escape special characters like & |
84 | 80 | COMPONENT_CONTENT=$(echo "$COMPONENT_CONTENT" | sed 's/&/\\&/g') |
| 81 | + |
| 82 | + # Replace the placeholder with the component content |
85 | 83 | line="${line/\{\{$COMPONENT_ID\}\}/$COMPONENT_CONTENT}" |
| 84 | + echo "Replaced {{${COMPONENT_ID}}} successfully." |
86 | 85 | 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}}}}" |
88 | 90 | fi |
89 | 91 | done |
90 | 92 |
|
91 | 93 | # Write the processed line to the output file |
92 | 94 | echo "$line" >> "$OUTPUT_FILE" |
93 | 95 | done < "$TEMPLATE_FILE" |
94 | 96 |
|
| 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 | + |
95 | 105 | echo "Template processing complete. Check '$OUTPUT_FILE'." |
0 commit comments