Skip to content

Commit

Permalink
Item12938: updates to LatexModePlugin, to remove use of 'defined'
Browse files Browse the repository at this point in the history
git-svn-id: http://svn.foswiki.org/trunk/LatexModePlugin@17761 0b4bb1d4-4e5a-0410-9cc4-b2b747904278
  • Loading branch information
ScottHoge authored and ScottHoge committed Jun 10, 2014
1 parent 151f2bf commit dab1547
Show file tree
Hide file tree
Showing 5 changed files with 51 additions and 38 deletions.
2 changes: 2 additions & 0 deletions data/System/LatexModePlugin.txt
Original file line number Diff line number Diff line change
Expand Up @@ -424,6 +424,8 @@ This plugin is an enhanced version of the Foswiki:Extensions.MathModePlugin main
| Plugin Author: | Foswiki:Main.ScottHoge |
| Plugin Version: | 27 Aug 2009 (v 4.0, SVN:%$VERSION%) |
| Change History: | %ICON{warning}% v4.0 and above requires Foswiki 1.x.x and above %ICON{warning}% <!-- versions below in reverse order --> |
| 10 Jun 2014 (v 4.2) | removed all use of depreciated function 'defined' |
| 15 Jun 2010 (v 4.1) | added option for multiple attachments in !LaTeX rendering |
| 27 Aug 2009 (v 4.0) | Ported to Foswiki and fixed a few bugs. |
| Change History: | %ICON{warning}% v3.x requires TWiki 4.x.x and above %ICON{warning}% <!-- versions below in reverse order --> |
| 27 Oct 2008 (v 3.74) | replaced plugin init with lazy-loading, to fix installer bug |
Expand Down
7 changes: 1 addition & 6 deletions lib/Foswiki/Plugins/LatexModePlugin.pm
Original file line number Diff line number Diff line change
Expand Up @@ -57,15 +57,10 @@ use strict;
use vars qw( $VERSION $RELEASE $debug
$sandbox $initialized
);
# @EXPORT_OK
# $user $installWeb
# $default_density $default_gamma $default_scale $preamble
# $eqn $fig $tbl $use_color @norender $tweakinline $rerender


# number the release version of this plugin
our $VERSION = '$Rev$';
our $RELEASE = '4.0';
our $RELEASE = '4.2';
# our $SHORTDESCRIPTION = 'Enables <nop>LaTeX markup (mathematics and more) in Foswiki topics';

