| 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__ |