📄 lyxport
字号:
#!/usr/bin/env perl##*****************************************************************************## lyxport - script for exporting lyx docs to HTML, PostScript and PDF## Inspired on the lyx2html script by Steffen Evers (tron@cs.tu-berlin.de)# (many thanks to him).## Copyright (C) 2001 Fernando P閞ez (fperez@pizero.colorado.edu)##*****************************************************************************## This program is free software; you can redistribute it and/or modify# it under the terms of the GNU General Public License as published by# the Free Software Foundation; either version 2 of the License, or# (at your option) any later version.## 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.## If you do not have a copy of the GNU General Public License, write# to the Free Software Foundation, Inc., 675 Mass Ave, Cambridge,# MA 02139, USA.## If the author of this software was too lazy to include the full GPL# text along with the code, you can find it at:## http://www.gnu.org/copyleft/gpl.html##*****************************************************************************=pod=head1 NAMEB<lyxport> - Export a LyX or LaTeX file to HTML, PostScript and PDF.=head1 SYNOPSISB<lyxport> [options] F<file>Perl script which takes a LyX or LaTeX file as its only argument and producesHTML, PostScript and PDF versions of the document. The name is short for "lyxexport".You can call B<lyxport> with a filename with or without extension: F<file>,F<file.lyx> and F<file.tex> will all work. B<lyxport> will update the LaTeXfile if there is a corresponding LyX file with a newer timestamp.Use B<lyxport --help> for more information, and B<lyxport --man> for a fullman page.=cut#*****************************************************************************# modify here the command names to suit your local conditionsmy %cmd= ( lyx => "/usr/local/lyx/bin/lyx-mdk", latex => "latex", latex2html => "latex2html", dvips => "dvips", pdflatex => "pdflatex", epstopdf => "epstopdf" );#************ DO NOT CHANGE ANYTHING BELOW THIS ULESS YOU *REALLY* KNOW#************ WHAT YOU ARE DOING.#*****************************************************************************# modules and globalsuse strict;use File::Copy;use File::Basename;my (%opt); # command line options#*****************************************************************************# "main" (simple minded way to keep variable scoping under control)main();sub main { my $version = "0.3"; # keep this up to date with the docs (at end)! my ($runs, # number of latex runs $file_in, # input filename as given at cmd line $file_base, # base (no extension) name of file to work on $lyx_time, # timestamps of lyx/tex files $tex_time, $l2h_file, # tex file cleaned up for latex2html $targets_built, $targets_failed, $status, # status string for diagnostics printing @latex_from_lyx # LaTeX files was created from LyX file ); #------------------------------------------------------------------------ # code begins cmdline_process(\%opt,\$file_in); # set defaults and filenames needed throughout $runs=$opt{runs}; set_cmd_defaults(\%cmd); $file_base=check_file_exists($file_in); # various filenames (with extensions) my @exts=qw(lyx tex aux dvi log ps pdf out toc); my ($lyx,$tex,$aux,$dvi,$log,$ps,$pdf,$out,$toc) = map { "$file_base.$_" } @exts; # first, if tex file is older than lyx file, update @latex_from_lyx=update_tex($lyx,$tex); if ($opt{clean}) { lyxport_info("Cleanup of old auxiliary files requested"); safe_system("rm -rf $file_base"); unlink ($aux,$log,$out,$toc); } # run latex for both html (needs .aux file) and ps (needs .dvi) if ($opt{html} or $opt{ps}) { run_latex($cmd{latex},$tex,\$runs); } # now make targets if ($opt{html}) { make_html($tex,$file_base,$opt{opts_l2h},\$status, \$targets_built,\$targets_failed); } if ($opt{ps}) { make_ps($dvi,$ps,$file_base,\$status,\$targets_built,\$targets_failed); } if ($opt{pdf}) { make_pdf($tex,$pdf,\$runs,$file_base, \$status,\$targets_built,\$targets_failed); } #cleanup before exiting and print some diagnostics info unless ($opt{debug}) { unlink ($dvi,$log,$out); } # extra cleanup if ($opt{tidy}) { print "tidy up $opt{tidy},$aux,$log,$out,$toc,@latex_from_lyx\n"; tidy_up($opt{tidy},$aux,$log,$out,$toc,@latex_from_lyx); } final_diagnostics($file_in,$status,$targets_built,$targets_failed); exit(0);} # end of main()#*****************************************************************************# subroutines#-----------------------------------------------------------------------------sub make_html { my($tex,$html_dir,$opts_l2h,$stat_ref,$built_ref,$failed_ref)=@_; my($success); lyxport_info("Making HTML"); run_latex2html($tex,$opts_l2h); $success=check_targets("${html_dir}/${html_dir}.html",'HTML', $built_ref,$failed_ref); if ($success) {$$stat_ref .= "Target HTML built in directory $html_dir\n" }} # end of make_html()#-----------------------------------------------------------------------------sub make_ps { my($dvi,$ps,$html_dir,$stat_ref,$built_ref,$failed_ref)=@_; my($success); lyxport_info("Making PostScript"); safe_system("$cmd{dvips} $dvi -o $ps"); $success=check_targets($ps,'PostScript',$built_ref,$failed_ref); if ($success) { move2html_dir('PostScript',$ps,$html_dir,$stat_ref,$built_ref) }} # end of make_ps()#-----------------------------------------------------------------------------sub make_pdf { my($tex,$pdf,$runs_ref,$html_dir,$stat_ref,$built_ref,$failed_ref)=@_; my($success); lyxport_info("Making PDF"); run_pdflatex($tex,$pdf,$runs_ref); $success=check_targets($pdf,'PDF',$built_ref,$failed_ref); if ($success) { move2html_dir('PDF',$pdf,$html_dir,$stat_ref,$built_ref) }} # end of make_pdf()#-----------------------------------------------------------------------------# move a given target to the html dir, only if it exists. leaves diagnostics # info in a status stringsub move2html_dir { my($name,$file,$dir,$stat_ref,$html_status_ref)=@_; if ($$html_status_ref =~ /HTML/) { safe_system("mv $file $dir"); $$stat_ref .= "Target $name moved to directory $dir\n"; } else { $$stat_ref .= "Target $name left in current directory\n"; }} # end of move2html_dir()#-----------------------------------------------------------------------------# make sure that the tex file is up to date vs the lyx original before starting# returns a list of the included .tex files which were generated (besides the main one)sub update_tex { my($lyx,$tex)=@_; my($lyx_time,$tex_time); my(@lyx_out,@made_tex,$lc); @made_tex=(); unless (-e $lyx) { print "LyX file not found. Working off the LaTeX file alone.\n\n"; return; } $lyx_time=(stat($lyx))[9]; $tex_time=(stat($tex))[9]; if ($lyx_time>$tex_time or not(-e $tex)) { lyxport_info("LaTeX file outdated or not existent, regenerating it..."); unlink $tex; @lyx_out=`$cmd{lyx} -dbg latex --export latex $lyx 2>&1 `; # try again without -dbg option: LyX has a bug here! Note that this will # disable the ability to remove extra .tex files generated from \include # statements. But at least it will work, until they fix the bug in LyX. unless (-e $tex) { @lyx_out=`$cmd{lyx} --export latex $lyx 2>&1 `; } # end of ugly workaround unless (-e $tex) {die "Aborting: couldn't create LaTeX file with LyX.\n\n"}; push (@made_tex,$tex); # find whether lyx made auxiliary (included) .tex files and report foreach $lc (0..$#lyx_out-1) { $_=$lyx_out[$lc]; if (/^incfile:.*\.lyx/) { $lyx_out[$lc+1] =~ /^writefile:(.*)$/; push (@made_tex,basename($1)); } } if (@made_tex) { lyxport_info("Made LaTeX included files: @made_tex"); } lyxport_info("Done with LaTeX generation. Moving on."); } return @made_tex;} # end of update_tex()#-----------------------------------------------------------------------------# run latex on a file as many times as needed# if the given # of runs is > 0, that many are done; otherwise latex is run# as many times as needed until cross-references work.# can be used to run either normal latex or pdflatexsub run_latex { my($latex_cmd,$file,$runs_ref)=@_; my($output); # pre-determined # of runs if ($$runs_ref > 0) { foreach (1..$$runs_ref) { safe_system("$latex_cmd $file"); } } # or make as many runs as needed to get cross-references right else { $$runs_ref=0; while (1) { $output=`$latex_cmd $file`; $$runs_ref++; last unless ($output =~ /Rerun to get cross-references right/); } }} # end of run_latex()#-----------------------------------------------------------------------------# cleanup the tex code so that latex2html doesn't get too confused# this is essentially a Perl version (made with s2p) of Steffan Effer's# original improvetex sed script, part of his lyx2html script collectionsub improve_tex4html { my ($texfile,$newfile)=@_; my ($len1,$pflag); my $printit=1; local *OLD,*NEW; open(OLD,"< $texfile") || die "Can't read from file $texfile: $!\n"; open(NEW,"> $newfile") || die "Can't write to file $newfile: $!\n"; select(NEW) || die "Can't make $newfile default filehandle: $!\n";# code generated by s2p follows. Emacs can't reindent it properly!# this code is ugly (once in Perl, original sed was ok). Clean it up...$pflag=$\; # save print flag$\="\n";LINE:while (<OLD>) { chomp; # remove pagerefs over two lines (senseless in HTML) if (/on *$\|on *page *$/) { $_ .= "\n"; $len1 = length; $_ .= <OLD>; chop if $len1 < length; s/on *\n*page *\n*\\pageref{[^}]*}/\n/g; } # remove regular pagerefs (senseless in HTML) s/on *page *\\pageref{[^}]*}//g; # comment out redefintion of url tag (confuses latex2html) if (/^\\IfFileExists{url.sty}/) { s/^/%/; print; $_ = <OLD>; s/^/%/; } # remove empty pages if (/^\\thispagestyle{empty}~\\newpage$/) { $printit = 0; next LINE; } if (/^\\thispagestyle{empty}~$/) { $printit = 0; next LINE; } # remove custom latex commands for fancyheaders s/\\fancyhead[^{]*{[^{}]*([^{}]*{[^{}]*})*[^{}]*}*//g; s/\\thispagestyle{[^{}]*}//g; # change documentclass from scrbook to book s/^(\\documentclass[^{}]*{)scrbook}/$1book}/; # comment out unsupported packages s/^(\\usepackage\[T1\]{fontenc})/%$1/; s/^(\\usepackage{a4wide})/%$1/; s/^(\\usepackage{fancyhdr})/%$1/; s/^(\\usepackage{ae)/%$1/; s/^(\\pagestyle{fancy})/%$1/; # the geometry package doesn't make sense in html s/^(\\usepackage{geometry})/%$1/; s/^(\\geometry)/%$1/; # comment out ident/skip block command (produces error message; why?) s/^(\\setlength\\parskip{.*})/%$1/; s/^(\\setlength\\parindent{.*})/%$1/;} continue { if ($printit) { print } else { $printit++ }}close(OLD);close(NEW);select(STDOUT);$\=$pflag; # restore defaults} # end of improve_tex4html()
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -