📄 lyxport
字号:
#-----------------------------------------------------------------------------sub run_latex2html { my ($tex,$latex2html_opts)=@_; my ($l2h_file,$symlink_exists,$htmldir); ($htmldir=$tex) =~ s/\.tex$//; $l2h_file="${tex}_#tmp_2html#"; improve_tex4html($tex,$l2h_file); safe_system("$cmd{latex2html} $latex2html_opts $l2h_file"); unless ($opt{debug}) { unlink($l2h_file,"$htmldir/labels.pl"); } # latex2html always leaves 2 copies of the file (one as file.html, one as # index.html). In systems that support symlinks, remove one and replace it # with a link: $symlink_exists = eval { symlink("",""); 1 }; if ($symlink_exists) { unlink("$htmldir/index.html"); symlink("$htmldir.html","$htmldir/index.html"); }} # end of run_latex2html()#-----------------------------------------------------------------------------# remove calls to eps figures if they have an accompanying tiff, jpg or pngsub improve_tex_figs { my ($tex,$textmp)=@_; # begin changes to tex file my ($printit,$figname,$fignoneps,$figbase,$figext,$fignew,@figlist,@tmpfiles); open(TEX,"< $tex") || die "Can't read from LaTeX file $tex: $!\n"; open(TMP,"> $textmp") || die "Can't write to temp file $textmp: $!\n"; $printit=1; while (<TEX>) { if (/includegraphics{([^\}]*)/) { $figname=$1; # remove .eps from fig name and make a .pdf version if necessary if ($figname =~ /\.eps$/i) { ($figbase = $figname) =~ s/\.eps$//i; # now we need to find if there's any non-eps figure for this file: # pdflatex can handle jpegs, tiffs, etc. So we only need to build # an associated pdf if there is no other figure file for pdflatex # to work with @figlist=grep {/\.jpe?g$|\.tiff?$|\.png$|\.pdf$/i} <$figbase.*>; if (@figlist > 1) { lyxport_info("Problem! More than one figure file found: @figlist"); die "I don't know what to do here. Sorry, aborting...\n\n"; } elsif (@figlist==1) { # problem: pdftex only recognizes jpg (not jpeg, not JPG, etc) # and tif (not tiff, not TIF, etc). It also gets confused by files # called a.b.c.jpg (it thinks .b.c.jpg is the extension). argh!!! ($fignoneps)=(@figlist); # so first, extract the 3 or 4 letter extension and lowercase it $fignoneps =~ /.*\.(....??)$/; ($figext = $1) =~ tr/[A-Z]/[a-z]/; # and remove any periods from the base of the name (replace by _) $figbase =~ s/\./_/g; $fignew="$figbase.$figext"; if ($fignoneps =~ /\.JPE?G$|\.TIFF?$|\.PNG$|\.PDF$/) { lyxport_info("pdflatex only recognizes the following extensions:\n". "pdf, png, jpg, tif. (all lowercase, no variations like jpeg or tiff).\n". "lyxport will make a copy of $fignoneps to $fignew so that pdflatex is happy"); copy($fignoneps,$fignew); push(@tmpfiles,$fignew); } s/$figname/$fignew/; # in $_, for printing to temp file } else { s/$figname/$figbase.pdf/; lyxport_info("Making PDF figure <$figbase.pdf> from <$figname>"); safe_system("$cmd{epstopdf} $figname"); } } } } continue { if ($printit) { print TMP $_} else { $printit++ } } close(TEX); close(TMP); return @tmpfiles;} # end of improve_tex_figs()#-----------------------------------------------------------------------------# Make the pdf directly from the latex file# Notes: for this to work ok, the following must have been done:# 1. ~/.dvipsrc file must contain the lines# p+ psfonts.cmz# p+ psfonts.amz# 2. The latex preamble of the lyx file must have# \usepackage{ae,aecompl}# This is so that T1 encoded fonts come out looking good in the final pdf.sub run_pdflatex { my ($tex,$pdf,$runs_ref)=@_; my ($texbase,$tmpbase,$textmp,@tmpfiles,@extensions,$printit); local *TEX,*TMP; # first fix references to eps figures (make sure that pdf versions exist!!!) # make all changes in a temp tex file ($texbase=$tex) =~ s/\.tex$//; $tmpbase = "${texbase}_#tmp_pdf#"; @extensions=qw(tex aux out toc log); ($textmp,@tmpfiles)= map { "${tmpbase}.$_" } @extensions; push(@tmpfiles,improve_tex_figs($tex,$textmp)); # now run the actual pdflatex converter run_latex("$cmd{pdflatex} -interaction=nonstopmode",$textmp,$runs_ref); rename( "${tmpbase}.pdf",$pdf); unless ($opt{debug}) { unlink ($textmp,@tmpfiles,"texput.log"); }} # end of run_pdflatex()#-----------------------------------------------------------------------------# general utility routines (not related to latex/html/pdf) follow#-------------------------------------------------------------------------sub cmdline_process{ my($opt_ref,$file_ref)=@_; # modules no strict "vars"; # avoid some unpleasant warnings while checking options use Getopt::Long; # allow bundling of single letter options (-a -b == -ab) Getopt::Long::Configure ("bundling"); use Pod::Usage; # note: the second name for each option (after |) is an alias for user # convenience only. Internally, the only created hash entries use the *first* # name as a key (i.e. $opt{h} doesn't exist, $opt{html} is set with -h or --html) my(@option_list) = qw(html|h ps|p pdf|f runs|r=i opts_l2h|o=s clean|c tidy|t+ cld|l debug|d help man|m version|v); # debug mode overrides all post-run cleanup options if ($opt{debug}) { $opt{t}=0 } # default: a negative # of runs means auto-detect $$opt_ref{runs}= -99; # dash options first GetOptions($opt_ref,@option_list) || pod2usage(-verbose => 0); # execute all possible "die" modes first cmdline_debug(%$opt_ref) if ($$opt_ref{cld}); pod2usage(-verbose => 1) if ($$opt_ref{help}); pod2usage(-verbose => 2) if ($$opt_ref{man}); die "\nlyxport: version $version\n\n" if ($$opt_ref{version}); ## Now get filename (only ONE) pod2usage(-verbose => 0, -message => "\nERROR: lyxport works with exactly ONE file at a time.\n") if (@ARGV != 1); ($$file_ref)=@ARGV; # choose whether to make all targets or just the explicitly specified ones unless ($$opt_ref{html} or $$opt_ref{ps} or $$opt_ref{pdf}) { $$opt_ref{html}=$$opt_ref{ps}=$$opt_ref{pdf}=1; }} # end of cmdline_process()#-----------------------------------------------------------------------------# quick and dirty hash printing by key/value pairssub print_hash { my($key_msg,$val_msg,%hash)=@_; my($op,$val); while ( ($op,$val)=each(%hash) ) {print "$key_msg $op $val_msg $val\n" }} # end of print_hash()#-----------------------------------------------------------------------------sub cmdline_debug{ my(%opt)=@_; print "\nlyxport command line debug mode\n"; print "-------------------------------\n\n"; print "This is a dump of your command line options, as key-value pairs:\n\n"; print_hash("","->",%opt); print "\nExiting...\n\n"; exit;} # end of cmdline_debug()#-----------------------------------------------------------------------------# execute a system call but die with some info if return value is non-zerosub safe_system { my $error; $error=system(@_)/256; if ($error) { print "\nERROR: Command\n @_\nfailed.\n"; } return $error;} # end of safe_system()#-----------------------------------------------------------------------------# check that the command names specified at the top exist in the system, # otherwise choose bare defaults and hope for the best.sub set_cmd_defaults { my ($cmd)=@_; my ($prog,$cmd_name); print "\n"; while (($prog,$cmd_name)=each(%cmd)) { print "Checking for program <$prog>, as <$cmd_name>... \n"; if (system("which $cmd_name")/256) { $$cmd{$prog}=$prog; print "Not found. Reverting to default name $prog.\n"; } else { print "OK, found it.\n" } } print "\nDone configuring command names\n\n";} # end of set_cmd_defaults()#-----------------------------------------------------------------------------# make sure there's either a .lyx or a .tex file to work with# returns a stripped name (without .lyx or .tex extension) of the filesub check_file_exists { my($file_in)=@_; my($base_file); $_=$file_in; if (/\.lyx$/) { s/\.lyx$// } elsif (/\.tex$/) { s/\.tex$// } $base_file=$_; unless (-e "${base_file}.lyx" or -e "${base_file}.tex") { die "I can't find a LyX or LaTeX file to work with!\nAborting...\n\n"; } return $base_file;} # end of check_file_exists()#-----------------------------------------------------------------------------sub check_targets{ my($file,$tag,$built,$failed)=@_; my($success)=0; $tag .= ' '; if (-s $file) { $$built .= $tag; $success=1; } else { $$failed .= $tag } return $success;} # end of check_targets()#-----------------------------------------------------------------------------# do extra cleaning of aux, toc, log files generated during runningsub tidy_up { my($tidy,$aux,$log,$out,$toc,@latex_from_lyx)=@_; lyxport_info("Cleanup of leftover auxiliary files"); print "Removing files: $aux, $log, $out, $toc\n"; unlink ($aux,$log,$out,$toc); if ($tidy>1 and @latex_from_lyx) { lyxport_info("Extra cleanup: removing LaTeX file(s) @latex_from_lyx"); unlink(@latex_from_lyx); foreach (@latex_from_lyx) { s/\.tex$/\.aux/; if (-e) { print "Removing aux file $_\n"; unlink($_); } } }} # end of tidy_up()#-----------------------------------------------------------------------------sub lyxport_info { my ($target)=@_; print "\n",'*'x75,"\n"; print "<lyxport> $target\n\n";} # end of lyxport_info()#-----------------------------------------------------------------------------sub final_diagnostics{ my($file_in,$status,$targets_built,$targets_failed)=@_; lyxport_info("All done!"); print "Input file: $file_in\n\n"; print "Targets built : $targets_built\n\n"; if ($targets_failed) { print "PROBLEM!\nTargets failed: $targets_failed\n\n"; } print "Diagnostics of build process:\n\n$status\nBye!\n\n";} # end of final_diagnostics()#************************ end of code for <lyxport> *******************__END__=pod=head1 DESCRIPTION=head2 PurposeLyX ( http://www.lyx.org ) is a wonderful document processor, which can producefrom a single source multiple versions for different purposes: a PostScriptfile for printing on a Unix-type system, a PDF file for distribution acrossmultiple operating systems, or an HTML file for Internet display. Itaccomplishes this by exporting its own file format to a LaTeX file and thenrunning various converters on this resulting file.However, it turns out that this process isn't exactly foolproof, as theseconverters have all sorts of little quirks which can produce anything fromsurprises in the way the final result looks like to outright failure of theexport process. The purpose of B<lyxport> is to serve as a "smart wrapper"around those export facilities which LyX normally uses, trying to massage theLaTeX file that everything starts from in the hopes of having better successin producing HTML and PDF (PostScript usually poses no problems). B<lyxport> also allows you to keep around only the LyX file, and possibly anyancillary figure files. B<lyxport> takes care of generating (and removingafterwards if instructed to do so) any intermediate files necessary for theexport process.For example, in order to make PDF from a LaTeX file, any included eps figuresneed to be converted to pdf format. But LyX likes to have the figures in epsformat for on-screen display, which is a great feature to have. B<lyxport>allows you to keep your LyX file as usual (with references to .eps figures)and will make .pdf versions of any included figure on the fly as needed. Youcan even ask it to remove those pdf files after it finishes, if you reallywant to maintain a minimum of files around (though it will have to remake themagain if you ever need to update your pdf exported document).=head2 Command line useIf you simply type B<lyxport> F<file>, it will try to make PostScript, HTML,and PDF versions of your file, putting them all in a single directory namedF<file> (without a .lyx or .tex extension if your file had one). But it hasL<command line options|OPTIONS AND ARGUMENTS> for making only theformats you want, and fairly detailed control over its behavior.=head2 If you don't have LyXDespite its name, if you are a regular LaTeX user and don't even have LyXinstalled in your system, B<lyxport> can still be useful to you. In fact,B<lyxport> only uses LyX once in each run: if there is no F<file.tex> or ifF<file.lyx> file is newer than F<file.tex>, B<lyxport> updates F<file.tex>from F<file.lyx>. But if there is no F<file.lyx> at all it will simply useF<file.tex> and proceed with all its functionality intact.=cut###########################################################################=pod=head1 OPTIONS AND ARGUMENTS
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -