File size: 11,783 Bytes
2b16052
 
 
 
 
 
 
7688b4a
 
 
2b16052
 
 
 
 
 
 
 
fe5248a
2b16052
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
7688b4a
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
2b16052
 
7688b4a
 
 
 
 
2b16052
7688b4a
 
 
 
 
 
2b16052
7688b4a
2b16052
7688b4a
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
2b16052
7688b4a
 
2b16052
7688b4a
 
 
 
2b16052
7688b4a
2b16052
 
 
fe5248a
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
2b16052
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
fe5248a
 
 
 
 
 
 
 
 
 
2b16052
 
 
 
 
 
 
 
 
 
fe5248a
 
2b16052
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
fe5248a
 
 
 
 
 
 
 
 
 
 
 
2b16052
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
#!/usr/bin/env node

/**
 * Export TXT to DOCX format for book publishing
 * 
 * This script converts the exported TXT file to a simple DOCX document:
 * - Preserves headings, paragraphs, lists
 * - Renders inline formatting: <b> bold, <i> italic, <a> links, <ref> citations
 * - Renders <ic> inline code, <il> inline LaTeX
 * - Keeps block tags (<f>, <t>, <l>, <n>) with color coding
 * - Formats code blocks
 * - Creates a clean document ready for further editing
 * 
 * Usage:
 *   node scripts/export-docx.mjs [--input=path/to/file.txt]
 *   npm run export:docx
 */

import { Document, Packer, Paragraph, TextRun, HeadingLevel, AlignmentType, LineRuleType } from 'docx';
import { promises as fs } from 'node:fs';
import { resolve } from 'node:path';
import process from 'node:process';

function parseArgs(argv) {
  const out = {};
  for (const arg of argv.slice(2)) {
    if (!arg.startsWith('--')) continue;
    const [k, v] = arg.replace(/^--/, '').split('=');
    out[k] = v === undefined ? true : v;
  }
  return out;
}

