From 6088773e5fd2027fcfdd5f47b37a0403944d04f9 Mon Sep 17 00:00:00 2001 From: MichaelDaum Date: Mon, 7 May 2012 10:33:39 +0000 Subject: [PATCH] Item11808: reverting perltidy git-svn-id: http://svn.foswiki.org/trunk/MathModePlugin@14732 0b4bb1d4-4e5a-0410-9cc4-b2b747904278 --- lib/Foswiki/Plugins/MathModePlugin.pm | 50 +- lib/Foswiki/Plugins/MathModePlugin/Core.pm | 695 +++++++++----------- lib/Foswiki/Plugins/MathModePlugin/build.pl | 6 +- 3 files changed, 343 insertions(+), 408 deletions(-) diff --git a/lib/Foswiki/Plugins/MathModePlugin.pm b/lib/Foswiki/Plugins/MathModePlugin.pm index 9722ba4..7dedb1c 100644 --- a/lib/Foswiki/Plugins/MathModePlugin.pm +++ b/lib/Foswiki/Plugins/MathModePlugin.pm @@ -11,7 +11,7 @@ # This program is distributed in the hope that it will be useful, # but WITHOUT ANY WARRANTY; without even the implied warranty of # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the -# GNU General Public License for more details, published at +# GNU General Public License for more details, published at # http://www.gnu.org/copyleft/gpl.html # ############################################################################### @@ -23,54 +23,54 @@ use vars qw( $web $topic $VERSION $RELEASE $core %FoswikiCompatibility $NO_PREFS_IN_TOPIC $SHORTDESCRIPTION ); -$VERSION = '$Rev$'; -$RELEASE = '4.02'; +$VERSION = '$Rev$'; +$RELEASE = '4.02'; $NO_PREFS_IN_TOPIC = 1; -$SHORTDESCRIPTION = 'Include LaTeX formatted math in your Foswiki pages'; +$SHORTDESCRIPTION = 'Include LaTeX formatted math in your Foswiki pages'; $FoswikiCompatibility{endRenderingHandler} = 1.1; ############################################################################### sub initPlugin { - ( $topic, $web ) = @_; + ($topic, $web) = @_; - undef $core; + undef $core; - # Tell WyswiygPlugin to protect ... markup - if ( defined &Foswiki::Plugins::WysiwygPlugin::addXMLTag ) { - Foswiki::Plugins::WysiwygPlugin::addXMLTag( 'latex', sub { 1 } ); - } + # Tell WyswiygPlugin to protect ... markup + if (defined &Foswiki::Plugins::WysiwygPlugin::addXMLTag) { + Foswiki::Plugins::WysiwygPlugin::addXMLTag('latex', sub { 1 } ); + } - return 1; + return 1; } ############################################################################### sub commonTagsHandler { ### my ( $text, $topic, $web ) = @_; - $_[0] =~ s/%\\\[(.*?)\\\]%/&handleMath($1,0)/geo; - $_[0] =~ s/%\$(.*?)\$%/&handleMath($1,1)/geo; - $_[0] =~ s/(.*?)<\/latex>/&handleMath($2,2,$1)/geos; + $_[0] =~ s/%\\\[(.*?)\\\]%/&handleMath($1,0)/geo; + $_[0] =~ s/%\$(.*?)\$%/&handleMath($1,1)/geo; + $_[0] =~ s/(.*?)<\/latex>/&handleMath($2,2,$1)/geos; } ############################################################################### sub getCore { - return $core if $core; + return $core if $core; + + require Foswiki::Plugins::MathModePlugin::Core; + $core = new Foswiki::Plugins::MathModePlugin::Core; - require Foswiki::Plugins::MathModePlugin::Core; - $core = new Foswiki::Plugins::MathModePlugin::Core; - - return $core; + return $core; } ############################################################################### -sub handleMath { - return getCore()->handleMath( $web, $topic, @_ ); +sub handleMath { + return getCore()->handleMath($web, $topic, @_); } ############################################################################### -sub postRenderingHandler { - return unless $core; # no math - $core->postRenderingHandler( $web, $topic, @_ ); +sub postRenderingHandler { + return unless $core; # no math + $core->postRenderingHandler($web, $topic, @_) } - + 1; diff --git a/lib/Foswiki/Plugins/MathModePlugin/Core.pm b/lib/Foswiki/Plugins/MathModePlugin/Core.pm index 4ef3a57..7a16ef0 100644 --- a/lib/Foswiki/Plugins/MathModePlugin/Core.pm +++ b/lib/Foswiki/Plugins/MathModePlugin/Core.pm @@ -11,7 +11,7 @@ # This program is distributed in the hope that it will be useful, # but WITHOUT ANY WARRANTY; without even the implied warranty of # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the -# GNU General Public License for more details, published at +# GNU General Public License for more details, published at # http://www.gnu.org/copyleft/gpl.html # ############################################################################### @@ -22,187 +22,163 @@ use strict; use Digest::MD5 qw( md5_hex ); use File::Copy qw( move ); use File::Temp (); - #use FindBin (); use Foswiki::Sandbox (); -use constant DEBUG => 0; # toggle me +use constant DEBUG => 0; # toggle me ############################################################################### # static sub writeDebug { - - #&Foswiki::Func::writeDebug('- MathModePlugin - '.$_[0]) if DEBUG; - print STDERR '- MathModePlugin - ' . $_[0] . "\n" if DEBUG; + #&Foswiki::Func::writeDebug('- MathModePlugin - '.$_[0]) if DEBUG; + print STDERR '- MathModePlugin - '.$_[0]."\n" if DEBUG; } ############################################################################### sub new { - my $class = shift; - - my $this = { - hashedMathStrings => {}, - - # contains the math strings, indexed by their hash code - - fgColors => {}, - - # contains the foreground color of a math string - - bgColor => {}, - - # contains the background color for all formulas - - sizes => {}, - - # font size of a math string, can be; can be - # tiny, scriptsize, footnotesize, small, normalsize, large, Large, LARGE, - # huge or Huge - - hashCodeLength => $Foswiki::cfg{MathModePlugin}{HashCodeLength} || 32, - - # length of the hash code. If you switch to a different - # hash function, you will likely have to change this - - imagePrefix => $Foswiki::cfg{MathModePlugin}{ImagePrefix} - || '_MathModePlugin_', + my $class = shift; - # string to be prepended to any auto-generated image + my $this = { + hashedMathStrings => {}, + # contains the math strings, indexed by their hash code - latex2Img => $Foswiki::cfg{MathModePlugin}{Latex2Img}, + fgColors => {}, + # contains the foreground color of a math string - # the command to convert latex to a png or gif + bgColor => {}, + # contains the background color for all formulas - scaleFactor => $Foswiki::cfg{MathModePlugin}{ScaleFactor} || 1.2, + sizes => {}, + # font size of a math string, can be; can be + # tiny, scriptsize, footnotesize, small, normalsize, large, Large, LARGE, + # huge or Huge - # factor to scale images; - # may be overridden by a LATEXSCALEFACTOR preference variable + hashCodeLength => $Foswiki::cfg{MathModePlugin}{HashCodeLength} || 32, + # length of the hash code. If you switch to a different + # hash function, you will likely have to change this - latexFGColor => $Foswiki::cfg{MathModePlugin}{LatexFGColor} || 'black', + imagePrefix => $Foswiki::cfg{MathModePlugin}{ImagePrefix} || '_MathModePlugin_', + # string to be prepended to any auto-generated image - # default text color + latex2Img => $Foswiki::cfg{MathModePlugin}{Latex2Img}, + # the command to convert latex to a png or gif - latexBGColor => $Foswiki::cfg{MathModePlugin}{LatexBGColor} || 'white', + scaleFactor => $Foswiki::cfg{MathModePlugin}{ScaleFactor} || 1.2, + # factor to scale images; + # may be overridden by a LATEXSCALEFACTOR preference variable - # default background color + latexFGColor => $Foswiki::cfg{MathModePlugin}{LatexFGColor} || 'black', + # default text color - latexFontSize => $Foswiki::cfg{MathModePlugin}{LatexFontSize} - || 'normalsize', + latexBGColor => $Foswiki::cfg{MathModePlugin}{LatexBGColor} || 'white', + # default background color - # default text color + latexFontSize => $Foswiki::cfg{MathModePlugin}{LatexFontSize} || 'normalsize', + # default text color - latexPreamble => $Foswiki::cfg{MathModePlugin}{Preamble} - || '\usepackage{latexsym}', + latexPreamble => $Foswiki::cfg{MathModePlugin}{Preamble} || + '\usepackage{latexsym}', + # latex preamble, e.g. to include additional packages; may be + # overridden by a LATEXPREAMBLE preference variable; + # Example: \usepackage{mathptmx} to change the math font - # latex preamble, e.g. to include additional packages; may be - # overridden by a LATEXPREAMBLE preference variable; - # Example: \usepackage{mathptmx} to change the math font + imageType => $Foswiki::cfg{MathModePlugin}{ImageType} || 'png', + # extension of the image type; + # may be overridden by a LATEXIMAGETYPE preference variable - imageType => $Foswiki::cfg{MathModePlugin}{ImageType} || 'png', + @_ + }; - # extension of the image type; - # may be overridden by a LATEXIMAGETYPE preference variable - - @_ - }; - - return bless( $this, $class ); + return bless($this, $class); } ############################################################################### # delayed initialization sub init { - my ( $this, $web, $topic ) = @_; - - # prevent a doubled invokation - return if $this->{isInitialized}; - $this->{isInitialized} = 1; - - # get preverences - my $value = Foswiki::Func::getPreferencesValue('LATEXSCALEFACTOR'); - $this->{scaleFactor} = $value if $value; - - $value = Foswiki::Func::getPreferencesValue('LATEXIMAGETYPE'); - $this->{imageType} = $value if $value; - $this->{imageType} = 'png' unless $this->{imageType} =~ /^(png|gif)$/i; - - $value = Foswiki::Func::getPreferencesValue('LATEXPREAMBLE'); - $this->{latexPreamble} = $value if $value; - - $value = Foswiki::Func::getPreferencesValue('LATEXBGCOLOR'); - $this->{latexBGColor} = $value if $value; - - $value = Foswiki::Func::getPreferencesValue('LATEXFGCOLOR'); - $this->{latexFGColor} = $value if $value; - - $value = Foswiki::Func::getPreferencesValue('LATEXFONTSIZE'); - $this->{latexFontSize} = $value if $value; - - # get the current cgi - my $pathInfo = $ENV{'PATH_INFO'} || ''; - my $script = $ENV{'REQUEST_URI'} || ''; - if ( $script =~ /^.*?\/([^\/]+)$pathInfo.*$/ ) { - $script = $1; + my ($this, $web, $topic) = @_; + + # prevent a doubled invokation + return if $this->{isInitialized}; + $this->{isInitialized} = 1; + + # get preverences + my $value = Foswiki::Func::getPreferencesValue('LATEXSCALEFACTOR'); + $this->{scaleFactor} = $value if $value; + + $value = Foswiki::Func::getPreferencesValue('LATEXIMAGETYPE'); + $this->{imageType} = $value if $value; + $this->{imageType} = 'png' unless $this->{imageType} =~ /^(png|gif)$/i; + + $value = Foswiki::Func::getPreferencesValue('LATEXPREAMBLE'); + $this->{latexPreamble} = $value if $value; + + $value = Foswiki::Func::getPreferencesValue('LATEXBGCOLOR'); + $this->{latexBGColor} = $value if $value; + + $value = Foswiki::Func::getPreferencesValue('LATEXFGCOLOR'); + $this->{latexFGColor} = $value if $value; + + $value = Foswiki::Func::getPreferencesValue('LATEXFONTSIZE'); + $this->{latexFontSize} = $value if $value; + + # get the current cgi + my $pathInfo = $ENV{'PATH_INFO'} || ''; + my $script = $ENV{'REQUEST_URI'} || ''; + if ($script =~ /^.*?\/([^\/]+)$pathInfo.*$/) { + $script = $1; + } else { + $script = 'view'; + } + $this->{cgiScript} = $script; + + # compute filenname length of an image + $this->{imageFileNameLength} = + $this->{hashCodeLength}+length($this->{imageType})+length($this->{imagePrefix})+1; + + # get refresh request + my $query = Foswiki::Func::getCgiQuery(); + my $refresh = $query->param('refresh') || ''; + $this->{doRefresh} = ($refresh =~ /^(on|yes|1)$/)?1:0; + + # create the topic pubdir if it does not exist already + my $pubDir = $Foswiki::cfg{PubDir}; + my $topicPubDir = $pubDir; + foreach my $dir (split(/\//, "$web/$topic")) { + $topicPubDir .= '/'.$dir; + $topicPubDir = normalizeFileName($topicPubDir); + unless (-d $topicPubDir) { + mkdir $topicPubDir or die "can't create directory $topicPubDir"; } - else { - $script = 'view'; + } + $this->{topicPubDir} = $topicPubDir; + + # default to supplied tools + unless ($this->{latex2Img}) { + # Build a list of paths that might be the tools directory + # SMELL: There should be an API to provide the path to /tools + my @possibleToolsDirs = (); + if (exists $Foswiki::cfg{LocalesDir}) { + # Assumes that /tools and /locale are in the same directory + push @possibleToolsDirs, "$Foswiki::cfg{LocalesDir}/../tools"; } - $this->{cgiScript} = $script; - - # compute filenname length of an image - $this->{imageFileNameLength} = - $this->{hashCodeLength} + - length( $this->{imageType} ) + - length( $this->{imagePrefix} ) + 1; - - # get refresh request - my $query = Foswiki::Func::getCgiQuery(); - my $refresh = $query->param('refresh') || ''; - $this->{doRefresh} = ( $refresh =~ /^(on|yes|1)$/ ) ? 1 : 0; - - # create the topic pubdir if it does not exist already - my $pubDir = $Foswiki::cfg{PubDir}; - my $topicPubDir = $pubDir; - foreach my $dir ( split( /\//, "$web/$topic" ) ) { - $topicPubDir .= '/' . $dir; - $topicPubDir = normalizeFileName($topicPubDir); - unless ( -d $topicPubDir ) { - mkdir $topicPubDir or die "can't create directory $topicPubDir"; - } + if (defined $FindBin::Bin) { + # Assumes that /tools and /bin are in the same directory + # Assumes that $FindBin::Bin gives /bin + # - which might be wrong under mod_perl + $FindBin::Bin =~ /(.*)/; #untaint + push @possibleToolsDirs, "$1/../tools"; } - $this->{topicPubDir} = $topicPubDir; - - # default to supplied tools - unless ( $this->{latex2Img} ) { - - # Build a list of paths that might be the tools directory - # SMELL: There should be an API to provide the path to /tools - my @possibleToolsDirs = (); - if ( exists $Foswiki::cfg{LocalesDir} ) { - - # Assumes that /tools and /locale are in the same directory - push @possibleToolsDirs, "$Foswiki::cfg{LocalesDir}/../tools"; - } - if ( defined $FindBin::Bin ) { - - # Assumes that /tools and /bin are in the same directory - # Assumes that $FindBin::Bin gives /bin - # - which might be wrong under mod_perl - $FindBin::Bin =~ /(.*)/; #untaint - push @possibleToolsDirs, "$1/../tools"; - } - - # Look in these directories to see if the default supplied tool is there - my $defaultScriptName = 'MathModePlugin_latex2img'; - POSSIBLE_DIR: for my $possibleToolsDir (@possibleToolsDirs) { - if ( -e "$possibleToolsDir/$defaultScriptName" - and -x "$possibleToolsDir/$defaultScriptName" ) - { - $this->{latex2Img} = "$possibleToolsDir/$defaultScriptName"; - last POSSIBLE_DIR; - } - } + # Look in these directories to see if the default supplied tool is there + my $defaultScriptName = 'MathModePlugin_latex2img'; + POSSIBLE_DIR: for my $possibleToolsDir (@possibleToolsDirs) { + if (-e "$possibleToolsDir/$defaultScriptName" and + -x "$possibleToolsDir/$defaultScriptName") + { + $this->{latex2Img} = "$possibleToolsDir/$defaultScriptName"; + last POSSIBLE_DIR; + } } + } } @@ -210,88 +186,75 @@ sub init { # This function takes a string of math, computes its hash code, and returns a # link to what will be the image representing this math. sub handleMath { - my ( $this, $web, $topic, $text, $inlineFlag, $args ) = @_; - - # store the string in a hash table, indexed by the MD5 hash - $text =~ s/^\s+//go; - $text =~ s/\s+$//go; - - # extract latex options - $args ||= ''; - require Foswiki::Attrs; - my $params = new Foswiki::Attrs($args); - $this->{fgColors}{$text} = $params->{color} || $this->{latexFGColor}; - $this->{bgColor} = $params->{bgcolor} || $this->{latexBGColor}; - - my $size = $params->{size} || ''; - $this->{sizes}{$text} = $size if $size; - - # TODO: add global settings to hash - my $hashCode = - md5_hex( $text . $this->{fgColors}{$text} . $this->{bgColor} . $size ); - $this->{hashedMathStrings}{$hashCode} = $text; - - #writeDebug("hashing '$text' as $hashCode"); - - # construct url path to image - my $url = - Foswiki::Func::getPubUrlPath() . '/' - . $web . '/' - . $topic . '/' - . $this->{imagePrefix} - . $hashCode . '.' - . $this->{imageType}; - - # return a link to an attached image, which we will create later - my $container = $inlineFlag ? 'span' : 'div'; - my $alt = entityEncode($text); - my $result = - '' 
-      . $alt
-      . ''; - $result = - "<$container class='mmpContainer' align='center'>" - . $result - . "<\/$container>" - unless $inlineFlag == 2; - - return $result; + my ($this, $web, $topic, $text, $inlineFlag, $args) = @_; + + # store the string in a hash table, indexed by the MD5 hash + $text =~ s/^\s+//go; + $text =~ s/\s+$//go; + + # extract latex options + $args ||= ''; + require Foswiki::Attrs; + my $params = new Foswiki::Attrs($args); + $this->{fgColors}{$text} = $params->{color} || $this->{latexFGColor}; + $this->{bgColor} = $params->{bgcolor} || $this->{latexBGColor}; + + my $size = $params->{size} || ''; + $this->{sizes}{$text} = $size if $size; + + # TODO: add global settings to hash + my $hashCode = md5_hex($text.$this->{fgColors}{$text}.$this->{bgColor}.$size); + $this->{hashedMathStrings}{$hashCode} = $text; + #writeDebug("hashing '$text' as $hashCode"); + + # construct url path to image + my $url = Foswiki::Func::getPubUrlPath().'/'.$web.'/'.$topic. + '/'.$this->{imagePrefix}.$hashCode.'.'.$this->{imageType}; + + # return a link to an attached image, which we will create later + my $container = $inlineFlag?'span':'div'; + my $alt = entityEncode($text); + my $result = ''.$alt.''; + $result = "<$container class='mmpContainer' align='center'>".$result."<\/$container>" + unless $inlineFlag == 2; + + return $result; } ############################################################################### # from Foswiki.pm sub entityEncode { - my ( $text, $extra ) = @_; - $extra ||= ''; + my ($text, $extra) = @_; + $extra ||= ''; - if (0) { - $text =~ s/([[\x01-\x1f"%&'*<=>@[_\|$extra])/'&#'.ord($1).';'/ge; - } + if (0) { + $text =~ + s/([[\x01-\x1f"%&'*<=>@[_\|$extra])/'&#'.ord($1).';'/ge; + } - $text =~ s/([[\x01-\x1f"%&'*<=>@[_\|$extra])/'&#'.ord($1).';'/ge; + $text =~ + s/([[\x01-\x1f"%&'*<=>@[_\|$extra])/'&#'.ord($1).';'/ge; - return $text; + return $text; } ############################################################################### sub postRenderingHandler { - my ( $this, $web, $topic ) = @_; + my ($this, $web, $topic) = @_; - return unless keys %{ $this->{hashedMathStrings} }; + return unless keys %{$this->{hashedMathStrings}}; - # initialize this call - $this->init( $web, $topic ); + # initialize this call + $this->init($web, $topic); - # check if there are any new images to render - return unless $this->checkImages(); + # check if there are any new images to render + return unless $this->checkImages(); - # do it - my $msg = $this->renderImages() || ''; + # do it + my $msg = $this->renderImages() || ''; - # append to text - $_[3] .= $msg; + # append to text + $_[3] .= $msg; } ############################################################################### @@ -299,80 +262,72 @@ sub postRenderingHandler { # existing files are checkd if they are still in use; # returns the number of images to be re-rendered sub checkImages { - my $this = shift; - - # only delete during a save - my $deleteFiles = - ( $this->{cgiScript} =~ /^save/ || $this->{doRefresh} ) ? 1 : 0; - - #writeDebug("deleteFiles=$deleteFiles, cgiScript=$this->{cgiScript}"); - - # look for existing images, delete old ones - opendir( DIR, $this->{topicPubDir} ) - or die "can't open directory $this->{topicPubDir}"; - my @files = - grep( /$this->{imagePrefix}.*\.$this->{imageType}$/, readdir(DIR) ); - foreach my $fileName (@files) { - $fileName = normalizeFileName($fileName); - - #writeDebug( "found image: $fileName"); - - # is the filename the same length as one of our images? - next unless length($fileName) == $this->{imageFileNameLength}; - - # is the filename composed of the same characters as ours? - my $hashCode = $fileName; - next - unless $hashCode =~ /^$this->{imagePrefix}(.*)\.$this->{imageType}$/; - $hashCode = $1; - next unless length($hashCode) == $this->{hashCodeLength}; - - # is the image still used in the document? - if ( exists( $this->{hashedMathStrings}{$hashCode} ) ) { - - # the image is already there, we don't need to re-render; - # refresh the cache only if we asked for it - unless ( $this->{doRefresh} ) { - - #writeDebug("skipping $this->{hashedMathStrings}{$hashCode}"); - delete $this->{hashedMathStrings}{$hashCode}; - next; - } - } - - # maintenance - next unless $deleteFiles; - $fileName = $this->{topicPubDir} . '/' . $fileName; - - #writeDebug("deleting old image $fileName"); - unlink $fileName or die "can't delete file $fileName"; + my $this = shift; + + # only delete during a save + my $deleteFiles = ($this->{cgiScript} =~ /^save/ || $this->{doRefresh})?1:0; + + #writeDebug("deleteFiles=$deleteFiles, cgiScript=$this->{cgiScript}"); + + # look for existing images, delete old ones + opendir(DIR,$this->{topicPubDir}) or die "can't open directory $this->{topicPubDir}"; + my @files = grep(/$this->{imagePrefix}.*\.$this->{imageType}$/,readdir(DIR)); + foreach my $fileName (@files) { + $fileName = normalizeFileName($fileName); + #writeDebug( "found image: $fileName"); + + # is the filename the same length as one of our images? + next unless length($fileName) == $this->{imageFileNameLength}; + + # is the filename composed of the same characters as ours? + my $hashCode = $fileName; + next unless $hashCode =~ /^$this->{imagePrefix}(.*)\.$this->{imageType}$/; + $hashCode = $1; + next unless length($hashCode) == $this->{hashCodeLength}; + + # is the image still used in the document? + if (exists($this->{hashedMathStrings}{$hashCode} ) ) { + # the image is already there, we don't need to re-render; + # refresh the cache only if we asked for it + unless ($this->{doRefresh}) { + #writeDebug("skipping $this->{hashedMathStrings}{$hashCode}"); + delete $this->{hashedMathStrings}{$hashCode}; + next; + } } - - return scalar( keys %{ $this->{hashedMathStrings} } ); + + # maintenance + next unless $deleteFiles; + $fileName = $this->{topicPubDir}.'/'.$fileName; + #writeDebug("deleting old image $fileName"); + unlink $fileName or die "can't delete file $fileName"; + } + + return scalar(keys %{$this->{hashedMathStrings}}); } ############################################################################### sub renderImages { - my $this = shift; + my $this = shift; - # used for reporting errors - my $msg; + # used for reporting errors + my $msg; - # create temporary storage - my $tempDir = File::Temp::tempdir( CLEANUP => 1 ); - my $tempFile = new File::Temp( DIR => $tempDir ); - chdir $tempDir or die "can't change to temp dir $tempDir"; + # create temporary storage + my $tempDir = File::Temp::tempdir(CLEANUP =>1); + my $tempFile = new File::Temp(DIR=>$tempDir); + chdir $tempDir or die "can't change to temp dir $tempDir"; - # maps math strings' hash codes to the filename latex2html generates - my %imageFile = (); + # maps math strings' hash codes to the filename latex2html generates + my %imageFile = (); - # latex2html names its image img(n).png where (n) is an integer - # we will rename these files, so need to know which math string gets with image - my $imageNumber = 0; + # latex2html names its image img(n).png where (n) is an integer + # we will rename these files, so need to know which math string gets with image + my $imageNumber = 0; - # create the latex file on the fly - print $tempFile "\\documentclass[fleqn,12pt]{article}\n"; - print $tempFile <<'PREAMBLE'; + # create the latex file on the fly + print $tempFile "\\documentclass[fleqn,12pt]{article}\n"; + print $tempFile <<'PREAMBLE'; \usepackage{amsmath} \usepackage[normal]{xcolor} \setlength{\mathindent}{0cm} @@ -383,125 +338,105 @@ sub renderImages { \definecolor{maroon}{rgb}{0.5,0,0} \definecolor{silver}{gray}{0.75} PREAMBLE - print $tempFile $this->{latexPreamble} . "\n"; - print $tempFile '\begin{document}' . "\n"; - print $tempFile '\pagestyle{empty}' . "\n"; - print $tempFile "\\pagecolor" . formatColorSpec( $this->{bgColor} ) . "\n"; - while ( my ( $key, $value ) = each( %{ $this->{hashedMathStrings} } ) ) { - $imageNumber++; - print $tempFile "{\n"; - print $tempFile "\\color" - . formatColorSpec( $this->{fgColors}{$value} ) . "\n" - if $this->{fgColors}{$value}; - if ( $this->{sizes}{$value} ) { - print $tempFile "\\$this->{sizes}{$value}\n"; - } - else { - print $tempFile "\\$this->{latexFontSize}\n" - if $this->{latexFontSize} ne "normalsize"; - } - - # analyze which environment to use - my $environment = 'math'; - $environment = 'multline*' if $value =~ /\\\\/; - $environment = 'eqnarray*' if $value =~ '&\s*=\s*&'; - - #writeDebug("using $environment for $value"); - print $tempFile - "\\begin{$environment}\\displaystyle $value\\end{$environment}\n"; - print $tempFile "}\n"; - - print $tempFile "\\clearpage\n"; - - # remember the filename it ends up - $imageFile{$key} = $tempFile . $imageNumber . '.' . $this->{imageType}; + print $tempFile $this->{latexPreamble}."\n"; + print $tempFile '\begin{document}'."\n"; + print $tempFile '\pagestyle{empty}'."\n"; + print $tempFile "\\pagecolor".formatColorSpec($this->{bgColor})."\n"; + while (my ($key, $value) = each(%{$this->{hashedMathStrings}})) { + $imageNumber++; + print $tempFile "{\n"; + print $tempFile "\\color".formatColorSpec($this->{fgColors}{$value})."\n" + if $this->{fgColors}{$value}; + if ($this->{sizes}{$value}) { + print $tempFile "\\$this->{sizes}{$value}\n"; + } else { + print $tempFile "\\$this->{latexFontSize}\n" + if $this->{latexFontSize} ne "normalsize"; } - print $tempFile "\\end{document}\n"; - - my ( $data, $exit ); - if ( $this->{latex2Img} ) { - - # run latex2html on the latex file we generated - my $latex2ImgCmd = $this->{latex2Img} . ' %FILENAME|F%'; - $latex2ImgCmd .= " $this->{bgColor}"; - $latex2ImgCmd .= ' -D ' . int(100) * $this->{scaleFactor}; - $latex2ImgCmd .= ' --' . $this->{imageType}; - #writeDebug("executing $latex2ImgCmd"); - ( $data, $exit ) = - Foswiki::Sandbox->sysCommand( $latex2ImgCmd, - FILENAME => "$tempFile" ); + # analyze which environment to use + my $environment = 'math'; + $environment = 'multline*' if $value =~ /\\\\/; + $environment = 'eqnarray*' if $value =~ '&\s*=\s*&'; + #writeDebug("using $environment for $value"); + print $tempFile "\\begin{$environment}\\displaystyle $value\\end{$environment}\n"; + print $tempFile "}\n"; + + print $tempFile "\\clearpage\n"; + + # remember the filename it ends up + $imageFile{$key} = $tempFile.$imageNumber.'.'.$this->{imageType}; + } + print $tempFile "\\end{document}\n"; + + my ($data, $exit); + if ($this->{latex2Img}) { + # run latex2html on the latex file we generated + my $latex2ImgCmd = $this->{latex2Img} . ' %FILENAME|F%'; + $latex2ImgCmd .= " $this->{bgColor}"; + $latex2ImgCmd .= ' -D '.int(100)*$this->{scaleFactor}; + $latex2ImgCmd .= ' --'.$this->{imageType}; + + #writeDebug("executing $latex2ImgCmd"); + ($data, $exit) = Foswiki::Sandbox->sysCommand($latex2ImgCmd, FILENAME=>"$tempFile"); + } + else { + $exit = 1; + $data = "MathModePlugin cannot find the latex2img script. Please check the settings in configure"; + } + #writeDebug("exit=$exit"); + #writeDebug("data=$data"); + if ($exit) { + $msg = '
Error during latex2img:
'.
+      $data.'
'; + } else { + # rename the files to the hash code, so we can uniquely identify them + while ((my $key, my $value) = each(%imageFile)) { + my $source = $value; + my $target = $this->{topicPubDir}.'/'.$this->{imagePrefix}.$key.'.'.$this->{imageType}; + #writeDebug("source=$source, target=$target"); + #writeDebug("created new image $target"); + move($source, $target);# or die "can't move $source to $target: $@"; } - else { - $exit = 1; - $data = -"MathModePlugin cannot find the latex2img script. Please check the settings in configure"; - } - - #writeDebug("exit=$exit"); - #writeDebug("data=$data"); - if ($exit) { - $msg = - '
Error during latex2img:
' 
-          . $data
-          . '
'; - } - else { - - # rename the files to the hash code, so we can uniquely identify them - while ( ( my $key, my $value ) = each(%imageFile) ) { - my $source = $value; - my $target = - $this->{topicPubDir} . '/' - . $this->{imagePrefix} - . $key . '.' - . $this->{imageType}; - - #writeDebug("source=$source, target=$target"); - #writeDebug("created new image $target"); - move( $source, $target ) - ; # or die "can't move $source to $target: $@"; - } - } - - # cleanup - $this->{hashedMathStrings} = {}; + } - #File::Temp::cleanup(); # SMELL: n/a in perl < 5.8.8 - close $tempFile; - return $msg; + # cleanup + $this->{hashedMathStrings} = {}; + #File::Temp::cleanup(); # SMELL: n/a in perl < 5.8.8 + close $tempFile; + return $msg; } ############################################################################### # returns the arguments to the latex commands \color or \pagecolor sub formatColorSpec { - my $color = shift; + my $color = shift; - # try to auto-detect the color spec - return "{$color}" if $color =~ /^[a-zA-Z]+$/; # named - return "[HTML]{$color}" if $color =~ /^[a-fA-F0-9]{6}$/; # named - return "$color"; + # try to auto-detect the color spec + return "{$color}" if $color =~ /^[a-zA-Z]+$/; # named + return "[HTML]{$color}" if $color =~ /^[a-fA-F0-9]{6}$/; # named + return "$color"; } ############################################################################### # wrapper sub normalizeFileName { - my $fileName = shift; - - if ( defined &Foswiki::Sandbox::_cleanUpFilePath ) { - return Foswiki::Sandbox::_cleanUpFilePath($fileName); - } - - if ( defined &Foswiki::Sandbox::normalizeFileName ) { - return Foswiki::Sandbox::normalizeFileName($fileName); - } - - if ( defined &Foswiki::normalizeFileName ) { - return Foswiki::normalizeFileName($fileName); - } - - # outch - return $fileName; + my $fileName = shift; + + if (defined &Foswiki::Sandbox::_cleanUpFilePath) { + return Foswiki::Sandbox::_cleanUpFilePath($fileName); + } + + if (defined &Foswiki::Sandbox::normalizeFileName) { + return Foswiki::Sandbox::normalizeFileName($fileName); + } + + if (defined &Foswiki::normalizeFileName) { + return Foswiki::normalizeFileName($fileName); + } + + # outch + return $fileName; } 1; diff --git a/lib/Foswiki/Plugins/MathModePlugin/build.pl b/lib/Foswiki/Plugins/MathModePlugin/build.pl index ea54c88..71d74d9 100755 --- a/lib/Foswiki/Plugins/MathModePlugin/build.pl +++ b/lib/Foswiki/Plugins/MathModePlugin/build.pl @@ -3,14 +3,14 @@ # Build for MathModePlugin # BEGIN { - unshift @INC, split( /:/, $ENV{FOSWIKI_LIBS} ); + unshift @INC, split(/:/, $ENV{FOSWIKI_LIBS}); } use Foswiki::Contrib::Build; # Create the build object -$build = new Foswiki::Contrib::Build('MathModePlugin'); +$build = new Foswiki::Contrib::Build( 'MathModePlugin' ); # Build the target on the command line, or the default target -$build->build( $build->{target} ); +$build->build($build->{target});