From 7791a6c6b224e5ec3c1a40489e231c887a8864fa Mon Sep 17 00:00:00 2001 From: Hans Hagen Date: Wed, 25 May 2005 00:00:00 +0200 Subject: stable 2005.05.25 --- scripts/context/perl/makempy.pl | 4 +- scripts/context/perl/texexec.pl | 280 +++++-- scripts/context/perl/texexec.rme | 2 +- scripts/context/perl/texfont.pl | 15 +- scripts/context/perl/texutil.pl | 49 +- scripts/context/ruby/base/file.rb | 133 +++ scripts/context/ruby/base/kpse.rb | 270 ++++++ scripts/context/ruby/base/logger.rb | 104 +++ scripts/context/ruby/base/pdf.rb | 53 ++ scripts/context/ruby/base/state.rb | 75 ++ scripts/context/ruby/base/switch.rb | 599 ++++++++++++++ scripts/context/ruby/base/system.rb | 102 +++ scripts/context/ruby/base/tex.rb | 1281 +++++++++++++++++++++++++++++ scripts/context/ruby/base/tool.rb | 305 +++++++ scripts/context/ruby/base/variables.rb | 37 + scripts/context/ruby/ctxtools.rb | 97 ++- scripts/context/ruby/exa/logger.rb | 108 --- scripts/context/ruby/graphics/gs.rb | 631 ++++++++++++++ scripts/context/ruby/graphics/inkscape.rb | 103 +++ scripts/context/ruby/graphics/magick.rb | 161 ++++ scripts/context/ruby/newimgtopdf.rb | 86 ++ scripts/context/ruby/newpstopdf.rb | 527 ++++++++++++ scripts/context/ruby/newtexexec.rb | 588 +++++++++++++ scripts/context/ruby/rlxtools.rb | 261 ++++++ scripts/context/ruby/texmfstart.rb | 428 ++++++---- scripts/context/ruby/texsync.rb | 19 +- scripts/context/ruby/textools.rb | 16 +- scripts/context/ruby/xmltools.rb | 15 +- scripts/context/ruby/xmpl/switch.rb | 489 ----------- 29 files changed, 5930 insertions(+), 908 deletions(-) create mode 100644 scripts/context/ruby/base/file.rb create mode 100644 scripts/context/ruby/base/kpse.rb create mode 100644 scripts/context/ruby/base/logger.rb create mode 100644 scripts/context/ruby/base/pdf.rb create mode 100644 scripts/context/ruby/base/state.rb create mode 100644 scripts/context/ruby/base/switch.rb create mode 100644 scripts/context/ruby/base/system.rb create mode 100644 scripts/context/ruby/base/tex.rb create mode 100644 scripts/context/ruby/base/tool.rb create mode 100644 scripts/context/ruby/base/variables.rb delete mode 100644 scripts/context/ruby/exa/logger.rb create mode 100644 scripts/context/ruby/graphics/gs.rb create mode 100644 scripts/context/ruby/graphics/inkscape.rb create mode 100644 scripts/context/ruby/graphics/magick.rb create mode 100644 scripts/context/ruby/newimgtopdf.rb create mode 100644 scripts/context/ruby/newpstopdf.rb create mode 100644 scripts/context/ruby/newtexexec.rb create mode 100644 scripts/context/ruby/rlxtools.rb delete mode 100644 scripts/context/ruby/xmpl/switch.rb (limited to 'scripts') diff --git a/scripts/context/perl/makempy.pl b/scripts/context/perl/makempy.pl index 49298f318..5b52ee0bb 100644 --- a/scripts/context/perl/makempy.pl +++ b/scripts/context/perl/makempy.pl @@ -18,8 +18,8 @@ eval '(exit $?0)' && eval 'exec perl -S $0 ${1+"$@"}' && eval 'exec perl -S $0 $ # of binaries on UNIX as well as did some usefull suggestions # to improve the functionality. -# This script uses GhostScript and PStoEdit as well as -# pdfTeX, and if requested TeXEdit and ConTeXt. +# This script uses GhostScript and PStoEdit as well as +# pdfTeX, and if requested TeXEdit and ConTeXt. # todo: we can nowadays do without the intermediate step, because GS # can now handle PDF quite good diff --git a/scripts/context/perl/texexec.pl b/scripts/context/perl/texexec.pl index fe6c8421f..0e6e5d098 100644 --- a/scripts/context/perl/texexec.pl +++ b/scripts/context/perl/texexec.pl @@ -34,6 +34,8 @@ eval '(exit $?0)' && eval 'exec perl -w -S $0 ${1+"$@"}' && eval 'exec perl -w - use strict ; +my $OriginalArgs = join(' ',@ARGV) ; + #~ use warnings ; # strange warnings, todo # todo: second run of checksum of mp file with --nomprun changes @@ -48,6 +50,9 @@ use Getopt::Long; use Class::Struct; # needed for help subsystem use FindBin; use File::Compare; +use File::Temp; + +use IO::Handle; autoflush STDOUT 1; my %ConTeXtInterfaces; # otherwise problems with strict my %ResponseInterface; # since i dunno how to allocate else @@ -195,11 +200,15 @@ my $Paranoid = 0 ; my $NotParanoid = 0 ; my $BoxType = '' ; +my $TempDir = '' ; + my $StartLine = 0 ; my $StartColumn = 0 ; my $EndLine = 0 ; my $EndColumn = 0 ; +my $MpEngineSupport = 0 ; # not now, we also need to patch executemp in context itself + # makempy : my $MakeMpy = ''; @@ -303,6 +312,8 @@ my $MakeMpy = ''; "paranoid" => \$Paranoid, "notparanoid" => \$NotParanoid, "boxtype=s" => \$BoxType, # media art crop bleed trim + #### unix is unsafe (symlink viruses) + "tempdir=s" => \$TempDir, #### experiment "startline=s" => \$StartLine, "startcolumn=s" => \$StartColumn, @@ -409,7 +420,7 @@ if ( ( $LogFile ne '' ) && ( $LogFile =~ /\w+\.log$/io ) ) { *STDERR = *LOGFILE; } -my $Program = " TeXExec 5.2.5 - ConTeXt / PRAGMA ADE 1997-2005"; +my $Program = " TeXExec 5.3.2 - ConTeXt / PRAGMA ADE 1997-2005"; print "\n$Program\n\n"; @@ -742,7 +753,9 @@ my $MpVirginFlag = IniValue( 'MpVirginFlag', '-ini' ); my $MpPassString = IniValue( 'MpPassString', '' ); my $MpFormat = IniValue( 'MpFormat', $MetaFun ); my $MpFormatPath = IniValue( 'MpFormatPath', $TeXFormatPath ); -my $UseEnginePath = IniValue( 'UseEnginePath', $TheEnginePath); +my $UseEnginePath = IniValue( 'UseEnginePath', ''); + +if ($TheEnginePath) { $UseEnginePath = 1 } my $FmtLanguage = IniValue( 'FmtLanguage', '' ); my $FmtBodyFont = IniValue( 'FmtBodyFont', '' ); @@ -787,18 +800,20 @@ if ( $MpFormatFlag eq "" ) { $MpFormatFlag = "-mem=" ; } -if ($UseEnginePath && (! $MakeFormats)) { - $MpFormatFlag .= $MpExecutable . '/' ; - $TeXFormatFlag .= $TeXExecutable . '/' ; -} +#~ if ($UseEnginePath && (! $MakeFormats)) { + #~ if ($MpEngineSupport) { + #~ $MpFormatFlag .= $MpExecutable . '/' ; + #~ } ; + #~ $TeXFormatFlag .= $TeXExecutable . '/' ; +#~ } #~ if ( $TeXFormatFlag eq "" ) { $TeXFormatFlag = "&" } #~ if ( $MpFormatFlag eq "" ) { $MpFormatFlag = "&" } -unless ( $dosish && !$escapeshell ) { - if ( $TeXFormatFlag eq "&" ) { $TeXFormatFlag = "\\&" } - if ( $MpFormatFlag eq "&" ) { $MpFormatFlag = "\\&" } -} +#~ unless ( $dosish && !$escapeshell ) { + #~ if ( $TeXFormatFlag eq "&" ) { $TeXFormatFlag = "\\&" } + #~ if ( $MpFormatFlag eq "&" ) { $MpFormatFlag = "\\&" } +#~ } if ($TeXProgram) { $TeXExecutable = $TeXProgram } @@ -1490,6 +1505,10 @@ sub PrepRunTeX { if ( $TeXTranslation ne '' ) { $cmd .= "-translate-file=$TeXTranslation " } $cmd .= "$TeXFormatFlag$TeXFormatPath$Format $JobName.$JobSuffix $PipeString"; if ($Verbose) { print "\n$cmd\n\n" } +unless ( $dosish && ! $escapeshell ) { + #~ $cmd =~ s/[^\\]\&/\\\&/io ; + $cmd =~ s/([^\\])\&/$1\\\&/io ; +} return $cmd; } @@ -1497,6 +1516,7 @@ sub RunTeX { my ( $JobName, $JobSuffix ) = @_; my $StartTime = time; my $cmd = PrepRunTeX($JobName, $JobSuffix, ''); + print $cmd if ($Verbose) ; if ($EnterBatchMode) { $Problems = system("$cmd"); } else { @@ -1667,7 +1687,7 @@ sub CopyFile { # agressive copy, works for open files like in gs close(OUT); } -sub CheckChanges { # also tub +sub CheckMPChanges { my $JobName = shift; my $checksum = 0; my $MPJobName = MPJobName( $JobName, "mpgraph" ); @@ -1691,6 +1711,18 @@ sub CheckChanges { # also tub return $checksum; } +sub CheckTubChanges { + my $JobName = shift; + my $checksum = 0; + if ( open( TUB, "$JobName.tub" ) ) { + while () { + $checksum += do { unpack( "%32C*", ) % 65535 } + } + close(TUB); + } + return $checksum; +} + my $DummyFile = 0; sub isXMLfile { @@ -1854,9 +1886,12 @@ if ($JobSuffix =~ /\_fo$/i) { } print " TeX run : $TeXRuns\n\n"; my ( $mpchecksumbefore, $mpchecksumafter ) = ( 0, 0 ); - if ($AutoMPRun) { $mpchecksumbefore = CheckChanges($JobName) } + my ( $tubchecksumbefore, $tubchecksumafter ) = ( 0, 0 ); + if ($AutoMPRun) { $mpchecksumbefore = CheckMPChanges($JobName) } + $tubchecksumbefore = CheckTubChanges($JobName) ; $Problems = RunTeX( $JobName, $JobSuffix ); - if ($AutoMPRun) { $mpchecksumafter = CheckChanges($JobName) } + $tubchecksumafter = CheckTubChanges($JobName) ; + if ($AutoMPRun) { $mpchecksumafter = CheckMPChanges($JobName) } if ( ( !$Problems ) && ( $NOfRuns > 1 ) ) { if ( !$NoMPMode ) { $MPrundone = RunTeXMP( $JobName, "mpgraph" ); @@ -1868,6 +1903,9 @@ if ($JobSuffix =~ /\_fo$/i) { ( $StopRunning && ( $mpchecksumafter == $mpchecksumbefore ) ); } + $StopRunning = + ( $StopRunning + && ( $tubchecksumafter == $tubchecksumbefore ) ); } } if ( ( $NOfRuns == 1 ) && $ForceTeXutil ) { @@ -2043,7 +2081,7 @@ sub RunListing { foreach $FileName (@FileNames) { $CleanFileName = lc CleanTeXFileName($FileName); print LIS "\\page\n"; - print LIS "\\setupfootertexts[\\tttf $CleanFileName][\\tttf \pagenumber]\n"; + print LIS "\\setupfootertexts[\\tttf $CleanFileName][\\tttf \\pagenumber]\n"; print LIS "\\typefile\{$FileName\}\n"; } print LIS "\\stoptext\n"; @@ -2248,33 +2286,76 @@ sub RunCombine { sub LocatedFormatPath { my $FormatPath = shift; my $EnginePath = shift; + my $EngineDone = shift; if ( ( $FormatPath eq '' ) && ( $kpsewhich ne '' ) ) { - $FormatPath = `$kpsewhich --expand-var=\$TEXFORMATS` ; - chomp $FormatPath; + unless ($EngineDone) { + my $str = $ENV{"TEXFORMATS"} ; + $str =~ s/\$ENGINE//io ; + $ENV{"TEXFORMATS"} = $str ; + } + # expanded paths + print " assuming engine : $EnginePath\n"; + if (($UseEnginePath)&&($EngineDone)) { + $FormatPath = `$kpsewhich --engine=$EnginePath --show-path=fmt` ; + } else { + $FormatPath = `$kpsewhich --show-path=fmt` ; + } + chomp $FormatPath ; + if ( ( $FormatPath ne '' ) && $Verbose ) { + print "located formatpath (1) : $FormatPath\n"; + } + # fall back if ($FormatPath eq '') { - $FormatPath = `$kpsewhich --show-path=fmt`; - chomp $FormatPath; + if (($UseEnginePath)&&($EngineDone)) { + if ($dosish) { + $FormatPath = `$kpsewhich --engine=$EnginePath --expand-var=\$TEXFORMATS` ; + } else { + $FormatPath = `$kpsewhich --engine=$EnginePath --expand-var=\\\$TEXFORMATS` ; + } + } else { + if ($dosish) { + $FormatPath = `$kpsewhich --expand-var=\$TEXFORMATS` ; + } else { + $FormatPath = `$kpsewhich --expand-var=\\\$TEXFORMATS` ; + } + } + } + chomp $FormatPath ; + if ( ( $FormatPath ne '' ) && $Verbose ) { + print "located formatpath (2) : $FormatPath\n"; } - $FormatPath =~ s/\.+\;//o; # should be a sub - $FormatPath =~ s/\;.*//o; - $FormatPath =~ s/\!//go; + # + if ( ( $FormatPath ne '' ) && ($FormatPath =~ /unsetengine/) ) { + $FormatPath =~ s/unsetengine/$EnginePath/; + if ( ( $FormatPath ne '' ) && $Verbose ) { + print "located formatpath (!) : $FormatPath (unbugged)\n"; + } + } + # take first one + if ($dosish) { + $FormatPath =~ s/\;.*//o; + } else { + $FormatPath =~ s/\:.*//o; + } + # remove clever things + $FormatPath =~ s/[\!\{\}\,]//go; $FormatPath =~ s/\\/\//go; $FormatPath =~ s/\/\//\//go; $FormatPath =~ s/[\/\\]$//; if ( ( $FormatPath ne '' ) && $Verbose ) { - print " located formatpath : $FormatPath\n"; + print "located formatpath (3) : $FormatPath\n"; } $FormatPath .= '/'; } - if ($UseEnginePath && ($FormatPath ne '' && ($FormatPath !~ /$EnginePath\/$/))) { + if ($UseEnginePath && $EngineDone && ($FormatPath ne '') && ($FormatPath !~ /$EnginePath\/$/)) { $FormatPath .= $EnginePath ; unless (-d $FormatPath) { mkdir $FormatPath ; } $FormatPath .= '/' ; } - + print " using formatpath : $FormatPath\n" if $Verbose ; return $FormatPath; } @@ -2300,7 +2381,7 @@ sub RunOneFormat { $TeXPrefix = "*"; } my $CurrentPath = cwd(); - my $TheTeXFormatPath = LocatedFormatPath($TeXFormatPath, $TeXExecutable); + my $TheTeXFormatPath = LocatedFormatPath($TeXFormatPath, $TeXExecutable,1); if ( $TheTeXFormatPath ne '' ) { chdir $TheTeXFormatPath } MakeUserFile; MakeResponseFile; @@ -2314,6 +2395,12 @@ sub RunOneFormat { RestoreUserFile; if ( ( $TheTeXFormatPath ne '' ) && ( $CurrentPath ne '' ) ) { + print "\n"; + if ($UseEnginePath) { + print " used engineformatpath : $TheTeXFormatPath\n"; + } else { + print " used formatpath : $TheTeXFormatPath\n"; + } chdir $CurrentPath; } } @@ -2355,31 +2442,110 @@ sub RunFormats { } sub RunMpFormat { + # engine is not supported by MP my $MpFormat = shift; return if ( $MpFormat eq '' ); my $CurrentPath = cwd(); - my $TheMpFormatPath = LocatedFormatPath($MpFormatPath, $MpExecutable); + my $TheMpFormatPath = LocatedFormatPath($MpFormatPath,$MpExecutable,$MpEngineSupport); if ( $TheMpFormatPath ne '' ) { chdir $TheMpFormatPath } $own_quote = ($MpExecutable =~ m/^[^\"].* / ? "\"" : "") ; my $cmd = "$own_quote$MpExecutable$own_quote $MpVirginFlag $MpPassString $MpFormat"; + +unless ( $dosish && !$escapeshell ) { + $cmd =~ s/[^\\]\&/\\\&/io ; +} + if ($Verbose) { print "\n$cmd\n\n" } system($cmd ) ; if ( ( $TheMpFormatPath ne '' ) && ( $CurrentPath ne '' ) ) { + print "\n"; + #~ if ($UseEnginePath) { + #~ print " used engineformatpath : $TheMpFormatPath\n"; + #~ } else { + print " used formatpath : $TheMpFormatPath\n"; + #~ } chdir $CurrentPath; } } + +my $dir = File::Temp::tempdir(CLEANUP=>1) ; +my ($fh, $filename) = File::Temp::tempfile(DIR=>$dir, UNLINK=>1); + +sub checktexformatpath { + # engine support is either broken of not implemented in some + # distributions, so we need to take care of it ourselves + my $texformats ; + if (defined($ENV{'TEXFORMATS'})) { + $texformats = $ENV{'TEXFORMATS'} ; + } else{ + $texformats = '' ; + } + if ($texformats eq '') { + if ($dosish) { + $texformats = `kpsewhich --expand-var=\$TEXFORMATS`.chomp ; + } else { + $texformats = `kpsewhich --expand-var=\\\$TEXFORMATS`.chomp ; + } + } + if ($texformats !~ /web2c[\/\\].*\$ENGINE/) { + $texformats =~ s/web2c/web2c\/{\$ENGINE,}/ ; + $ENV{'TEXFORMATS'} = $texformats ; + print " fixing texformat path : $ENV{'TEXFORMATS'}\n"; + } + if (! defined($ENV{'ENGINE'})) { + if ($MpEngineSupport) { + $ENV{'ENGINE'} .= $MpExecutable ; + } ; + $ENV{'ENGINE'} = $TeXExecutable ; + } +} + sub RunFiles { my $currentpath = cwd() ; + my $oldrunpath = $RunPath ; + # new + checktexformatpath ; # test if current path is writable if (! -w "$currentpath") { print " current path readonly : $currentpath\n"; - if ($ENV{"TEMP"} && -e $ENV{"TEMP"}) { - $RunPath = $ENV{"TEMP"} ; - } elsif ($ENV{"TMP"} && -e $ENV{"TMP"}) { - $RunPath = $ENV{"TMP"} ; + # + # we cannot use the following because then the result will + # also be removed and users will not know where to look + # + # $RunPath = File::Temp::tempdir(CLEANUP=>1) ; + # if ($RunPath) { + # print " using temp path : $RunPath\n"; + # } else { + # print " problematic temp path : $currentpath\n"; + # exit ; + # } + # + foreach my $d ($ENV{"TMPDIR"},$ENV{"TEMP"},$ENV{"TMP"},"/tmp") { + if ($d && -e $d) { $RunPath = $d ; last ; } + } + if ($TempDir eq '') { + print " provide temp path for : $RunPath\n"; + exit ; + } elsif ($RunPath ne $oldrunpath) { + chdir ($RunPath) ; + unless (-e $TempDir) { + print " creating texexec path : $TempDir\n"; + mkdir ("$TempDir", 077) + } + if (-e $TempDir) { + $RunPath += $TempDir ; + } else { + # we abort this run because on unix an invalid tmp + # path can be an indication of a infected system + print " problematic temp path : $RunPath\n"; + exit ; + } + } else { + print " no writable temp path : $RunPath\n"; + exit ; } } # test if we need to change paths @@ -2640,6 +2806,7 @@ sub doRunMP { ########### } # prevent nameclash, experimental my $MpMpName = "$MpName"; + print "$cmd $MpMpName" if ($Verbose) ; $Problems = system("$cmd $MpMpName"); open( MPL, "$MpName.log" ); while () # can be one big line unix under win @@ -2732,45 +2899,6 @@ sub load_set_file { if ( $SetFile ne "" ) { load_set_file( $SetFile, $Verbose ) } -# todo : more consistent argv handling -# -# sub ifargs -# { $problems = (@ARGV==0) ; -# if ($problems) -# { print " warning : nothing to do\n" } -# return $problems } - -# sub check_texmf_root -# { return if ($TeXRoot eq "") ; -# my $root = $TeXRoot ; -# $root =~ s/\\/\//goi ; -# if (-d $root) -# { print " using tex root : $root \n" ; -# $ENV{TEXROOT} = $root ; -# $ENV{TEXMFCNF} = "$root/texmf-local/web2c" ; -# $ENV{TEXMFFONTS} = "$root/texmf-fonts" ; -# $ENV{TEXMFLOCAL} = "$root/texmf-local" ; -# $ENV{TEXMFMAIN} = "$root/texmf" } -# else -# { print " invalid tex root : $root \n" } } -# -# sub check_texmf_tree -# { return if ($TeXTree eq "") ; -# my $tree = $TeXTree ; -# unless (-d $tree) -# { $tree = $ENV{TEXMFLOCAL} ; -# $tree =~ s/texmf.*//io ; -# $tree .= $TeXTree } -# if (-d $tree) -# { print " using texmf tree : $tree \n" ; -# $ENV{TEXMFPROJECT} = $tree ; -# if ((-f "$tree/web2c/cont-en.efmt")|| -# (-f "$tree/web2c/cont-nl.efmt")) -# { $ENV{TEXFORMATS} = "$tree/web2c" } -# $ENV{TEXMF} = '{$TEXMFPROJECT,$TEXMFFONTS,$TEXMFLOCAL,!!$TEXMFMAIN}' } -# else -# { print " invalid texmf tree : $tree \n" } } - sub check_texmf_root { } sub check_texmf_tree { } @@ -2834,6 +2962,7 @@ if ($HelpAsked) { show_help_info } elsif ($Version) { show_version_info + #~ system("texmfstart ctxtools --check $OriginalArgs") ; } elsif ($TypesetListing) { check_texmf_root; check_texmf_tree; @@ -2859,23 +2988,12 @@ if ($HelpAsked) { else { RunFormats ; } + #~ system("texmfstart ctxtools $OriginalArgs") ; } elsif (@ARGV) { check_texmf_root; check_texmf_tree; @ARGV = <@ARGV>; RunFiles; -#~ } else { - #~ if ($Modules ne "") { # kind of fall back: texexec --use=set-02 --pdf - #~ my @tmp = split(',', $Modules) ; - #~ @ARGV[0] = @tmp[0] ; - #~ } - #~ if (@ARGV) { - #~ check_texmf_root; - #~ check_texmf_tree; - #~ RunFiles; - #~ } elsif ( !$HelpAsked ) { - #~ show_help_options; -#~ } } } elsif ( !$HelpAsked ) { show_help_options; } diff --git a/scripts/context/perl/texexec.rme b/scripts/context/perl/texexec.rme index 419d1faec..f38b4be7f 100644 --- a/scripts/context/perl/texexec.rme +++ b/scripts/context/perl/texexec.rme @@ -51,7 +51,7 @@ set TeXShell to tetex % % Here are some general defaults. They can be overruled later. -set UseEnginePath to false +set UseEnginePath to true set UsedInterfaces to en nl metafun mptopdf set UserInterface to en diff --git a/scripts/context/perl/texfont.pl b/scripts/context/perl/texfont.pl index 71433cb56..732fb0f3e 100644 --- a/scripts/context/perl/texfont.pl +++ b/scripts/context/perl/texfont.pl @@ -144,7 +144,7 @@ my $remove = 0 ; my $expert = 0 ; my $trace = 0 ; my $afmpl = 0 ; -my $trees = 'TEXMFFONTS,TEXMFLOCAL,TEXMFEXTRA,TEXMFMAIN' ; +my $trees = 'TEXMFFONTS,TEXMFLOCAL,TEXMFEXTRA,TEXMFMAIN,TEXMFDIST' ; my $pattern = '' ; my $fontsuffix = "" ; @@ -649,12 +649,13 @@ sub UnLink sub globafmfiles { my ($runpath, $pattern) = @_ ; my @files = validglob("$runpath/$pattern.afm") ; + report("locating afm files : using pattern $runpath/$pattern.afm"); if ($preproc && !$lcdf) { @files = validglob("$runpath/$pattern.*tf") ; - report("locating otf files : using pattern $pattern"); + report("locating otf files : using pattern $runpath/$pattern.*tf"); unless (@files) { @files = validglob("$sourcepath/$pattern.ttf") ; - report("locating ttf files : using pattern $pattern") } + report("locating ttf files : using pattern $sourcepath/$pattern.ttf") } } if (@files) # also elsewhere { report("locating afm files : using pattern $pattern") } @@ -758,13 +759,9 @@ unless ($tex) { report "warning : can't open $texfile" } if ($map) { print MAP "% This file is generated by the TeXFont Perl script.\n" ; print MAP "%\n" ; - print MAP "% You need to add the following line to pdftex.cfg:\n" ; + print MAP "% You need to add the following line to your file:\n" ; print MAP "%\n" ; - print MAP "% map +$mapfile\n" ; - print MAP "%\n" ; - print MAP "% Alternatively in your TeX source you can say:\n" ; - print MAP "%\n" ; - print MAP "% \\pdf \{+$mapfile\}\n" ; + print MAP "% \\pdfmapfile{+$mapfile}\n" ; print MAP "%\n" ; print MAP "% In ConTeXt you can best use:\n" ; print MAP "%\n" ; diff --git a/scripts/context/perl/texutil.pl b/scripts/context/perl/texutil.pl index 025fe8946..d5e6ef9ea 100644 --- a/scripts/context/perl/texutil.pl +++ b/scripts/context/perl/texutil.pl @@ -134,11 +134,38 @@ my $dosish = ($Config{'osname'} =~ /^(ms)?dos|^os\/2|^(ms|cyg)win/i) ; "verbose" => \$ProcessVerbose, "interface=s" => \$UserInterface) ; +# A bit old, this code, could be an array. Anyhow, we will +# replace texutil soon. + +$InputFile = "@ARGV" ; # niet waterdicht + #D We need some hacks to suppress terminal output. This #D piece of code is based on page~193 of "Programming Perl". $ProgramLog = "texutil.log" ; +# Well, it seems that unix' symlinks are sensitive for being +# hijacked. The assumption is that a known file can be a problem. +# Of course when one knows that certains files are processed, +# the names are always known and hijacking can always take +# place. But let's use a slightly less predictable name here: +# +# if ((@ARGV[0]) && (@ARGV[0] ne "")) { +# $ProgramLog = "@ARGV[0]-$ProgramLog" ; +# } else { +# # no need to be silent +# $ProcessSilent = 0 ; +# } +# +# or better, let's drop this feature, since i'm not in the mood +# now to test hacks like this (i'll just wait till the age of +# computer anarchy has ended). + +$ProgramLog = "/dev/null" ; + +# Maybe we should just write to the nul device. (In the rewritten +# version I can treat unix more strick.) + sub RedirectTerminal { open SAVEDSTDOUT, ">&STDOUT" ; open STDOUT, ">$ProgramLog" ; @@ -166,8 +193,6 @@ sub CloseTerminal #D subroutine below is therefore only needed when no file or #D pattern is given. -$InputFile = "@ARGV" ; # niet waterdicht - sub CompFileName { my ($a,$b) = @_ ; my ($fa,$sa) = split(/\./,$a) ; @@ -1360,6 +1385,7 @@ sub FlushSavedLine print TUO "\\registerto$SavedTo" } else { print TUO "\\registerpage$SavedFrom" } } + $SavedHow = "" ; $SavedFrom = "" ; $SavedTo = "" ; $SavedEntry = "" } @@ -1383,6 +1409,7 @@ sub FlushRegisters $SavedFrom = "" ; $SavedTo = "" ; $SavedEntry = "" ; + $SavedHow = "" ; for ($n=1 ; $n<=$NOfEntries ; ++$n) { ($Class, $LCKey, $Key, $Entry, $TextHow, $RegisterState, @@ -1393,7 +1420,6 @@ sub FlushRegisters # if ($SortN) { $AlfKey = $Key ; -# $AlfKey =~ s/(.).*\x00(.).*/$1$2/o ; $AlfKey =~ s/(.).*\x00(.).*/$2/o ; if (defined($ALF{$AlfKey})) { $TestAlfa = $ALF{$AlfKey} } } @@ -1445,11 +1471,16 @@ sub FlushRegisters print TUO "\\registersee{$Class}{$PageHow,$TextHow}{$SeeToo}{$Page}\n" ; $LastPage = $Page ; $LastRealPage = $RealPage } - elsif (($Copied) || - ! (($LastPage eq $Page) and ($LastRealPage eq $RealPage))) + else { +if (($SavedHow ne $PageHow) && ($PageHow ne "")) { + # last valid page attribute counts + $SavedHow = $PageHow ; +} + if (($Copied) || ! (($LastPage eq $Page) && ($LastRealPage eq $RealPage))) { # print "$LastPage / $Page // $LastRealPage / $RealPage\n" ; $NextEntry = "{$Class}{$PreviousA}{$PreviousB}{$PreviousC}{$PageHow,$TextHow}" ; - $SavedLine = "{$Class}{$PageHow,$TextHow}{$Location}{$Page}{$RealPage}\n" ; + #~ $SavedLine = "{$Class}{$PageHow,$TextHow}{$Location}{$Page}{$RealPage}\n" ; + $SavedLine = "{$Class}{$SavedHow,$TextHow}{$Location}{$Page}{$RealPage}\n" ; if ($RegisterState eq $RegStat{"f"}) { FlushSavedLine ; print TUO "\\registerfrom$SavedLine" } @@ -1468,7 +1499,7 @@ sub FlushRegisters } ++$NOfSanePages ; $LastPage = $Page ; - $LastRealPage = $RealPage } } + $LastRealPage = $RealPage } } } FlushSavedLine ; @@ -2205,10 +2236,10 @@ sub HandlePdfFigure { if (($MediaBoxFound < 2) && ($SomeLine =~ /\/ArtBox\s*\[/io)) { $MediaBoxFound = 3 ; $MediaBox = $SomeLine } - elsif (($MediaBoxFound < 2) && ($SomeLine =~ /\/CropBox\s*\[ /io)) + elsif (($MediaBoxFound < 2) && ($SomeLine =~ /\/CropBox\s*\[/io)) { $MediaBoxFound = 2 ; $MediaBox = $SomeLine } - elsif (($MediaBoxFound == 0) && ($SomeLine =~ /\/MediaBox\s*\[ /io)) + elsif (($MediaBoxFound == 0) && ($SomeLine =~ /\/MediaBox\s*\[/io)) { $MediaBoxFound = 1 ; $MediaBox = $SomeLine } } } close ( PDF ) ; diff --git a/scripts/context/ruby/base/file.rb b/scripts/context/ruby/base/file.rb new file mode 100644 index 000000000..b7170d555 --- /dev/null +++ b/scripts/context/ruby/base/file.rb @@ -0,0 +1,133 @@ +require 'ftools' + +class File + + def File.suffixed(name,sufa,sufb=nil) + if sufb then + unsuffixed(name) + "-#{sufa}.#{sufb}" + else + unsuffixed(name) + ".#{sufa}" + end + end + + def File.unsuffixed(name) + name.sub(/\.[^\.]*?$/o, '') + end + + def File.suffix(name,default='') + if name =~ /\.([^\.]*?)$/o then + $1 + else + default + end + end + + def File.splitname(name,suffix='') + if name =~ /(.*)\.([^\.]*?)$/o then + [$1, $2] + else + [name, suffix] + end + end + +end + +class File + + def File.silentopen(name,method='r') + begin + f = File.open(name,method) + rescue + return nil + else + return f + end + end + + def File.silentread(name) + begin + data = IO.read(name) + rescue + return nil + else + return data + end + end + + def File.atleast?(name,n=0) + begin + size = FileTest.size(name) + rescue + return false + else + return size > n + end + end + + def File.appended(name,str='') + if FileTest.file?(name) then + begin + if f = File.open(name,'a') then + f << str + f.close + return true + end + rescue + end + end + return false + end + + def File.written(name,str='') + begin + if f = File.open(name,'w') then + f << str + f.close + return true + end + rescue + end + return false + end + + def File.silentdelete(filename) + begin File.delete(filename) ; rescue ; end + end + + def File.silentcopy(oldname,newname) + return if File.expand_path(oldname) == File.expand_path(newname) + begin File.copy(oldname,newname) ; rescue ; end + end + + def File.silentrename(oldname,newname) + # in case of troubles, we just copy the file; we + # maybe working over multiple file systems or + # apps may have mildly locked files (like gs does) + return if File.expand_path(oldname) == File.expand_path(newname) + begin File.delete(newname) ; rescue ; end + begin + File.rename(oldname,newname) + rescue + begin File.copy(oldname,newname) ; rescue ; end + end + end + +end + +class File + + # handles "c:\tmp\test.tex" as well as "/${TEMP}/test.tex") + + def File.unixfied(filename) + begin + str = filename.gsub(/\$\{*([a-z0-9\_]+)\}*/oi) do + if ENV.key?($1) then ENV[$1] else $1 end + end + str.gsub(/[\/\\]+/o, '/') + rescue + filename + end + end + +end + diff --git a/scripts/context/ruby/base/kpse.rb b/scripts/context/ruby/base/kpse.rb new file mode 100644 index 000000000..6dbd6e6cf --- /dev/null +++ b/scripts/context/ruby/base/kpse.rb @@ -0,0 +1,270 @@ +# module : base/kpse +# copyright : PRAGMA Advanced Document Engineering +# version : 2002-2005 +# author : Hans Hagen +# +# project : ConTeXt / eXaMpLe +# concept : Hans Hagen +# info : j.hagen@xs4all.nl +# www : www.pragma-ade.com + +# rename this one to environment + +module Kpse + + @@located = Hash.new + @@paths = Hash.new + @@scripts = Hash.new + @@formats = ['tex','texmfscripts','other text files'] + @@progname = 'context' + @@ownpath = $0.sub(/[\\\/][a-z0-9\-]*?\.rb/i,'') + @@problems = false + @@tracing = false + @@distribution = 'web2c' + @@crossover = true + @@mswindows = Config::CONFIG['host_os'] =~ /mswin/ + + # check first in bin path + + ENV['PATH'].split(File::PATH_SEPARATOR).each do |path| + @@distribution = 'miktex' if path =~ /miktex/o + end + + # if @@crossover then + # ENV.keys.each do |k| + # case k + # when /\_CTX\_KPSE\_V\_(.*?)\_/io then @@located[$1] = ENV[k].dup + # when /\_CTX\_KPSE\_P\_(.*?)\_/io then @@paths [$1] = ENV[k].dup.split(';') + # when /\_CTX\_KPSE\_S\_(.*?)\_/io then @@scripts[$1] = ENV[k].dup + # end + # end + # end + + if @@crossover then + ENV.keys.each do |k| + case k + when /\_CTX\_KPSE\_V\_(.*?)\_/io then @@located[$1] = ENV[k].dup + when /\_CTX\_KPSE\_P\_(.*?)\_/io then @@paths [$1] = ENV[k].dup.split(';') + when /\_CTX\_KPSE\_S\_(.*?)\_/io then @@scripts[$1] = ENV[k].dup + end + end + end + + def Kpse.inspect + @@located.keys.sort.each do |k| puts("located : #{k} -> #{@@located[k]}\n") end + @@paths .keys.sort.each do |k| puts("paths : #{k} -> #{@@paths [k]}\n") end + @@scripts.keys.sort.each do |k| puts("scripts : #{k} -> #{@@scripts[k]}\n") end + end + + def Kpse.distribution + @@distribution + end + + def Kpse.found(filename, progname=nil, format=nil) + begin + tag = Kpse.key(filename) # all + if @@located.key?(tag) then + return @@located[tag] + elsif FileTest.file?(filename) then + setvariable(tag,filename) + return filename + elsif FileTest.file?(File.join(@@ownpath,filename)) then + setvariable(tag,File.join(@@ownpath,filename)) + return @@located[tag] + else + [progname,@@progname].flatten.compact.uniq.each do |prg| + [format,@@formats].flatten.compact.uniq.each do |fmt| + begin + tag = Kpse.key(filename,prg,fmt) + if @@located.key?(tag) then + return @@located[tag] + elsif p = Kpse.kpsewhich(filename,prg,fmt) then + setvariable(tag,p.chomp) + return @@located[tag] + end + rescue + end + end + end + setvariable(tag,filename) + return filename + end + rescue + filename + end + end + + def Kpse.kpsewhich(filename,progname,format) + Kpse.run("-progname=#{progname} -format=\"#{format}\" #{filename}") + end + + def Kpse.which + Kpse.Kpsewhich + end + + def Kpse.run(arguments) + puts arguments if @@tracing + begin + if @@problems then + results = '' + else + results = `kpsewhich #{arguments}`.chomp + end + rescue + puts "unable to run kpsewhich" if @@tracing + @@problems, results = true, '' + end + puts results if @@tracing + return results + end + + def Kpse.formatpaths + unless @@paths.key?('formatpaths') then + begin + setpath('formatpaths',run("--show-path=fmt").gsub(/\\/,'/').split(File::PATH_SEPARATOR)) + rescue + setpath('formatpaths',[]) + end + end + return @@paths['formatpaths'] + end + + def Kpse.key(filename='',progname='all',format='all') + [progname,format,filename].join('-') + end + + def Kpse.formatpath(engine='pdfetex',enginepath=true) + + unless @@paths.key?(engine) then + # overcome a bug/error in web2c/distributions/kpse + if ENV['TEXFORMATS'] then + ENV['TEXFORMATS'] = ENV['TEXFORMATS'].sub(/\$ENGINE/io,'') + end + # use modern method + if enginepath then + formatpath = run("--engine=#{engine} --show-path=fmt") + else + formatpath = run("--show-path=fmt") + end + # use ancient method + if formatpath.empty? then + if enginepath then + if @@mswindows then + formatpath = run("--engine=#{engine} --expand-var=\$TEXFORMATS") + else + formatpath = run("--engine=#{engine} --expand-var=\\\$TEXFORMATS") + end + else + if enginepath then + formatpath = run("--expand-var=\$TEXFORMATS") + else + formatpath = run("--expand-var=\\\$TEXFORMATS") + end + end + end + # overcome a bug/error in web2c/distributions/kpse + formatpath.sub!(/unsetengine/, engine) + # take first one + if ! formatpath.empty? then + formatpath = formatpath.split(File::PATH_SEPARATOR).first + # remove clever things + formatpath.gsub!(/[\!\{\}\,]/, '') + unless formatpath.empty? then + if enginepath then + newformatpath = File.join(formatpath,engine).gsub(/[\/\\]+/, '/') + if FileTest.directory?(newformatpath) then + formatpath = newformatpath + else + begin + File.makedirs(newformatpath) + rescue + else + formatpath = newformatpath if FileTest.directory?(newformatpath) + end + end + else + formatpath = formatpath.gsub(/[\/\\]+/, '/').gsub(/\/$/, '') + end + end + end + setpath(engine,formatpath) + end + return @@paths[engine] + end + + def Kpse.update + case @@distribution + when 'miktex' then + system('initexmf --update-fndb') + else + # always mktexlsr anyway + + end + system('mktexlsr') + end + + def Kpse.distribution + ENV['PATH'].split(File::PATH_SEPARATOR).each do |path| + if path =~ /miktex/ then + return 'miktex' + end + end + return 'web2c' + end + + def Kpse.miktex? + distribution == 'miktex' + end + + def Kpse.web2c? + distribution == 'web2c' + end + + # engine support is either broken of not implemented in some + # distributions, so we need to take care of it ourselves + + def Kpse.fixtexmfvars(engine=nil) + ENV['ENGINE'] = engine if engine + texformats = if ENV['TEXFORMATS'] then ENV['TEXFORMATS'].dup else '' end + if @@mswindows then + texformats = `kpsewhich --expand-var=\$TEXFORMATS`.chomp if texformats.empty? + else + texformats = `kpsewhich --expand-var=\\\$TEXFORMATS`.chomp if texformats.empty? + end + if texformats !~ /web2c[\/\\].*\$ENGINE/ then + ENV['TEXFORMATS'] = texformats.gsub(/web2c/, "web2c/{\$ENGINE,}") + return true + else + return false + end + + end + + def Kpse.runscript(name,filename=[],options=[]) + setscript(name,`texmfstart --locate #{name}`) unless @@scripts.key?(name) + system("#{@@scripts[name]} #{[options].flatten.join(' ')} #{[filename].flatten.join(' ')}") + end + + def Kpse.pipescript(name,filename=[],options=[]) + setscript(name,`texmfstart --locate #{name}`) unless @@scripts.key?(name) + `#{@@scripts[name]} #{[options].flatten.join(' ')} #{[filename].flatten.join(' ')}` + end + + private + + def Kpse.setvariable(key,value) + @@located[key] = value + ENV["_CTX_K_V_#{key}_"] = @@located[key] if @@crossover + end + + def Kpse.setscript(key,value) + @@scripts[key] = value + ENV["_CTX_K_S_#{key}_"] = @@scripts[key] if @@crossover + end + + def Kpse.setpath(key,value) + @@paths[key] = [value].flatten.uniq + ENV["_CTX_K_P_#{key}_"] = @@paths[key].join(';') if @@crossover + end + +end diff --git a/scripts/context/ruby/base/logger.rb b/scripts/context/ruby/base/logger.rb new file mode 100644 index 000000000..2526cdb0e --- /dev/null +++ b/scripts/context/ruby/base/logger.rb @@ -0,0 +1,104 @@ +# module : base/logger +# copyright : PRAGMA Advanced Document Engineering +# version : 2002-2005 +# author : Hans Hagen +# +# project : ConTeXt / eXaMpLe +# concept : Hans Hagen +# info : j.hagen@xs4all.nl +# www : www.pragma-ade.com + +require 'thread' + +# The next calls are valid: + +# @log.report('a','b','c', 'd') +# @log.report('a','b',"c #{d}") +# @log.report("a b c #{d}") + +# Keep in mind that "whatever #{something}" is two times faster than +# 'whatever ' + something or ['whatever',something].join and that +# when verbosity is not needed the following is much faster too: + +# @log.report('a','b','c', 'd') if @log.verbose? +# @log.report('a','b',"c #{d}") if @log.verbose? +# @log.report("a b c #{d}") if @log.verbose? + +# The last three cases are equally fast when verbosity is turned off. + +# Under consideration: verbose per instance + +class Logger + + @@length = 0 + @@verbose = false + + def initialize(tag=nil,length=0,verbose=false) + @tag = tag || '' + @@verbose = @@verbose || verbose + @@length = @tag.length if @tag.length > @@length + @@length = length if length > @@length + end + + def report(*str) + begin + case str.length + when 0 + print("\n") + return true + when 1 + message = str.first + else + message = [str].flatten.collect{|s| s.to_s}.join(' ').chomp + end + if @tag.empty? then + print("#{message}\n") + else + # try to avoid too many adjustments + @tag = @tag.ljust(@@length) unless @tag.length == @@length + print("#{@tag} | #{message}\n") + end + rescue + end + return true + end + + def reportlines(*str) + unless @tag.empty? then + @tag = @tag.ljust(@@length) unless @tag.length == @@length + end + report([str].flatten.collect{|s| s.gsub(/\n/,"\n#{@tag} | ")}.join(' ')) + end + + def debug(*str) + report(str) if @@verbose + end + + def error(*str) + if ! $! || $!.to_s.empty? then + report(str) + else + report(str,$!) + end + end + + def verbose + @@verbose = true + end + + def silent + @@verbose = false + end + + def verbose? + @@verbose + end + + # attr_reader :tag + + # alias fatal error + # alias info debug + # alias warn debug + # alias debug? :verbose? + +end diff --git a/scripts/context/ruby/base/pdf.rb b/scripts/context/ruby/base/pdf.rb new file mode 100644 index 000000000..49d177b5d --- /dev/null +++ b/scripts/context/ruby/base/pdf.rb @@ -0,0 +1,53 @@ +module PDFview + + @files = Hash.new + + def PDFview.open(*list) + begin + [*list].flatten.each do |file| + filename = fullname(file) + if FileTest.file?(filename) then + result = `pdfopen --file #{filename} 2>&1` + @files[filename] = true + end + end + rescue + end + end + + def PDFview.close(*list) + [*list].flatten.each do |file| + filename = fullname(file) + begin + if @files.key?(filename) then + result = `pdfclose --file #{filename} 2>&1` + else + closeall + return + end + rescue + end + @files.delete(filename) + end + end + + def PDFview.closeall + begin + result = `pdfclose --all 2>&1` + rescue + end + @files.clear + end + + def PDFview.fullname(name) + name + if name =~ /\.pdf$/ then '' else '.pdf' end + end + +end + +puts ('' || false) + +# PDFview.open("t:/document/show-exa.pdf") +# PDFview.open("t:/document/show-gra.pdf") +# PDFview.close("t:/document/show-exa.pdf") +# PDFview.close("t:/document/show-gra.pdf") diff --git a/scripts/context/ruby/base/state.rb b/scripts/context/ruby/base/state.rb new file mode 100644 index 000000000..f57231592 --- /dev/null +++ b/scripts/context/ruby/base/state.rb @@ -0,0 +1,75 @@ +require "md5" + +# todo: register omissions per file + +class FileState + + def initialize + @states = Hash.new + @omiter = Hash.new + end + + def reset + @states.clear + @omiter.clear + end + + def register(filename,omit=nil) + unless @states.key?(filename) then + @states[filename] = Array.new + @omiter[filename] = omit + end + @states[filename] << checksum(filename,@omiter[filename]) + end + + def update(filename=nil) + [filename,@states.keys].flatten.compact.uniq.each do |fn| + register(fn) + end + end + + def inspect(filename=nil) + result = '' + [filename,@states.keys].flatten.compact.uniq.sort.each do |fn| + if @states.key?(fn) then + result += "#{fn}: #{@states[fn].inspect}\n" + end + end + result + end + + def changed?(filename) + if @states.key?(filename) then + n = @states[filename].length + if n>1 then + changed = @states[filename][n-1] != @states[filename][n-2] + else + changed = true + end + else + changed = true + end + return changed + end + + def checksum(filename,omit=nil) + sum = '' + begin + if FileTest.file?(filename) && (data = IO.read(filename)) then + data.gsub!(/\n.*?(#{[omit].flatten.join('|')}).*?\n/ms,"\n") if omit + sum = MD5.new(data).hexdigest.upcase + end + rescue + sum = '' + end + return sum + end + + def stable? + @states.keys.each do |s| + return false if changed?(s) + end + return true + end + +end diff --git a/scripts/context/ruby/base/switch.rb b/scripts/context/ruby/base/switch.rb new file mode 100644 index 000000000..d011a78ae --- /dev/null +++ b/scripts/context/ruby/base/switch.rb @@ -0,0 +1,599 @@ +# module : base/switch +# copyright : PRAGMA Advanced Document Engineering +# version : 2002-2005 +# author : Hans Hagen +# +# project : ConTeXt / eXaMpLe +# concept : Hans Hagen +# info : j.hagen@xs4all.nl +# www : www.pragma-ade.com + +# we cannot use getoptlong because we want to be more +# tolerant; also we want to be case insensitive (2002). + +# we could make each option a class itself, but this is +# simpler; also we can put more in the array + +# beware: regexps/o in methods are optimized globally + +require "rbconfig" + +$mswindows = Config::CONFIG['host_os'] =~ /mswin/ +$separator = File::PATH_SEPARATOR + +class String + + def has_suffix?(suffix) + self =~ /\.#{suffix}$/i + end + +end + +# may move to another module + +class File + + def File.needsupdate(oldname,newname) + begin + if $mswindows then + return File.stat(oldname).mtime > File.stat(newname).mtime + else + return File.stat(oldname).mtime != File.stat(newname).mtime + end + rescue + return true + end + end + + def File.syncmtimes(oldname,newname) + begin + if $mswindows then + # does not work (yet) + t = File.mtime(oldname) # i'm not sure if the time is frozen, so we do it here + File.utime(0,t,oldname,newname) + else + t = File.mtime(oldname) # i'm not sure if the time is frozen, so we do it here + File.utime(0,t,oldname,newname) + end + rescue + end + end + + def File.timestamp(name) + begin + "#{File.stat(name).mtime}" + rescue + return 'unknown' + end + end + +end + +# main thing + +module CommandBase + + # this module can be used as a mixin in a command handler + + $stdout.sync = true + + def initialize(commandline,logger,banner) + @commandline, @logger, @banner = commandline, logger, banner + @forcenewline, @versiondone = false, false + version if @commandline.option('version') + end + + def reportlines(*str) + @logger.reportlines(str) + end + + # only works in 1.8 + # + # def report(*str) + # @logger.report(str) + # end + # + # def version # just a bit of playing with defs + # report(@banner.join(' - ')) + # def report(*str) + # @logger.report + # @logger.report(str) + # def report(*str) + # @logger.report(str) + # end + # end + # def version + # end + # end + + def report(*str) + initlogger ; @logger.report(str) + end + + def debug(*str) + initlogger ; @logger.debug(str) + end + + def error(*str) + initlogger ; @logger.error(str) + end + + def initlogger + if @forcenewline then + @logger.report + @forcenewline = false + end + end + + def logger + @logger + end + + def version # just a bit of playing with defs + unless @versiondone then + report(@banner.join(' - ')) + @forcenewline = true + @versiondone = true + end + end + + def help + version # is nilled when already given + @commandline.helpkeys.each do |k| + if @commandline.help?(k) then + message = @commandline.helptext(k) + message = '' if message == CommandLine::NOHELP + report("#{('--'+k).ljust(@commandline.helplength+2)} #{message}") + end + end + end + + def option(key) + @commandline.option(key) + end + def oneof(*key) + @commandline.oneof(*key) + end + + def globfiles(pattern='*',suffix=nil) + @commandline.setarguments([pattern].flatten) + if files = findfiles(suffix) then + @commandline.setarguments(files) + else + @commandline.setarguments + end + end + + private + + def findfiles(suffix=nil) + + if @commandline.arguments.length>1 then + return @commandline.arguments + else + pattern = @commandline.argument('first') + pattern = '*' if pattern.empty? + if suffix && ! pattern.match(/\..+$/o) then + suffix = '.' + suffix + pattern += suffix unless pattern =~ /#{suffix}$/ + end + # not {} safe + pattern = '**/' + pattern if @commandline.option('recurse') + files = Dir[pattern] + if files && files.length>0 then + return files + else + pattern = @commandline.argument('first') + if FileTest.file?(pattern) then + return [pattern] + else + report("no files match pattern #{pattern}") + return nil + end + end + end + + end + + def globbed(pattern,recurse=false) + + files = Array.new + pattern.split(' ').each do |p| + if recurse then + if p =~ /^(.*)(\/.*?)$/i then + p = $1 + '/**' + $2 + else + p = '**/' + p + end + p.gsub!(/[\\\/]+/, '/') + end + files.push(Dir.glob(p)) + end + files.flatten.sort do |a,b| + pathcompare(a,b) + end + end + + def pathcompare(a,b) + + aa, bb = a.split('/'), b.split('/') + if aa.length == bb.length then + aa.each_index do |i| + if aa[i]bb[i] then + return +1 + end + end + return 0 + else + return aa.length <=> bb.length + end + + end + +end + +class CommandLine + + VALUE, FLAG = 1, 2 + NOHELP = 'no arguments' + + def initialize(prefix='-') + + @registered = Array.new + @options = Hash.new + @unchecked = Hash.new + @arguments = Array.new + @original = ARGV.join(' ') + @helptext = Hash.new + @mandated = Hash.new + @provided = Hash.new + @prefix = prefix + @actions = Array.new + + # The quotes in --switch="some value" get lost in ARGV, so we need to do some trickery here. + + @original = '' + ARGV.each do |a| + aa = a.strip.gsub(/^([#{@prefix}]+\w+\=)([^\"].*?\s+.*[^\"])$/) do + $1 + "\"" + $2 + "\"" + end + @original += if @original.empty? then '' else ' ' end + aa + end + + end + + def setarguments(args=[]) + @arguments = if args then args else [] end + end + + def register(option,shortcut,kind,default=false,action=false,helptext='') + if kind == FLAG then + @options[option] = default + elsif not default then + @options[option] = '' + else + @options[option] = default + end + @registered.push([option,shortcut,kind]) + @mandated[option] = false + # @provided[option] = false + @helptext[option] = helptext + @actions.push(option) if action + end + + def registerflag(option,default=false,helptext='') + if default.class == String then + register(option,'',FLAG,false,false,default) + else + register(option,'',FLAG,false,false,helptext) + end + end + + def registervalue(option,default='',helptext='') + register(option,'',VALUE,default,false,helptext) + end + + def registeraction(option,helptext='') + register(option,'',FLAG,false,true,helptext) + end + + def registermandate(*option) + [*option].each do |o| + [o].each do |oo| + @mandated[oo] = true + end + end + end + + def actions + a = @actions.delete_if do |t| + ! option(t) + end + if a && a.length>0 then + return a + else + return nil + end + end + + def action + @actions.each do |t| + return t if option(t) + end + return nil + end + + def forgotten + @mandated.keys.sort - @provided.keys.sort + end + + def registerhelp(option,text='') + @helptext['unknown'] = if text.empty? then option else text end + end + + def helpkeys(option='.*') + @helptext.keys.sort.grep(/#{option}/) + end + + def helptext(option) + @helptext.fetch(option,'') + end + + def help?(option) + @helptext[option] && ! @helptext[option].empty? + end + + def helplength + n = 0 + @helptext.keys.each do |h| + n = h.length if h.length>n + end + return n + end + + def expand + + # todo : '' or false, depending on type + # @options.clear + # @arguments.clear + + dirtyvalue(@original).split(' ').each do |arg| + case arg + when /^[#{@prefix}][#{@prefix}](.+?)\=(.*?)$/ then locatedouble($1,$2) + when /^[#{@prefix}][#{@prefix}](.+?)$/ then locatedouble($1,false) + when /^[#{@prefix}](.)\=(.)$/ then locatesingle($1,$2) + when /^[#{@prefix}](.+?)$/ then locateseries($1,false) + when /^[\+\-]+/o then # do nothing + else + arguments.push(arg) + end + end + + @options or @unchecked or @arguments + + end + + def extend (str) + @original = @original + ' ' + str + end + + def replace (str) + @original = str + end + + def show + # print "-- options --\n" + @options.keys.sort.each do |key| + print "option: #{key} -> #{@options[key]}\n" + end + # print "-- arguments --\n" + @arguments.each_index do |key| + print "argument: #{key} -> #{@arguments[key]}\n" + end + end + + def option(str,default=nil) + if @options.key?(str) then + @options[str] + elsif default then + default + else + @options[str] + end + end + + def checkedoption(str,default='') + if @options.key?(str) then + if @options[str].empty? then default else @options[str] end + else + default + end + end + + def foundoption(str,default='') + str = str.split(',') if str.class == String + str.each do |s| + return str if @options.key?(str) + end + return default + end + + def oneof(*key) + [*key].flatten.compact.each do |k| + return true if @options.key?(k) && @options[k] + end + return false + end + + def setoption(str,value) + @options[str] = value + end + + def getoption(str,value='') # value ? + @options[str] + end + + def argument(n=0) + if n.class == String then + case n + when 'first' then argument(0) + when 'second' then argument(1) + when 'third' then argument(2) + else + argument(0) + end + elsif @arguments[n] then + @arguments[n] + else + '' + end + end + + # a few local methods, cannot be defined nested (yet) + + private + + def dirtyvalue(value) + if value then + value.gsub(/([\"\'])(.*?)\1/) do + $2.gsub(/\s+/o, "\xFF") + end + else + '' + end + end + + def cleanvalue(value) + if value then + # value.sub(/^([\"\'])(.*?)\1$/) { $2.gsub(/\xFF/o, ' ') } + value.gsub(/\xFF/o, ' ') + else + '' + end + end + + def locatedouble(key, value) + + foundkey, foundkind = nil, nil + + @registered.each do |option, shortcut, kind| + if option == key then + foundkey, foundkind = option, kind + break + end + end + unless foundkey then + @registered.each do |option, shortcut, kind| + n = 0 + if option =~ /^#{key}/i then + case n + when 0 + foundkey, foundkind = option, kind + n = 1 + when 1 + # ambiguous matches, like --fix => --fixme --fixyou + foundkey, foundkind = nil, nil + break + end + end + end + end + if foundkey then + @provided[foundkey] = true + if foundkind == VALUE then + @options[foundkey] = cleanvalue(value) + else + @options[foundkey] = true + end + else + if value.class == FalseClass then + @unchecked[key] = true + else + @unchecked[key] = cleanvalue(value) + end + end + + end + + def locatesingle(key, value) + + @registered.each do |option, shortcut, kind| + if shortcut == key then + @provided[option] = true + @options[option] = if kind == VALUE then '' else cleanvalue(value) end + break + end + end + + end + + def locateseries(series, value) + + series.each do |key| + locatesingle(key,cleanvalue(value)) + end + + end + + public + + attr_reader :arguments, :options, :original, :unchecked + +end + +# options = CommandLine.new +# +# options.register("filename", "f", CommandLine::VALUE) +# options.register("request" , "r", CommandLine::VALUE) +# options.register("verbose" , "v", CommandLine::FLAG) +# +# options.expand +# options.extend(str) +# options.show +# +# c = CommandLine.new +# +# c.registervalue('aaaa') +# c.registervalue('test') +# c.registervalue('zzzz') +# +# c.registerhelp('aaaa','some aaaa to enter') +# c.registerhelp('test','some text to enter') +# c.registerhelp('zzzz','some zzzz to enter') +# +# c.registermandate('test') +# +# c.expand +# +# class CommandLine +# +# def showhelp (banner,*str) +# if helpkeys(*str).length>0 +# print banner +# helpkeys(*str).each do |h| +# print helptext(h) + "\n" +# end +# true +# else +# false +# end +# end +# +# def showmandate(banner) +# if forgotten.length>0 +# print banner +# forgotten.each do |f| +# print helptext(f) + "\n" +# end +# true +# else +# false +# end +# end +# +# end +# +# c.showhelp("you can provide:\n\n") +# c.showmandate("you also need to provide:\n\n") diff --git a/scripts/context/ruby/base/system.rb b/scripts/context/ruby/base/system.rb new file mode 100644 index 000000000..ed8c2756e --- /dev/null +++ b/scripts/context/ruby/base/system.rb @@ -0,0 +1,102 @@ +# module : base/system +# copyright : PRAGMA Advanced Document Engineering +# version : 2002-2005 +# author : Hans Hagen +# +# project : ConTeXt / eXaMpLe +# concept : Hans Hagen +# info : j.hagen@xs4all.nl +# www : www.pragma-ade.com + +require "rbconfig" + +module System + + @@mswindows = Config::CONFIG['host_os'] =~ /mswin/ + @@binpaths = ENV['PATH'].split(File::PATH_SEPARATOR) + @@binsuffixes = if $mswindows then ['.exe','.com','.bat'] else ['','.sh','.csh'] end + @@located = Hash.new + @@binnames = Hash.new + + if @@mswindows then + @@binnames['ghostscript'] = ['gswin32c.exe','gs.cmd','gs.bat'] + @@binnames['imagemagick'] = ['imagemagick.exe','convert.exe'] + @@binnames['inkscape'] = ['inkscape.exe'] + else + @@binnames['ghostscript'] = ['gs'] + @@binnames['imagemagick'] = ['convert'] + @@binnames['inkscape'] = ['inkscape'] + end + + + def System.null + if @@mswindows then 'nul' else '/dev/null/' end + end + + def System.binnames(str) + if @@binnames.key?(str) then + @@binnames[str] + else + [str] + end + end + + def System.locatedprogram(program) + if @@located.key?(program) then + return @@located[program] + else + System.binnames(program).each do |binname| + if binname =~ /\..*$/io then + @@binpaths.each do |path| + if FileTest.file?(str = File.join(path,binname)) then + return @@located[program] = str + end + end + end + binname.gsub!(/\..*$/io, '') + @@binpaths.each do |path| + @@binsuffixes.each do |suffix| + if FileTest.file?(str = File.join(path,"#{binname}#{suffix}")) then + return @@located[program] = str + end + end + end + end + end + return @@located[program] = "texmfstart #{program}" + end + + def System.command(program,arguments='') + if program =~ /^(.*?) (.*)$/ then + program = System.locatedprogram($1) + ' ' + $2 + else + program = System.locatedprogram(program) + end + program = program + ' ' + arguments if ! arguments.empty? + program.gsub!(/\s+/io, ' ') + program.gsub!(/(\.\/)+/io, '') + program.gsub!(/\\/io, '/') + return program + end + + def System.run(program,arguments='',pipe=false,collect=false) + if pipe then + if collect then + `#{System.command(program,arguments)} 2>&1` + else + `#{System.command(program,arguments)}` + end + else + system(System.command(program,arguments)) + end + end + + def System.pipe(program,arguments='',collect=false) + System.run(program,arguments,true) + end + + def System.safepath(path) + if path.match(/ /o) then "\"#{path}\"" else path end + end + +end diff --git a/scripts/context/ruby/base/tex.rb b/scripts/context/ruby/base/tex.rb new file mode 100644 index 000000000..e9f66ea8d --- /dev/null +++ b/scripts/context/ruby/base/tex.rb @@ -0,0 +1,1281 @@ +# module : base/tex +# copyright : PRAGMA Advanced Document Engineering +# version : 2005 +# author : Hans Hagen +# +# project : ConTeXt / eXaMpLe +# concept : Hans Hagen +# info : j.hagen@xs4all.nl +# www : www.pragma-ade.com + +# todo: write systemcall for mpost to file so that it can be run +# faster + +require 'base/variables' +require 'base/kpse' +require 'base/system' +require 'base/state' +require 'base/pdf' +require 'base/file' + +class TEX + + # The make-part of this class was made on a rainy day while listening + # to "10.000 clowns on a rainy day" by Jan Akkerman. Unfortunately the + # make method is not as swinging as this live cd. + + include Variables + + @@texengines = Hash.new + @@mpsengines = Hash.new + @@backends = Hash.new + @@runoptions = Hash.new + @@texformats = Hash.new + @@mpsformats = Hash.new + @@prognames = Hash.new + + ['tex','pdftex','pdfetex','standard'] .each do |e| @@texengines[e] = 'pdfetex' end + ['aleph','omega'] .each do |e| @@texengines[e] = 'aleph' end + ['xetex'] .each do |e| @@texengines[e] = 'xetex' end + + ['metapost','mpost','standard'] .each do |e| @@mpsengines[e] = 'mpost' end + + ['pdfetex','pdftex','pdf','pdftex','standard'] .each do |b| @@backends[b] = 'pdftex' end + ['dvipdfmx','dvipdfm','dpx','dpm'] .each do |b| @@backends[b] = 'dvipdfmx' end + ['xetex','xtx'] .each do |b| @@backends[b] = 'xetex' end + ['dvips','ps'] .each do |b| @@backends[b] = 'dvips' end + ['dvipsone'] .each do |b| @@backends[b] = 'dvipsone' end + ['acrobat','adobe','distiller'] .each do |b| @@backends[b] = 'acrobat' end + + # todo norwegian (no) + + ['plain'] .each do |f| @@texformats[f] = 'plain' end + ['cont-en','en','english','context','standard'].each do |f| @@texformats[f] = 'cont-en' end + ['cont-nl','nl','dutch'] .each do |f| @@texformats[f] = 'cont-nl' end + ['cont-de','de','german'] .each do |f| @@texformats[f] = 'cont-de' end + ['cont-it','it','italian'] .each do |f| @@texformats[f] = 'cont-it' end + ['cont-cz','cz','czech'] .each do |f| @@texformats[f] = 'cont-cz' end + ['cont-ro','ro','romanian'] .each do |f| @@texformats[f] = 'cont-ro' end + ['cont-uk','uk','brittish'] .each do |f| @@texformats[f] = 'cont-uk' end + ['mptopdf'] .each do |f| @@texformats[f] = 'mptopdf' end + + ['plain','mpost'] .each do |f| @@mpsformats[f] = 'plain' end + ['metafun','context','standard'] .each do |f| @@mpsformats[f] = 'metafun' end + + ['pdfetex','aleph','omega'] .each do |p| @@prognames[p] = 'context' end + ['mpost'] .each do |p| @@prognames[p] = 'metafun' end + + @@runoptions['xetex'] = ['--no-pdf'] + + @@booleanvars = [ + 'batchmode', 'nonstopmode', 'fastmode', 'fastdisabled', 'silentmode', 'final', + 'paranoid', 'notparanoid', 'nobanner', 'once', 'allpatterrns', + 'nompmode', 'nomprun', 'automprun', + 'nomapfiles', + 'arrange', 'noarrange', + 'forcexml', 'foxet', + 'mpyforce', 'forcempy', + 'forcetexutil', 'texutil', + 'globalfile', 'autopath', + 'purge', 'pdfopen', 'simplerun', + ] + @@stringvars = [ + 'modefile', 'result', 'suffix', 'response', 'path', + 'filters', 'usemodules', 'environments', 'separation', 'setuppath', + 'arguments', 'input', 'output', 'randomseed', 'modes', 'filename', + 'modefile', + ] + @@standardvars = [ + 'mainlanguage', 'bodyfont', 'language' + ] + @@knownvars = [ + 'engine', 'distribution', 'texformats', 'mpsformats', 'progname', 'interface', + 'runs', 'backend' + ] + + @@extrabooleanvars = [] + @@extrastringvars = [] + + def booleanvars + [@@booleanvars,@@extrabooleanvars].flatten + end + def stringvars + [@@stringvars,@@extrastringvars].flatten + end + def standardvars + @@standardvars + end + def knownvars + @@knownvars + end + + def setextrastringvars(vars) + @@extrastringvars << vars + end + def setextrabooleanvars(vars) + @@extrabooleanvars << vars + end + + @@temprunfile = 'texexec' + @@temptexfile = 'texexec.tex' + + def initialize(logger) + @logger = logger + @cleanups = Array.new + @variables = Hash.new + @startuptime = Time.now + # options + booleanvars.each do |k| + setvariable(k,false) + end + stringvars.each do |k| + setvariable(k,'') + end + standardvars.each do |k| + setvariable(k,'standard') + end + setvariable('distribution', Kpse.distribution) + setvariable('texformats', defaulttexformats) + setvariable('mpsformats', defaultmpsformats) + setvariable('progname', 'context') + setvariable('interface', 'standard') + setvariable('engine', 'standard') + setvariable('backend', 'pdftex') + setvariable('runs', '8') + setvariable('randomseed', rand(1440)) + # files + setvariable('files', []) + end + + def runtime + Time.now - @startuptime + end + + def reportruntime + report("runtime: #{runtime}") + end + + def inspect(name=nil) + if ! name || name.empty? then + name = [booleanvars,stringvars,standardvars,knownvars] + end + [name].flatten.each do |n| + if str = getvariable(n) then + unless (str.class == String) && str.empty? then + report("option '#{n}' is set to '#{str}'") + end + end + end + end + + def tempfilename(suffix='') + @@temprunfile + if suffix.empty? then '' else ".#{suffix}" end + end + + def cleanup + @cleanups.each do |name| + begin + File.delete(name) if FileTest.file?(name) + rescue + report("unable to delete #{name}") + end + end + end + + def cleanuptemprunfiles + begin + Dir.glob("#{@@temprunfile}*").each do |name| + if File.file?(name) && (File.splitname(name)[1] !~ /(pdf|dvi)/o) then + begin File.delete(name) ; rescue ; end + end + end + rescue + end + end + + def texengines + @@texengines.keys.sort + end + + def mpsengines + @@mpsengines.keys.sort + end + + def backends + @@backends.keys.sort + end + + def texformats + @@texformats.keys.sort + end + + def mpsformats + @@mpsformats.keys.sort + end + + def defaulttexformats + ['en','nl','mptopdf'] + end + + def defaultmpsformats + ['metafun'] + end + + def runoptions(engine) + if @@runoptions.key?(engine) then @@runoptions[engine].join(' ') else '' end + end + + # private + + def cleanuplater(name) + begin + @cleanups.push(File.expand_path(name)) + rescue + @cleanups.push(name) + end + end + + def openedfile(name) + cleanuplater(name) if f = File.open(name, 'w') + return f + end + + def prefixed(format,engine) + case engine + when /etex|eetex|pdfetex|pdfeetex|pdfxtex|xpdfetex|eomega|aleph|xetex/io then + "*#{format}" + else + format + end + end + + def quoted(str) + if str =~ /^[^\"].* / then "\"#{str}\"" else str end + end + + def getarrayvariable(str='') + str = getvariable(str) + if str.class == String then str.split(',') else str.flatten end + end + + def validtexformat(str) + validsomething(str,@@texformats) + end + def validmpsformat(str) + validsomething(str,@@mpsformats) + end + def validtexengine(str) + validsomething(str,@@texengines) + end + def validmpsengine(str) + validsomething(str,@@mpsengines) + end + def validsomething(str,something) + if str then + list = [str].flatten.collect do |s| + something[s] + end .compact.uniq + if list.length>0 then + if str.class == String then list.first else list end + else + false + end + else + false + end + end + + def validbackend(str) + if str && @@backends.key?(str) then + @@backends[str] + else + @@backends['standard'] + end + end + + def validprogname(str,engine='standard') + if str && @@prognames.key?(str) then + @@prognames[str] + elsif (engine != 'standard') && @@prognames.key?(engine) then + @@prognames[engine] + else + str + end + end + + # we no longer support the & syntax + + def formatflag(engine=nil,format=nil) + case getvariable('distribution') + when 'standard' then prefix = "--fmt" + when /web2c/io then prefix = web2cformatflag(engine) + when /miktex/io then prefix = "--undump" + else return "" + end + if format then + if engine then + "#{prefix}=#{engine}/#{format}" + else + "#{prefix}=#{format}" + end + else + prefix + end + end + + def web2cformatflag(engine=nil) + # funny that we've standardized on the fmt suffix (at the cost of + # upward compatibility problems) but stuck to the bas/mem/fmt flags + if engine then + case validmpsengine(engine) + when /mpost/ then "--mem" + when /mfont/ then "--bas" + else "--fmt" + end + else + "--fmt" + end + end + + def prognameflag(progname=nil) + case getvariable('distribution') + when 'standard' then prefix = "--progname" + when /web2c/io then prefix = "--progname" + when /miktex/io then prefix = "--alias" + else return "" + end + if progname then + if progname = validprogname(progname) then + "#{prefix}=#{progname}" + else + "" + end + else + prefix + end + end + + def iniflag + "--ini" + end + def tcxflag + "--translate-file=natural.tcx" + end + + def filestate(file) + File.mtime(file).strftime("%d/%m/%Y %H:%M:%S") + end + + # will go to context/process context/listing etc + + def contextversion # ook elders gebruiken + filename = Kpse.found('context.tex') + version = 'unknown' + begin + if FileTest.file?(filename) && IO.read(filename).match(/\\contextversion\{(\d+\.\d+\.\d+)\}/) then + version = $1 + end + rescue + end + return version + end + + def makeformats + # goody + if getvariable('texformats') == 'standard' then + setvariable('texformats',[getvariable('interface')]) unless getvariable('interface').empty? + end + # prepare + texformats = validtexformat(getarrayvariable('texformats')) + mpsformats = validmpsformat(getarrayvariable('mpsformats')) + texengine = validtexengine(getvariable('texengine')) + mpsengine = validmpsengine(getvariable('mpsengine')) + # save current path + savedpath = Dir.getwd + # generate tex formats + if texformats && texengine && (progname = validprogname(getvariable('progname'),texengine)) then + report("using tex engine #{texengine}") + texformatpath = Kpse.formatpath(texengine,true) + # can be empty, to do + report("using tex format path #{texformatpath}") + begin + Dir.chdir(texformatpath) + rescue + end + if texformats.length > 0 then + makeuserfile + makeresponsefile + end + texformats.each do |format| + if texformat = validtexformat(format) then + report("generating tex format #{texformat}") + # report("some texmf environment variables are fixed") if Kpse.fixtexmfvars(texengine) + command = [quoted(texengine),prognameflag(progname),iniflag,tcxflag,prefixed(texformat,texengine)].join(' ') + report(command) if getvariable('verbose') + system(command) + end + end + else + texformatpath = '' + end + # generate mps formats + if mpsformats && mpsengine && (progname = validprogname(getvariable('progname'),mpsengine)) then + report("using mp engine #{mpsengine}") + mpsformatpath = Kpse.formatpath(mpsengine,false) + report("using mps format path #{mpsformatpath}") + begin + Dir.chdir(mpsformatpath) + rescue + end + mpsformats.each do |format| + if mpsformat = validmpsformat(format) then + report("generating mps format #{mpsformat}") + # report("some texmf environment variables are fixed") if Kpse.fixtexmfvars(mpsengine) + command = [quoted(mpsengine),prognameflag(progname),iniflag,tcxflag,mpsformat].join(' ') + report(command) if getvariable('verbose') + system(command) + end + end + else + mpsformatpath = '' + end + # check for problems + report("tex engine path: #{texformatpath}") unless texformatpath.empty? + report("mps engine path: #{mpsformatpath}") unless mpsformatpath.empty? + [['fmt','tex'],['mem','mps']].each do |f| + [[texformatpath,'global'],[mpsformatpath,'global'],[savedpath,'current']].each do |p| + begin + Dir.chdir(p[0]) + rescue + else + Dir.glob("*.#{f[0]}").each do |file| + report("#{f[1]}format: #{filestate(file)} > #{File.expand_path(file)}") + end + end + end + end + # to be sure, go back to current path + begin + Dir.chdir(savedpath) + rescue + end + # finalize + cleanup + reportruntime + end + + def checkcontext + + # todo : report texmf.cnf en problems + + # basics + report("current distribution: #{Kpse.distribution}") + report("context source date: #{contextversion}") + formatpaths = Kpse.formatpaths + globpattern = "**/{#{formatpaths.join(',')}}/*/*.{fmt,efmt,ofmt,xfmt,mem}" + report("format path: #{formatpaths.join(' ')}") + # utilities + report + ['texexec','texutil','ctxtools'].each do |program| + result = `texmfstart #{program} --help` + result.sub!(/.*?(#{program}[^\n]+)\n.*/mi) do $1 end + report("#{result}") + end + # formats + cleanuptemprunfiles + if formats = Dir.glob(globpattern) then + formats.sort.each do |name| + cleanuptemprunfiles + if f = open(tempfilename('tex'),'w') then + # kind of aleph-run-out-of-par safe + f << "\\starttext\n" + f << " \\relax test \\relax\n" + f << "\\stoptext\n" + f << "\\endinput\n" + f.close + if FileTest.file?(tempfilename('tex')) then + format = File.basename(name) + engine = if name =~ /(pdfetex|aleph|xetex)[\/\\]#{format}/ then $1 else '' end + if engine.empty? then + engineflag = "" + else + engineflag = "--engine=#{$1}" + end + case format + when /cont\-([a-z]+)/ then + interface = $1.sub(/cont\-/,'') + report + report("testing interface #{interface}") + flags = ['--process','--batch','--once',"--interface=#{interface}",engineflag] +# to be adapted ! +result = Kpse.pipescript('newtexexec',tempfilename,flags) +# to just texexec + if FileTest.file?("#{@@temprunfile}.log") then + logdata = IO.read("#{@@temprunfile}.log") + if logdata =~ /^\s*This is (.*?)[\s\,]+(.*?)$/mois then + if validtexengine($1.downcase) then + report("#{$1} #{$2.gsub(/\(format.*$/,'')}".strip) + end + end + if logdata =~ /^\s*(ConTeXt)\s+(.*int:\s+[a-z]+.*?)\s*$/mois then + report("#{$1} #{$2}".gsub(/\s+/,' ').strip) + end + else + report("format #{format} does not work") + end + when /metafun/ then + # todo + when /mptopdf/ then + # todo + end + else + report("error in creating #{tempfilename('tex')}") + end + end + cleanuptemprunfiles + end + end + cleanuptemprunfiles + + end + + private + + def makeuserfile + language = getvariable('language') + mainlanguage = getvariable('mainlanguage') + bodyfont = getvariable('bodyfont') + if f = openedfile("cont-fmt.tex") then + f << "\\unprotect" + case language + when 'all' then + f << "\\preloadallpatterns\n" + when '' then + f << "% no language presets\n" + when 'standard' + f << "% using defaults\n" + else + languages = language.split(',') + languages.each do |l| + f << "\\installlanguage[\\s!#{l}][\\c!state=\\v!start]\n" + end + mainlanguage = languages.first + end + unless mainlanguage == 'standard' then + f << "\\setupcurrentlanguage[\\s!#{mainlanguage}]\n"; + end + unless bodyfont == 'standard' then + # ~ will become obsolete when lmr is used + f << "\\definetypescriptsynonym[cmr][#{bodyfont}]" + # ~ is already obsolete for some years now + f << "\\definefilesynonym[font-cmr][font-#{bodyfont}]\n" + end + f << "\\protect\n" + f << "\\endinput\n" + f.close + end + end + + def makeresponsefile + interface = getvariable('interface') + if f = openedfile("mult-def.tex") then + case interface + when 'standard' then + f << "% using default response interface" + else + f << "\\def\\currentresponses\{#{interface}\}\n" + end + f << "\\endinput\n" + f.close + end + end + + private # will become baee/context + + @@preamblekeys = [ + ['tex','texengine'],['program','texengine'], + ['ctx','ctxfilter'],['translate','ctxfilter'], + ['output','backend'],['modes','modes'],['version','contextversion'], + ['format','texformat'],['interface','texformat'] + ] + + def scantexpreamble(filename) + if tex = File.open(filename) then + while str = tex.gets.chomp do + if str =~ /^\%\s*(.*)/o then + vars = Hash.new + $1.split(/\s+/o).each do |s| + k, v = s.split('=') + vars[k] = v + end + @@preamblekeys.each do |v| + setvariable(v[1],vars[v[0]]) if vars.key?(v[0]) + end + else + break + end + end + tex.close + end + end + + def scantexcontent(filename) + if tex = File.open(filename) then + while str = tex.gets.chomp do + case str + when /^\%/o then + # next + when /\\(starttekst|stoptekst|startonderdeel|startdocument|startoverzicht)/o then + setvariable('texformat','nl') ; break + when /\\(stelle|verwende|umgebung|benutze)/o then + setvariable('texformat','de') ; break + when /\\(stel|gebruik|omgeving)/o then + setvariable('texformat','nl') ; break + when /\\(use|setup|environment)/o then + setvariable('texformat','en') ; break + when /\\(usa|imposta|ambiente)/o then + setvariable('texformat','it') ; break + when /(height|width|style)=/o then + setvariable('texformat','en') ; break + when /(hoehe|breite|schrift)=/o then + setvariable('texformat','de') ; break + when /(hoogte|breedte|letter)=/o then + setvariable('texformat','nl') ; break + when /(altezza|ampiezza|stile)=/o then + setvariable('texformat','it') ; break + when /externfiguur/o then + setvariable('texformat','nl') ; break + when /externalfigure/o then + setvariable('texformat','en') ; break + when /externeabbildung/o then + setvariable('texformat','de') ; break + when /figuraesterna/o then + setvariable('texformat','it') ; break + end + end + tex.close + end + + end + + private # will become base/context + + def pushresult(filename,resultname) + fname = File.unsuffixed(filename) + rname = File.unsuffixed(resultname) + if ! rname.empty? && (rname != fname) then + report("outputfile #{result}") + ['tuo','log','dvi','pdf'].each do |s| + File.silentrename(File.suffixed(fname,s),File.suffixed('texexec',s)) + end + ['tuo'].each do |s| + File.silentrename(File.suffixed(rname,s),File.suffixed(fname,s)) if FileTest(suffixed(rname,s)) + end + end + end + + def popresult(filename,resultname) + fname = File.unsuffixed(filename) + rname = File.unsuffixed(resultname) + if ! rname.empty? && (rname != fname) then + report("renaming #{fname} to #{rname}") + ['tuo','log','dvi','pdf'].each do |s| + File.silentrename(File.suffixed(fname,s),File.suffixed(rname,s)) + end + report("restoring #{fname}") + unless $fname == 'texexec' then + ['tuo','log','dvi','pdf'].each do |s| + File.silentrename(File.suffixed('texexec',s),File.suffixed(fname,s)) + end + end + end + end + + def makestubfile(jobname,jobsuffix,forcexml=false) + if tmp = File.open(File.suffixed(jobname,'run'),'w') then + fullname = File.suffixed(jobname,jobsuffix) + tmp << "\\starttext\n" + if forcexml then + if FileTest.file?(fullname) && (xml = File.open(fullname)) then + xml.each do |line| + if line =~ /<[a-z]+/io then + break + elsif line =~ /<\?context\-directive\s+(.+?)\s+(.+?)\s+(.+?)\s*\?>/o then + category, key, value = $1, $2, $3 + if category == 'job' then + if key == 'stylefile' then + tmp << "\\environment $value\n" + elsif key == 'module' then + tmp << "\\usemodule[$value]\n" + elsif key == 'interface' then + contextinterface = value + end + end + end + end + xml.close + end + tmp << "\\processXMLfilegrouped{#{fullname}}\n" + else + tmp << "\\processfile{#{fullname}}\n" + end + tmp << "\\stoptext\n" + tmp.close + jobsuffix = "run" + end + return jobsuffix + end + +end + +class TEX + + def processtex # much to do: mp, xml, runs etc + setvariable('texformats',[getvariable('interface')]) unless getvariable('interface').empty? + getarrayvariable('files').each do |filename| + setvariable('filename',filename) + report("processing document '#{filename}'") + processcontextfile + end + reportruntime + end + + def processmptex + getarrayvariable('files').each do |filename| + setvariable('filename',filename) + report("processing graphic '#{filename}'") + runtexmp(filename) + end + reportruntime + end + + def processmpxtex + getarrayvariable('files').each do |filename| + setvariable('filename',filename) + report("processing text of graphic '#{filename}'") + processmpx(filename,true) + end + reportruntime + end + + def makeoptionfile(jobname, jobsuffix, finalrun, fastdisabled, kindofrun) + if opt = File.open(File.suffixed(jobname,'top'),'w') then + # local handies + opt << "\% $JobName.top\n" + opt << "\\unprotect\n" + opt << "\\setupsystem[\\c!n=#{kindofrun}]\n" + opt << "\\def\\MPOSTformatswitch\{#{prognameflag('metafun')} #{formatflag('mpost')}=\}" + if getvariable('batchmode') then + opt << "\\batchmode\n" + end + if getvariable('nonstopmode') then + opt << "\\nonstopmode\n" + end + if getvariable('paranoid') then + opt << "\\def\\maxreadlevel{1}\n" + end + if (str = File.unixfied(getvariable('modefile'))) && ! str.empty? then + opt << "\\readlocfile{#{str}}{}{}\n" + end + if (str = File.unixfied(getvariable('result'))) && ! str.empty? then + opt << "\\setupsystem[file=#{str}]\n" + elsif (str = getvariable('suffix')) && ! str.empty? then + opt << "\\setupsystem[file=#{File.suffixed(jobname,str,nil)}]\n" + end + if (str = File.unixfied(getvariable('path'))) && ! str.empty? then + opt << "\\usepath[#{str}]\n" unless str.empty? + end + if (str = getvariable('mainlanguage').downcase) && ! str.empty? && ! str.standard? then + opt << "\\setuplanguage[#{str}]\n" + end + if str = validbackend(getvariable('backend')) then + opt << "\\setupoutput[#{str}]\n" + end + if getvariable('color') then + opt << "\\setupcolors[\\c!state=\\v!start]\n" + end + if getvariable('nompmode') || getvariable('nomprun') || getvariable('automprun') then + opt << "\\runMPgraphicsfalse\n" + end + if getvariable('fastmode') && ! getvariable('fastdisabled') then + opt << "\\fastmode\n" + end + if getvariable('silent') then + opt << "\\silentmode\n" + end + if (str = getvariable('separation')) && ! str.empty? then + opt << "\\setupcolors[\\c!split=#{str}]\n" + end + if (str = getvariable('setuppath')) && ! str.empty? then + opt << "\\setupsystem[\\c!directory=\{#{str}\}]\n" + end + if (str = getvariable('paperformat')) && ! str.empty? && ! str.standard? then + if str =~ /^([a-z]+\d+)([a-z]+\d+)$/io then # A5A4 A4A3 A2A1 ... + opt << "\\setuppapersize[#{$1.upcase}][#{$2.upcase}]\n" + else # ...*... + pf = str.upcase.split(/[x\*]/o) + pf << pf[0] if pd.size == 1 + opt << "\\setuppapersize[#{pf[0]}][#{pf[1]}]\n" + end + end + if (str = getvariable('background')) && ! str.empty? then + opt << "\\defineoverlay[whatever][{\\externalfigure[#{str}][\\c!factor=\\v!max]}]\n" + opt << "\\setupbackgrounds[\\v!page][\\c!background=whatever]\n" + end + if getvariable('centerpage') then + opt << "\\setuplayout[\\c!location=\\v!middle,\\c!marking=\\v!on]\n" + end + if getvariable('nomapfiles') then + opt << "\\disablemapfiles\n" + end + if getvariable('noarrange') then + opt << "\\setuparranging[\\v!disable]\n" + elsif getvariable('arrange') then + arrangement = Array.new + if finalrun then + arrangement << "\\v!doublesided" unless getvariable('noduplex') + case printformat + when '' then arrangement << "\\v!normal" + when /.*up/oi then arrangement << "\\v!rotated" + when /.*down/oi then arrangement << ["2DOWN","\\v!rotated"] + when /.*side/oi then arrangement << ["2SIDE","\\v!rotated"] + end + else + arrangement << "\\v!disable" + end + opt << "\\setuparranging[#{arrangement.flatten.join(',')}]\n" if arrangement.size > 0 + end + if (str = getvariable('modes')) && ! str.empty? then + opt << "\\enablemode[#{modes}]\n" + end + if (str = getvariable('arguments')) && ! str.empty? then + opt << "\\setupenv[#{str}]\n" + end + if (str = getvariable('randomseed')) && ! str.empty? then + opt << "\\setupsystem[\\c!random=#{str}]\n" + end + if (str = getvariable('input')) && ! str.empty? then + opt << "\\setupsystem[inputfile=#{str}]\n" + else + opt << "\\setupsystem[inputfile=#{File.suffixed(jobname,jobsuffix)}]\n" + end + if (str = getvariable('pages')) && ! str.empty? then + if str.downcase == 'odd' then + opt << "\\chardef\\whichpagetoshipout=1\n" + elsif str.downcase == 'even' then + opt << "\\chardef\\whichpagetoshipout=2\n" + else + pagelist = Array.new + str.split(/\,/).each do |page| + pagerange = page.split(/(\:|\.\.)/o ) + if pagerange.size > 1 then + pagerange.first.to_i.upto(pagerange.last.to_i) do |p| + pagelist << p.to_s + end + else + pagelist << page + end + end + opt << "\\def\\pagestoshipout\{pagelist.join(',')\}\n"; + end + end + opt << "\\protect\n"; + begin + getvariable('filters').split(',').each do |f| opt << "\\useXMLfilter[#{f}]\n" end + rescue + end + begin + getvariable('usemodules').split(',').each do |m| opt << "\\usemodule[#{m}]\n" end + rescue + end + begin + getvariable('environments').split(',').each do |e| opt << "\\usemodule[#{e}]\n" end + rescue + end + opt << "\\endinput\n" + opt.close + end + end + + def takeprecautions + ENV['MPXCOMAND'] = '0' # else loop + if getvariable('paranoid') then + ENV['SHELL_ESCAPE'] = ENV['SHELL_ESCAPE'] || 'f' + ENV['OPENOUT_ANY'] = ENV['OPENOUT_ANY'] || 'p' + ENV['OPENIN_ANY'] = ENV['OPENIN_ANY'] || 'p' + else + ENV['SHELL_ESCAPE'] = ENV['SHELL_ESCAPE'] || 't' + ENV['OPENOUT_ANY'] = ENV['OPENOUT_ANY'] || 'p' + ENV['OPENIN_ANY'] = ENV['OPENIN_ANY'] || 'a' + end + if (ENV['OPENIN_ANY'] == 'p') || (ENV['OPENOUT_ANY'] == 'p') then + setvariable('paranoid', true) + end + if ENV.key?('SHELL_ESCAPE') && (ENV['SHELL_ESCAPE'] == 'f') then + setvariable('automprun',true) + end + ['TXRESOURCES','MPRESOURCES','MFRESOURCES'].each do |res| + [getvariable('runpath'),getvariable('path')].each do |pat| + unless pat.empty? then + if ENV.key?(res) then + ENV[res] = if ENV[res].empty? then pat else pat + ":" + ENV[res] end + else + ENV[res] = pat + end + end + end + end + end + + def runtex(filename) + texengine = validtexengine(getvariable('texengine')) + texformat = validtexformat(getarrayvariable('texformats').first) + progname = validprogname(getvariable('progname')) + report("tex engine: #{texengine}") + report("tex format: #{texformat}") + report("progname: #{progname}") + if texengine && texformat && progname then + command = [quoted(texengine),prognameflag(progname),formatflag(texengine,texformat),runoptions(texengine),filename].join(' ') + report(command) if getvariable('verbose') + system(command) + else + false + end + end + + def runmp(filename) + mpsengine = validmpsengine(getvariable('mpsengine')) + mpsformat = validmpsformat(getarrayvariable('mpsformats').first) + progname = validprogname(getvariable('progname')) + if mpsengine && mpsformat && progname then + command = [quoted(mpsengine),prognameflag(progname),formatflag(mpsengine,mpsformat),runoptions(mpsengine),filename].join(' ') + report(command) if getvariable('verbose') + system(command) + else + false + end + end + + def runtexmp(filename,filetype='') + mpfile = File.suffixed(filename,filetype,'mp') + if File.atleast?(mpfile,25) then + # first run needed + File.silentdelete(File.suffixed(mpfile,'mpt')) + doruntexmp(mpfile,false) + mpgraphics = checkmpgraphics(mpfile) + mplabels = checkmplabels(mpfile) + if mpgraphics || mplabels then + # second run needed + doruntexmp(mpfile,mplabels) + end + end + end + + def runtexmpjob(filename,filetype='') + mpfile = File.suffixed(filename,filetype,'mp') + if File.atleast?(mpfile,25) && (data = File.silentread(mpfile)) then + textranslation = if data =~ /^\%\s+translate.*?\=([\w\d\-]+)/io then $1 else '' end + mpjobname = if data =~ /collected graphics of job \"(.+)\"/io then $1 else '' end + if File.unsuffixed(filename) =~ /#{mpjobname}/ then # don't optimize + options = Array.new + options.push("--mptex") + options.push("--nomp") + options.push("--mpyforce") if getvariable('forcempy') || getvariable('mpyforce') + options.push("--translate=#{textranslation}") unless textranslation.empty? + options.push("--batch") if getvariable('batchmode') + options.push("--nonstop") if getvariable('nonstopmode') + return runtexexec(mpfile,options) + end + end + return false + end + + def runtexutil(filename=[], options=['--ref','--ij','--high']) + Kpse.runscript('texutil',filename,options) + end + + def runtexexec(filename=[], options=[]) + Kpse.runscript('texexec',filename,options) + end + + def processcontextfile + + takeprecautions + + jobname = getvariable('filename') + suffix = getvariable('suffix') + result = getvariable('result') + + final = getvariable('final') + + if getvariable('autopath') then + jobname = File.basename(jobname) + inppath = File.dirname(jobname) + else + inppath = '' + end + + jobname, jobsuffix = File.splitname(jobname,'tex') + + jobname = File.unixfied(jobname) + inppath = File.unixfied(inppath) + result = File.unixfied(result) + + orisuffix = jobsuffix # still needed ? + + PDFview.closeall if getvariable('pdfopen') + + forcexml = jobsuffix === /(xml|fo|fox|rlg|exa|)/io # === returns true|false, =~ returns position + dummyfile = false + + # fuzzy code snippet: (we kunnen kpse: prefix gebruiken) + + unless FileTest.file?(File.suffixed(jobname,jobsuffix)) then + inppath.split(',').each do |ip| + break if dummyfile = FileTest.file?(File.join(ip,File.suffixed(jobname,jobsuffix))) + end + end + + jobsuffix = makestubfile(jobname,jobsuffix,forcexml) if dummyfile || forcexml + + if getvariable('globalfile') || FileTest.file?(File.suffixed(jobname,jobsuffix)) then + + unless dummyfile # we don't need this for xml + scantexpreamble(File.suffixed(jobname,jobsuffix)) + scantexcontent(File.suffixed(jobname,jobsuffix)) if getvariable('texformat').standard? + end + + result = File.suffixed(jobname,suffix) unless suffix.empty? + + pushresult(jobname,result) + + if getvariable('simplerun') then + makeoptionfile(jobname,orisuffix,true,true,3) + problems = runtex(File.suffixed(jobname,jobsuffix)) + if problems then + ok = runtexutil(jobname) if getvariable('texutil') || getvariable('forcetexutil') + popresult(jobname,result) + end + File.silentrename(File.suffixed(jobname,'top'),File.suffixed(jobname,'tmp')) + else + mprundone, ok, stoprunning = false, true, false + texruns, nofruns = 0, getvariable('runs').to_i + state = FileState.new + ['tub','tuo'].each do |s| + state.register(File.suffixed(jobname,s)) + end + ['mprun','mpgraph'].each do |s| + state.register(File.suffixed(jobname,s,'mp'),'randomseed') + # state.register(File.suffixed(jobname,s,'mpo')) + end + while ! stoprunning && (texruns < nofruns) && ok do + texruns += 1 + report("TeX run #{texruns}") + if texruns == 1 then + makeoptionfile(jobname,orisuffix,false,false,1) + else + makeoptionfile(jobname,orisuffix,false,false,2) + end + ok = runtex(File.suffixed(jobname,jobsuffix)) + if ok && (nofruns > 1) then + unless getvariable('nomprun') then + mprundone = runtexmpjob(jobname, "mpgraph") + mprundone = runtexmpjob(jobname, "mprun") + end + ok = runtexutil(jobname) + state.update + stoprunning = state.stable? + end + end + ok = runtexutil(jobname) if (nofruns == 1) && getvariable('texutil') + finalrun = getvariable('arrange') && ! getvariable('noarrange') + if ! problems && finalrun && (nofruns > 1) then + makeoptionfile(jobname,orisuffix,true,finalrun,4) + report("final TeX run #{texruns}") + problems = runtex(File.suffixed(jobname,jobsuffix)) + end + File.silentcopy(File.suffixed(jobname,'top'),File.suffixed(jobname,'tmp')) + ['tup','top'].each do |s| # previous tuo file / runtime option file + File.silentdelete(File.suffixed(jobname,s)) + end + case validbackend(getvariable('driver')) + when 'dvipdfmx' then system("dvipdfmx -f dvipdfmx.map -d 4 #{File.unsuffixed(jobname)}") + when 'xetex' then system("xdv2pdf #{File.suffixed(jobname,'xdv')}") + when 'dvips' then system("dvips #{File.unsuffixed(jobname)}") + end + popresult(jobname,result) + end + + Kpse.runscript('ctxtools',jobname,'--purge') if getvariable('purge') + + begin + File.delete(File.suffixed(jobname,jobsuffix)) if dummyfile || forcexml + rescue + report("unable to delete stub file") + end + + if ! problems && getvariable('pdfopen') then + PDFview.open(if resultname.empty? then jobname else resultname end) + end + + end + + end + + # mp specific + + def doruntexmp(mpname,mergebe=true,context=true) + texfound = false + mpbetex = Hash.new + mpfile = File.suffixed(mpname,'mp') + mpcopy = File.suffixed(mpname,'copy','mp') + setvariable('mp.file',mpfile) + setvariable('mp.line','') + setvariable('mp.error','') + if mpdata = File.silentread(mpfile) then + mpdata.gsub!(/^\#.*\n/o,'') + File.silentrename(mpfile,mpcopy) + texfound = mergebe || mpdata =~ /btex .*? etex/o + if mp = File.silentopen(mpfile,'w') then + mpdata.gsub!(/(btex.*?)\;(.*?etex)/o) do "#{$1}@@@#{$2}" end + mpdata.gsub!(/(\".*?)\;(.*?\")/o) do "#{$1}@@@#{$2}" end + mpdata.gsub!(/\;/o, "\;\n") + mpdata.gsub!(/\n+/o, "\n") + mpdata.gsub!(/(btex.*?)@@@(.*?etex)/o) do "#{$1}\;#{$2}" end + mpdata.gsub!(/(\".*?)@@@(.*?\")/o) do "#{$1};#{$2}" end + if mergebe then + mpdata.gsub!(/beginfig\s*\((\d+)\)\s*\;(.*?)endfig\s*\;/o) do + n, str = $1, $2 + if str =~ /(.*?)(verbatimtex.*?etex)\s*\;(.*)/o then + "beginfig(#{n})\;\n$1$2\;\n#{mpbetex(n)}\n$3\;endfig\;\n" + else + "beginfig(#{n})\;\n#{mpbetex(n)}\n#{str}\;endfig\;\n" + end + end + end + unless mpdata =~ /beginfig\s*\(\s*0\s*\)/o then + mp << mpbetex[0] if mpbetex.key?(0) + end + mp << mpdata # ?? + mp << "\n" + mp << "end" + mp << "\n" + mp.close + end + processmpx(mpname) if texfound + if getvariable('batchmode') then + options = ' --interaction=batch' + elsif getvariable('nonstopmode') then + options = ' --interaction=nonstop' + else + options = '' + end + # todo plain|mpost|metafun + ok = runmp(mpname) + if f = File.silentopen(File.suffixed(mpfile,'log')) then + while str = f.gets do + if str =~ /^l\.(\d+)\s(.*?)\n/o then + setvariable('mp.line',$1) + setvariable('mp.error',$2) + break + end + end + f.close + end + File.silentrename(mpfile,"mptrace.tmp") + File.silentrename(mpcopy, mpfile) + end + end + + def processmpx(mpname,context=true) + mpname = File.suffixed(mpname,'mp') + if File.atleast?(mpname,10) && (data = File.silentread(mpname)) then + begin + if data =~ /(btex|etex|verbatimtex)/o then + mptex = File.suffixed(mpname,'temp','tex') + mpdvi = File.suffixed(mpname,'temp','dvi') + mplog = File.suffixed(mpname,'temp','log') + mpmpx = File.suffixed(mpname,'temp','mpx') + ok = system("mpto #{mpname} > #{mptex}") + if ok && File.appended(mptex, "\\end\n") then + if context then + ok = RunConTeXtFile(mptex) + else + ok = RunSomeTeXFile(mptex) + end + ok = ok && FileTest.file?(mpdvi) && system("dvitomp #{mpdvi} #{mpmpx}") + [mptex,mpdvi,mplog].each do |mpfil| + File.silentdelete(mpfil) + end + end + end + rescue + # error in processing mpx file + end + end + end + + def checkmpgraphics(mpname) + mpoptions = '' + if getvariable('makempy') then + mpoptions += " --makempy " + end + if getvariable('mpyforce') || getvariable('forcempy') then + mpoptions += " --force " + else + mponame = File.suffixed(mpname,'mpo') + mpyname = File.suffixed(mpname,'mpy') + return false unless File.atleast?(mponame,32) + mpochecksum = State.new.checksum(mponame) + return false if mpochecksum.empty? + # where does the checksum get into the file? + # maybe let texexec do it? + # solution: add one if not present or update when different + if f = File.open(mpyname) then + str = f.gets.chomp + f.close + if str =~ /^\%\s*mpochecksum\s*\:\s*(\d+)/o then + return false if mpochecksum == $1 + end + end + end + return Kpse.runscript('makempy',mpname) + end + + def checkmplabels(mpname) + mpname = File.suffixed(mpname,'mpt') + if File.atleast?(mpname,10) && (mp = File.open(mpname)) then + labels = Hash.new + while str = mp.gets do + if str =~ /%\s*setup\s*:\s*(.*)/o then + t = $1 + else + t = '' + end + if str =~ /%\s*figure\s*(\d+)\s*:\s*(.*)/o then + unless t.empty? then + labels[$1] += "#{t}\n" + t = '' + end + labels[$1] += "$2\n" + end + end + mp.close + return labels if labels.size>0 + end + return nil + end + +end + +class String + + def standard? + begin + self == 'standard' + rescue + false + end + end + +end diff --git a/scripts/context/ruby/base/tool.rb b/scripts/context/ruby/base/tool.rb new file mode 100644 index 000000000..c9e6a1890 --- /dev/null +++ b/scripts/context/ruby/base/tool.rb @@ -0,0 +1,305 @@ +# module : base/tool +# copyright : PRAGMA Advanced Document Engineering +# version : 2002-2005 +# author : Hans Hagen +# +# project : ConTeXt / eXaMpLe +# concept : Hans Hagen +# info : j.hagen@xs4all.nl +# www : www.pragma-ade.com + +require 'timeout' +require 'socket' +require 'rbconfig' + +module Tool + + $constructedtempdir = '' + + def Tool.constructtempdir(create,mainpath='',fallback='') + begin + mainpath += '/' unless mainpath.empty? + timeout(5) do + begin + t = Time.now + u = t.usec.to_s % [1..2] [0..3] + pth = t.strftime("#{mainpath}%Y%m%d-%H%M%S-#{u}-#{Process.pid}") + if pth == $constructedtempdir + # sleep(0.01) + retry + end + Dir.mkdir(pth) if create + $constructedtempdir = pth + return pth + rescue + # sleep(0.01) + retry + end + end + rescue TimeoutError + # ok + rescue + # ok + end + unless fallback.empty? + begin + pth = "#{mainpath}#{fallback}" + mkdir(pth) if create + $constructedtempdir = path + return pth + rescue + return '.' + end + else + return '.' + end + + end + + def Tool.findtempdir(*vars) + constructtempdir(false,*vars) + end + + def Tool.maketempdir(*vars) + constructtempdir(true,*vars) + end + + # print maketempdir + "\n" + # print maketempdir + "\n" + # print maketempdir + "\n" + # print maketempdir + "\n" + # print maketempdir + "\n" + + + def Tool.ruby_platform + case RUBY_PLATFORM + when /(mswin|bccwin|mingw|cygwin)/i then 'mswin' + when /(linux)/i then 'linux' + when /(netbsd|unix)/i then 'unix' + when /(darwin|rhapsody|nextstep)/i then 'macosx' + else 'unix' + end + end + + $defaultlineseparator = $/ # $RS in require 'English' + + def Tool.file_platform(filename) + + begin + if f = open(filename,'rb') then + str = f.read(4000) + f.close + nn = str.count("\n") + nr = str.count("\r") + if nn>nr then + return 2 + elsif nn0 then + return buffer.slice(0..length-1) + else + # when the path or file does not exist, nothing is returned + # so we try to handle the path separately from the basename + basename = File.basename(filename) + pathname = File.dirname(filename) + length = filemethod.call(pathname,buffer,260) + if length>0 then + return buffer.slice(0..length-1) + '/' + basename + else + return filename + end + end + else + # no danger + return filename + end + end + + def Tool.shortpathname(filename) + dowith_pathname(filename,GetShortPathName) + end + + def Tool.longpathname(filename) + dowith_pathname(filename,GetLongPathName) + end + + else + + def Tool.shortpathname(filename) + filename + end + + def Tool.longpathname(filename) + filename + end + + end + + # print shortpathname("C:/Program Files/ABBYY FineReader 6.0/matrix.str")+ "!\n" + # print shortpathname("C:/Program Files/ABBYY FineReader 6.0/matrix.strx")+ "!\n" + + def Tool.checksuffix(old) + + return old unless test(?f,old) + + new = old + + unless new =~ /\./io # no suffix + f = open(filename,'rb') + if str = f.gets + case str + when /^\%\!PS/io + # logging.report(filename, 'analyzed as EPS') + new = new + '.eps' + when /^\%PDF/io + # logging.report(filename, 'analyzed as PDF') + new = new + '.pdf' + else + # logging.report(filename, 'fallback as TIF') + new = new + '.tif' + end + end + f.close + end + + new.sub!(/\.jpeg$/io) do + '.jpg' + end + new.sub!(/\.tiff$/io) do + '.tif' + end + new.sub!(/\.ai$/io) do + '.eps' + end + new.sub!(/\.ai(.*?)$/io) do + '-' + $1 + '.eps' + end + new + + end + + def Tool.cleanfilename(old,logging=nil) + + return old unless test(?f,old) + + new = checksuffix(simplefilename(old)) + unless new == old + begin + File.rename(old,new) + logging.report("renaming fuzzy name #{old} to #{new}") unless logging + return old + rescue + logging.report("unable to rename fuzzy name #{old} to #{new}") unless logging + end + end + return new + + end + + def Tool.preventduplicates(old,logging=nil) + + return false unless test(?f,old) + + if old =~ /\.(tif|jpg|png|tiff)$/io + suffix = $1 + new = old + newn, news = new.split('.') + if test(?e,'newn.eps') + new = newn + '-' + suffix + '.' + suffix + begin + File.rename(old,new) + logging.report("renaming duplicate #{old} to #{new}") unless logging + return true + rescue + logging.report("unable to rename duplicate #{old} to #{new}") unless logging + end + end + end + return false + + end + + def Tool.servername + host = Socket::gethostname + begin + Socket::gethostbyname(host)[0] + rescue + host + end + end + + # print file_platform(ARGV[0]) + +end diff --git a/scripts/context/ruby/base/variables.rb b/scripts/context/ruby/base/variables.rb new file mode 100644 index 000000000..0c495d22d --- /dev/null +++ b/scripts/context/ruby/base/variables.rb @@ -0,0 +1,37 @@ +# module : base/variables +# copyright : PRAGMA Advanced Document Engineering +# version : 2002-2005 +# author : Hans Hagen +# +# project : ConTeXt / eXaMpLe +# concept : Hans Hagen +# info : j.hagen@xs4all.nl +# www : www.pragma-ade.com + +# ['base/tool','tool'].each do |r| begin require r ; rescue Exception ; else break ; end ; end + +require 'base/tool' + +module Variables + + def setvariable(key,value='') + @variables[key] = value + end + + def replacevariable(key,value='') + @variables[key] = value if @variables.key?(key) + end + + def getvariable(key,default='') + if @variables.key?(key) then @variables[key] else default end + end + + def report(*str) + @logger.report(*str) + end + + def debug(*str) + @logger.debug(str) + end + +end diff --git a/scripts/context/ruby/ctxtools.rb b/scripts/context/ruby/ctxtools.rb index 9953087d8..1a87e298e 100644 --- a/scripts/context/ruby/ctxtools.rb +++ b/scripts/context/ruby/ctxtools.rb @@ -2,24 +2,37 @@ # program : ctxtools # copyright : PRAGMA Advanced Document Engineering +# version : 2004-2005 # author : Hans Hagen +# +# project : ConTeXt / eXaMpLe +# concept : Hans Hagen +# info : j.hagen@xs4all.nl +# www : www.pragma-ade.com # This script will harbor some handy manipulations on context # related files. # todo: move scite here +# +# todo: move kpse call to kpse class/module + -banner = ['CtxTools', 'version 1.2.1', '2004/2005', 'PRAGMA ADE/POD'] +banner = ['CtxTools', 'version 1.2.2', '2004/2005', 'PRAGMA ADE/POD'] unless defined? ownpath ownpath = $0.sub(/[\\\/][a-z0-9\-]*?\.rb/i,'') $: << ownpath end -require 'ftools' -require 'xmpl/switch' -require 'exa/logger' +require 'base/switch' +require 'base/logger' +require 'base/system' + require 'rexml/document' +require 'ftools' + +exit if defined?(REQUIRE2LIB) class String @@ -362,18 +375,19 @@ class Commands public - def purgefiles + def purgefiles(all=false) pattern = @commandline.arguments - purgeall = @commandline.option("all") + purgeall = @commandline.option("all") || all $dontaskprefixes.push(Dir.glob("mpx-*")) $dontaskprefixes.flatten! $dontaskprefixes.sort! if purgeall then - forsuresuffixes.push(texnonesuffixes) - texnonesuffixes = [] + $forsuresuffixes.push($texnonesuffixes) + $texnonesuffixes = [] + $forsuresuffixes.flatten! end if ! pattern || pattern.empty? then @@ -442,6 +456,10 @@ class Commands end + def purgeall + purgefiles(true) # for old times sake + end + private $removedfiles = 0 @@ -680,25 +698,29 @@ class Commands pdffile = "#{filename}.pdf" tuofile = "#{filename}.tuo" if FileTest.file?(pdffile) then - prevline, n = '', 0 - if (pdf = File.open(pdffile)) && (tuo = File.open(tuofile,'a')) then - report('filtering page object numbers') - pdf.binmode - while line = pdf.gets do - line.chomp - # typical pdftex search - if (line =~ /\/Type \/Page/o) && (prevline =~ /^(\d+)\s+0\s+obj/o) then - p = $1 - n += 1 - tuo.puts("\\objectreference{PDFP}{#{n}}{#{p}}{#{n}}\n") - else - prevline = line + begin + prevline, n = '', 0 + if (pdf = File.open(pdffile)) && (tuo = File.open(tuofile,'a')) then + report('filtering page object numbers') + pdf.binmode + while line = pdf.gets do + line.chomp + # typical pdftex search + if (line =~ /\/Type \/Page/o) && (prevline =~ /^(\d+)\s+0\s+obj/o) then + p = $1 + n += 1 + tuo.puts("\\objectreference{PDFP}{#{n}}{#{p}}{#{n}}\n") + else + prevline = line + end end end + pdf.close + tuo.close + report("number of pages : #{n}") + rescue + report("fatal error in filtering pages") end - pdf.close - tuo.close - report("number of pages : #{n}") end end @@ -836,15 +858,18 @@ class Language rmename = "lang-#{@language}.rme" logname = "lang-#{@language}.log" + desname = "lang-all.xml" + @data.gsub!(/\\[nc]\{(.+?)\}/) do $1 end @data.gsub!(/\{\}/) do '' end @data.gsub!(/\n+/mo) do "\n" end @read.gsub!(/\n+/mo) do "\n" end description = '' + commentfile = rmename.dup begin - desfile = `kpsewhich -progname=context lang-all.xml`.chomp + desfile = `kpsewhich -progname=context #{desname}`.chomp if f = File.new(desfile) then if doc = REXML::Document.new(f) then if e = REXML::XPath.first(doc.root,"/descriptions/description[@language='#{@language}']") then @@ -856,6 +881,7 @@ class Language description = '' else unless description.empty? then + commentfile = desname.dup str = "