# =========================
Expand Down
13 changes: 7 additions & 6 deletions lib/Foswiki/Plugins/LatexModePlugin/CrossRef.pm
Original file line number Diff line number Diff line change
Expand Up @@ -100,10 +100,11 @@ sub handleReferences
} else {

# my %tblrefs = %{ $LMPc{'tblrefs'} };
my %tblrefs = defined(%{ $LMPc{'tblrefs'} }) ? %{$LMPc{'tblrefs'}} : ();
my %figrefs = defined(%{ $LMPc{'figrefs'} }) ? %{$LMPc{'figrefs'}} : ();
my %eqnrefs = defined(%{ $LMPc{'eqnrefs'} }) ? %{$LMPc{'eqnrefs'}} : ();
my %secrefs = defined(%{ $LMPc{'secrefs'} }) ? %{$LMPc{'secrefs'}} : ();
# my %tblrefs = defined(%{ $LMPc{'tblrefs'} }) ? %{$LMPc{'tblrefs'}} : ();
my %tblrefs = (ref($LMPc{'tblrefs'}) eq 'HASH') ? %{$LMPc{'tblrefs'}} : ();
my %figrefs = (ref($LMPc{'figrefs'}) eq 'HASH') ? %{$LMPc{'figrefs'}} : ();
my %eqnrefs = (ref($LMPc{'eqnrefs'}) eq 'HASH') ? %{$LMPc{'eqnrefs'}} : ();
my %secrefs = (ref($LMPc{'secrefs'}) eq 'HASH') ? %{$LMPc{'secrefs'}} : ();
# my %eqnrefs = %{ $LMPc{'eqnrefs'} };
# print STDERR map {"$_ => $secrefs{$_}\n"} keys %secrefs;

Expand Down Expand Up @@ -197,8 +198,8 @@ sub handleFloat
} else {
## otherwise, generate HTML ...

my %figrefs = defined(%{ $LMPc{'figrefs'} }) ? %{$LMPc{'figrefs'}} : ();
my %tblrefs = defined(%{ $LMPc{'tblrefs'} }) ? %{$LMPc{'tblrefs'}} : ();
my %figrefs = (ref($LMPc{'figrefs'}) eq 'HASH') ? %{$LMPc{'figrefs'}} : ();
my %tblrefs = (ref($LMPc{'tblrefs'}) eq 'HASH') ? %{$LMPc{'tblrefs'}} : ();

my $infrmt = '<tr><td><td align="center">%s</td><td></tr>';
my $cpfrmt = '<tr><td><td width="90%%" id="lmp-caption"> *%s %d*: %s</td><td></tr>';
Expand Down
12 changes: 6 additions & 6 deletions lib/Foswiki/Plugins/LatexModePlugin/Parse.pm
Original file line number Diff line number Diff line change
Expand Up @@ -495,7 +495,7 @@ sub expandComplexBlocks {
$str = extractBlocks($str) if ($str =~ m/\\/);
# convertSimple($str) if ($str =~ m/\\/);

if (defined($cmd)) {
if ($cmd) {
$str =~ s/^(\"|\')|(\"|\')$//g;
printF("Try dynamic command: $cmd($str)\n");
my @z = grep(@regSubs,$cmd);
Expand Down Expand Up @@ -674,9 +674,9 @@ sub extractBlocks {
([\\\w\d\.\=\,\s]+?)
\])?$!!xs) # test for a latex command;
);
$star = '' unless defined($star);
$star = '' unless ($star);
printF( "\nFound command: $cmd$star ") if ($cmd ne '');
(defined($opts) and ($opts ne '') ) ?
(($opts) and ($opts ne '') ) ?
printF(" opts = $opts \n") : printF("\n");

if ($cmd =~ m!\\[\`\"\'\^\~\.duvtbH]$!) {
Expand All @@ -699,7 +699,7 @@ sub extractBlocks {
$t = ' %SECLABEL{'.$t.'}% ';
$txt =~ s/(---\++\!?\s+)([\w\s\$\%\\]+)$/$1$t$2/s;
} else {
# (defined($opts)) ?
# ($opts) ?
# $b =~ s/\$cmd\*?\Q$opts\E//;
# $b =~ s/\$cmd\*?//;
$txt .= $b;
Expand All @@ -714,8 +714,8 @@ sub extractBlocks {
my $s1 = "%BEGINLATEX<nop>% ";
# $b =~ s/(\\[\"\'\`\^\~\.\w]+)$/$s1$1/;
$s1 .= $cmd;
$s1 .= $star if defined($star);
$s1 .= $opts if defined($opts);
$s1 .= $star if ($star);
$s1 .= $opts if ($opts);
$b = $s1.$b;
# if the first character of the next block is a brace, it
# likely means we have a complex command... So group
Expand Down
55 changes: 35 additions & 20 deletions lib/Foswiki/Plugins/LatexModePlugin/Render.pm
Original file line number Diff line number Diff line change
Expand Up @@ -144,7 +144,10 @@ sub handleLatex

my %LMPc = %{ Foswiki::Func::getContext()->{'LMPcontext'} };

my %eqnrefs = defined(%{ $LMPc{'eqnrefs'} }) ? %{$LMPc{'eqnrefs'}} : ();
my %eqnrefs = ();
if ( $LMPc{'eqnrefs'} ) {
%eqnrefs = %{$LMPc{'eqnrefs'}};
}

# remove latex-common HTML entities from within math env
$math_string =~ s/\<br\s*\/\>//og;
Expand Down Expand Up @@ -180,7 +183,8 @@ sub handleLatex
# type inputs). alpha-numeric OK. slash, space, and brackets
# are valid in preamble. need semi-colon in eqn lables!
# allow '-' and '_' in eqn labels too.
$b =~ m/([\.\\\w\s\:\-\_\{\}]+)/;
# added a ',', to allow multiple attachments.
$b =~ m/([\,\.\\\w\s\:\-\_\{\}]+)/;
$b = $1;

$opts{$a} = $b;
Expand Down Expand Up @@ -422,7 +426,7 @@ sub createTempLatexFiles {

# restore the declared rendering options
my %opts = %{ $markup_opts{$key} };
# my %opts = defined($LMPc{'markup_opts'}{$key}) ? %{$LMPc{'markup_opts'}{$key}} : ();
# my %opts = ($LMPc{'markup_opts'}{$key}) ? %{$LMPc{'markup_opts'}{$key}} : ();

$value =~ s/\n\s*?\n/\n/gs;
# disable commands flagged as 'do not render'
Expand All @@ -432,20 +436,26 @@ sub createTempLatexFiles {
}

if( exists($opts{'attachment'}) ) {

$markup_opts{$key}->{'attachment'} = $opts{'attachment'};
# copy image attachments to the working directory

&Foswiki::Func::writeDebug( "copy opts{'attachment'} = ".$opts{'attachment'} ) if $debug;
foreach my $fname (split(/,/,$opts{'attachment'})) {

my ($ext,$af) = ('','');
my @extlist = ('','.eps','.eps.gz','.pdf','.png','.jpg');

if ( ( $Foswiki::Plugins::VERSION < 1.1 ) or
( $bypassattach ) ) {
# filesystem interface

$af = join( $pathSep, &Foswiki::Func::getPubDir(),
$LMPc{'web'}, $LMPc{'topic'},
$opts{'attachment'} );

$af = Foswiki::Sandbox::normalizeFileName( $af );
$fname );

$af = Foswiki::Sandbox::normalizeFileName( $af );
print "render attachment name: $fname\n";
foreach my $e (@extlist) {
$ext = $e;
if (-f $af.$ext) {
Expand All @@ -454,43 +464,45 @@ sub createTempLatexFiles {
# copy( $af.$ext, $LATEXWDIR ) || do {
copy( $af.$ext, "." ) || do {
&Foswiki::Func::writeDebug( "LatexModePlugin: copy failed ".$! );
$value = "attachment \{".$markup_opts{$key}->{'attachment'}."\} \ not found";
$value .= "attachment \{".$af.$ext."\} \ not found";
};
$markup_opts{$key}->{'attachment'} .= $ext;

if ($ext ne '') {
# if the Plugin chooses the extension, then
# set the rendering engine as well.
$markup_opts{$key}->{'attachment'} =~ s/$fname/$fname$ext/;
if ($ext =~ m/\.eps/) {
$markup_opts{$key}->{'engine'} = 'ps';
} else {
$markup_opts{$key}->{'engine'} = 'pdf';
}
}
$fname .= $ext;

last;
}
}
} else {
# database interface
my $ext;
my $af= $opts{'attachment'};
my $af= $fname;
foreach my $e (@extlist) {
$ext = $e;
if ( Foswiki::Func::attachmentExists( $LMPc{'web'},
$LMPc{'topic'},
$af.$ext ) ) {

$markup_opts{$key}->{'attachment'} .= $ext;
if ($ext ne '') {
# if the Plugin chooses the extension, then
# set the rendering engine as well.
$markup_opts{$key}->{'attachment'} =~ s/$fname/$fname$ext/;
if ($ext =~ m/\.eps/) {
$markup_opts{$key}->{'engine'} = 'ps';
} else {
$markup_opts{$key}->{'engine'} = 'pdf';
}
}
$fname .= $ext;

open(F,">".'.'.$pathSep.$af.$ext);
print F Foswiki::Func::readAttachment( $LMPc{'web'},
Expand All @@ -501,11 +513,12 @@ sub createTempLatexFiles {
}
}
}
} # end of copy attachment piece
$value = " (attachment ".$markup_opts{$key}->{'attachment'}." not found) "
if ( exists($markup_opts{$key}->{'attachment'}) and
!(-f $markup_opts{$key}->{'attachment'}) );

$value .= " (attachment ".$fname." not found) "
if ( exists($markup_opts{$key}->{'attachment'}) and
!(-f $fname) );
}
} # end of copy attachment piece

&Foswiki::Func::writeDebug( "LatexModePlugin: ".
$value . " :: " .
Expand Down Expand Up @@ -571,7 +584,7 @@ sub renderEquations {
#&Foswiki::Func::writeDebug( "- LatexModePlugin: @revinfo" ) if $debug;

#check if there was any math in this document
return unless defined( $LMPc{'hashed_math_strings'} );
return unless ( $LMPc{'hashed_math_strings'} );
# return unless scalar( keys( %hashed_math_strings ) );

## 'halt-on-error' is not supported in older versions of tetex, so check to see if it exists:
Expand Down Expand Up @@ -620,7 +633,7 @@ sub renderEquations {
# database interface
my ( $meta, undef ) = Foswiki::Func::readTopic( $LMPc{'web'}, $LMPc{'topic'} );

if ( defined( $meta->{FILEATTACHMENT} ) ) {
if ( $meta->{FILEATTACHMENT} ) {
foreach my $c ( @{ $meta->{FILEATTACHMENT} } ) {
$extfiles{$c}->{name} = $c->{name};
$extfiles{$c}->{date} = $c->{date} || 0;
Expand Down Expand Up @@ -826,7 +839,7 @@ sub renderEquations {
$_[0] .= "<br>Latex rendering error!! pdf file was not created.<br>";

$_[0] .= "<pre>";
open(LF,"$LATEXBASENAME.log");
open(LF,"pdf_$LATEXBASENAME.log");
while (<LF>) {
$_[0] .= $_ if (m/Error/);
}
Expand All @@ -845,10 +858,12 @@ sub renderEquations {
my %opts = %{ $markup_opts{$key} };

if( exists($opts{'attachment'}) ) {
&Foswiki::Func::writeDebug( "unlink opts{'attachment'} = ".$opts{'attachment'} ) if $debug;
# delete image attachments from the working directory
my $af = join( '/', $LATEXWDIR,
$opts{'attachment'} );
unlink($af);
foreach my $fname (split(/,/,$opts{'attachment'})) {
my $af = join( '/', $LATEXWDIR, $fname );
unlink($af);
}
}

if ( $opts{'engine'} eq 'mimetex' ) {
Expand Down

0 comments on commit dab1547

Please sign in to comment.