| 1 |
#! /usr/bin/perl |
| 2 |
|
| 3 |
# |
| 4 |
# Combine scripts in the MathJax project into single-file-load |
| 5 |
# configuration files. |
| 6 |
# |
| 7 |
# Usage: ./combineMJlist proj-dir [files.lis] |
| 8 |
# |
| 9 |
|
| 10 |
use Cwd; |
| 11 |
use FindBin; |
| 12 |
|
| 13 |
$DIR = $FindBin::Bin; |
| 14 |
$MJX = shift || getcwd; |
| 15 |
|
| 16 |
$name = $ARGV[0]; $name =~ s!.*/!!; |
| 17 |
die "Usage: ./combineMJlist srcdir files.lis\n" unless $name =~ m/\S/; |
| 18 |
die "Project directory must contain a config directory\n" unless -d "$MJX/config"; |
| 19 |
|
| 20 |
$outfile = "config/$name"; $outfile =~ s/\.[^.]*/.js/; |
| 21 |
|
| 22 |
@files = (); |
| 23 |
while ($file = <>) { |
| 24 |
chomp($file); |
| 25 |
if ($file =~ m/\S/) { |
| 26 |
push (@files,$file); |
| 27 |
} else { |
| 28 |
@config = <>; |
| 29 |
chomp(@config); |
| 30 |
last; |
| 31 |
} |
| 32 |
} |
| 33 |
|
| 34 |
$MMLorHTML = 0; |
| 35 |
@list = (); |
| 36 |
|
| 37 |
foreach $file (@files) { |
| 38 |
$MMLorHTML = 1 if $file =~ m!/MMLorHTML.js!; |
| 39 |
push(@list,"[MathJax]/$file"); |
| 40 |
} |
| 41 |
$files = ' "'.join("\",\n \"",@list).'"'; |
| 42 |
$MMLorHTML = ("","\nMathJax.Hub.Config({delayJaxRegistration: true});\n")[$MMLorHTML]; |
| 43 |
|
| 44 |
open(CONFIG,"<","$DIR/template.js") || die "Can't open $DIR/template.js: $!\n"; |
| 45 |
@lines = <CONFIG>; |
| 46 |
close(CONFIG); |
| 47 |
$config = join("",@lines); |
| 48 |
|
| 49 |
$config =~ s!%%% NAME %%%!/MathJax/$outfile!; |
| 50 |
$config =~ s!%%% FILES %%%!$files!; |
| 51 |
$config =~ s!%%% MMLorHTML %%%!$MMLorHTML!; |
| 52 |
|
| 53 |
print "Creating $outfile\n"; |
| 54 |
open(CONFIG,">","$MJX/$outfile") || die "Can't write $MJX/$outfile: $!\n"; |
| 55 |
print CONFIG $config; |
| 56 |
|
| 57 |
if (scalar(@config)) { |
| 58 |
print CONFIG "MathJax.Hub.Config({\n "; |
| 59 |
print CONFIG join("\n ",@config); |
| 60 |
print CONFIG "\n});\n\n"; |
| 61 |
} |
| 62 |
|
| 63 |
foreach $file (@files) { |
| 64 |
print " $file\n"; |
| 65 |
open(JS,"<","$MJX/$file") || warn "Can't read $MJX/$file: $!\n"; |
| 66 |
@lines = <JS>; |
| 67 |
close(JS); |
| 68 |
$lines = join("",@lines); |
| 69 |
$lines =~ s!/.*?\*/\n\n!!s; |
| 70 |
print CONFIG $lines; |
| 71 |
} |
| 72 |
|
| 73 |
print CONFIG "MathJax.Ajax.loadComplete(\"[MathJax]/$outfile\");\n"; |
| 74 |
|
| 75 |
close(CONFIG); |
| 76 |
|
| 77 |
1; |