File Coverage

blib/lib/TeX/AutoTeX/HyperTeX.pm
Criterion Covered Total %
statement 3 122 2.4
branch 0 66 0.0
condition 0 15 0.0
subroutine 1 3 33.3
pod 2 2 100.0
total 6 208 2.8


line stmt bran cond sub pod time code
1             package TeX::AutoTeX::HyperTeX;
2              
3             #
4             # $Id: HyperTeX.pm,v 1.10.2.7 2011/01/27 18:42:28 thorstens Exp $
5             # $Revision: 1.10.2.7 $
6             # $Source: /cvsroot/arxivlib/arXivLib/lib/TeX/AutoTeX/HyperTeX.pm,v $
7             #
8             # $Date: 2011/01/27 18:42:28 $
9             # $Author: thorstens $
10             #
11              
12 2     2   12 use strict;
  2         4  
  2         5040  
13             ### use warnings;
14              
15             our ($VERSION) = '$Revision: 1.10.2.7 $' =~ m{ \$Revision: \s+ (\S+) }x;
16              
17             sub copy_source_from_hyper {
18 0     0 1   my ($this_type, $file, $dir, $log, $local_hyper_transform) = @_;
19              
20 0           my $protect = q{};
21 0           my $stripping = 0;
22              
23 0 0 0       if ($this_type eq 'TYPE_LATEX' ||
      0        
24             $this_type eq 'TYPE_LATEX2e' ||
25             $this_type eq 'TYPE_PDFLATEX') {
26 0           $protect = '\\protect';
27 0           $stripping = 1;
28             }
29              
30 0           my $customfilter = 0;
31 0 0         if (ref($local_hyper_transform) =~ /Filter/) {
32 0           $customfilter = 1;
33 0           $local_hyper_transform->{protect} = $protect;
34             }
35              
36 0           my $checktexsis = 0;
37 0 0         if ($file !~ /texsis/) {
38 0           $checktexsis = 1;
39             }
40              
41             # aipproc often does not work with hypertex
42             # supercite, crckapb10, crckapb..., lamuphys do not work with hypertex
43             # heron2e.sty style does not work with hypertex
44 0           my $incompatiblemacropackages = qr/aipproc|supercite|crckapb|lamuphys|heron2e/;
45              
46 0 0         open (my $WITH_HYPER, '>', "$dir/$file.with_hyper")
47             || $log->error("failed to create '$file.with_hyper': $!");
48 0 0         open (my $WITHOUT_HYPER, '>', "$dir/$file.without_hyper")
49             || $log->error("failed to create '$file.without_hyper': $!");
50 0 0         open (my $SOURCE, '<', "$dir/$file")
51             || $log->error("failed to open latex source file '$file' for parsing: $!");
52              
53 0 0         if ($stripping) {
54 0           my $pos = tell $SOURCE;
55 0           LEADINGCRUFT: while (<$SOURCE>) {
56 0 0         if (/^\s*[\\\{]/) {
57 0           seek $SOURCE, $pos, 0;
58 0           $.--;
59 0           last LEADINGCRUFT;
60             }
61 0           $pos = tell $SOURCE
62             }
63             }
64              
65 0           my $seen_doc_style_or_class;
66             my $h_included;
67 0           my $try_amslplain = 0;
68 0           my $dont_hypertex = 0;
69              
70 0           SOURCE: while (<$SOURCE>) {
71              
72             # look for commented out phyzzx reference
73 0           s/%macropackage=phyzzx/\\input\ phyzzx/;
74             # Fix old versions of revtex by adding a version number
75 0           s/^(\s*\\documentstyle\s*\[.*)revtex(.*)\]\s*{aps}/$1aps$2,version2]{revtex}/;
76             # convert \texsis to \input mtexsis
77 0 0         if ($checktexsis){
78 0           s/(?
79             }
80              
81             # include some stuff for hlatex2e - ignored by hlatex
82 0 0 0       if (!$h_included &&
      0        
83             $seen_doc_style_or_class &&
84             /^[^%]*(?:\\newtheorem|\\begin\s*{document}|\\usepackage[^%{]*{amsrefs})/) {
85 0 0         if ($this_type eq 'TYPE_PDFLATEX') {
86 0           print {$WITH_HYPER} "\\RequirePackage[hyperindex,pdftex]{hyperref}\n";
  0            
87             } else {
88 0           print {$WITH_HYPER} "\\RequirePackage[hyperindex]{hyperref}\n";
  0            
89             }
90 0           $h_included = 1;
91             }
92              
93 0 0         if (/^[^%]*\\(?:documentstyle|documentclass|usepackage|input)([^%]*)/) {
94 0           my $macrostring = $1;
95 0 0         if ($macrostring =~ /($incompatiblemacropackages)/) {
96 0           $dont_hypertex = nohypertex($log, $1);
97             }
98             # current (as of 10/2007) ws-procs9x6.cls, ws-procs10x7.sty ...
99             # do not work with hypertex. some previous versions were ok
100 0 0         if ($macrostring =~ /ws-procs\d{1,2}x\d/) {
101 0           $dont_hypertex = nohypertex($log, 'ws-procs');
102             }
103 0 0         if (/\\document(?:style|class)/) {
104 0           $seen_doc_style_or_class = 1;
105             }
106             # look for amsppt style - it must be AMSTeX in disguise
107 0 0         if (s/^([^%]*\\documentstyle(\[.*])?\{amsppt\})/\\input\ amstex\n$1/) {
108 0           $log->verbose('Whoahaa. This looks like AMSTeX not latex');
109 0           $this_type = 'TYPE_TEX'; # Passed back to calling routine at end
110             }
111 0 0         if (/^[^%]*\\documentstyle(\[.*])?\{amsart\}/) {
112 0           $log->verbose("File contains 'amsart' doc style. Will remember that and\n revert to amslplain should things fail");
113 0           $try_amslplain = 1;
114             }
115             }
116              
117 0           print {$WITHOUT_HYPER} $_;
  0            
118              
119             # amslatex can now include hyper stuff
120             # TS: 12/09 should this test be limited to $. == 1 or $. < 5?
121 0 0         if (substr $_, 0, 5 eq '%&ams') {
122 0           s/^%&amslplain/\\input hyperlatex%/;
123 0           s/^%&amstex/\\input hyperbasics%/;
124             }
125              
126             # TS: 12/10 skip past verbatim environments. handles multiple
127             # consecutive verbatim environments on the same line (for those
128             # pathologically newline challenged OSes). does not bother parsing
129             # things interspersed, preceeding, or following the verbatim env on
130             # the same source line. a line for arXiv's parsing purposes is
131             # defined by unix line delimiter \n (012)
132             # FIXME: What other environments should be skipped?
133              
134 0 0         if (0 <= index $_, '\begin{verbatim}') {
135 0 0         if (/^([^%]*(?:(?<=\\)%)*)*\\begin{verbatim}/gc) {
136 0 0         if (0 <= index $_, '\end{verbatim}', 16 + pos) { # 16 == length '\begin{verbatim}'
137 0           print {$WITH_HYPER} $_;
  0            
138 0           next SOURCE;
139             }
140 0           print {$WITH_HYPER} $_;
  0            
141 0           while (<$SOURCE>) {
142 0           print {$WITHOUT_HYPER} $_;
  0            
143 0 0         if (0 <= (my $pos = rindex $_, '\end{verbatim}')) {
144 0           pos = $pos + 14; # 14 == length '\end{verbatim}'
145 0 0         if (/\G([^%]*(?:(?<=\\)%)*)*\\begin{verbatim}/gc) {
146 0           print {$WITH_HYPER} $_;
  0            
147 0           next;
148             }
149 0           print {$WITH_HYPER} $_;
  0            
150 0           next SOURCE;
151             }
152 0           print {$WITH_HYPER} $_;
  0            
153             }
154             }
155             }
156              
157 0           my $n = 0; # counter to prevent deep recursion
158 0           while (m!(^|[^{"]\s*)((ftp|http)://[^*)\s",>&;%]+)!i) {
159 0           my $tex_url = $2;
160 0 0         last if (/\\verb\*?\S\s*\Q$tex_url\E/);
161 0 0         if ($tex_url =~ /\\\~\{?$/) {
162 0           s/\\\~\s*(\{\s*\}\s*)?/\$\\sim\$/g;
163 0           m#(^|[^{]\s*)((ftp|http)://[^]*)\s",>&;]+)#i;
164 0           $tex_url = $2;
165             }
166 0           $tex_url =~ s/^([^\{]+)\}.*/$1/;
167 0           $tex_url =~ s/^([^\[]+)\].*/$1/;
168 0           $tex_url =~ s/^([^\$]+)\$[^\$]*$/$1/;
169 0           while ($tex_url =~ tr/\}/\}/ > $tex_url =~ tr/\{/\{/) {
170 0 0         last unless $tex_url =~ s/\}[^\}]*$//;
171             }
172 0           $tex_url =~ s/[\.\+\{\|\!\'\\]+(\[[^]]*\])?$//;
173 0 0         s/\\verb(.)~\1/\$\\sim\$/g if
174             $tex_url =~ s/\\verb(.)~\1/\$\\sim\$/g; #/\verb|~|
175 0 0         s{/~}{/\$\\sim\$}g if $tex_url =~ s{/~}{/\$\\sim\$}g;
176             # now $\sim$ -> \string~, and e.g. \_ -> \string_
177 0           my $special_url = $tex_url;
178             # $special_url =~ s/\\,//g;
179 0           $special_url =~ s/\\lower[^{}]*\{([^{}]*)\}/$1/g;
180 0           $special_url =~ s/\\kern//g;
181 0           $special_url =~ s/(\$\^?\\sim\$|\\symbol\{126\}|\\char\'176|\$\\(wide)?tilde\{[^\}]*\}\$|\\homepage)/\\string~/g;
182 0           $special_url =~ s/(\\linebreak|\\\\)(\[[^]]*\])?//g;
183 0           $special_url =~ s/\\([\W\_])(\s*\{\s*\}\s*)?/\\string$1/g;
184 0           $special_url =~ tr/\{\}//d;
185 0           $special_url =~ s/\\mbox//g;
186             # FIXME: TS 7/2007
187             # \mbox is but one special case
188             # just skip out if we see any undesired character
189             # e.g. '\', '|', '^', '[', ']', '`', or at least any \?
190 0 0 0       last if $n++ >= 5 && s/\Q$tex_url/$protect\\vrule width0pt$protect\\href\{$special_url}\{$tex_url}/;
191             }
192 0 0         if ($customfilter) {
193 0           $_ = $local_hyper_transform->filter($_);
194             }
195 0           print {$WITH_HYPER} $_;
  0            
196             }
197 0 0         close $WITH_HYPER or $log->verbose("couldn't close source file with markup: $!");
198 0 0         close $WITHOUT_HYPER or $log->verbose("couldn't close source file without markup: $!");
199 0 0         close $SOURCE or $log->verbose("couldn't close source file: $!");
200              
201 0           my $setbacktime = time - 5;
202 0           utime $setbacktime, $setbacktime, "$dir/$file", "$dir/$file.with_hyper", "$dir/$file.without_hyper";
203             # Pass back information about new identifications
204 0           return ($this_type, $dont_hypertex, $try_amslplain);
205             }
206              
207             sub nohypertex {
208 0     0 1   my ($log, $msg) = @_;
209 0           $log->verbose("nohypertex: turning off hypertex because $msg does not work with hypertex");
210 0           return 1;
211             }
212              
213             1;
214              
215             __END__