#!/usr/bin/perl -s use File::Copy; ## Options: ### -s = skip prel ### -Q = Quiet ### -clear = clear temporary files after build ### -fast = do not check if redo is needed: ### - pdflatex once, bibtex or index if needed, but not pdflatex again our ($s,$f,$clear,$fast,$o,$Q,$debug); $clear=1; $clear=0 if $debug; my $preloption = ""; $preloption .= "-f" if $f; my $base = "__ppdflatex__$$"; $Q = "-Q" if $Q; my $a = shift or die("usage $0 file.pre\n"); if (!-f $a) { $a =~ s/\.$//; for my $e (qw.tlpp ptex pre tex.) { if (-f "$a.$e") { $a = "$a.$e"; last; } } } my $b = $a; $b =~ s/\.(tlpp|ptex|pre|tex)$//; my $teximporter=`grep -E '[(\\|begin)\{](import_|inline_|_(html|camila|dot|abc|pod|makefile|gnuplot))' $a`; $teximporter=" | teximporter $Q " if $teximporter; my $c; if ($s) { copy($a, "$base.tex"); } else { $c=`grep '^=\\.' $a`; my $pout="$teximporter > $base.tex"; open(FI,$a) or die("Error: cant open $a\n"); open(FO,$pout) or die("Error: cant open $pout\n"); local $/; undef $/; my $text= ; if($c){ print FO prelstr(beamerpp($text)); } else { print FO prelstr( $text ); } close FI; close FO; } syst("pdflatex $base"); $c=`grep '\\\\bibliography{' $a`; $c =~ s/\%.*//s; $ind=`grep '\\\\printindex' $a`; $ind =~ s/\%.*//s; my $toc=`grep '\\\\tableofcontents' $a`; $toc =~ s/\%.*//s; ##print "bibtex? = $c\n"; ##print "makeindex? = $ind\n"; $toc && ($redo = 1); if ($ind) { syst("makeindex $base") ; $redo=1;} if ($c) { syst("bibtex $base","continue") ; $redo=1;} if ($redo && !$fast) { syst("pdflatex $base"); syst("pdflatex $base"); } if($o) { rename("$base.pdf" , $o );} else { rename("$base.pdf" , "$b.pdf");} ##warn("\nOutput written to $b.pdf\n"); unlink("$base.tex") unless $debug; if ($clear && !$fast) { ## As caution, do not clear in fast mode for my $file (<$base.*>) { unlink $file unless $debug; } for my $file (){ unlink $file unless $debug; } } sub syst{ my $a=shift; my $b=shift || "die"; # print STDERR "debug: $a\n"; if(system($a) != 0){ if($b eq "die"){ die ("system $a failed:\n\t $? $!")} else { warn("system $a failed:\n\t $? $!")} } } sub prel{ my ($f,$pout)=@_; open(FI,$f) or die("Error: cant open $f\n"); open(FO,$pout) or die("Error: cant open $pout\n"); local $/; undef $/; print FO prelstr(); } sub prelstr{ my $s=shift; for ($s){ my %save = (); my $n1 = 0; s{( \\begin\{[Vv]erbatim\} .*? \\end\{[Vv]erbatim\}) }{ $save{++$n1}=$1;"__SSAVE${n1}__"}xges; ## save verbatim s{([^\\]) (\%.*) }{ $save{++$n1}=$2;"$1__SSAVE${n1}__"}xge; ## save comm s{\n===\*(.*)}{\n\\subsubsection*{$1}}g; s{\n===(.*)}{\n\\subsubsection{$1}}g; s{\n\n\d+\.\d+\.\d+\.([^\d.].*)}{\n\n\\subsubsection{$1}}g; s{\n==\*(.*)}{\n\\subsection*{$1}}g; s{\n==(.*)}{\n\\subsection{$1}}g; s{\n\n\d+\.\d+\.([^\d.].*)}{\n\n\\subsection{$1}}g; s{\n=\*(.*)}{\n\\section*{$1}}g; s{\n=(.*)}{\n\\section{$1}}g; s{\n\n\d+\.([^\d.].*)}{\n\n\\section{$1}}g; s{\\:(\n[\t ]*)\.\d}{\n\\begin{enumerate}#+#$1\\item}g; s{\\:(\n[\t ]*)\.\[}{\n\\begin{description}#+#$1\\item[}g; s{\\:(\n[\t ]*)\.}{\n\\begin{itemize}#+#$1\\item }g; s{:(\n[\t ]*)\.\d}{:\n\\begin{enumerate}#+#$1\\item}g; s{:(\n[\t ]*)\.\[}{:\n\\begin{description}#+#$1\\item[}g; s{:(\n[\t ]*)\.}{:\n\\begin{itemize}#+#$1\\item }g; while( s{(#\+\#)([^#]*)(\n[\t ]*)\.\d?}{$1$2$3\\item}g or s{(\\begin{ (enumerate|description|itemize) } ) \#\+\# ([^#]*) \#([^+]) }{$1$3\\end{$2}$4}xg){}; s{__SSAVE(\d+)__}{$save{$1}}g; } $s; } sub beamerpp{ my $aa =shift; for ($aa){ $_ = sp2tex($_); s{\n(=\.)(=+)[ \t]*(.*)}{\n$2 $3\n$1 $3}g; s{ \n=\.[ \t]*(.*) (\n(.|\n)*?) (?=\n(?:=|\d\.))} { my ($a,$b)=($1,$2); if($a){ if($b =~ /Verbatim/){ "\n\\begin{frame}[fragile]\\frametitle{$a}\n$b\\end{frame}\n"} else{ "\n\\begin{frame}\\frametitle{$a}\n$b\\end{frame}\n"}} else{ if($b =~ /Verbatim/){ "\n\\begin{frame}[fragile]\n$b\\end{frame}\n"} else{ "\n\\begin{frame}\n$b\\end{frame}\n"}} }xeg; s{\n=\.}{}g; s{\n(:\n[ \t]*\.)}{\\$1}g; } $aa; } sub math2tex{ my $f=shift; $f =~ s/-->/\\rightarrow /g; # a --> b $f =~ s/<--/\\leftarrow /g; # a <-- b $f =~ s!/\\!\\and !g; # a /\ b $f =~ s!\\/!\\vee !g; # a \/ b $f =~ s/==>/\\doublerightarrow /g; # a ==> b $f =~ s/<==/\\doubleleftarrow /g; # a <== b $f; } sub txt2tex{ my $f=shift; $f; } sub sp2tex { my $pagina = shift; my %verb=(); my $n=0; my $orange="orange"; if ($pagina) { for ($pagina) { s/\cM//g; s/[ \t]+\n/\n/g; # delete after __END__ s/\n__END__.*/\n/s; s/(\\end\{document\}).*/$1\n/s; #Verbatim paragraphs s{(\\begin\{Verbatim\}[^\n]*)(.*?)(\\end\{Verbatim\})}{ $verb{++$n}= $1.txt2tex($2).$3; "\n\n\caMARCA\ca$n\n"}segi; #Verbatim paragraphs s{\n\n([ \t]+\S.*?)(?=\s*\n\S|$)}{ $verb{++$n}= "\\begin{Verbatim}\n".txt2tex($1)."\n\\end{Verbatim}"; "\n\n\caMARCA\ca$n\n"}seg; # Hard rule s/(\n)---+[^\n+]*($|\n)/$1\\hrule\n/g; # bolds, italics, tts, wiki-like s!_\((.+?)\)_!"\\color{$orange}\\(".math2tex($1)."\\)\\color{black}"!eg; s!_\[(.+?)\]_!"\n\\color{$orange}\\(".math2tex($1)."\\)\\color{black}\n"!eg; s!\b_\B((?:[^_\n]|\B_\B)+)\B_\b!\\emph{$1}!g; s!\B=\b([^=\n]+)\b=\B!"\\texttt{".txt2tex($1)."}"!eg; s!\*(\[|\b)([^*]+)(\]|\b)\*!\\textbf{\\color{$orange}$2\\color{black}}!g; # Links... s/\n\x01MARCA\x01(\d+)\n/$verb{$1}/g; } return $pagina; } else { return ""; } } __END__ =head1 NAME ppdflatex - preprocessor for LaTeX =head1 SYNOPSIS =head1 Options -s = skip prel -Q = Quiet -debug = does nod delete temporary files =head1 DESCRIPTION C may use C, C and C. And C and C. In order to make beamer slides, if beamerpp sintax is found (^=. title) beamerpp preprocessor is executed too. In order to easilly importer external formats, if teximporter sintax is found (C<\\import_graphviz{file}>) teximporter preprocessor is executed too. In order to make bibliography if C<\\bibliography> is found BibTeX is executed too. In order to make conceptual indexes, if C<\\printindex> is found makeindex is executed too. =head1 Beamerpp - slide wiki... =head2 SYNOPSIS beamerpp f =head1 DESCRIPTION = section title == subsection title =. slide title =.== slide title and section at the same time _( math )_ _ [math ]_ =texttt= _italic_ *bold orange* =head1 AUTHOR J.Joao Almeida, jj@di.uminho.pt =head1 SEE ALSO teximport brev2s LaTeX, BibTeX makeindex perl(1). =cut