1
- Set-Location - Path $PSScriptRoot # C:/Users/ashraaf/Downloads/VScode/hadithmv.github.io/notes/info
1
+ # HTML Minification Script
2
+ try {
3
+ # Store the initial location to return to it at the end if needed
4
+ $initialLocation = Get-Location
5
+ Set-Location - Path $PSScriptRoot - ErrorAction Stop
6
+ $startTime = Get-Date
7
+ $processedFiles = 0
8
+ $successfulFiles = 0
9
+ $failedFiles = 0
10
+ }
11
+ catch {
12
+ Write-Error " Failed to initialize script: $_ "
13
+ exit 1
14
+ }
2
15
3
- html- minifier -- collapse- boolean- attributes -- collapse- whitespace -- decode- entities -- minify- css true -- minify- js true -- process- scripts [text / html ] -- remove-attribute - quotes -- remove-comments -- remove-empty - attributes -- remove-optional - tags -- remove-redundant - attributes -- remove-script - type- attributes -- remove-style - link- type- attributes -- remove-tag - whitespace -- sort- attributes -- sort- class- name -- trim- custom- fragments -- use-short - doctype index- uc.html - o index.html
16
+ function Run-MinifierCommand {
17
+ param (
18
+ [string ]$InputFile ,
19
+ [string ]$OutputFile ,
20
+ [string []]$Options
21
+ )
22
+
23
+ try {
24
+ Write-Host " DEBUG: Running minifier on: $InputFile " - ForegroundColor Gray
25
+
26
+ # Create process info
27
+ $pinfo = New-Object System.Diagnostics.ProcessStartInfo
28
+ $pinfo.FileName = " html-minifier"
29
+ $pinfo.RedirectStandardError = $true
30
+ $pinfo.RedirectStandardOutput = $true
31
+ $pinfo.UseShellExecute = $false
32
+
33
+ # Build arguments
34
+ $args = $Options + @ ($InputFile , " -o" , $OutputFile )
35
+ $pinfo.Arguments = $args
36
+
37
+ # Create and start the process
38
+ $p = New-Object System.Diagnostics.Process
39
+ $p.StartInfo = $pinfo
40
+ $p.Start () | Out-Null
41
+
42
+ # Wait for the process to exit and capture output
43
+ $output = $p.StandardOutput.ReadToEnd ()
44
+ $errorOutput = $p.StandardError.ReadToEnd ()
45
+ $p.WaitForExit ()
46
+
47
+ if ($p.ExitCode -ne 0 ) {
48
+ Write-Host " Error executing html-minifier command" - ForegroundColor Red
49
+ Write-Host " Error output: $errorOutput " - ForegroundColor Red
50
+ return $false
51
+ }
52
+
53
+ # Check file sizes for reporting
54
+ $originalSize = (Get-Item $InputFile ).Length
55
+ $minifiedSize = (Get-Item $OutputFile ).Length
56
+ $savingsBytes = $originalSize - $minifiedSize
57
+ $savingsPercent = if ($originalSize -gt 0 ) { [math ]::Round(($savingsBytes / $originalSize ) * 100 , 2 ) } else { 0 }
58
+
59
+ # Store info for reporting
60
+ $script :fileStats += @ {
61
+ InputFile = $InputFile
62
+ OutputFile = $OutputFile
63
+ OriginalSize = $originalSize
64
+ MinifiedSize = $minifiedSize
65
+ SavingsBytes = $savingsBytes
66
+ SavingsPercent = $savingsPercent
67
+ }
68
+
69
+ return $true
70
+ }
71
+ catch {
72
+ Write-Host " Exception occurred: $_ " - ForegroundColor Red
73
+ return $false
74
+ }
75
+ }
76
+
77
+ function Process-Files {
78
+ param (
79
+ [array ]$FilesToProcess
80
+ )
81
+
82
+ try {
83
+ Write-Host " `n 🔄 Starting HTML Minification Process..." - ForegroundColor Cyan
84
+ Write-Host " 🔍 Found $ ( $FilesToProcess.Count ) files to process" - ForegroundColor Cyan
85
+ Write-Host " ═══════════════════════════════════════════════════" - ForegroundColor DarkGray
86
+
87
+ $script :fileStats = @ ()
88
+ $totalFiles = $FilesToProcess.Count
89
+ $script :processedFiles = 0
90
+ $script :successfulFiles = 0
91
+ $script :failedFiles = 0
92
+
93
+ # Process each file
94
+ foreach ($file in $FilesToProcess ) {
95
+ $script :processedFiles ++
96
+ $percentComplete = [math ]::Round(($script :processedFiles / $totalFiles ) * 100 )
97
+
98
+ $inputFile = $file.InputFile
99
+ $outputFile = $file.OutputFile
100
+ $options = $file.Options
101
+
102
+ $fileName = Split-Path $inputFile - Leaf
103
+
104
+ Write-Host " [$script :processedFiles /$totalFiles ] $percentComplete % " - NoNewline
105
+ Write-Host " $fileName " - NoNewline
106
+
107
+ try {
108
+ # Check if file exists
109
+ if (-not (Test-Path $inputFile )) {
110
+ Write-Host " ❌ (File not found)" - ForegroundColor Red
111
+ $script :failedFiles ++
112
+ continue
113
+ }
114
+
115
+ # Run minifier command
116
+ $success = Run- MinifierCommand - InputFile $inputFile - OutputFile $outputFile - Options $options
117
+
118
+ if ($success ) {
119
+ Write-Host " ✅ " - ForegroundColor Green - NoNewline
120
+
121
+ # Get the last added stats
122
+ $stats = $script :fileStats [-1 ]
123
+ Write-Host " (Saved $ ( $stats.SavingsPercent ) %, $ ( $stats.SavingsBytes ) bytes)" - ForegroundColor Cyan
124
+ $script :successfulFiles ++
125
+ }
126
+ else {
127
+ Write-Host " ❌ (Minification failed)" - ForegroundColor Red
128
+ $script :failedFiles ++
129
+ }
130
+ }
131
+ catch {
132
+ Write-Host " ❌" - ForegroundColor Red
133
+ Write-Error " Error processing $fileName : $_ "
134
+ $script :failedFiles ++
135
+ }
136
+ }
137
+
138
+ # Display summary for minification
139
+ Write-Host " `n ═══════════════════════════════════════════════════" - ForegroundColor DarkGray
140
+ Write-Host " 📊 HTML MINIFICATION SUMMARY" - ForegroundColor Cyan
141
+ Write-Host " ───────────────────────────────────────────────────" - ForegroundColor DarkGray
142
+
143
+ # Overall statistics
144
+ $totalOriginalSize = ($script :fileStats | Measure-Object - Property OriginalSize - Sum).Sum
145
+ $totalMinifiedSize = ($script :fileStats | Measure-Object - Property MinifiedSize - Sum).Sum
146
+ $totalSavings = $totalOriginalSize - $totalMinifiedSize
147
+ $overallSavingsPercent = if ($totalOriginalSize -gt 0 ) { [math ]::Round(($totalSavings / $totalOriginalSize ) * 100 , 2 ) } else { 0 }
148
+
149
+ Write-Host " 📏 Original Size: " - ForegroundColor Cyan - NoNewline
150
+ Write-Host " $ ( [math ]::Round($totalOriginalSize / 1 KB , 2 )) KB" - ForegroundColor White
151
+ Write-Host " 📏 Minified Size: " - ForegroundColor Cyan - NoNewline
152
+ Write-Host " $ ( [math ]::Round($totalMinifiedSize / 1 KB , 2 )) KB" - ForegroundColor White
153
+ Write-Host " 💾 Space Saved: " - ForegroundColor Green - NoNewline
154
+ Write-Host " $ ( [math ]::Round($totalSavings / 1 KB , 2 )) KB ($overallSavingsPercent %)" - ForegroundColor White
155
+ Write-Host " ───────────────────────────────────────────────────" - ForegroundColor DarkGray
156
+ Write-Host " ✅ Successful: " - ForegroundColor Green - NoNewline
157
+ Write-Host " $script :successfulFiles files" - ForegroundColor White
158
+ Write-Host " ❌ Failed: " - ForegroundColor Red - NoNewline
159
+ Write-Host " $script :failedFiles files" - ForegroundColor White
160
+ Write-Host " ───────────────────────────────────────────────────" - ForegroundColor DarkGray
161
+
162
+ if ($script :failedFiles -eq 0 -and $script :successfulFiles -gt 0 ) {
163
+ Write-Host " ✅ HTML MINIFICATION COMPLETED SUCCESSFULLY ✅" - ForegroundColor Green
164
+ return $true
165
+ }
166
+ elseif ($script :failedFiles -gt 0 -and $script :successfulFiles -gt 0 ) {
167
+ Write-Host " ⚠️ HTML MINIFICATION COMPLETED WITH ERRORS ⚠️" - ForegroundColor Yellow
168
+ return $true
169
+ }
170
+ else {
171
+ Write-Host " ❌ HTML MINIFICATION FAILED ❌" - ForegroundColor Red
172
+ return $false
173
+ }
174
+ }
175
+ catch {
176
+ Write-Error " HTML minification failed: $_ "
177
+ return $false
178
+ }
179
+ }
180
+
181
+ # Main execution block
182
+ try {
183
+ # Define the files to process with their options
184
+ $filesToProcess = @ (
185
+ @ {
186
+ InputFile = " index-uc.html"
187
+ OutputFile = " index.html"
188
+ Options = @ (
189
+ " --collapse-boolean-attributes" ,
190
+ " --collapse-whitespace" ,
191
+ " --decode-entities" ,
192
+ " --minify-css" , " true" ,
193
+ " --minify-js" , " true" ,
194
+ " --process-scripts" , " [text/html]" ,
195
+ " --remove-attribute-quotes" ,
196
+ " --remove-comments" ,
197
+ " --remove-empty-attributes" ,
198
+ " --remove-optional-tags" ,
199
+ " --remove-redundant-attributes" ,
200
+ " --remove-script-type-attributes" ,
201
+ " --remove-style-link-type-attributes" ,
202
+ " --remove-tag-whitespace" ,
203
+ " --sort-attributes" ,
204
+ " --sort-class-name" ,
205
+ " --trim-custom-fragments" ,
206
+ " --use-short-doctype"
207
+ )
208
+ }
209
+ # Add more files here as needed, for example:
210
+ # @{
211
+ # InputFile = "about-uc.html"
212
+ # OutputFile = "about.html"
213
+ # Options = @( ... same options ... )
214
+ # }
215
+ )
216
+
217
+ # Process the files
218
+ $success = Process - Files - FilesToProcess $filesToProcess
219
+
220
+ # Calculate execution time
221
+ $endTime = Get-Date
222
+ $executionTime = ($endTime - $startTime ).TotalSeconds
223
+
224
+ # Display final summary
225
+ Write-Host " `n ═══════════════════════════════════════════════════" - ForegroundColor DarkGray
226
+ Write-Host " 📊 FINAL SUMMARY" - ForegroundColor Cyan
227
+ Write-Host " ───────────────────────────────────────────────────" - ForegroundColor DarkGray
228
+
229
+ Write-Host " 🕒 Total time: " - ForegroundColor Cyan - NoNewline
230
+ Write-Host " $ ( [math ]::Round($executionTime , 2 )) seconds" - ForegroundColor White
231
+
232
+ if ($success ) {
233
+ $totalSavings = ($script :fileStats | Measure-Object - Property SavingsBytes - Sum).Sum
234
+ $totalSavingsKB = [math ]::Round($totalSavings / 1 KB , 2 )
235
+
236
+ Write-Host " 💾 Total space saved: " - ForegroundColor Green - NoNewline
237
+ Write-Host " $totalSavingsKB KB" - ForegroundColor White
238
+ Write-Host " ───────────────────────────────────────────────────" - ForegroundColor DarkGray
239
+ Write-Host " ✅ HTML MINIFICATION COMPLETED SUCCESSFULLY ✅" - ForegroundColor Green
240
+ }
241
+ else {
242
+ Write-Host " ───────────────────────────────────────────────────" - ForegroundColor DarkGray
243
+ Write-Host " ⚠️ HTML MINIFICATION COMPLETED WITH ERRORS ⚠️" - ForegroundColor Yellow
244
+ }
245
+
246
+ # Return to initial location
247
+ Set-Location - Path $initialLocation
248
+ }
249
+ catch {
250
+ Write-Host " `n ❌ Script execution failed: $_ " - ForegroundColor Red
251
+
252
+ # Try to return to initial location
253
+ try {
254
+ Set-Location - Path $initialLocation
255
+ }
256
+ catch {
257
+ # Ignore errors when returning to initial location
258
+ }
259
+
260
+ exit 1
261
+ }
0 commit comments