summaryrefslogtreecommitdiff
path: root/lib/App/DocKnot/Spin/Pointer.pm
blob: 160b11ed3e9652a68e25353fe200147b0f444953 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
# Generate HTML from a pointer to an external file.
#
# The input tree for spin may contain pointers to external files in various
# formats.  This module parses those pointer files and performs the conversion
# of those external files into HTML.
#
# SPDX-License-Identifier: MIT

##############################################################################
# Modules and declarations
##############################################################################

package App::DocKnot::Spin::Pointer 7.00;

use 5.024;
use autodie;
use parent qw(App::DocKnot);
use warnings FATAL => 'utf8';

use App::DocKnot::Config;
use App::DocKnot::Util qw(is_newer);
use Carp qw(croak);
use Encode qw(decode);
use File::BaseDir qw(config_files);
use IPC::System::Simple qw(capturex);
use Path::Tiny qw(path);
use Pod::Thread 3.01 ();
use POSIX qw(strftime);
use Template ();
use YAML::XS ();

# The URL to the software page for all of my web page generation software,
# used to embed a link to the software that generated the page.
my $URL = 'https://www.eyrie.org/~eagle/software/web/';

##############################################################################
# Format conversions
##############################################################################

# Convert a Markdown file to HTML.
#
# $data_ref - Data from the pointer file
#   path  - Path to the Markdown file to convert
#   style - Style sheet to use
# $base     - Base path of pointer file (for relative paths)
# $output   - Path to the output file
#
# Throws: Text exception on conversion failure
sub _spin_markdown {
    my ($self, $data_ref, $base, $output) = @_;
    my $source = path($data_ref->{path})->absolute($base);

    # Do the Markdown conversion using pandoc.
    my $html = capturex(
        $self->{pandoc_path}, '--wrap=preserve', '-f', 'markdown',
        '-t', 'html', $source,
    );

    # Pull the title out of the contents of the <h1> header if not set.
    my $title = $data_ref->{title};
    if (!defined($title)) {
        ($title) = $html =~ m{ <h1 [^>]+ > (.*?) </h1> }xms;
    }

    # Construct the template variables.
    my ($links, $navbar, $style);
    if ($self->{sitemap}) {
        my $page = $output->relative($self->{output});
        my @links = $self->{sitemap}->links($page);
        if (@links) {
            $links = join(q{}, @links);
        }
        my @navbar = $self->{sitemap}->navbar($page);
        if (@navbar) {
            $navbar = join(q{}, @navbar);
        }
    }
    if ($data_ref->{style}) {
        $style = $self->{style_url} . $data_ref->{style};
    }
    #<<<
    my %vars = (
        docknot_url => $URL,
        html        => decode('utf-8', $html),
        links       => $links,
        modified    => strftime('%Y-%m-%d', gmtime($source->stat()->[9])),
        navbar      => $navbar,
        now         => strftime('%Y-%m-%d', gmtime()),
        style       => $style,
        title       => $title,
    );
    #>>>

    # Construct the output page from those template variables.
    my $result;
    $self->{template}->process($self->{template_path}, \%vars, \$result)
      or croak($self->{template}->error());

    # Write the result to the output file.
    $output->spew_utf8($result);
    return;
}

# Convert a POD file to HTML.
#
# $data_ref - Data from the pointer file
#   options - Hash of conversion options
#     contents - Whether to add a table of contents
#     navbar   - Whether to add a navigation bar
#   path    - Path to the POD file to convert
#   style   - Style sheet to use
# $base     - Base path of pointer file (for relative paths)
# $output   - Path to the output file
#
# Throws: Text exception on conversion failure
sub _spin_pod {
    my ($self, $data_ref, $base, $output) = @_;
    my $source = path($data_ref->{path})->absolute($base);

    # Construct the Pod::Thread formatter object.
    #<<<
    my %options = (
        contents => $data_ref->{options}{contents},
        style    => $data_ref->{style} // 'pod',
        title    => $data_ref->{title},
    );
    #<<<
    if (exists($data_ref->{options}{navbar})) {
        $options{navbar} = $data_ref->{options}{navbar};
    } else {
        $options{navbar} = 1;
    }
    my $podthread = Pod::Thread->new(%options);

    # Convert the POD to thread.
    my $data;
    $podthread->output_string(\$data);
    $podthread->parse_file("$source");
    $data = decode('utf-8', $data);

    # Spin that page into HTML.
    $self->{thread}->spin_thread_output($data, $source, 'POD', $output);
    return;
}

