1 |
package Pod::HtmlPsPdf::Config; |
2 |
|
3 |
use strict; |
4 |
use Carp; |
5 |
|
6 |
# META: probably move FindBin here |
7 |
use File::Basename (); |
8 |
|
9 |
# passed only once and then cached |
10 |
use vars qw($config_file); |
11 |
|
12 |
# called in BEGIN block before the rest of the code |
13 |
#################### |
14 |
sub set_config_file{ |
15 |
$config_file = shift || ''; |
16 |
|
17 |
croak "Configuration file $config_file is not readable: $!" |
18 |
unless -r $config_file; |
19 |
|
20 |
} # end of sub set_config_file |
21 |
|
22 |
######## |
23 |
sub new{ |
24 |
my ($class) = shift; |
25 |
|
26 |
my $self = bless {}, ref($class)||$class; |
27 |
$self->_init(@_); |
28 |
|
29 |
return $self; |
30 |
} # end of sub new |
31 |
|
32 |
########## |
33 |
sub _init{ |
34 |
my $self = shift; |
35 |
my $new_config_file = shift || ''; |
36 |
|
37 |
set_config_file($new_config_file) if $new_config_file; |
38 |
|
39 |
croak "Configuration file is not specified" |
40 |
unless $config_file; |
41 |
|
42 |
|
43 |
# The root directory of the project |
44 |
my $root = File::Basename::dirname $config_file; |
45 |
$self->{root} = $root; |
46 |
|
47 |
# do all the work from the base directory |
48 |
chdir $root; |
49 |
|
50 |
# process user's configuration file |
51 |
require $config_file; |
52 |
use vars qw(%c); |
53 |
local *c = \%Pod::HtmlPsPdf::Config::Local::c; |
54 |
|
55 |
# dirs (will be mostly created dynamically |
56 |
$self->{src_root} = $c{dir}{src} || cnf_err('$c{dir}{src}'); |
57 |
$self->{ps_root} = $c{dir}{rel_ps} || cnf_err('$c{dir}{rel_ps}'); |
58 |
$self->{rel_root} = $c{dir}{rel_html} || cnf_err('$c{dir}{rel_html}'); |
59 |
$self->{split_root} = $c{dir}{split_html} || cnf_err('$c{dir}{split_html}'); |
60 |
$self->{out_dir} = $c{dir}{out} || cnf_err('$c{dir}{out}'); |
61 |
|
62 |
# setting the filenames to process and checking their existance |
63 |
$self->{pod_files} = $c{ordered_pod_files} || cnf_err('$c{ordered_pod_files}'); |
64 |
$self->{nonpod_files} = $c{non_pod_files} || cnf_err('$c{non_pod_files}'); |
65 |
for (@{$self->{pod_files}}, @{$self->{nonpod_files}}) { |
66 |
croak "Can't find @{[$self->{src_root}]}/$_: $!" |
67 |
unless -r $self->{src_root}."/$_"; |
68 |
} |
69 |
|
70 |
# set and check that we can read the template files |
71 |
my @tmpl_files = qw(index_html index_ps page_html page_ps page_split_html); |
72 |
for (@tmpl_files) { |
73 |
$self->{'tmpl_'.$_} = $c{tmpl}{$_} || cnf_err('$c{tmpl}{'.$_.'}'); |
74 |
croak "Can't find @{[$self->{'tmpl_'.$_}]}: $!" |
75 |
unless -r $self->{'tmpl_'.$_}; |
76 |
} |
77 |
|
78 |
$self->{out_name} = $c{out_name} || cnf_err('$c{out_name}'); |
79 |
|
80 |
$self->{toc_file} = $c{file}{toc_file} || cnf_err('$c{file}{toc_file}'); |
81 |
|
82 |
$self->{version_file} = $c{file}{version_file} || cnf_err('$c{file}{version_file}'); |
83 |
$self->{package_name} = $c{package_name} || cnf_err('$c{package_name}'); |
84 |
|
85 |
$self->{html2ps_conf} = $c{file}{html2ps_conf} || cnf_err('$c{file}{html2ps_conf}'); |
86 |
|
87 |
$self->{dir_mode} = $c{mode}{dir} || cnf_err('$c{mode}{dir}'); |
88 |
$self->{html2ps_exec} = which( 'html2ps' ); |
89 |
chomp $self->{html2ps_exec}; |
90 |
|
91 |
return $self; |
92 |
|
93 |
} # end of sub _init |
94 |
|
95 |
# user configuration parsing errors reporter |
96 |
############ |
97 |
sub cnf_err{ |
98 |
croak "$_[0] from package Pod::HtmlPsPdf::Config::Local is not |
99 |
defined\n"; |
100 |
}# end of sub cnf_err |
101 |
|
102 |
|
103 |
# you can only retrieve data from this class, you cannot modify it. |
104 |
############## |
105 |
sub get_param{ |
106 |
my $self = shift; |
107 |
|
108 |
return () unless @_; |
109 |
return unless defined wantarray; |
110 |
my @values = map {defined $self->{$_} ? $self->{$_} : ''} @_; |
111 |
|
112 |
return wantarray ? @values : $values[0]; |
113 |
|
114 |
} # end of sub get_param |
115 |
|
116 |
|
117 |
sub which { |
118 |
my $cmd = shift; |
119 |
|
120 |
foreach my $dir (split( ':', $ENV{PATH})) { |
121 |
return "$dir/$cmd" if -x "$dir/$cmd"; |
122 |
} |
123 |
|
124 |
return; |
125 |
} |
126 |
|
127 |
|
128 |
|
129 |
1; |
130 |
__END__ |