File size: 9,944 Bytes
e903a32
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
#!/usr/bin/env node
import { spawn } from 'node:child_process';
import { promises as fs } from 'node:fs';
import { resolve, dirname, basename, extname } from 'node:path';
import process from 'node:process';

async function run(command, args = [], options = {}) {
  return new Promise((resolvePromise, reject) => {
    const child = spawn(command, args, { stdio: 'inherit', shell: false, ...options });
    child.on('error', reject);
    child.on('exit', (code) => {
      if (code === 0) resolvePromise(undefined);
      else reject(new Error(`${command} ${args.join(' ')} exited with code ${code}`));
    });
  });
}

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 slugify(text) {
  return String(text || '')
    .normalize('NFKD')
    .replace(/\p{Diacritic}+/gu, '')
    .toLowerCase()
    .replace(/[^a-z0-9]+/g, '-')
    .replace(/^-+|-+$/g, '')
    .slice(0, 120) || 'article';
}

async function checkPandocInstalled() {
  try {
    await run('pandoc', ['--version'], { stdio: 'pipe' });
    return true;
  } catch {
    return false;
  }
}

async function readMdxFile(filePath) {
  try {
    const content = await fs.readFile(filePath, 'utf-8');
    return content;
  } catch (error) {
    console.warn(`Warning: Could not read ${filePath}:`, error.message);
    return '';
  }
}

function extractFrontmatter(content) {
  const frontmatterMatch = content.match(/^---\n([\s\S]*?)\n---\n/);
  if (!frontmatterMatch) return { frontmatter: {}, content };

  const frontmatterText = frontmatterMatch[1];
  const contentWithoutFrontmatter = content.replace(frontmatterMatch[0], '');

  // Simple YAML parsing for basic fields
  const frontmatter = {};
  const lines = frontmatterText.split('\n');
  let currentKey = null;
  let currentValue = '';

  for (const line of lines) {
    const trimmed = line.trim();
    if (trimmed.includes(':') && !trimmed.startsWith('-')) {
      if (currentKey) {
        frontmatter[currentKey] = currentValue.trim();
      }
      const [key, ...valueParts] = trimmed.split(':');
      currentKey = key.trim();
      currentValue = valueParts.join(':').trim();
    } else if (currentKey) {
      currentValue += '\n' + trimmed;
    }
  }

  if (currentKey) {
    frontmatter[currentKey] = currentValue.trim();
  }

  return { frontmatter, content: contentWithoutFrontmatter };
}

function cleanMdxToMarkdown(content) {
  // Remove import statements
  content = content.replace(/^import .+?;?\s*$/gm, '');

  // Remove JSX component calls like <ComponentName />
  content = content.replace(/<[A-Z][a-zA-Z0-9]*\s*\/>/g, '');

  // Convert JSX components to simpler markdown
  // Handle Sidenote components specially
  content = content.replace(/<Sidenote>([\s\S]*?)<\/Sidenote>/g, (match, innerContent) => {
    // Extract main content and aside content
    const asideMatch = innerContent.match(/<Fragment slot="aside">([\s\S]*?)<\/Fragment>/);
    const mainContent = innerContent.replace(/<Fragment slot="aside">[\s\S]*?<\/Fragment>/, '').trim();
    const asideContent = asideMatch ? asideMatch[1].trim() : '';

    let result = mainContent;
    if (asideContent) {
      result += `\n\n> **Note:** ${asideContent}`;
    }
    return result;
  });

  // Handle Note components
  content = content.replace(/<Note[^>]*>([\s\S]*?)<\/Note>/g, (match, innerContent) => {
    return `\n> **Note:** ${innerContent.trim()}\n`;
  });

  // Handle Wide and FullWidth components
  content = content.replace(/<(Wide|FullWidth)>([\s\S]*?)<\/\1>/g, '$2');

  // Handle HtmlEmbed components (convert to simple text)
  content = content.replace(/<HtmlEmbed[^>]*\/>/g, '*[Interactive content not available in LaTeX]*');

  // Remove remaining JSX fragments
  content = content.replace(/<Fragment[^>]*>([\s\S]*?)<\/Fragment>/g, '$1');
  content = content.replace(/<[A-Z][a-zA-Z0-9]*[^>]*>([\s\S]*?)<\/[A-Z][a-zA-Z0-9]*>/g, '$1');

  // Clean up className attributes
  content = content.replace(/className="[^"]*"/g, '');

  // Clean up extra whitespace
  content = content.replace(/\n{3,}/g, '\n\n');

  return content.trim();
}

async function processChapterImports(content, contentDir) {
  let processedContent = content;

  // First, extract all import statements and their corresponding component calls
  const importPattern = /import\s+(\w+)\s+from\s+["']\.\/chapters\/([^"']+)["'];?/g;
  const imports = new Map();
  let match;

  // Collect all imports
  while ((match = importPattern.exec(content)) !== null) {
    const [fullImport, componentName, chapterPath] = match;
    imports.set(componentName, { path: chapterPath, importStatement: fullImport });
  }

  // Remove all import statements
  processedContent = processedContent.replace(importPattern, '');

  // Process each component call
  for (const [componentName, { path: chapterPath }] of imports) {
    const componentCallPattern = new RegExp(`<${componentName}\\s*\\/>`, 'g');

    try {
      const chapterFile = resolve(contentDir, 'chapters', chapterPath);
      const chapterContent = await readMdxFile(chapterFile);
      const { content: chapterMarkdown } = extractFrontmatter(chapterContent);
      const cleanChapter = cleanMdxToMarkdown(chapterMarkdown);

      processedContent = processedContent.replace(componentCallPattern, cleanChapter);
      console.log(`βœ… Processed chapter: ${chapterPath}`);
    } catch (error) {
      console.warn(`Warning: Could not process chapter ${chapterPath}:`, error.message);
      processedContent = processedContent.replace(componentCallPattern, `\n*[Chapter ${chapterPath} could not be loaded]*\n`);
    }
  }

  return processedContent;
}