##############################################################################
# Public interface
##############################################################################

# Create a new HTML converter for pointers.  This object can (and should) be
# reused for all pointer conversions done while spinning a tree of files.
#
# $args - Anonymous hash of arguments with the following keys:
#   output    - Root of the output tree
#   sitemap   - App::DocKnot::Spin::Sitemap object
#   style-url - Partial URL to style sheets
#   thread    - App::DocKnot::Spin::Thread object
#
# Returns: Newly created object
#  Throws: Text exception on failure to initialize Template Toolkit
sub new {
    my ($class, $args_ref) = @_;

    # Get the configured path to pandoc, if any.
    my $config_reader = App::DocKnot::Config->new();
    my $global_config_ref = $config_reader->global_config();
    my $pandoc = $global_config_ref->{pandoc} // 'pandoc';

    # Add a trailing slash to the partial URL for style sheets.
    my $style_url = $args_ref->{'style-url'} // q{};
    if ($style_url) {
        $style_url =~ s{ /* \z }{/}xms;
    }

    # Create and return the object.
    my $tt = Template->new({ ABSOLUTE => 1, ENCODING => 'utf8' })
      or croak(Template->error());
    #<<<
    my $self = {
        output      => $args_ref->{output},
        pandoc_path => $pandoc,
        sitemap     => $args_ref->{sitemap},
        style_url   => $style_url,
        template    => $tt,
        thread      => $args_ref->{thread},
    };
    #>>>
    bless($self, $class);
    $self->{template_path} = $self->appdata_path('templates', 'html.tmpl');
    return $self;
}

# Check if the result of a pointer file needs to be regenerated.
#
# $pointer - Path to pointer file
# $output  - Path to corresponding output file
#
# Returns: True if the output file does not exist or has a modification date
#          older than either the pointer file or the underlying source file,
#          false otherwise
#  Throws: YAML::XS exception on invalid pointer
sub is_out_of_date {
    my ($self, $pointer, $output) = @_;
    my $data_ref = $self->load_yaml_file($pointer, 'pointer');
    my $path = path($data_ref->{path})->absolute($pointer->parent());
    if (!$path->exists()) {
        die "$pointer: path $data_ref->{path} ($path) does not exist\n";
    }
    return !is_newer($output, $pointer, $path);
}

# Process a given pointer file.
#
# $pointer - Path to pointer file to process
# $output  - Path to corresponding output file
#
# Throws: YAML::XS exception on invalid pointer
#         Text exception for missing input file
#         Text exception on failure to convert the file
sub spin_pointer {
    my ($self, $pointer, $output, $options_ref) = @_;
    my $data_ref = $self->load_yaml_file($pointer, 'pointer');
    $data_ref->{options} //= {};

    # Dispatch to the appropriate conversion function.
    if ($data_ref->{format} eq 'markdown') {
        $self->_spin_markdown($data_ref, $pointer->parent(), $output);
    } elsif ($data_ref->{format} eq 'pod') {
        $self->_spin_pod($data_ref, $pointer->parent(), $output);
    } else {
        die "$pointer: unknown output format $data_ref->{format}\n";
    }
    return;
}

##############################################################################
# Module return value and documentation
##############################################################################

1;

__END__

=for stopwords
Allbery DocKnot MERCHANTABILITY NONINFRINGEMENT Kwalify sublicense unstyled
navbar

=head1 NAME

App::DocKnot::Spin::Pointer - Generate HTML from a pointer to an external file

=head1 SYNOPSIS

    use App::DocKnot::Spin::Pointer;
    use App::DocKnot::Spin::Sitemap;
    use Path::Tiny qw(path);

    my $sitemap_path = path('/input/.sitemap');
    my $sitemap = App::DocKnot::Spin::Sitemap->new($sitemap_path);
    my $pointer = App::DocKnot::Spin::Pointer->new({
        output  => path('/output'),
        sitemap => $sitemap,
    });
    my $input = path('/input/file.spin');
    my $output = path('/output/file.html');
    $pointer->spin_pointer($input, $output);

=head1 REQUIREMENTS

Perl 5.24 or later and the modules File::ShareDir, Kwalify, List::SomeUtils,
Path::Tiny, Pod::Thread, and YAML::XS, all of which are available from CPAN.

=head1 DESCRIPTION

The tree of input files for App::DocKnot::Spin may contain pointers to
external files in various formats.  These files are in YAML format and end in
C<.spin>.  This module processes those files and converts them to HTML and, if
so configured, adds the links to integrate the page with the rest of the site.

For the details of the pointer file format, see L<POINTER FILES> below.

=head1 CLASS METHODS

=over 4

=item new(ARGS)

Create a new App::DocKnot::Spin::Pointer object.  A single converter object
can be used repeatedly to convert pointers in a tree of files.  ARGS should
be a hash reference with one or more of the following keys, all of which are
optional:

=over 4

=item output

The path to the root of the output tree when converting a tree of files.  This
will be used to calculate relative path names for generating inter-page links
using the provided C<sitemap> argument.  If C<sitemap> is given, this option
should also always be given.

=item sitemap

An App::DocKnot::Spin::Sitemap object.  This will be used to create inter-page
links.  For inter-page links, the C<output> argument must also be provided.

=item style-url

The base URL for style sheets.  A style sheet specified in a pointer file will
be considered to be relative to this URL and this URL will be prepended to it.
If this option is not given, the name of the style sheet will be used verbatim
as its URL, except with C<.css> appended.

=item thread

An App::DocKnot::Spin::Thread object, used for converting POD into HTML.  It
should be configured with the same App::DocKnot::Spin::Sitemap object as the
C<sitemap> argument.

=back

=back

=head1 INSTANCE METHODS

=over 4

=item is_out_of_date(POINTER, OUTPUT)

Returns true if OUTPUT is missing or if it was modified less recently than the
modification time of either POINTER or the underlying file that it points to.
Both paths must be Path::Tiny objects.

=item spin_pointer(POINTER, OUTPUT)

Convert a single pointer file to HTML.  POINTER is the path to the pointer
file, and OUTPUT is the path to where to write the output.  Both paths must
be Path::Tiny objects.

=back

=head1 POINTER FILES

A pointer file is a YAML file ending in C<.spin> that points to the source
file for a generated HTML page and provides additional configuration for its
conversion.  The valid keys for a pointer file are:

=over 4

=item format

The format of the source file.  Supported values are C<markdown> and C<pod>.
Required.

=item path

The path to the source file.  It may be relative, in which case it's relative
to the pointer file.  Required.

=item options

Additional options that control the conversion to HTML.  These will be
different for each supported format.

C<markdown> has no supported options.

The supported options for a format of C<pod> are:

=over 4

=item contents

Boolean saying whether to generate a table of contents.  The default is false.

=item navbar

Boolean saying whether to generate a navigation bar at the top of the page.
The default is true.

=back

=item style

The style sheet to use for the converted output.  Optional.  If not set,
converted C<markdown> output will be unstyled and converted C<pod> output will
use a style sheet named C<pod>.

=item title

The title of the converted page.  Optional.  If not set, the title will be
taken from the converted file in a format-specific way.  For Markdown, the
title will be the contents of the first top-level heading.  For POD, the title
will be taken from a NAME section formatted according to the conventions for
manual pages.

=back

=head1 AUTHOR

Russ Allbery <rra@cpan.org>

=head1 COPYRIGHT AND LICENSE

Copyright 2021 Russ Allbery <rra@cpan.org>

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.

=head1 SEE ALSO

L<docknot(1)>, L<App::DocKnot::Spin>, L<App::DocKnot::Spin::Sitemap>

This module is part of the App-DocKnot distribution.  The current version of
DocKnot is available from CPAN, or directly from its web site at
L<https://www.eyrie.org/~eagle/software/docknot/>.

=cut

# Local Variables:
# copyright-at-end-flag: t