Skip to content

Commit 3022567

Browse files
create project data theme injection
1 parent cc7604b commit 3022567

File tree

6 files changed

+62
-69
lines changed

6 files changed

+62
-69
lines changed

projects/example/index.html

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
<link href="https://cdn.jsdelivr.net/npm/daisyui@4.12.12/dist/full.min.css" rel="stylesheet" />
1010
<script src="https://cdn.tailwindcss.com"></script>
1111
</head>
12-
<body class="flex flex-col min-h-screen">
12+
<body class="flex flex-col min-h-screen" data-theme="default">
1313

1414
<div>
1515
<div id="sidebar_default" data-state="closed" class="bg-neutral w-64 h-full fixed top-0 left-0 flex flex-col justify-between transform -translate-x-full md:translate-x-0 transition-transform duration-300 ease-in-out z-10">

projects/example/run_generate.sh

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

21-
# Find the project root directory
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)
2225
project_root=$(find_project_root "$(dirname "${BASH_SOURCE[0]}")")
2326

2427
# Check if the project root was found
@@ -30,14 +33,20 @@ fi
3033
# Append the scripts directory to the root path
3134
scripts_dir="$project_root/scripts"
3235

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-
# Run the generate_site.sh script
43-
"$scripts_dir/generate_site.sh"
42+
# Check if a theme was provided as an argument, default to empty string if none
43+
if [[ -n "$1" ]]; then
44+
theme="$1"
45+
echo "Theme '$theme' will be used."
46+
else
47+
theme="default"
48+
echo "No theme provided, proceeding with default theme."
49+
fi
50+
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"

public/index.html

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
<link href="https://cdn.jsdelivr.net/npm/daisyui@4.12.12/dist/full.min.css" rel="stylesheet" />
1010
<script src="https://cdn.tailwindcss.com"></script>
1111
</head>
12-
<body class="flex flex-col min-h-screen">
12+
<body class="flex flex-col min-h-screen" data-theme="default">
1313

1414
<div>
1515
<div id="sidebar_default" data-state="closed" class="bg-neutral w-64 h-full fixed top-0 left-0 flex flex-col justify-between transform -translate-x-full md:translate-x-0 transition-transform duration-300 ease-in-out z-10">

run_create_project.sh

Lines changed: 10 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
#!/bin/bash
22

3-
# Find the project root
3+
# Function to find the project root
44
find_project_root() {
55
local dir="$(cd "${1:-$(pwd)}" && pwd)" # Convert to absolute path
66
local root_files=("README.md" "LICENSE" "CONTRIBUTE.md" "CNAME")
@@ -81,6 +81,10 @@ else
8181
exit 1
8282
fi
8383

84+
# Replace the data-theme attribute in the template with the selected theme
85+
sed -i "s/data-theme=\"\"/data-theme=\"$theme\"/" "$target_dir/template_default.html"
86+
echo "Applied theme '$theme' to template_default.html."
87+
8488
# Copy all components from the components directory to the target project directory
8589
if [[ -d "$components_dir" ]]; then
8690
cp "$components_dir"/* "$target_dir/"
@@ -117,15 +121,10 @@ else
117121
echo "Error: run_serve.sh not found in '$scripts_dir'."
118122
fi
119123

120-
# Insert the selected theme into the template_default.html file
121-
template_file="$target_dir/template_default.html"
122-
if [[ -f "$template_file" ]]; then
123-
# Replace the data-theme attribute with the selected theme
124-
sed -i "s/data-theme=\"\"/data-theme=\"$theme\"/g" "$template_file"
125-
echo "Applied theme '$theme' to template_default.html."
126-
else
127-
echo "Error: template_default.html not found in '$target_dir'."
128-
exit 1
129-
fi
124+
# Apply the theme (this is a placeholder, you can customize how to handle theme selection)
125+
echo "Setting theme to '$theme' in project..."
126+
127+
# Run the project generate script with the theme
128+
cd "$target_dir" && ./run_generate.sh "$theme"
130129

131130
echo "Project setup complete with theme '$theme' in directory '$base_dir/$project_name'."

scripts/generate_site.sh

Lines changed: 20 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -7,37 +7,35 @@ 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-
# 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"
2123
else
22-
echo "No theme provided, proceeding without data-theme attribute."
24+
THEME_NAME="default"
25+
echo "No theme provided, proceeding with default theme."
2326
fi
2427

25-
# Check if the template file exists
28+
# Check if the template file exists in the project directory
2629
if [[ ! -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
2932
fi
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
3735
ensure_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
5755
ensure_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"
10993
done < "$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-
11995
echo "Template processing complete. Check '$OUTPUT_FILE'."

scripts/run_generate.sh

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

21-
# Find the project root directory
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)
2225
project_root=$(find_project_root "$(dirname "${BASH_SOURCE[0]}")")
2326

2427
# Check if the project root was found
@@ -30,14 +33,20 @@ fi
3033
# Append the scripts directory to the root path
3134
scripts_dir="$project_root/scripts"
3235

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-
# Run the generate_site.sh script
43-
"$scripts_dir/generate_site.sh"
42+
# Check if a theme was provided as an argument, default to empty string if none
43+
if [[ -n "$1" ]]; then
44+
theme="$1"
45+
echo "Theme '$theme' will be used."
46+
else
47+
theme="default"
48+
echo "No theme provided, proceeding with default theme."
49+
fi
50+
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"

0 commit comments

Comments
 (0)