-
Notifications
You must be signed in to change notification settings - Fork 11
Expand file tree
/
Copy pathfix-presentation-lines.js
More file actions
executable file
Β·343 lines (295 loc) Β· 10.1 KB
/
fix-presentation-lines.js
File metadata and controls
executable file
Β·343 lines (295 loc) Β· 10.1 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
#!/usr/bin/env node
/**
* Fix Line Breaks in Existing Presentations
*
* Applies deterministic line breaking to existing presentation JSON files
* to ensure all code blocks fit within the 60-character limit.
*
* Usage:
* node scripts/fix-presentation-lines.js --all # Fix all presentations
* node scripts/fix-presentation-lines.js --file lesson-4.json # Fix specific file
* node scripts/fix-presentation-lines.js --dry-run --all # Show what would change
*/
import { readFileSync, writeFileSync, readdirSync, existsSync, statSync } from 'fs';
import { join, dirname, basename } from 'path';
import { fileURLToPath } from 'url';
import { processPresentation } from './lib/line-breaker.js';
// ES module __dirname equivalent
const __filename = fileURLToPath(import.meta.url);
const __dirname = dirname(__filename);
// Line breaking has been disabled - this script is no longer functional
console.log('β οΈ Line breaking has been disabled in the presentation generation pipeline.');
console.log('βΉοΈ This script is no longer functional and will not modify presentations.');
process.exit(0);
const PRESENTATIONS_DIR = join(__dirname, '../website/static/presentations');
/**
* Parse command-line arguments
*/
function parseArgs() {
const args = process.argv.slice(2);
const config = {
all: false,
file: null,
dryRun: false,
};
for (let i = 0; i < args.length; i++) {
if (args[i] === '--all') {
config.all = true;
} else if (args[i] === '--file' && i + 1 < args.length) {
config.file = args[i + 1];
i++;
} else if (args[i] === '--dry-run') {
config.dryRun = true;
}
}
return config;
}
/**
* Find all presentation JSON files (recursively)
*/
function findPresentationFiles(dir) {
if (!existsSync(dir)) {
console.error(`β Presentations directory not found: ${dir}`);
return [];
}
const files = [];
function traverse(currentDir) {
const items = readdirSync(currentDir);
for (const item of items) {
const fullPath = join(currentDir, item);
const stat = statSync(fullPath);
if (stat.isDirectory()) {
traverse(fullPath);
} else if (item.endsWith('.json') && item !== 'manifest.json') {
files.push(fullPath);
}
}
}
traverse(dir);
return files.sort();
}
/**
* Analyze a presentation for line length issues
*/
function analyzePresentation(presentation) {
const issues = [];
let maxLineLength = 0;
let totalLongLines = 0;
for (const slide of presentation.slides || []) {
// Check code slides
if (slide.type === 'code' && slide.code) {
const lines = slide.code.split('\n');
for (let i = 0; i < lines.length; i++) {
if (lines[i].length > 60) {
totalLongLines++;
maxLineLength = Math.max(maxLineLength, lines[i].length);
issues.push({
slideTitle: slide.title || 'Untitled',
location: 'code',
line: i + 1,
length: lines[i].length,
preview: lines[i].substring(0, 50) + '...',
});
}
}
}
// Check codeComparison slides
if (slide.type === 'codeComparison') {
if (slide.leftCode?.code) {
const lines = slide.leftCode.code.split('\n');
for (let i = 0; i < lines.length; i++) {
if (lines[i].length > 60) {
totalLongLines++;
maxLineLength = Math.max(maxLineLength, lines[i].length);
issues.push({
slideTitle: slide.title || 'Untitled',
location: 'leftCode',
line: i + 1,
length: lines[i].length,
preview: lines[i].substring(0, 50) + '...',
});
}
}
}
if (slide.rightCode?.code) {
const lines = slide.rightCode.code.split('\n');
for (let i = 0; i < lines.length; i++) {
if (lines[i].length > 60) {
totalLongLines++;
maxLineLength = Math.max(maxLineLength, lines[i].length);
issues.push({
slideTitle: slide.title || 'Untitled',
location: 'rightCode',
line: i + 1,
length: lines[i].length,
preview: lines[i].substring(0, 50) + '...',
});
}
}
}
}
// Check codeExecution slides
if (slide.type === 'codeExecution' && Array.isArray(slide.steps)) {
for (let i = 0; i < slide.steps.length; i++) {
if (slide.steps[i].line && typeof slide.steps[i].line === 'string') {
// Split by newlines and check each line individually
const lines = slide.steps[i].line.split('\n');
for (let j = 0; j < lines.length; j++) {
if (lines[j].length > 60) {
totalLongLines++;
maxLineLength = Math.max(maxLineLength, lines[j].length);
issues.push({
slideTitle: slide.title || 'Untitled',
location: `step ${i + 1}, line ${j + 1}`,
line: j + 1,
length: lines[j].length,
preview: lines[j].substring(0, 50) + '...',
});
}
}
}
}
}
}
return {
totalLongLines,
maxLineLength,
issues: issues.slice(0, 5), // Show first 5 issues
hasIssues: totalLongLines > 0,
};
}
/**
* Process a single presentation file
*/
function processFile(filePath, dryRun = false) {
const fileName = basename(filePath);
console.log(`\nπ ${fileName}`);
try {
// Read and parse JSON
const content = readFileSync(filePath, 'utf-8');
const presentation = JSON.parse(content);
// Analyze before processing
const beforeAnalysis = analyzePresentation(presentation);
if (!beforeAnalysis.hasIssues) {
console.log(' β
No lines exceed 60 characters');
return {
success: true,
changed: false,
fileName,
};
}
console.log(` β οΈ Found ${beforeAnalysis.totalLongLines} long lines (max: ${beforeAnalysis.maxLineLength} chars)`);
if (beforeAnalysis.issues.length > 0) {
console.log(' π Sample issues:');
for (const issue of beforeAnalysis.issues) {
console.log(` - "${issue.slideTitle}" (${issue.location}): ${issue.length} chars`);
}
}
// Apply line breaking
const { presentation: processedPresentation, stats } = processPresentation(presentation);
// Analyze after processing
const afterAnalysis = analyzePresentation(processedPresentation);
if (dryRun) {
console.log(` π DRY RUN: Would fix ${stats.linesShortened} lines (max reduction: ${stats.maxReduction} chars)`);
if (afterAnalysis.hasIssues) {
console.log(` β οΈ ${afterAnalysis.totalLongLines} lines would still exceed limit`);
} else {
console.log(' β
All lines would be within limit after processing');
}
} else {
// Write back
writeFileSync(filePath, JSON.stringify(processedPresentation, null, 2), 'utf-8');
console.log(` βοΈ Fixed ${stats.linesShortened} lines (max reduction: ${stats.maxReduction} chars)`);
if (afterAnalysis.hasIssues) {
console.log(` β οΈ Warning: ${afterAnalysis.totalLongLines} lines still exceed limit`);
} else {
console.log(' β
All lines now within 60-character limit');
}
}
return {
success: true,
changed: stats.linesShortened > 0,
fileName,
stats,
};
} catch (error) {
console.error(` β Error: ${error.message}`);
return {
success: false,
changed: false,
fileName,
error: error.message,
};
}
}
/**
* Main execution
*/
function main() {
const config = parseArgs();
if (!config.all && !config.file) {
console.log('Usage:');
console.log(' node scripts/fix-presentation-lines.js --all');
console.log(' node scripts/fix-presentation-lines.js --file lesson-4.json');
console.log(' node scripts/fix-presentation-lines.js --dry-run --all');
process.exit(1);
}
console.log('π§ Fixing Presentation Line Breaks');
console.log('=' .repeat(50));
if (config.dryRun) {
console.log('π DRY RUN MODE - No files will be modified\n');
}
let files = [];
if (config.all) {
files = findPresentationFiles(PRESENTATIONS_DIR);
console.log(`\nFound ${files.length} presentation files\n`);
} else if (config.file) {
let filePath;
// Handle absolute paths, relative paths from project root, or just filename
if (config.file.startsWith('/')) {
// Absolute path
filePath = config.file;
} else if (config.file.startsWith('website/')) {
// Relative path from project root
filePath = join(__dirname, '..', config.file);
} else {
// Just filename - look in PRESENTATIONS_DIR
filePath = join(PRESENTATIONS_DIR, config.file);
}
if (!existsSync(filePath)) {
console.error(`β File not found: ${filePath}`);
process.exit(1);
}
files = [filePath];
}
// Process all files
const results = files.map(file => processFile(file, config.dryRun));
// Summary
console.log('\n' + '='.repeat(50));
console.log('π Summary\n');
const successful = results.filter(r => r.success);
const changed = results.filter(r => r.changed);
const errors = results.filter(r => !r.success);
console.log(`Total files processed: ${results.length}`);
console.log(`β
Successful: ${successful.length}`);
console.log(`βοΈ Modified: ${changed.length}`);
if (errors.length > 0) {
console.log(`β Errors: ${errors.length}`);
console.log('\nFailed files:');
for (const result of errors) {
console.log(` - ${result.fileName}: ${result.error}`);
}
}
if (changed.length > 0) {
const totalLinesFixed = changed.reduce((sum, r) => sum + (r.stats?.linesShortened || 0), 0);
const maxReduction = Math.max(...changed.map(r => r.stats?.maxReduction || 0));
console.log(`\nTotal lines fixed: ${totalLinesFixed}`);
console.log(`Maximum reduction: ${maxReduction} characters`);
}
if (config.dryRun) {
console.log('\nπ‘ Run without --dry-run to apply changes');
}
console.log();
process.exit(errors.length > 0 ? 1 : 0);
}
main();