| 1 | package DocSet::DocSet::HTML; | ||
| 2 | |||
| 3 | use strict; | ||
| 4 | use warnings; | ||
| 5 | |||
| 6 | use File::Spec::Functions; | ||
| 7 | |||
| 8 | use DocSet::Util; | ||
| 9 | use DocSet::NavigateCache (); | ||
| 10 | |||
| 11 | use vars qw(@ISA); | ||
| 12 | use DocSet::DocSet (); | ||
| 13 | @ISA = qw(DocSet::DocSet); | ||
| 14 | |||
| 15 | # what's the output format | ||
| 16 | sub trg_ext { | ||
| 17 | return 'html'; | ||
| 18 | } | ||
| 19 | |||
| 20 | sub init { | ||
| 21 | my $self = shift; | ||
| 22 | |||
| 23 | $self->SUPER::init(@_); | ||
| 24 | |||
| 25 | # configure HTML specific run-time | ||
| 26 | $self->set(dst_mime => 'text/html'); | ||
| 27 | $self->set(tmpl_mode => 'html'); | ||
| 28 | $self->set_dir(dst_root => $self->get_dir('dst_html')); | ||
| 29 | |||
| 30 | note "\n"; | ||
| 31 | banner("[scan] HTML DocSet: " . $self->get('title') ); | ||
| 32 | } | ||
| 33 | |||
| 34 | sub complete { | ||
| 35 | my ($self) = @_; | ||
| 36 | |||
| 37 | note "\n"; | ||
| 38 | banner("[render] HTML DocSet: " . $self->get('title') ); | ||
| 39 | |||
| 40 | $self->write_sitemap_file() if $self->sitemap; | ||
| 41 | |||
| 42 | $self->write_index_file(); | ||
| 43 | } | ||
| 44 | |||
| 45 | # generate the sitemap.html of the docset below the current root | ||
| 46 | ################################## | ||
| 47 | sub write_sitemap_file { | ||
| 48 | my ($self) = @_; | ||
| 49 | |||
| 50 | my $cache = $self->cache; | ||
| 51 | |||
| 52 | my $dir = { | ||
| 53 | abs_doc_root => $self->get_dir('abs_doc_root'), | ||
| 54 | rel_doc_root => $self->get_dir('rel_parent_root'), | ||
| 55 | path_from_base => $self->get_dir('path_from_base'), | ||
| 56 | }; | ||
| 57 | |||
| 58 | my $meta = $self->sitemap; | ||
| 59 | my $file = exists $meta->{link} ? $meta->{link} : "sitemap.html"; | ||
| 60 | my $navigator = DocSet::NavigateCache->new($self->cache->path, $meta->{id}); | ||
| 61 | my %args = ( | ||
| 62 | nav => $navigator, | ||
| 63 | meta => $meta, | ||
| 64 | dir => $dir, | ||
| 65 | version => $self->get('version')||'', | ||
| 66 | date => get_date(), | ||
| 67 | last_modified => get_timestamp(), | ||
| 68 | ); | ||
| 69 | |||
| 70 | my $dst_root = $self->get_dir('dst_html'); | ||
| 71 | my $dst_file = "$dst_root/$file"; | ||
| 72 | my $mode = $self->get('tmpl_mode'); | ||
| 73 | my $tmpl_file = 'sitemap'; | ||
| 74 | my $vars = { doc => \%args }; | ||
| 75 | my $tmpl_root = $self->get_dir('tmpl'); | ||
| 76 | my $content = proc_tmpl($tmpl_root, $tmpl_file, $mode, $vars); | ||
| 77 | note "+++ Creating $dst_file"; | ||
| 78 | DocSet::Util::write_file($dst_file, $content); | ||
| 79 | } | ||
| 80 | |||
| 81 | # generate the index.html based on the doc entities it includes, in | ||
| 82 | # the following order: docsets, books, chapters | ||
| 83 | # | ||
| 84 | # XXX: Using the same template file create the long and the short index | ||
| 85 | # html files | ||
| 86 | ################################## | ||
| 87 | sub write_index_file { | ||
| 88 | my ($self) = @_; | ||
| 89 | |||
| 90 | my @toc = (); | ||
| 91 | my $cache = $self->cache; | ||
| 92 | |||
| 93 | # TOC | ||
| 94 | my @node_groups = @{ $self->node_groups }; | ||
| 95 | my @ids = $cache->ordered_ids; | ||
| 96 | |||
| 97 | # create the toc while skipping over hidden files | ||
| 98 | if (@node_groups && @ids) { | ||
| 99 | # index's toc is built from groups of items' meta data | ||
| 100 | while (@node_groups) { | ||
| 101 | my ($title, $count) = splice @node_groups, 0, 2; | ||
| 102 | push @toc, { | ||
| 103 | group_title => $title, | ||
| 104 | subs => [map {$cache->get($_, 'meta')} | ||
| 105 | grep !$cache->is_hidden($_), | ||
| 106 | splice @ids, 0, $count], | ||
| 107 | }; | ||
| 108 | } | ||
| 109 | } | ||
| 110 | else { | ||
| 111 | # index's toc is built from items' meta data | ||
| 112 | for my $id (grep !$cache->is_hidden($_), $cache->ordered_ids) { | ||
| 113 | push @toc, $cache->get($id, 'meta'); | ||
| 114 | } | ||
| 115 | } | ||
| 116 | |||
| 117 | my $dir = { | ||
| 118 | abs_doc_root => $self->get_dir('abs_doc_root'), | ||
| 119 | rel_doc_root => $self->get_dir('rel_parent_root'), | ||
| 120 | path_from_base => $self->get_dir('path_from_base'), | ||
| 121 | }; | ||
| 122 | |||
| 123 | my $meta = { | ||
| 124 | id => $self->get('id'), | ||
| 125 | stitle => $self->get('stitle'), | ||
| 126 | title => $self->get('title'), | ||
| 127 | abstract => $self->get('abstract'), | ||
| 128 | }; | ||
| 129 | |||
| 130 | my $navigator = DocSet::NavigateCache->new($self->cache->path, | ||
| 131 | $self->get('id')); | ||
| 132 | my %args = ( | ||
| 133 | nav => $navigator, | ||
| 134 | toc => \@toc, | ||
| 135 | meta => $meta, | ||
| 136 | dir => $dir, | ||
| 137 | version => $self->get('version')||'', | ||
| 138 | date => get_date(), | ||
| 139 | last_modified => get_timestamp(), | ||
| 140 | pdf_doc => $self->pdf_doc, | ||
| 141 | ); | ||
| 142 | |||
| 143 | # plaster index top and bottom docs if defined (after converting them) | ||
| 144 | if (my $body = $self->get('body')) { | ||
| 145 | my $src_root = $self->get_dir('src_root'); | ||
| 146 | my $dst_mime = $self->get('dst_mime'); | ||
| 147 | |||
| 148 | for my $sec (qw(top bot)) { | ||
| 149 | my $src_file = $body->{$sec}; | ||
| 150 | next unless $src_file; | ||
| 151 | |||
| 152 | my $src_ext = filename_ext($src_file) | ||
| 153 | or die "cannot get an extension for $src_file"; | ||
| 154 | my $src_mime = $self->ext2mime($src_ext) | ||
| 155 | or die "unknown extension: $src_ext"; | ||
| 156 | my $conv_class = $self->conv_class($src_mime, $dst_mime); | ||
| 157 | require_package($conv_class); | ||
| 158 | |||
| 159 | my $chapter = $conv_class->new( | ||
| 160 | tmpl_mode => $self->get('tmpl_mode'), | ||
| 161 | tmpl_root => $self->get_dir('tmpl'), | ||
| 162 | src_uri => $src_file, | ||
| 163 | src_path => catfile($src_root, $src_file), | ||
| 164 | ); | ||
| 165 | $chapter->scan(); | ||
| 166 | $args{body}{$sec} = $chapter->converted_body(); | ||
| 167 | } | ||
| 168 | |||
| 169 | } | ||
| 170 | |||
| 171 | my $dst_root = $self->get_dir('dst_html'); | ||
| 172 | my $dst_file = "$dst_root/index.html"; | ||
| 173 | my $mode = $self->get('tmpl_mode'); | ||
| 174 | my $tmpl_file = 'index'; | ||
| 175 | my $vars = { doc => \%args }; | ||
| 176 | my $tmpl_root = $self->get_dir('tmpl'); | ||
| 177 | my $content = proc_tmpl($tmpl_root, $tmpl_file, $mode, $vars); | ||
| 178 | note "+++ Creating $dst_file"; | ||
| 179 | DocSet::Util::write_file($dst_file, $content); | ||
| 180 | } | ||
| 181 | |||
| 182 | # search for a pdf version in the parallel tree and copy it to | ||
| 183 | # the same dir as the html version (we link to it from the html) | ||
| 184 | sub pdf_doc { | ||
| 185 | my $self = shift; | ||
| 186 | |||
| 187 | my $id = $self->get('id'); | ||
| 188 | my $dst_path = catfile $self->get_dir('dst_root'), "$id.pdf"; | ||
| 189 | my $src_path = catfile $self->get_dir('dst_ps') , "$id.pdf"; | ||
| 190 | |||
| 191 | #print "TRYING $dst_path $src_path \n"; | ||
| 192 | |||
| 193 | my %pdf = (); | ||
| 194 | if (-e $src_path) { | ||
| 195 | copy_file($src_path, $dst_path); | ||
| 196 | %pdf = ( | ||
| 197 | size => format_bytes(-s $dst_path), | ||
| 198 | link => filename($dst_path), | ||
| 199 | ); | ||
| 200 | # gzip_file($dst_path); | ||
| 201 | # my $gzip_path = "$dst_path.gz"; | ||
| 202 | # %pdf = ( | ||
| 203 | # size => format_bytes(-s $gzip_path), | ||
| 204 | # link => filename($gzip_path), | ||
| 205 | # ); | ||
| 206 | } | ||
| 207 | #dumper \%pdf; | ||
| 208 | |||
| 209 | return \%pdf; | ||
| 210 | |||
| 211 | } | ||
| 212 | |||
| 213 | |||
| 214 | 1; | ||
| 215 | __END__ |