function detectHeadingLevel(line) {
  const match = line.match(/^(#{1,6})\s+(.+)$/);
  if (!match) return null;
  const level = match[1].length;
  const text = match[2].trim();
  return { level, text };
}

/**
 * Extract simple properties from a TextRun for re-wrapping.
 * docx TextRun stores options internally β€” this grabs what we need.
 */
function extractRunProps(run) {
  // TextRun constructor options are stored in run.options (docx β‰₯ 8)
  const opts = run.options || {};
  return {
    text: opts.text || '',
    bold: opts.bold,
    italics: opts.italics,
    font: opts.font,
    color: opts.color,
    underline: opts.underline,
    superScript: opts.superScript,
    shading: opts.shading,
    size: opts.size,
  };
}

function parseInlineFormatting(text) {
  const runs = [];
  let pos = 0;

  // Match all supported inline tags (including nested content)
  // Order matters: longer tag names first to avoid partial matches
  const tagPattern = /<(ic|il|ref|b|i|a)(\s[^>]*)?>([^<]*(?:<(?!\/\1>)[^<]*)*)<\/\1>/g;
  let match;

  while ((match = tagPattern.exec(text)) !== null) {
    // Text before the tag
    if (match.index > pos) {
      const before = text.substring(pos, match.index);
      if (before) runs.push(new TextRun(before));
    }

    const tagType = match[1];
    const attrs = match[2] || '';
    const content = match[3];

    switch (tagType) {
      case 'ic':
        runs.push(new TextRun({
          text: content,
          font: 'Courier New',
          color: '333333',
          shading: { fill: 'E8E8E8', type: 'clear' },
        }));
        break;

      case 'il':
        runs.push(new TextRun({
          text: content,
          italics: true,
          color: '0066CC',
        }));
        break;

      case 'b':
        // Bold β€” check for nested tags, otherwise simple bold
        if (content.includes('<')) {
          // Has nested tags: parse inner content and add bold to each run
          for (const innerRun of parseInlineFormatting(content)) {
            // Extract properties from existing run and add bold
            const props = {};
            if (innerRun.properties) Object.assign(props, innerRun.properties);
            runs.push(new TextRun({ ...extractRunProps(innerRun), bold: true }));
          }
        } else {
          runs.push(new TextRun({ text: content, bold: true }));
        }
        break;

      case 'i':
        if (content.includes('<')) {
          for (const innerRun of parseInlineFormatting(content)) {
            runs.push(new TextRun({ ...extractRunProps(innerRun), italics: true }));
          }
        } else {
          runs.push(new TextRun({ text: content, italics: true }));
        }
        break;

      case 'a': {
        // Link β€” extract href, render as underlined blue text
        const hrefMatch = attrs.match(/href="([^"]*)"/);
        const href = hrefMatch ? hrefMatch[1] : '';
        runs.push(new TextRun({
          text: content,
          color: '0066CC',
          underline: { type: 'single' },
        }));
        // Add the URL in parentheses if it's a full URL
        if (href && href.startsWith('http')) {
          runs.push(new TextRun({
            text: ` [${href}]`,
            color: '888888',
            size: 18,
          }));
        }
        break;
      }

      case 'ref':
        runs.push(new TextRun({
          text: content,
          superScript: true,
          color: '0066CC',
        }));
        break;

      default:
        runs.push(new TextRun(match[0]));
    }

    pos = match.index + match[0].length;
  }

  // Remaining text after last tag
  if (pos < text.length) {
    runs.push(new TextRun(text.substring(pos)));
  }

  return runs.length > 0 ? runs : [new TextRun(text)];
}

/**
 * Convert a code block (array of lines) into a DOCX Paragraph with proper
 * line breaks. Uses Courier New + gray background shading.
 */
function codeBlockToParagraph(codeLines) {
  const runs = [];
  for (let i = 0; i < codeLines.length; i++) {
    if (i > 0) runs.push(new TextRun({ break: 1 }));
    runs.push(new TextRun({
      text: codeLines[i],
      font: 'Courier New',
      size: 18,
      color: '333333',
    }));
  }
  return new Paragraph({
    children: runs,
    shading: { fill: 'F5F5F5', type: 'clear' },
    spacing: {
      before: 200,
      after: 200,
      line: 276,
      lineRule: LineRuleType.AUTO,
    },
  });
}

async function convertTxtToDocx(txtPath, outputPath) {
  console.log(`πŸ“– Reading TXT file: ${txtPath}`);
  const content = await fs.readFile(txtPath, 'utf-8');
  const lines = content.split('\n');
  
  const paragraphs = [];
  let inCodeBlock = false;
  let codeLines = [];
  
  for (let i = 0; i < lines.length; i++) {
    const line = lines[i];
    
    // Skip empty lines unless in code block
    if (!line.trim() && !inCodeBlock) {
      paragraphs.push(new Paragraph({ text: '' }));
      continue;
    }
    
    // Handle code blocks <c>...</c>
    if (line.trim().startsWith('<c>')) {
      inCodeBlock = true;
      codeLines = [];
      // Single-line code block: <c>code</c>
      if (line.trim().endsWith('</c>') && line.trim() !== '<c></c>') {
        const inner = line.trim().replace(/^<c>/, '').replace(/<\/c>$/, '');
        if (inner) codeLines.push(inner);
        paragraphs.push(codeBlockToParagraph(codeLines));
        inCodeBlock = false;
        codeLines = [];
        continue;
      }
      const firstLine = line.replace(/^<c>\s*/, '').trimStart();
      if (firstLine && !firstLine.startsWith('</c>')) {
        codeLines.push(firstLine);
      }
      continue;
    }
    
    if (line.trim().endsWith('</c>')) {
      const lastLine = line.replace(/<\/c>\s*$/, '');
      if (lastLine) codeLines.push(lastLine);
      
      // Add code block as paragraph with proper line breaks
      paragraphs.push(codeBlockToParagraph(codeLines));
      
      inCodeBlock = false;
      codeLines = [];
      continue;
    }
    
    if (inCodeBlock) {
      codeLines.push(line);
      continue;
    }
    
    // Handle figure tags <f>...</f>
    if (line.trim().startsWith('<f>')) {
      paragraphs.push(new Paragraph({
        children: [new TextRun({
          text: line.trim(),
          color: '0066CC',
          bold: true
        })],
        spacing: { before: 200, after: 100 }
      }));
      continue;
    }
    
    // Handle table tags <t>...</t>
    if (line.trim().startsWith('<t>')) {
      paragraphs.push(new Paragraph({
        children: [new TextRun({
          text: line.trim(),
          color: '009688',
          bold: true
        })],
        spacing: { before: 200, after: 100 }
      }));
      continue;
    }
    
    // Handle LaTeX display tags <l>...</l>
    if (line.trim().startsWith('<l>')) {
      paragraphs.push(new Paragraph({
        children: [new TextRun({
          text: line.trim(),
          color: '9C27B0',
          bold: true
        })],
        alignment: AlignmentType.CENTER,
        spacing: { before: 200, after: 200 }
      }));
      continue;
    }
    
    // Handle note/callout tags <n>...</n>
    if (line.trim().startsWith('<n>') && line.trim().endsWith('</n>')) {
      const inner = line.trim().replace(/^<n>/, '').replace(/<\/n>$/, '');
      paragraphs.push(new Paragraph({
        children: parseInlineFormatting(inner),
        indent: { left: 360 },
        shading: { fill: 'FFF8E1', type: 'clear' },
        spacing: { before: 200, after: 200 },
      }));
      continue;
    }
    
    // Handle headings
    const heading = detectHeadingLevel(line);
    if (heading) {
      const headingLevels = {
        1: HeadingLevel.HEADING_1,
        2: HeadingLevel.HEADING_2,
        3: HeadingLevel.HEADING_3,
        4: HeadingLevel.HEADING_4,
        5: HeadingLevel.HEADING_5,
        6: HeadingLevel.HEADING_6
      };
      
      paragraphs.push(new Paragraph({
        text: heading.text,
        heading: headingLevels[heading.level],
        spacing: { before: 400, after: 200 }
      }));
      continue;
    }
    
    // Handle list items
    if (line.trim().startsWith('- ')) {
      const text = line.trim().substring(2);
      paragraphs.push(new Paragraph({
        children: parseInlineFormatting(text),
        bullet: { level: 0 },
        spacing: { before: 100, after: 100 }
      }));
      continue;
    }
    
    // Handle numbered lists
    const numberedMatch = line.trim().match(/^(\d+)\.\s+(.+)$/);
    if (numberedMatch) {
      const text = numberedMatch[2];
      paragraphs.push(new Paragraph({
        children: parseInlineFormatting(text),
        numbering: { reference: 'default-numbering', level: 0 },
        spacing: { before: 100, after: 100 }
      }));
      continue;
    }
    
    // Handle blockquotes
    if (line.trim().startsWith('> ')) {
      const text = line.trim().substring(2);
      paragraphs.push(new Paragraph({
        children: parseInlineFormatting(text),
        italics: true,
        indent: { left: 720 },
        spacing: { before: 200, after: 200 }
      }));
      continue;
    }
    
    // Regular paragraph
    if (line.trim()) {
      paragraphs.push(new Paragraph({
        children: parseInlineFormatting(line.trim()),
        spacing: { before: 100, after: 100 }
      }));
    }
  }
  
  console.log(`πŸ“ Creating DOCX with ${paragraphs.length} paragraphs...`);
  
  const doc = new Document({
    sections: [{
      properties: {},
      children: paragraphs
    }]
  });
  
  console.log(`πŸ’Ύ Writing DOCX to: ${outputPath}`);
  const buffer = await Packer.toBuffer(doc);
  await fs.writeFile(outputPath, buffer);
  
  console.log(`βœ… DOCX created successfully!`);
}

async function main() {
  const cwd = process.cwd();
  const args = parseArgs(process.argv);
  
  const inputPath = args.input || resolve(cwd, 'dist', 'the-smol-training-playbook-the-secrets-to-building-world-class-llms.txt');
  const outputPath = args.output || inputPath.replace('.txt', '.docx');
  
  // Check if input exists
  try {
    await fs.access(inputPath);
  } catch {
    console.error(`❌ Error: Input file not found: ${inputPath}`);
    console.error('   Run "npm run export:txt" first to generate the TXT file.');
    process.exit(1);
  }
  
  await convertTxtToDocx(inputPath, outputPath);
  
  // Also copy to public folder
  const publicPath = outputPath.replace('/dist/', '/public/');
  try {
    await fs.mkdir(resolve(cwd, 'public'), { recursive: true });
    await fs.copyFile(outputPath, publicPath);
    console.log(`βœ… DOCX copied to: ${publicPath}`);
  } catch (e) {
    console.warn('Unable to copy DOCX to public/:', e?.message || e);
  }
}

main().catch((err) => {
  console.error('❌ Error:', err.message);
  console.error(err);
  process.exit(1);
});