| 1 | package DocSet::Config; | ||
| 2 | |||
| 3 | use strict; | ||
| 4 | use warnings; | ||
| 5 | |||
| 6 | use File::Basename (); | ||
| 7 | use File::Spec::Functions; | ||
| 8 | |||
| 9 | use DocSet::Util; | ||
| 10 | use DocSet::RunTime (); | ||
| 11 | |||
| 12 | use constant TRACE => 1; | ||
| 13 | |||
| 14 | # uri extension to MIME type mapping | ||
| 15 | my %ext2mime = ( | ||
| 16 | map({$_ => 'text/html' } qw(htm html)), | ||
| 17 | map({$_ => 'text/plain'} qw(txt text)), | ||
| 18 | map({$_ => 'text/pod' } qw(pod pm)), | ||
| 19 | ); | ||
| 20 | |||
| 21 | my %conv_class = ( | ||
| 22 | 'text/pod' => { | ||
| 23 | 'text/html' => 'DocSet::Doc::POD2HTML', | ||
| 24 | 'text/htmlps' => 'DocSet::Doc::POD2HTMLPS', | ||
| 25 | 'text/ps' => 'DocSet::Doc::POD2PS', | ||
| 26 | }, | ||
| 27 | 'text/html' => { | ||
| 28 | 'text/html' => 'DocSet::Doc::HTML2HTML', | ||
| 29 | 'text/htmlps' => 'DocSet::Doc::HTML2HTMLPS', | ||
| 30 | 'text/ps' => 'DocSet::Doc::HTML2PS', | ||
| 31 | }, | ||
| 32 | 'text/plain' => { | ||
| 33 | 'text/html' => 'DocSet::Doc::Text2HTML', | ||
| 34 | 'text/pdf' => 'DocSet::Doc::Text2PDF', | ||
| 35 | }, | ||
| 36 | ); | ||
| 37 | |||
| 38 | sub ext2mime { | ||
| 39 | my ($self, $ext) = @_; | ||
| 40 | exists $ext2mime{$ext} ? $ext2mime{$ext} : undef; | ||
| 41 | } | ||
| 42 | |||
| 43 | |||
| 44 | sub conv_class { | ||
| 45 | my ($self, $src_mime, $dst_mime) = @_; | ||
| 46 | # convert | ||
| 47 | $self->croak("src_mime is not defined") unless defined $src_mime; | ||
| 48 | $self->croak("dst_mime is not defined") unless defined $dst_mime; | ||
| 49 | $self->croak("unknown input/output MIME mapping: $src_mime => $dst_mime") | ||
| 50 | unless exists $conv_class{$src_mime}{$dst_mime}; | ||
| 51 | return $conv_class{$src_mime}{$dst_mime}; | ||
| 52 | } | ||
| 53 | |||
| 54 | |||
| 55 | my %node_attr = map {$_ => 1} qw(chapters docsets links sitemap); | ||
| 56 | my %hidden_attr = map {$_ => 1} qw(chapters docsets); | ||
| 57 | # the rest of the valid attributes in addition to 'hidden', 'changes', | ||
| 58 | # and %node_attr | ||
| 59 | my %other_attr = map {$_ => 1} qw(id stitle title abstract body | ||
| 60 | options copy_glob copy_skip dir | ||
| 61 | file); | ||
| 62 | sub read_config { | ||
| 63 | my ($self, $config_file, $parent_o) = @_; | ||
| 64 | die "Configuration file is not specified" unless defined $config_file; | ||
| 65 | |||
| 66 | $self->{config_file} = $config_file; | ||
| 67 | |||
| 68 | my $package = path2package($config_file); | ||
| 69 | $self->{package} = $package; | ||
| 70 | |||
| 71 | $parent_o->croak("can't read $config_file: $!") | ||
| 72 | unless -r $config_file; | ||
| 73 | my $content; | ||
| 74 | read_file($config_file, \$content); | ||
| 75 | |||
| 76 | eval join '', | ||
| 77 | "package $package;", | ||
| 78 | $content, ";1;"; | ||
| 79 | $self->croak("failed to eval config file:\n$@") if $@; | ||
| 80 | |||
| 81 | # parse the attributes of the docset's config file | ||
| 82 | no strict 'refs'; | ||
| 83 | use vars qw(@c); | ||
| 84 | *c = \@{"$package\::c"}; | ||
| 85 | |||
| 86 | #dumper \@c; | ||
| 87 | |||
| 88 | my $current_group = ''; | ||
| 89 | my $group_size; | ||
| 90 | my $non_grouped_node_seen = 0; | ||
| 91 | for ( my $i=0; $i < @c; $i +=2 ) { | ||
| 92 | my ($key, $val) = @c[$i, $i+1]; | ||
| 93 | if ($key eq 'group') { | ||
| 94 | $self->croak("grouped and non-grouped chapters cannot be mixed") | ||
| 95 | if $non_grouped_node_seen; | ||
| 96 | # close the previous group by storing the key of its last node | ||
| 97 | if ($current_group) { | ||
| 98 | push @{ $self->{node_groups} }, $current_group, $group_size; | ||
| 99 | } | ||
| 100 | # start the new group | ||
| 101 | $current_group = $val; | ||
| 102 | $group_size = 0; | ||
| 103 | } | ||
| 104 | elsif ($key eq 'hidden') { | ||
| 105 | $self->croak("hidden attribute's value must be an ARRAY reference") | ||
| 106 | unless ref $val eq 'ARRAY'; | ||
| 107 | my @h = @$val; | ||
| 108 | for ( my $j=0; $j < @h; $j +=2 ) { | ||
| 109 | my ($key1, $val1) = @h[$j, $j+1]; | ||
| 110 | $self->croak("the 'hidden' attribute can include only: ", | ||
| 111 | join(", ", keys %hidden_attr), | ||
| 112 | "attributes, $key1 is invalid") | ||
| 113 | unless exists $hidden_attr{$key1}; | ||
| 114 | $self->add_node($key1, $val1, 1); | ||
| 115 | } | ||
| 116 | } | ||
| 117 | elsif ($key eq 'changes') { | ||
| 118 | # add as a hidden chapter | ||
| 119 | $self->add_node('chapters', $val, 1); | ||
| 120 | # by this id we can reach this hidden object from the | ||
| 121 | # docset object | ||
| 122 | $self->{extra}{$key} = $val; | ||
| 123 | } | ||
| 124 | elsif (exists $node_attr{$key}) { | ||
| 125 | $non_grouped_node_seen = 1 unless $current_group; | ||
| 126 | $group_size += $self->add_node($key, $val, 0); | ||
| 127 | } | ||
| 128 | elsif (exists $other_attr{$key}) { | ||
| 129 | $self->{$key} = $val; | ||
| 130 | #dumper [$key => $val]; | ||
| 131 | } | ||
| 132 | else { | ||
| 133 | #dumper [$key => $val]; | ||
| 134 | $self->croak("unknown attribute: $key"); | ||
| 135 | } | ||
| 136 | } | ||
| 137 | if ($current_group) { | ||
| 138 | push @{ $self->{node_groups} }, $current_group, $group_size; | ||
| 139 | } | ||
| 140 | |||
| 141 | # - make sure that at least title or stitle were specified | ||
| 142 | # - alias one to another if only one was specified | ||
| 143 | $self->{title} = $self->{stitle} unless exists $self->{title}; | ||
| 144 | $self->{stitle} = $self->{title} unless exists $self->{stitle}; | ||
| 145 | $self->croak("Either 'title' or 'stitle' must appear in $config_file") | ||
| 146 | unless $self->{title}; | ||
| 147 | |||
| 148 | |||
| 149 | # XXX: some options expansion logic, consider refactoring with | ||
| 150 | # similar logic in docset_build | ||
| 151 | # XXX: also this logic should probably go directly to | ||
| 152 | # DocSet::Source::POD, which is the only place it's used. | ||
| 153 | if ($self->{options}{slides_mode}) { | ||
| 154 | $self->{options}{podify_items} = 1; | ||
| 155 | } | ||
| 156 | |||
| 157 | # merge_config will adjust this value, for nested docsets | ||
| 158 | # so this value is relevant only for the real top parent node | ||
| 159 | $self->{dir}{abs_doc_root} = '.'; | ||
| 160 | |||
| 161 | $self->{dir}{path_from_base} = '' | ||
| 162 | unless exists $self->{dir}{path_from_base}; | ||
| 163 | |||
| 164 | $self->{dir}{src_root} = File::Basename::dirname $config_file; | ||
| 165 | |||
| 166 | if ($self->{dir}{search_paths}) { | ||
| 167 | DocSet::RunTime::scan_src_docs($self->{dir}{src_root}, | ||
| 168 | $self->{dir}{search_paths}, | ||
| 169 | $self->{dir}{search_exts} | ||
| 170 | ); | ||
| 171 | } | ||
| 172 | |||
| 173 | # dumper $self; | ||
| 174 | |||
| 175 | } | ||
| 176 | |||
| 177 | # child config inherits parts from the parent config | ||
| 178 | # and adjusts its paths | ||
| 179 | sub merge_config { | ||
| 180 | my ($self, $src_rel_dir) = @_; | ||
| 181 | |||
| 182 | my $parent_o = $self->{parent_o}; | ||
| 183 | |||
| 184 | # inherit 'file' attributes if not set in the child | ||
| 185 | my $files = $self->{file} || {}; | ||
| 186 | while ( my ($k, $v) = each %{ $parent_o->{file}||{} }) { | ||
| 187 | $self->{file}{$k} = $v unless $files->{$k}; | ||
| 188 | } | ||
| 189 | |||
| 190 | # inherit the 'dir' attributes if not set in the child | ||
| 191 | my $dirs = $self->{dir} || {}; | ||
| 192 | while ( my ($k, $v) = each %{ $parent_o->{dir}||{} }) { | ||
| 193 | $self->{dir}{$k} = $v unless exists $dirs->{$k}; | ||
| 194 | } | ||
| 195 | |||
| 196 | # inherit/override the 'options' attr unless explicitly set | ||
| 197 | while ( my ($k, $v) = each %{ $parent_o->{options}||{} }) { | ||
| 198 | $self->{options}{$k} = $v unless exists $self->{options}{$k}; | ||
| 199 | } | ||
| 200 | |||
| 201 | # a chapter object won't set this one | ||
| 202 | if ($src_rel_dir) { | ||
| 203 | $self->{dir}{src_rel_dir} = $src_rel_dir; | ||
| 204 | |||
| 205 | # append the relative to parent_o's src dir segments | ||
| 206 | # META: hardcoded paths! | ||
| 207 | for my $k ( qw(dst_html dst_ps dst_split_html) ) { | ||
| 208 | $self->{dir}{$k} .= "/$src_rel_dir"; | ||
| 209 | } | ||
| 210 | |||
| 211 | # only path with no leading ./ or closing / | ||
| 212 | $self->{dir}{path_from_base} = join "/", grep /./, | ||
| 213 | $self->{dir}{path_from_base}, $src_rel_dir; | ||
| 214 | |||
| 215 | # set path to the abs_doc_root | ||
| 216 | # META: hardcoded paths! (but in this case it doesn't matter, | ||
| 217 | # as long as it's set in the config file | ||
| 218 | $self->{dir}{abs_doc_root} = | ||
| 219 | $self->{dir}{dst_html} =~ m|/| | ||
| 220 | ? join('/', ("..") x ($self->{dir}{dst_html} =~ tr|/|/|)) | ||
| 221 | : '.'; | ||
| 222 | } | ||
| 223 | |||
| 224 | # inherit the 'copy_skip' attribute if not set in the child | ||
| 225 | $self->{copy_skip} ||= $parent_o->{copy_skip} || []; | ||
| 226 | |||
| 227 | } | ||
| 228 | |||
| 229 | |||
| 230 | # merge the global run-time options with object scoped options by a | ||
| 231 | # simple OR, so if any of the sides sets an option to 1, this method | ||
| 232 | # will return 1 otherwise 0 | ||
| 233 | sub options { | ||
| 234 | my ($self, $option) = @_; | ||
| 235 | $option ||= ''; | ||
| 236 | return ($self->{options}{$option} || DocSet::RunTime::get_opts($option)) | ||
| 237 | ? 1 : 0; | ||
| 238 | } | ||
| 239 | |||
| 240 | # this sub controls the docset's 'modified' attribute which specifies | ||
| 241 | # whether the docset is in a "dirty" state and need to be rebuilt or | ||
| 242 | # not. | ||
| 243 | # | ||
| 244 | # get/set modified status | ||
| 245 | # ... if $self->modified(); | ||
| 246 | # $self->modified(1); | ||
| 247 | sub modified { | ||
| 248 | my $self = shift; | ||
| 249 | if (@_) { | ||
| 250 | my $status = shift; | ||
| 251 | |||
| 252 | # protect from modified status reset (once it's set to any | ||
| 253 | #value it cannot be reset to 0), must be a mistake. If we | ||
| 254 | # don't check this, it's possible that in one place the object | ||
| 255 | # is marked as dirty, but somewhere later a logic mistake | ||
| 256 | # resets this value to 0, (non-dirty). | ||
| 257 | if (exists $self->{modified} && !$status) { | ||
| 258 | $self->croak("Cannot reset the 'modified' status"); | ||
| 259 | } | ||
| 260 | $self->{modified} = $status; | ||
| 261 | } | ||
| 262 | return $self->{modified}; | ||
| 263 | |||
| 264 | } | ||
| 265 | |||
| 266 | # similar to DocSet::RunTime::get_opts('rebuild_all'); | ||
| 267 | # but can be set for the scope of a single docset (which affects only | ||
| 268 | # the immediate children) | ||
| 269 | sub rebuild { | ||
| 270 | my $self = shift; | ||
| 271 | if (@_) { | ||
| 272 | my $status = shift; | ||
| 273 | |||
| 274 | # protect from 'rebuild' status reset (once it's set to any | ||
| 275 | #value it cannot be reset to 0), must be a mistake. If we | ||
| 276 | # don't check this, it's possible that in one place the object | ||
| 277 | # is marked to rebuild the docset, but somewhere later a logic mistake | ||
| 278 | # resets this value to 0, (non-dirty). | ||
| 279 | if (exists $self->{rebuild} && !$status) { | ||
| 280 | $self->croak("Cannot reset the 'rebuild' status"); | ||
| 281 | } | ||
| 282 | $self->{rebuild} = $status; | ||
| 283 | } | ||
| 284 | return $self->{rebuild}; | ||
| 285 | } | ||
| 286 | |||
| 287 | # | ||
| 288 | # 1. put chapters together, docsets together, links together | ||
| 289 | # 2. store the normal nodes in the order they were listed in 'ordered_nodes' | ||
| 290 | # 2. store the hidden nodes in the order they were listed in 'hidden_nodes' | ||
| 291 | # | ||
| 292 | # return the number of added items | ||
| 293 | sub add_node { | ||
| 294 | my ($self, $key, $value, $hidden) = @_; | ||
| 295 | |||
| 296 | my @values = ref $value eq 'ARRAY' ? @$value : $value; | ||
| 297 | |||
| 298 | if ($hidden) { | ||
| 299 | push @{ $self->{hidden_nodes} }, $key, $_ for @values; | ||
| 300 | } | ||
| 301 | else { | ||
| 302 | push @{ $self->{ordered_nodes} }, $key, $_ for @values; | ||
| 303 | } | ||
| 304 | |||
| 305 | return scalar @values; | ||
| 306 | } | ||
| 307 | |||
| 308 | # register the new docset id and verify that it wasn't registered | ||
| 309 | # before that. Croak if it was registered already. | ||
| 310 | sub check_duplicated_docset_ids { | ||
| 311 | my $self = shift; | ||
| 312 | my $id = $self->get('id'); | ||
| 313 | my @entries = DocSet::RunTime::register('unique_docset_id', $id, | ||
| 314 | $self->{config_file}); | ||
| 315 | # should be enough to report only the first returned value, since | ||
| 316 | # there can be only one duplicate before this will croak | ||
| 317 | $self->croak("Duplicated docset id: '$id'\n", | ||
| 318 | "Used already by $entries[0]") if @entries > 1; | ||
| 319 | } | ||
| 320 | |||
| 321 | # return a list of files potentially to be copied | ||
| 322 | # | ||
| 323 | # due to a potentially huge list of files to be copied (e.g. the | ||
| 324 | # splash library) currently it's assumed that this function is called | ||
| 325 | # only once. Therefore no caching is done to save memory. | ||
| 326 | # | ||
| 327 | # The following conventions are used for $self->{copy_glob} | ||
| 328 | # 1. Explicitly specified files and directories are copied as is | ||
| 329 | # (directories aren't descended into) | ||
| 330 | # 2. Shell metachars (*?[]) can be used. e.g. if you want to grab | ||
| 331 | # directory foo and its contents, make sure to specify foo/*. | ||
| 332 | sub files_to_scan_copy { | ||
| 333 | my $self = shift; | ||
| 334 | |||
| 335 | my $copy_skip_patterns = $self->{copy_skip} || []; | ||
| 336 | # build one sub that will match many regex at once. | ||
| 337 | my $rsub_filter_out = build_matchmany_sub($copy_skip_patterns); | ||
| 338 | |||
| 339 | my $src_root = $self->get_dir('src_root'); | ||
| 340 | |||
| 341 | # expand $self->{copy_glob}, applying the filter to skip unwanted | ||
| 342 | # files | ||
| 343 | my @files = | ||
| 344 | grep !$rsub_filter_out->($_), # skip unwanted | ||
| 345 | # grep s|^(?:\./)?||, # strip the leading ./ | ||
| 346 | grep !-d $_, # skip empty dirs | ||
| 347 | map { -d $_ ? @{ expand_dir($_) } : $_ } # expand dirs | ||
| 348 | map { $_ =~ /[\*\?\[\]]/ ? glob($_) : $_ } # expand globs | ||
| 349 | map { "$src_root/$_" } # prefix with src_root | ||
| 350 | @{ $self->{copy_glob}||[] }; | ||
| 351 | |||
| 352 | return \@files; | ||
| 353 | } | ||
| 354 | |||
| 355 | # this functions sets/gets a ref to hash of files that need to be | ||
| 356 | # copied as is, all the checking were done already. (only the modified | ||
| 357 | # files will go here) | ||
| 358 | sub files_to_copy { | ||
| 359 | my $self = shift; | ||
| 360 | |||
| 361 | if (@_) { | ||
| 362 | $self->{files_to_copy} = shift; | ||
| 363 | } | ||
| 364 | else { | ||
| 365 | return $self->{files_to_copy} || {}; | ||
| 366 | } | ||
| 367 | |||
| 368 | } | ||
| 369 | |||
| 370 | sub set { | ||
| 371 | my ($self, %args) = @_; | ||
| 372 | @{$self}{keys %args} = values %args; | ||
| 373 | } | ||
| 374 | |||
| 375 | sub set_dir { | ||
| 376 | my ($self, %args) = @_; | ||
| 377 | @{ $self->{dir} }{keys %args} = values %args; | ||
| 378 | } | ||
| 379 | |||
| 380 | sub get { | ||
| 381 | my $self = shift; | ||
| 382 | return () unless @_; | ||
| 383 | my @values = map {exists $self->{$_} ? $self->{$_} : ''} @_; | ||
| 384 | return wantarray ? @values : $values[0]; | ||
| 385 | } | ||
| 386 | |||
| 387 | |||
| 388 | sub get_file { | ||
| 389 | my $self = shift; | ||
| 390 | return () unless @_; | ||
| 391 | my @values = map {exists $self->{file}{$_} ? $self->{file}{$_} : ''} @_; | ||
| 392 | return wantarray ? @values : $values[0]; | ||
| 393 | } | ||
| 394 | |||
| 395 | sub get_dir { | ||
| 396 | my $self = shift; | ||
| 397 | |||
| 398 | return () unless @_; | ||
| 399 | |||
| 400 | my @values = (); | ||
| 401 | for (@_) { | ||
| 402 | if (exists $self->{dir}{$_}) { | ||
| 403 | push @values, $self->{dir}{$_} | ||
| 404 | } | ||
| 405 | else { | ||
| 406 | $self->cluck("no entry for dir: $_"); | ||
| 407 | push @values, ''; | ||
| 408 | } | ||
| 409 | } | ||
| 410 | |||
| 411 | return wantarray ? @values : $values[0]; | ||
| 412 | } | ||
| 413 | |||
| 414 | sub nodes_by_type { | ||
| 415 | my $self = shift; | ||
| 416 | return $self->{ordered_nodes} || []; | ||
| 417 | } | ||
| 418 | |||
| 419 | sub hidden_nodes_by_type { | ||
| 420 | my $self = shift; | ||
| 421 | return $self->{hidden_nodes} || []; | ||
| 422 | } | ||
| 423 | |||
| 424 | sub node_groups { | ||
| 425 | my $self = shift; | ||
| 426 | return $self->{node_groups} || []; | ||
| 427 | } | ||
| 428 | |||
| 429 | |||
| 430 | #sub docsets { | ||
| 431 | # my $self = shift; | ||
| 432 | # return exists $self->{docsets} ? @{ $self->{docsets} } : (); | ||
| 433 | #} | ||
| 434 | |||
| 435 | #sub links { | ||
| 436 | # my $self = shift; | ||
| 437 | # return exists $self->{links} ? @{ $self->{links} } : (); | ||
| 438 | #} | ||
| 439 | |||
| 440 | sub sitemap { | ||
| 441 | my $self = shift; | ||
| 442 | return exists $self->{sitemap} ? $self->{sitemap} : (); | ||
| 443 | } | ||
| 444 | |||
| 445 | #sub src_chapters { | ||
| 446 | # my $self = shift; | ||
| 447 | # return exists $self->{chapters} ? @{ $self->{chapters} } : (); | ||
| 448 | #} | ||
| 449 | |||
| 450 | # chapter paths as they go into production | ||
| 451 | # $self->trg_chapters(@paths) : push a chapter(s) | ||
| 452 | # $self->trg_chapters : retrieve the list | ||
| 453 | sub trg_chapters { | ||
| 454 | my $self = shift; | ||
| 455 | if (@_) { | ||
| 456 | push @{ $self->{chapters_prod} }, @_; | ||
| 457 | } else { | ||
| 458 | return exists $self->{chapters_prod} ? @{ $self->{chapters_prod} } : (); | ||
| 459 | } | ||
| 460 | |||
| 461 | } | ||
| 462 | |||
| 463 | # set/get cache | ||
| 464 | sub cache { | ||
| 465 | my $self = shift; | ||
| 466 | |||
| 467 | if (@_) { | ||
| 468 | $self->{cache} = shift; | ||
| 469 | } | ||
| 470 | $self->{cache}; | ||
| 471 | } | ||
| 472 | |||
| 473 | sub path2package { | ||
| 474 | my $path = shift; | ||
| 475 | $path =~ s|[\W\.]|_|g; | ||
| 476 | return "MyDocSet::X$path"; | ||
| 477 | } | ||
| 478 | |||
| 479 | |||
| 480 | sub object_store { | ||
| 481 | my ($self, $object) = @_; | ||
| 482 | $self->croak("no object passed") unless defined $object and ref $object; | ||
| 483 | push @{ $self->{_objects_store} }, $object; | ||
| 484 | } | ||
| 485 | |||
| 486 | sub stored_objects { | ||
| 487 | my ($self) = @_; | ||
| 488 | return @{ $self->{_objects_store}||[] }; | ||
| 489 | } | ||
| 490 | |||
| 491 | # extended error diagnosis | ||
| 492 | { | ||
| 493 | require Carp; | ||
| 494 | no strict 'refs'; | ||
| 495 | for my $sub (qw(carp cluck croak confess)) { | ||
| 496 | undef &$sub if \&$sub; # overload Carp's functions | ||
| 497 | *$sub = sub { | ||
| 498 | my ($self, @msg) = @_; | ||
| 499 | &{"Carp::$sub"}("[scan $sub] ", @msg, "\n", | ||
| 500 | "[config file: " . $self->{config_file} . "]\n" | ||
| 501 | ); | ||
| 502 | }; | ||
| 503 | } | ||
| 504 | } | ||
| 505 | |||
| 506 | #sub chapter_data { | ||
| 507 | # my $self = shift; | ||
| 508 | # my $id = shift; | ||
| 509 | |||
| 510 | # if (@_) { | ||
| 511 | # $self->{chapter_data}{$id} = shift; | ||
| 512 | # } | ||
| 513 | # else { | ||
| 514 | # $self->{chapter_data}{$id}; | ||
| 515 | # } | ||
| 516 | #} | ||
| 517 | |||
| 518 | 1; | ||
| 519 | __END__ |