@@ -101,28 +101,13 @@ echo "Step 6/7: Generate URL shortcuts for all cheat sheets"
101101echo " Current directory: $( pwd) "
102102echo " WORK directory: $WORK "
103103
104- # Function to generate shortcut name from filename
105- generate_shortcut () {
106- local filename=$1
107- local shortcut=" "
108-
109- # Remove file extension and common suffixes
110- local basename=${filename%% .html}
111- basename=${basename%% _Cheat_Sheet}
112-
113- # For cheat sheets, use first letters of each word
114- shortcut=$( echo " $basename " | awk -F' _' ' {for(i=1;i<=NF;i++)printf "%s", substr($i,1,1)}' )
115-
116- # echo "$shortcut"
117- }
118-
119104# Function to create redirect file
120105create_redirect () {
121106 local shortcut=$1
122107 local target=$2
123108 local redirect_file=" $WORK /site/${shortcut} "
124109
125- # echo "Creating redirect: /${shortcut} -> ${target}"
110+ echo " Creating redirect: /${shortcut} -> ${target} "
126111
127112 # Create the redirect HTML file
128113 cat > " $redirect_file " << EOF
@@ -140,16 +125,20 @@ EOF
140125 # Also create .html version
141126 cp " $redirect_file " " ${redirect_file} .html"
142127
143- # Verify creation
128+ # Verify creation and handle errors properly
144129 if [ -f " $redirect_file " ] && [ -f " ${redirect_file} .html" ]; then
145- # echo "✅ Created shortcuts:"
130+ echo " ✅ Created shortcuts:"
146131 echo " - /${shortcut} "
147132 echo " - /${shortcut} .html"
148133 else
149- # echo "❌ Failed to create shortcuts for ${shortcut}"
134+ echo " ❌ Failed to create shortcuts for ${shortcut} "
135+ return 1
150136 fi
151137}
152138
139+ # Track used shortcuts to prevent duplicates
140+ declare -A used_shortcuts
141+
153142# Process all cheat sheet files
154143echo " Processing all cheat sheet files..."
155144find " $WORK /site/cheatsheets" -type f -name " *_Cheat_Sheet.html" | while read -r file; do
@@ -158,27 +147,54 @@ find "$WORK/site/cheatsheets" -type f -name "*_Cheat_Sheet.html" | while read -r
158147
159148 # echo "Processing: $filename"
160149
161- # Generate shortcut name
162- shortcut=$( generate_shortcut " $filename " )
150+ # First try to find a match in redirects.yml
151+ shortcut=" "
152+ if [ -f " redirects.yml" ]; then
153+ # Try to find a matching redirect in the YAML file
154+ while IFS=' : ' read -r key target || [ -n " $key " ]; do
155+ # Skip comments and empty lines
156+ [[ $key =~ ^# .*$ ]] && continue
157+ [ -z " $key " ] && continue
158+
159+ # Trim whitespace
160+ key= $( echo " $key " | xargs)
161+ target= $( echo " $target " | xargs)
162+
163+ if [ " $target " = " $filepath " ]; then
164+ shortcut=$key
165+ break
166+ fi
167+ done < " redirects.yml"
168+ fi
163169
164- # Skip if no shortcut generated
165- [ -z " $shortcut " ] && continue
170+ # If no shortcut found in redirects.yml, generate one
171+ if [ -z " $shortcut " ]; then
172+ # Generate shortcut from filename
173+ shortcut=$( echo " $filename " | awk -F' _' ' {for(i=1;i<=NF;i++)printf "%s", substr($i,1,1)}' | tr ' [:lower:]' ' [:upper:]' )
174+ fi
166175
167- # Convert to uppercase
168- # shortcut=$(echo "$shortcut" | tr '[:lower:]' '[:upper:]')
176+ # Handle duplicate shortcuts
177+ if [ " ${used_shortcuts[$shortcut]} " ]; then
178+ echo " ⚠️ Warning: Duplicate shortcut '$shortcut ' for '$filename '. Original was for '${used_shortcuts[$shortcut]} '"
179+ # Append a number to make it unique
180+ count=2
181+ while [ " ${used_shortcuts[${shortcut}${count}]} " ]; do
182+ (( count++ ))
183+ done
184+ shortcut=" ${shortcut}${count} "
185+ fi
186+
187+ # Record this shortcut as used
188+ used_shortcuts[$shortcut ]= $filepath
169189
170190 # Create redirect
171191 create_redirect " $shortcut " " $filepath "
172192done
173193
174194# Print all available shortcuts
175- # echo "Available shortcuts:"
176- for file in " $WORK " /site/[A-Z]* ; do
177- if [ -f " $file " ] && [[ ! " $file " =~ \. (html| xml| gz)$ ]]; then
178- shortcut=$( basename " $file " )
179- target=$( grep -o ' url=/[^"]*' " $file " | cut -d' =' -f2)
180- # echo "- /${shortcut} -> ${target}"
181- fi
195+ echo " Available shortcuts:"
196+ for shortcut in " ${! used_shortcuts[@]} " ; do
197+ echo " - /${shortcut} -> ${used_shortcuts[$shortcut]} "
182198done
183199
184200echo " Step 7/7 Cleanup."
@@ -187,3 +203,54 @@ rm -rf custom_theme
187203rm mkdocs.yml
188204
189205echo " Generation finished to the folder: $WORK /$GENERATED_SITE "
206+
207+ # Add redirect handling
208+ echo " Generating redirect pages..."
209+ mkdir -p $WORK /$GENERATED_SITE /redirects
210+
211+ # Process redirects.yml and generate redirect HTML files
212+ # SITE_DIR="$WORK/$GENERATED_SITE"
213+ python3 - << EOF
214+ import yaml
215+ import os
216+
217+ REDIRECT_TEMPLATE = " " "
218+ <!DOCTYPE html>
219+ <html>
220+ <head>
221+ <meta http-equiv=" refresh" content=" 0; url= {target_url}" >
222+ <script>window.location.href = " {target_url}" ;</script>
223+ </head>
224+ <body>
225+ Redirecting to <a href=" {target_url}" >{target_url}</a>...
226+ </body>
227+ </html>
228+ " " "
229+
230+ def create_redirect_page(shortcut, target_url, output_dir):
231+ # Handle relative URLs
232+ if not target_url.startswith(' http' ):
233+ target_url = f' /{target_url}'
234+
235+ content = REDIRECT_TEMPLATE.format(target_url=target_url)
236+
237+ # Create redirect file
238+ with open(f' {output_dir}/{shortcut}.html' , ' w' ) as f:
239+ f.write(content)
240+
241+ # Load redirects
242+ with open(' ../scripts/redirects.yml' , ' r' ) as f:
243+ try:
244+ redirects = yaml.safe_load(f)
245+ except yaml.YAMLError as e:
246+ print(f" Error parsing redirects.yml: {e}" )
247+ exit(1)
248+
249+ # Create redirect pages
250+ output_dir = ' $WORK/$GENERATED_SITE'
251+ for shortcut, target in redirects.items ():
252+ create_redirect_page(shortcut, target, output_dir)
253+ print(f" Created redirect: {shortcut} -> {target}" )
254+
255+ EOF
256+
0 commit comments