home *** CD-ROM | disk | FTP | other *** search
/ PC Professionell 2004 December / PCpro_2004_12.ISO / files / webserver / tsw / TSW_3.4.0.exe / Apache2 / perl / pod2latex < prev    next >
Encoding:
Text File  |  2003-09-03  |  8.9 KB  |  325 lines

  1. #!perl
  2.  
  3.     eval 'exec C:\Perl\bin\perl.exe -S $0 ${1+"$@"}'
  4.  
  5.     if $running_under_some_shell;
  6.  
  7.  
  8.  
  9. # pod2latex conversion program
  10.  
  11.  
  12.  
  13. use strict;
  14.  
  15. use Pod::LaTeX;
  16.  
  17. use Pod::Find qw/ pod_find /;
  18.  
  19. use Pod::Usage;
  20.  
  21. use Getopt::Long;
  22.  
  23. use File::Basename;
  24.  
  25.  
  26.  
  27. my $VERSION = "1.00";
  28.  
  29.  
  30.  
  31. # Read command line arguments
  32.  
  33.  
  34.  
  35. my %options = (
  36.  
  37.            "help"   => 0,
  38.  
  39.            "man"    => 0,
  40.  
  41.            "sections" => [],
  42.  
  43.            "full"   => 0,
  44.  
  45.            "out"    => undef,
  46.  
  47.            "verbose" => 0,
  48.  
  49.            "modify" => 0,
  50.  
  51.            "h1level" => 1,  # section is equivalent to H1
  52.  
  53.           );
  54.  
  55.  
  56.  
  57. GetOptions(\%options, 
  58.  
  59.        "help",
  60.  
  61.        "man",
  62.  
  63.        "verbose",
  64.  
  65.        "full",
  66.  
  67.        "sections=s@",
  68.  
  69.        "out=s",
  70.  
  71.        "modify",
  72.  
  73.        "h1level=i",
  74.  
  75.       ) || pod2usage(2);
  76.  
  77.  
  78.  
  79. pod2usage(1)  if ($options{help});
  80.  
  81. pod2usage(-verbose => 2)  if ($options{man});
  82.  
  83.  
  84.  
  85.  
  86.  
  87. # Read all the files from the command line
  88.  
  89. my @files = @ARGV;
  90.  
  91.  
  92.  
  93. # Now find which ones are real pods and convert 
  94.  
  95. # directories to their contents.
  96.  
  97.  
  98.  
  99. # Extract the pods from each arg since some of them might
  100.  
  101. # be directories
  102.  
  103. # This is not as efficient as using pod_find to search through
  104.  
  105. # everything at once but it allows us to preserve the order 
  106.  
  107. # supplied by the user
  108.  
  109.  
  110.  
  111. my @pods;
  112.  
  113. foreach my $arg (@files) {
  114.  
  115.   my %pods = pod_find($arg);
  116.  
  117.   push(@pods, sort keys %pods);
  118.  
  119. }
  120.  
  121.  
  122.  
  123. # Abort if nothing to do
  124.  
  125. if ($#pods == -1) {
  126.  
  127.   warn "None of the supplied Pod files actually exist\n";
  128.  
  129.   exit;
  130.  
  131. }
  132.  
  133.  
  134.  
  135.  
  136.  
  137.  
  138.  
  139. # If $options{'out'} is set we are processing to a single output file
  140.  
  141. my $multi_documents;
  142.  
  143. if (exists $options{'out'} && defined $options{'out'}) {
  144.  
  145.   $multi_documents = 0;
  146.  
  147. } else {
  148.  
  149.   $multi_documents = 1;
  150.  
  151. }
  152.  
  153.  
  154.  
  155. # If the output file is not specified it is assumed that
  156.  
  157. # a single output file is required per input file using
  158.  
  159. # a .tex extension rather than any exisiting extension
  160.  
  161.  
  162.  
  163. if ($multi_documents) {
  164.  
  165.  
  166.  
  167.   # Case where we just generate one input per output
  168.  
  169.  
  170.  
  171.   foreach my $pod (@pods) {
  172.  
  173.  
  174.  
  175.     if (-f $pod) {
  176.  
  177.  
  178.  
  179.       my $output = $pod;
  180.  
  181.       $output = basename($output, '.pm', '.pod','.pl') . '.tex';
  182.  
  183.  
  184.  
  185.       # Create a new parser object
  186.  
  187.       my $parser = new Pod::LaTeX(
  188.  
  189.                   AddPreamble => $options{'full'},
  190.  
  191.                   AddPostamble => $options{'full'},
  192.  
  193.                   MakeIndex => $options{'full'},
  194.  
  195.                   TableOfContents => $options{'full'},
  196.  
  197.                   ReplaceNAMEwithSection => $options{'modify'},
  198.  
  199.                   UniqueLabels => $options{'modify'},
  200.  
  201.                   Head1Level => $options{'h1level'},
  202.  
  203.                   LevelNoNum => $options{'h1level'} + 1,
  204.  
  205.                  );
  206.  
  207.  
  208.  
  209.       # Select sections if supplied
  210.  
  211.       $parser->select(@{ $options{'sections'}})
  212.  
  213.     if @{$options{'sections'}};
  214.  
  215.  
  216.  
  217.       # Derive the input file from the output file
  218.  
  219.       $parser->parse_from_file($pod, $output);
  220.  
  221.  
  222.  
  223.       print "Written output to $output\n" if $options{'verbose'};
  224.  
  225.  
  226.  
  227.     } else {
  228.  
  229.       warn "File $pod not found\n";
  230.  
  231.     }
  232.  
  233.  
  234.  
  235.   }
  236.  
  237. } else {
  238.  
  239.   
  240.  
  241.   # Case where we want everything to be in a single document
  242.  
  243.  
  244.  
  245.   # Need to open the output file ourselves
  246.  
  247.   my $output = $options{'out'};
  248.  
  249.   $output .= '.tex' unless $output =~ /\.tex$/;
  250.  
  251.  
  252.  
  253.   # Use auto-vivified file handle in perl 5.6
  254.  
  255.   use Symbol;
  256.  
  257.   my $outfh = gensym;
  258.  
  259.   open ($outfh, ">$output") || die "Could not open output file: $!\n";
  260.  
  261.  
  262.  
  263.   # Flag to indicate whether we have converted at least one file
  264.  
  265.   # indicates how many files have been converted
  266.  
  267.   my $converted = 0;
  268.  
  269.  
  270.  
  271.   # Loop over the input files
  272.  
  273.   foreach my $pod (@pods) {
  274.  
  275.  
  276.  
  277.     if (-f $pod) {
  278.  
  279.  
  280.  
  281.       warn "Converting $pod\n" if $options{'verbose'};
  282.  
  283.  
  284.  
  285.       # Open the file (need the handle)
  286.  
  287.       # Use auto-vivified handle in perl 5.6
  288.  
  289.       my $podfh = gensym;
  290.  
  291.       open ($podfh, "<$pod") || die "Could not open pod file $pod: $!\n";
  292.  
  293.  
  294.  
  295.       # if this is the first file to be converted we may want to add
  296.  
  297.       # a preamble (controlled by command line option)
  298.  
  299.       my $preamble = 0;
  300.  
  301.       $preamble = 1 if ($converted == 0 && $options{'full'});
  302.  
  303.  
  304.  
  305.       # if this is the last file to be converted may want to add
  306.  
  307.       # a postamble (controlled by command line option)
  308.  
  309.       # relies on a previous pass to check existence of all pods we
  310.  
  311.       # are converting.
  312.  
  313.       my $postamble = ( ($converted == $#pods && $options{'full'}) ? 1 : 0 );
  314.  
  315.  
  316.  
  317.       # Open parser object
  318.  
  319.       # May want to start with a preamble for the first one and
  320.  
  321.       # end with an index for the last
  322.  
  323.       my $parser = new Pod::LaTeX(
  324.  
  325.                   MakeIndex => $options{'full'},
  326.  
  327.                   TableOfContents => $preamble,
  328.  
  329.                   ReplaceNAMEwithSection => $options{'modify'},
  330.  
  331.                   UniqueLabels => $options{'modify'},
  332.  
  333.                   StartWithNewPage => $options{'full'},
  334.  
  335.                   AddPreamble => $preamble,
  336.  
  337.                   AddPostamble => $postamble,
  338.  
  339.                   Head1Level => $options{'h1level'},
  340.  
  341.                   LevelNoNum => $options{'h1level'} + 1,
  342.  
  343.                  );
  344.  
  345.  
  346.  
  347.       # Store the file name for error messages
  348.  
  349.       # This is a kluge that breaks the data hiding of the object
  350.  
  351.       $parser->{_INFILE} = $pod;
  352.  
  353.  
  354.  
  355.       # Select sections if supplied
  356.  
  357.       $parser->select(@{ $options{'sections'}})
  358.  
  359.     if @{$options{'sections'}};
  360.  
  361.  
  362.  
  363.       # Parse it
  364.  
  365.       $parser->parse_from_filehandle($podfh, $outfh);
  366.  
  367.  
  368.  
  369.       # We have converted at least one file
  370.  
  371.       $converted++;
  372.  
  373.  
  374.  
  375.     } else {
  376.  
  377.       warn "File $pod not found\n";
  378.  
  379.     }
  380.  
  381.  
  382.  
  383.   }
  384.  
  385.  
  386.  
  387.   # Should unlink the file if we didn't convert anything!
  388.  
  389.   # dont check for return status of unlink
  390.  
  391.   # since there is not a lot to be done if the unlink failed
  392.  
  393.   # and the program does not rely upon it.
  394.  
  395.   unlink "$output" unless $converted;
  396.  
  397.  
  398.  
  399.   # If verbose
  400.  
  401.   warn "Converted $converted files\n" if $options{'verbose'};
  402.  
  403.  
  404.  
  405. }
  406.  
  407.  
  408.  
  409. exit;
  410.  
  411.  
  412.  
  413. __END__
  414.  
  415.  
  416.  
  417. =head1 NAME
  418.  
  419.  
  420.  
  421. pod2latex - convert pod documentation to latex format
  422.  
  423.  
  424.  
  425. =head1 SYNOPSIS
  426.  
  427.  
  428.  
  429.   pod2latex *.pm
  430.  
  431.  
  432.  
  433.   pod2latex -out mytex.tex *.pod
  434.  
  435.  
  436.  
  437.   pod2latex -full -sections 'DESCRIPTION|NAME' SomeDir
  438.  
  439.  
  440.  
  441. =head1 DESCRIPTION
  442.  
  443.  
  444.  
  445. C<pod2latex> is a program to convert POD format documentation
  446.  
  447. (L<perlpod>) into latex. It can process multiple input documents at a
  448.  
  449. time and either generate a latex file per input document or a single
  450.  
  451. combined output file.
  452.  
  453.  
  454.  
  455. =head1 OPTIONS AND ARGUMENTS
  456.  
  457.  
  458.  
  459. This section describes the supported command line options. Minimum
  460.  
  461. matching is supported.
  462.  
  463.  
  464.  
  465. =over 4
  466.  
  467.  
  468.  
  469. =item B<-out>
  470.  
  471.  
  472.  
  473. Name of the output file to be used. If there are multiple input pods
  474.  
  475. it is assumed that the intention is to write all translated output
  476.  
  477. into a single file. C<.tex> is appended if not present.  If the
  478.  
  479. argument is not supplied, a single document will be created for each
  480.  
  481. input file.
  482.  
  483.  
  484.  
  485. =item B<-full>
  486.  
  487.  
  488.  
  489. Creates a complete C<latex> file that can be processed immediately
  490.  
  491. (unless C<=for/=begin> directives are used that rely on extra packages).
  492.  
  493. Table of contents and index generation commands are included in the
  494.  
  495. wrapper C<latex> code.
  496.  
  497.  
  498.  
  499. =item B<-sections>
  500.  
  501.  
  502.  
  503. Specify pod sections to include (or remove if negated) in the
  504.  
  505. translation.  See L<Pod::Select/"SECTION SPECIFICATIONS"> for the
  506.  
  507. format to use for I<section-spec>. This option may be given multiple
  508.  
  509. times on the command line.This is identical to the similar option in
  510.  
  511. the C<podselect()> command.
  512.  
  513.  
  514.  
  515. =item B<-modify>
  516.  
  517.  
  518.  
  519. This option causes the output C<latex> to be slightly
  520.  
  521. modified from the input pod such that when a C<=head1 NAME>
  522.  
  523. is encountered a section is created containing the actual
  524.  
  525. pod name (rather than B<NAME>) and all subsequent C<=head1>
  526.  
  527. directives are treated as subsections. This has the advantage
  528.  
  529. that the description of a module will be in its own section
  530.  
  531. which is helpful for including module descriptions in documentation.
  532.  
  533. Also forces C<latex> label and index entries to be prefixed by the
  534.  
  535. name of the module.
  536.  
  537.  
  538.  
  539. =item B<-h1level>
  540.  
  541.  
  542.  
  543. Specifies the C<latex> section that is equivalent to a C<H1> pod
  544.  
  545. directive. This is an integer between 0 and 5 with 0 equivalent to a
  546.  
  547. C<latex> chapter, 1 equivalent to a C<latex> section etc. The default
  548.  
  549. is 1 (C<H1> equivalent to a latex section).
  550.  
  551.  
  552.  
  553. =item B<-help>
  554.  
  555.  
  556.  
  557. Print a brief help message and exit.
  558.  
  559.  
  560.  
  561. =item B<-man>
  562.  
  563.  
  564.  
  565. Print the manual page and exit.
  566.  
  567.  
  568.  
  569. =item B<-verbose>
  570.  
  571.  
  572.  
  573. Print information messages as each document is processed.
  574.  
  575.  
  576.  
  577. =back
  578.  
  579.  
  580.  
  581. =head1 BUGS
  582.  
  583.  
  584.  
  585. Known bugs are:
  586.  
  587.  
  588.  
  589. =over 4
  590.  
  591.  
  592.  
  593. =item * 
  594.  
  595.  
  596.  
  597. Cross references between documents are not resolved when multiple
  598.  
  599. pod documents are converted into a single output C<latex> file.
  600.  
  601.  
  602.  
  603. =item * 
  604.  
  605.  
  606.  
  607. Functions and variables are not automatically recognized 
  608.  
  609. and they will therefore not be marked up in any special way
  610.  
  611. unless instructed by an explicit pod command.
  612.  
  613.  
  614.  
  615. =back
  616.  
  617.  
  618.  
  619. =head1 SEE ALSO
  620.  
  621.  
  622.  
  623. L<Pod::LaTeX>
  624.  
  625.  
  626.  
  627. =head1 AUTHOR
  628.  
  629.  
  630.  
  631. Tim Jenness E<lt>t.jenness@jach.hawaii.eduE<gt>
  632.  
  633.  
  634.  
  635. This program is free software; you can redistribute it
  636.  
  637. and/or modify it under the same terms as Perl itself.
  638.  
  639.  
  640.  
  641. Copyright (C) 2000, 2003 Tim Jenness. All Rights Reserved.
  642.  
  643.  
  644.  
  645. =cut
  646.  
  647.  
  648.  
  649.