CoolFace
Apppublic

Pq234/robot-learning-tutorial

sourceHugging Faceupdated 1y agoView on Hugging Face
0likes
index.mjs139 linesDownload Raw Back to latex-importer
1#!/usr/bin/env node2 3import { join, dirname } from 'path';4import { fileURLToPath } from 'url';5import { copyFileSync } from 'fs';6import { convertLatexToMarkdown } from './latex-converter.mjs';7import { convertToMdx } from './mdx-converter.mjs';8import { cleanBibliography } from './bib-cleaner.mjs';9 10const __filename = fileURLToPath(import.meta.url);11const __dirname = dirname(__filename);12 13// Default configuration14const DEFAULT_INPUT = join(__dirname, 'input', 'main.tex');15const DEFAULT_OUTPUT = join(__dirname, 'output');16const ASTRO_CONTENT_PATH = join(__dirname, '..', '..', 'src', 'content', 'article.mdx');17 18function parseArgs() {19    const args = process.argv.slice(2);20    const config = {21        input: DEFAULT_INPUT,22        output: DEFAULT_OUTPUT,23        clean: false,24        bibOnly: false,25        convertOnly: false,26        mdx: false,27    };28 29    for (const arg of args) {30        if (arg.startsWith('--input=')) {31            config.input = arg.split('=')[1];32        } else if (arg.startsWith('--output=')) {33            config.output = arg.split('=')[1];34        } else if (arg === '--clean') {35            config.clean = true;36        } else if (arg === '--bib-only') {37            config.bibOnly = true;38        } else if (arg === '--convert-only') {39            config.convertOnly = true;40        }41    }42 43    return config;44}45 46function showHelp() {47    console.log(`48๐Ÿš€ LaTeX to Markdown Toolkit49 50Usage:51  node index.mjs [options]52 53Options:54  --input=PATH      Input LaTeX file (default: input/main.tex)55  --output=PATH     Output directory (default: output/)56  --clean           Clean output directory before processing57  --bib-only        Only clean bibliography file58  --convert-only    Only convert LaTeX to Markdown (skip bib cleaning)59  --help, -h        Show this help60 61Examples:62  # Full conversion with bibliography cleaning63  node index.mjs --clean64 65  # Only clean bibliography66  node index.mjs --bib-only --input=paper.tex --output=clean/67 68  # Only convert LaTeX (use existing clean bibliography)69  node index.mjs --convert-only70 71  # Custom paths72  node index.mjs --input=../paper/main.tex --output=../results/ --clean73`);74}75 76function main() {77    const args = process.argv.slice(2);78 79    if (args.includes('--help') || args.includes('-h')) {80        showHelp();81        process.exit(0);82    }83 84    const config = parseArgs();85 86    console.log('๐Ÿš€ LaTeX to Markdown Toolkit');87    console.log('==============================');88 89    try {90        if (config.bibOnly) {91            // Only clean bibliography92            console.log('๐Ÿ“š Bibliography cleaning mode');93            const bibInput = config.input.replace('.tex', '.bib');94            const bibOutput = join(config.output, 'main.bib');95 96            cleanBibliography(bibInput, bibOutput);97            console.log('๐ŸŽ‰ Bibliography cleaning completed!');98 99        } else if (config.convertOnly) {100            // Only convert LaTeX101            console.log('๐Ÿ“„ Conversion only mode');102            convertLatexToMarkdown(config.input, config.output);103 104        } else {105            // Full workflow106            console.log('๐Ÿ”„ Full conversion workflow');107            convertLatexToMarkdown(config.input, config.output);108 109            // Convert to MDX if requested110            const markdownFile = join(config.output, 'main.md');111            const mdxFile = join(config.output, 'main.mdx');112 113            console.log('๐Ÿ“ Converting Markdown to MDX...');114            convertToMdx(markdownFile, mdxFile);115 116            // Copy MDX to Astro content directory117            console.log('๐Ÿ“‹ Copying MDX to Astro content directory...');118            try {119                copyFileSync(mdxFile, ASTRO_CONTENT_PATH);120                console.log(`    โœ… Copied to ${ASTRO_CONTENT_PATH}`);121            } catch (error) {122                console.warn(`    โš ๏ธ  Failed to copy MDX to Astro: ${error.message}`);123            }124        }125 126    } catch (error) {127        console.error('โŒ Error:', error.message);128        process.exit(1);129    }130}131 132// Export functions for use as module133export { convertLatexToMarkdown, cleanBibliography };134 135// Run CLI if called directly136if (import.meta.url === `file://${process.argv[1]}`) {137    main();138}139