1package DocSet::Doc::Common;
2
3use File::Spec::Functions;
4use DocSet::Util;
5use DocSet::RunTime;
6
7# See  HTML2HTMLPS.pm or POD2HTMLPS.pm
8sub postprocess_ps_pdf {
9    my $self = shift;
10
11    # convert to ps
12    my $html2ps_exec = DocSet::RunTime::can_create_ps();
13    my $html2ps_conf = $self->{docset}->get_file('html2ps_conf');
14    my $dst_path     = $self->{dst_path};
15
16    (my $dst_base  = $dst_path) =~ s/\.html//;
17
18    my $dst_root = $self->{dst_root};
19    my $command = "$html2ps_exec -f $html2ps_conf -o ${dst_base}.ps ${dst_base}.html";
20    note "% $command";
21    system $command;
22
23    # convert to pdf
24    $command = "ps2pdf ${dst_base}.ps ${dst_base}.pdf";
25    note "% $command";
26    system $command;
27
28    # META: can delete the .ps now
29
30}
31
32
33
34# search for a pdf version in the parallel tree and copy/gzip it to
35# the same dir as the html version (we link to it from the html)
36sub fetch_pdf_doc_ver {
37    my $self = shift;
38
39    my $dst_path = $self->{dst_path};
40    $dst_path =~ s/html$/pdf/;
41
42    my $pdf_path = $dst_path;
43
44    my $docset = $self->{docset};
45    my $ps_root = $docset->get_dir('dst_ps');
46    my $html_root = $docset->get_dir('dst_html');
47
48    $pdf_path =~ s/^$html_root/$ps_root/;
49
50#print "TRYING $dst_path $pdf_path \n";
51
52    my %pdf = ();
53    # if in the pdf tree (rel_pdf) there is nothing to copy
54    if (-e $pdf_path && $pdf_path ne $dst_path) {
55        copy_file($pdf_path, $dst_path);
56        %pdf = (
57            size => format_bytes(-s $dst_path),
58            link => filename($dst_path),
59        );
60#        gzip_file($dst_path);
61#        my $gzip_path = "$dst_path.gz";
62#        %pdf = (
63#            size => format_bytes(-s $gzip_path),
64#            link => filename($gzip_path),
65#        );
66    }
67#dumper \%pdf;
68
69    return \%pdf;
70
71}
72
73# search for the source version in the source tree and copy/gzip it to
74# the same dir as the html version (we link to it from the html)
75sub fetch_src_doc_ver {
76    my $self = shift;
77    #$self->src_uri
78
79    my $dst_path = catfile $self->{dst_root}, $self->{src_uri};
80    my $src_path = catfile $self->{src_root}, $self->{src_uri};
81
82    # the source file may have the same extension as the dest file, so
83    # add a new extension. This will also be useful for doing patches.
84    $dst_path .= ".orig";
85
86#print "TRYING $dst_path $src_path \n";
87
88    my %src = ();
89    if (-e $src_path) {
90        copy_file($src_path, $dst_path);
91        %src = (
92            size => format_bytes(-s $dst_path),
93            link => filename($dst_path),
94        );
95#        gzip_file($dst_path);
96#        my $gzip_path = "$dst_path.gz";
97#        %src = (
98#            size => format_bytes(-s $gzip_path),
99#            link => filename($gzip_path),
100#        );
101    }
102#dumper \%src;
103
104    return \%src;
105}
106
107sub pod_pom_html_view_seq_link_transform_path {
108    my ($self, $path) = @_;
109
110    $path =~ s|::|/|g;
111    my $doc_obj = get_render_obj();
112
113    my $res_path = $doc_obj->transform_src_doc($path);
114    unless ($res_path) {
115        # report broken links if we were told to
116        if (DocSet::RunTime::get_opts('validate_links')) {
117            print "!!! Broken link $doc_obj->{src_path}: [$path]\n";
118        }
119        return undef;
120    }
121
122    $res_path =~ s/\.[^.]+$/.html/;
123#    print "$res_path\n";
124    return $res_path;
125}
126
127
128#sub make_href {
129#    my ($url, $title) = @_;
130
131#    if (!defined $url) {
132#        return defined $title ? "<i>$title</i>"  : '';
133#    }
134
135#    $title = $url unless defined $title;
136#print "$url, $title\n";
137#    return qq{<a href="$url">$title</a>};
138#}
139
140sub pod_pom_html_anchor {
141    my ($self, $title) = @_;
142    my $anchor = "$title";
143    $anchor =~ s/^\s*|\s*$//g; # strip leading and closing spaces
144    $anchor =~ s/\W/_/g;
145    my $link = $title->present($self);
146
147    # die on duplicated anchors
148    my $render_obj = get_render_obj();
149    $render_obj->{__seen_anchors}{$anchor}++;
150    $render_obj->croak("a duplicated anchor: '$anchor'\nfor title: '$title'\n")
151        if $render_obj->{__seen_anchors}{$anchor} > 1;
152
153    return qq{<a name="$anchor"></a><a href="#toc_$anchor">$link</a>};
154}
155
156
157
158
159
1601;
161__END__
162
163=head1 NAME
164
165C<DocSet::Doc::Common> - Common functions used in C<DocSet::Doc> subclasses
166
167=head1 SYNOPSIS
168
169...
170
171=head1 DESCRIPTION
172
173Implements functions and bits of code which otherwise needed to be
174duplicated in many modules. These functions couldn't be put into the
175base class C<DocSet::Doc>. Certainly we could devise one more
176subclassing level but for now this gentle mix of inheritance and
177inclusion is doing its job just fine.
178
179=head1 METHODS
180
181=over
182
183=item * postprocess_ps_pdf
184
185  $self->postprocess_ps_pdf()
186
187renders ps and pdf version of a the current doc
188
189=item * fetch_pdf_doc_ver
190
191  %pdf_data = %{ $self->fetch_pdf_doc_ver() }
192
193search for a pdf version of the same document in the parallel tree
194(usually the I<dst_html> tree) and copy it to the same dir as the
195html version. Later we link to it from the html version of the
196document if the pdf version is found in the same directory as the html
197one.
198
199The function returns a reference to a hash with the keys: I<size> --
200for the size of the file and the location of the file relative
201to the current document (it's in the same directory after all).
202
203=item * fetch_src_doc_ver
204
205similar to fetch_pdf_doc_ver() but works with the source version of
206the document.
207
208  %src_data = %{ $self->fetch_src_doc_ver() }
209
210fetch the source version of the same document in the parallel tree
211(usually the I<src> tree) and copy it to the same dir as the html
212version. Later we link to it from the html version of the document if
213the source version is found in the same directory as the html
214one. Notice that we add a I<.orig> extension, because otherwise the
215rendered version of the source document may have the same full name as
216the source file (e.g. if the source was I<.html> and destination one
217is I<.html> too).
218
219The function returns a reference to a hash with the keys: I<size> --
220for the size of the source file and the location of the file relative
221to the current document (it's in the same directory after all).
222
223=item * pod_pom_html_view_seq_link_transform_path
224
225  my $linked_doc_path =
226      $self->pod_pom_html_view_seq_link_transform_path($src_path)
227
228this is an implementation of the view_seq_link_transform_path()
229callback used in C<Pod::POM::HTML::view_seq_link()>, using the
230C<DocSet::Doc>'s transform_src_doc() method over pre-scanned cache of
231the source documents the C<$src_path> is resolved into the path in the
232generated docset. So for example a the resource C<devel::help> in
233LE<lt>devel help doc|devel::helpL<gt> could get resolved as
234I<mydocs/devel/help.html>. For more info see the documentation for
235C<DocSet::Doc::transform_src_doc()>.
236
237Notice that since this method is a callback hook, it uses the runtime
238singleton function C<DocSet::RunTime::get_render_obj()> to retrieve
239the current document object.
240
241=item * pod_pom_html_anchor
242
243  my $anchor = $self->pod_pom_html_anchor($title);
244
245this is a common function that takes the C<$title> Pod::POM object,
246converts it into a E<lt>a nameE<gt> html anchor and returns it.
247
248=back
249
250
251=head1 AUTHORS
252
253Stas Bekman E<lt>stas (at) stason.orgE<gt>
254
255=cut
256
257