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 / PSPDF.pm < prev    next >
Encoding:
Perl POD Document  |  2002-06-30  |  6.3 KB  |  251 lines

  1. package DocSet::DocSet::PSPDF;
  2.  
  3. use strict;
  4. use warnings;
  5.  
  6. use DocSet::Util;
  7. use DocSet::RunTime;
  8. use DocSet::NavigateCache ();
  9.  
  10. use vars qw(@ISA);
  11. use DocSet::DocSet ();
  12. @ISA = qw(DocSet::DocSet);
  13.  
  14. # what's the output format
  15. sub trg_ext {
  16.     return 'html'; # in this case 'html' is just an intermediate format
  17. }
  18.  
  19. sub init {
  20.     my $self = shift;
  21.  
  22.     $self->SUPER::init(@_);
  23.  
  24.     # configure PS/PDF specific run-time
  25.     # though, we build ps/pdf the intermediate product is HTML
  26.     $self->set(dst_mime => 'text/htmlps');
  27.     $self->set(tmpl_mode => 'ps');
  28.     $self->set_dir(dst_root => $self->get_dir('dst_ps'));
  29.  
  30.     note "\n";
  31.     banner("[scan] PS/PDF DocSet: " . $self->get('title') );
  32. }
  33.  
  34. sub complete {
  35.     my($self) = @_;
  36.  
  37.     note "\n";
  38.     banner("[render] PS/PDF DocSet: " . $self->get('title') );
  39.  
  40.     $self->write_index_file();
  41.  
  42.     $self->create_ps_book;
  43.     $self->create_pdf_book if get_opts('generate_pdf');
  44. }
  45.  
  46.  
  47. # XXX: almost the same code as in ::HTML counterpart, consider
  48. # creating ::Common and re-use
  49. #
  50. # generate the index.html file based on the doc entities it includes,
  51. # in the following order: docsets, books, chapters
  52. #
  53. # Using the same template file create the long and the short index
  54. # html files
  55. ##################################
  56. sub write_index_file {
  57.     my($self) = @_;
  58.  
  59.     my @toc  = ();
  60.     my $cache = $self->cache;
  61.  
  62.     # TOC
  63.     my @node_groups = @{ $self->node_groups };
  64.     my @ids = $cache->ordered_ids;
  65.  
  66.     # create the toc while skipping over hidden files
  67.     if (@node_groups && @ids) {
  68.         # index's toc is built from groups of items' meta data
  69.         while (@node_groups) {
  70.             my($title, $count) = splice @node_groups, 0, 2;
  71.             push @toc, {
  72.                 group_title => $title,
  73.                 subs  => [map {$cache->get($_, 'meta')} 
  74.                           grep !$cache->is_hidden($_), 
  75.                           splice @ids, 0, $count],
  76.             };
  77.         }
  78.     }
  79.     else {
  80.         # index's toc is built from items' meta data
  81.         for my $id (grep !$cache->is_hidden($_), $cache->ordered_ids) {
  82.             push @toc, $cache->get($id, 'meta');
  83.         }
  84.     }
  85.  
  86.     my $dir = {
  87.         abs_doc_root   => $self->get_dir('abs_doc_root'),
  88.         rel_doc_root   => $self->get_dir('rel_parent_root'),
  89.         path_from_base => $self->get_dir('path_from_base'),
  90.     };
  91.  
  92.     my $meta = {
  93.          id       => $self->get('id'),
  94.          stitle   => $self->get('stitle'),
  95.          title    => $self->get('title'),
  96.          abstract => $self->get('abstract'),
  97.     };
  98.  
  99.     my $navigator = DocSet::NavigateCache->new($self->cache->path, 
  100.                                                $self->get('id'));
  101.  
  102.     my %args = 
  103.         (
  104.          nav      => $navigator,
  105.          toc      => \@toc,
  106.          meta     => $meta,
  107.          dir      => $dir,
  108.          version  => $self->get('version')||'',
  109.          date     => get_date(),
  110.          last_modified => get_timestamp(),
  111.         );
  112.  
  113.     # plaster index top and bottom docs if defined (after converting them)
  114.     if (my $body = $self->get('body')) {
  115.         my $src_root = $self->get_dir('src_root');
  116.         my $dst_mime = $self->get('dst_mime');
  117.  
  118.         for my $sec (qw(top bot)) {
  119.             my $src_file = $body->{$sec};
  120.             next unless $src_file;
  121.  
  122.             my $src_ext = filename_ext($src_file)
  123.                 or die "cannot get an extension for $src_file";
  124.             my $src_mime = $self->ext2mime($src_ext)
  125.                 or die "unknown extension: $src_ext";
  126.             my $conv_class = $self->conv_class($src_mime, $dst_mime);
  127.             require_package($conv_class);
  128.  
  129.             my $chapter = $conv_class->new(
  130.                 tmpl_mode    => $self->get('tmpl_mode'),
  131.                 tmpl_root    => $self->get_dir('tmpl'),
  132.                 src_uri      => $src_file,
  133.                 src_path     => "$src_root/$src_file",
  134.             );
  135.             $chapter->scan();
  136.             $args{body}{$sec} = $chapter->converted_body();
  137.         }
  138.  
  139.     }
  140.  
  141.     my $dst_root  = $self->get_dir('dst_root');
  142.     my $dst_file = "$dst_root/index.html";
  143.     my $mode = $self->get('tmpl_mode');
  144.     my $tmpl_file = 'index';
  145.     my $vars = { doc => \%args };
  146.     my $tmpl_root = $self->get_dir('tmpl');
  147.     my $content = proc_tmpl($tmpl_root, $tmpl_file, $mode, $vars);
  148.     note "+++ Creating $dst_file";
  149.     DocSet::Util::write_file($dst_file, $content);
  150. }
  151.  
  152. # generate the PS book
  153. ####################
  154. sub create_ps_book{
  155.     my($self) = @_;
  156.  
  157.     note "+++ Generating a PostScript Book";
  158.  
  159.     my $html2ps_exec = DocSet::RunTime::can_create_ps();
  160.     my $html2ps_conf = $self->get_file('html2ps_conf');
  161.     my $id = $self->get('id');
  162.     my $dst_root = $self->get_dir('dst_root');
  163.     my $command = "$html2ps_exec -f $html2ps_conf -o $dst_root/${id}.ps ";
  164.     $command .= join " ", map {"$dst_root/$_"} "index.html", $self->trg_chapters;
  165.     note "% $command";
  166.     system $command;
  167.  
  168. }
  169.  
  170. # generate the PDF book
  171. ####################
  172. sub create_pdf_book{
  173.     my($self) = @_;
  174.  
  175.     note "+++ Converting PS => PDF";
  176.     my $dst_root = $self->get_dir('dst_root');
  177.     my $id = $self->get('id');
  178.     my $command = "ps2pdf $dst_root/$id.ps $dst_root/$id.pdf";
  179.     note "% $command";
  180.     system $command;
  181.  
  182.     # META: can delete the .ps now
  183.  
  184. }
  185.  
  186. 1;
  187. __END__
  188.  
  189. =head1 NAME
  190.  
  191. C<DocSet::DocSet::PSPDF> - A subclass of C<DocSet::DocSet> for generating PS/PDF docset
  192.  
  193. =head1 SYNOPSIS
  194.  
  195. See C<DocSet::DocSet>
  196.  
  197. =head1 DESCRIPTION
  198.  
  199. This subclass of C<DocSet::DocSet> converts the source docset into PS
  200. and PDF "books". It uses C<html2ps> to generate the PS file, therefore
  201. it uses HTML as its intermediate product, though it uses different
  202. templates than C<DocSet::DocSet::HTML> since PS/PDF doesn't require
  203. the navigation widgets.
  204.  
  205. =head2 METHODS
  206.  
  207. See the majority of the methods in C<DocSet::DocSet>
  208.  
  209. =over
  210.  
  211. =item * trg_ext
  212.  
  213.   $self->trg_ext();
  214.  
  215. returns the extension of the target files. I<html> in the case of this
  216. sub-class.
  217.  
  218. =item * init
  219.  
  220.   $self->init(@_);
  221.  
  222. calls C<DocSet::DocSet::init> and then initializes its own HTML output
  223. specific settings.
  224.  
  225. =item * complete
  226.  
  227. see C<DocSet::DocSet>
  228.  
  229. =item * write_index_file
  230.  
  231.   $self->write_index_file();
  232.  
  233. creates I<index.html> file linking all the items of the docset
  234. together.
  235.  
  236. =item * create_ps_book
  237.  
  238. Generats a PostScript Book
  239.  
  240. =item * create_pdf_book
  241.  
  242. Converts PS into PDF (if I<generate_pdf> runtime option is set)
  243.  
  244. =back
  245.  
  246. =head1 AUTHORS
  247.  
  248. Stas Bekman E<lt>stas (at) stason.orgE<gt>
  249.  
  250. =cut
  251.