11import fs from "node:fs" ;
2- import fse from "fs-extra" ;
32import util from "util" ;
43import path from "path" ;
54
@@ -15,11 +14,6 @@ import {
1514 recursiveProcessTextLatex
1615} from "./parseXmlLatex.js" ;
1716import { setupSnippetsPdf } from "./processingFunctions/processSnippetPdf.js" ;
18- import { preamble , frontmatter , ending } from "./latexContent.js" ;
19- const latexmkrcContent = `$pdflatex = "xelatex %O %S";
20- $pdf_mode = 1;
21- $dvi_mode = 0;
22- $postscript_mode = 0;` ;
2317
2418// html (comparison version)
2519import { switchTitle } from "./htmlContent.js" ;
@@ -42,6 +36,7 @@ import { writeRewritedSearchData } from "./searchRewrite.js";
4236import { setupSnippetsJson } from "./processingFunctions/processSnippetJson.js" ;
4337import { createTocJson } from "./generateTocJson.js" ;
4438import { setupReferencesJson } from "./processingFunctions/processReferenceJson.js" ;
39+ import { createMain } from "./commands/utils.js" ;
4540
4641export let parseType ;
4742let version ;
@@ -243,62 +238,13 @@ const createIndexHtml = version => {
243238 } ) ;
244239} ;
245240
246- const createMain = ( ) => {
247- if ( ! fs . existsSync ( outputDir ) ) {
248- fs . mkdirSync ( outputDir ) ;
249- }
250-
251- if ( parseType == "js" || parseType == "json" ) {
252- return ;
253- }
254-
255- if ( parseType == "web" ) {
256- if ( ! fs . existsSync ( path . join ( outputDir , "/chapters" ) ) ) {
257- fs . mkdirSync ( path . join ( outputDir , "/chapters" ) ) ;
258- }
259- fse . copy ( path . join ( __dirname , "/../static" ) , outputDir , err => {
260- if ( err ) return console . error ( err ) ;
261- } ) ;
262- return ;
263- }
264-
265- // for latex version only
266- // create sicpjs.tex file
267- const chaptersFound = [ ] ;
268- const files = fs . readdirSync ( inputDir ) ;
269- files . forEach ( file => {
270- if ( file . match ( / c h a p t e r / ) ) {
271- chaptersFound . push ( file ) ;
272- }
273- } ) ;
274- const stream = fs . createWriteStream ( path . join ( outputDir , "sicpjs.tex" ) ) ;
275- stream . once ( "open" , fd => {
276- stream . write ( preamble ) ;
277- stream . write ( frontmatter ) ;
278- chaptersFound . forEach ( chapter => {
279- const pathStr = "./" + chapter + "/" + chapter + ".tex" ;
280- stream . write ( "\\input{" + pathStr + "}\n" ) ;
281- } ) ;
282- stream . write ( ending ) ;
283- stream . end ( ) ;
284- } ) ;
285- // makes the .latexmkrc file
286- const latexmkrcStream = fs . createWriteStream (
287- path . join ( outputDir , ".latexmkrc" )
288- ) ;
289- latexmkrcStream . once ( "open" , fd => {
290- latexmkrcStream . write ( latexmkrcContent ) ;
291- latexmkrcStream . end ( ) ;
292- } ) ;
293- } ;
294-
295241async function main ( ) {
296242 parseType = process . argv [ 2 ] ;
297243 if ( parseType == "pdf" ) {
298244 outputDir = path . join ( __dirname , "../latex_pdf" ) ;
299245
300246 switchParseFunctionsLatex ( parseType ) ;
301- createMain ( ) ;
247+ createMain ( inputDir , outputDir , parseType ) ;
302248
303249 console . log ( "setup snippets\n" ) ;
304250 await recursiveTranslateXml ( "" , "setupSnippet" ) ;
@@ -326,7 +272,7 @@ async function main() {
326272
327273 switchParseFunctionsHtml ( version ) ;
328274 switchTitle ( version ) ;
329- createMain ( ) ;
275+ createMain ( inputDir , outputDir , parseType ) ;
330276
331277 console . log ( "\ngenerate table of content\n" ) ;
332278 await recursiveTranslateXml ( "" , "generateTOC" ) ;
@@ -345,15 +291,15 @@ async function main() {
345291 } else if ( parseType == "js" ) {
346292 outputDir = path . join ( __dirname , "../js_programs" ) ;
347293
348- createMain ( ) ;
294+ createMain ( inputDir , outputDir , parseType ) ;
349295 console . log ( "setup snippets\n" ) ;
350296 await recursiveTranslateXml ( "" , "setupSnippet" ) ;
351297 console . log ( "setup snippets done\n" ) ;
352298 recursiveTranslateXml ( "" , "parseXml" ) ;
353299 } else if ( parseType == "json" ) {
354300 outputDir = path . join ( __dirname , "../json" ) ;
355301
356- createMain ( ) ;
302+ createMain ( inputDir , outputDir , parseType ) ;
357303
358304 console . log ( "\ngenerate table of content\n" ) ;
359305 await recursiveTranslateXml ( "" , "generateTOC" ) ;
0 commit comments