function createLatexPreamble(frontmatter) {
  const title = frontmatter.title ? frontmatter.title.replace(/\n/g, ' ') : 'Untitled Article';
  const subtitle = frontmatter.subtitle || '';
  const authors = frontmatter.authors || '';
  const date = frontmatter.published || '';

  return `\\documentclass[11pt,a4paper]{article}
\\usepackage[utf8]{inputenc}
\\usepackage[T1]{fontenc}
\\usepackage{amsmath,amsfonts,amssymb}
\\usepackage{graphicx}
\\usepackage{hyperref}
\\usepackage{booktabs}
\\usepackage{longtable}
\\usepackage{array}
\\usepackage{multirow}
\\usepackage{wrapfig}
\\usepackage{float}
\\usepackage{colortbl}
\\usepackage{pdflscape}
\\usepackage{tabu}
\\usepackage{threeparttable}
\\usepackage{threeparttablex}
\\usepackage{ulem}
\\usepackage{makecell}
\\usepackage{xcolor}
\\usepackage{listings}
\\usepackage{fancyvrb}
\\usepackage{geometry}
\\geometry{margin=1in}

\\title{${title}${subtitle ? `\\\\\\large ${subtitle}` : ''}}
${authors ? `\\author{${authors}}` : ''}
${date ? `\\date{${date}}` : ''}

\\begin{document}
\\maketitle
\\tableofcontents
\\newpage

`;
}

async function main() {
  const cwd = process.cwd();
  const args = parseArgs(process.argv);

  // Check if pandoc is installed
  const hasPandoc = await checkPandocInstalled();
  if (!hasPandoc) {
    console.error('❌ Pandoc is not installed. Please install it first:');
    console.error('   macOS: brew install pandoc');
    console.error('   Ubuntu: apt-get install pandoc');
    console.error('   Windows: choco install pandoc');
    process.exit(1);
  }

  const contentDir = resolve(cwd, 'src/content');
  const articleFile = resolve(contentDir, 'article.mdx');

  // Check if article.mdx exists
  try {
    await fs.access(articleFile);
  } catch {
    console.error(`❌ Could not find article.mdx at ${articleFile}`);
    process.exit(1);
  }

  console.log('> Reading article content...');
  const articleContent = await readMdxFile(articleFile);
  const { frontmatter, content } = extractFrontmatter(articleContent);

  console.log('> Processing chapters...');
  const processedContent = await processChapterImports(content, contentDir);

  console.log('> Converting MDX to Markdown...');
  const markdownContent = cleanMdxToMarkdown(processedContent);

  // Generate output filename
  const title = frontmatter.title ? frontmatter.title.replace(/\n/g, ' ') : 'article';
  const outFileBase = args.filename ? String(args.filename).replace(/\.(tex|pdf)$/i, '') : slugify(title);

  // Create temporary markdown file
  const tempMdFile = resolve(cwd, 'temp-article.md');
  await fs.writeFile(tempMdFile, markdownContent);


  console.log('> Converting to LaTeX with Pandoc...');
  const outputLatex = resolve(cwd, 'dist', `${outFileBase}.tex`);

  // Ensure dist directory exists
  await fs.mkdir(resolve(cwd, 'dist'), { recursive: true });

  // Pandoc conversion arguments
  const pandocArgs = [
    tempMdFile,
    '-o', outputLatex,
    '--from=markdown',
    '--to=latex',
    '--standalone',
    '--toc',
    '--number-sections',
    '--highlight-style=tango',
    '--listings'
  ];

  // Add bibliography if it exists
  const bibFile = resolve(contentDir, 'bibliography.bib');
  try {
    await fs.access(bibFile);
    pandocArgs.push('--bibliography', bibFile);
    pandocArgs.push('--citeproc');
    console.log('βœ… Found bibliography file, including citations');
  } catch {
    console.log('ℹ️  No bibliography file found');
  }

  try {
    await run('pandoc', pandocArgs);
    console.log(`βœ… LaTeX generated: ${outputLatex}`);

    // Optionally compile to PDF if requested
    if (args.pdf) {
      console.log('> Compiling LaTeX to PDF...');
      const outputPdf = resolve(cwd, 'dist', `${outFileBase}.pdf`);
      await run('pdflatex', ['-output-directory', resolve(cwd, 'dist'), outputLatex]);
      console.log(`βœ… PDF generated: ${outputPdf}`);
    }

  } catch (error) {
    console.error('❌ Pandoc conversion failed:', error.message);
    process.exit(1);
  } finally {
    // Clean up temporary file
    try {
      await fs.unlink(tempMdFile);
    } catch { }
  }
}

main().catch((err) => {
  console.error(err);
  process.exit(1);
});