PerlDistSite.pm

filter: Statementstmt 83.3% 10 / 12 Branchbran n/a Conditioncond n/a MC/DCmcdc n/a Subroutinesub 100% 4 / 4 Podpod n/a totaltotal 87.5% 14 / 16 CC 1 SCAR 0.7
CC
1
Cov
83%
BEGIN
1.3
BEGIN
1.0
BEGIN
1.0
CRAP 1.1
« PerlDistSite/MenuItem/PodFile.pm ↑ Summary PerlDistSite/MenuItem.pm »
1 package Web::PerlDistSite;
2 2 ▶use utf8;
Subroutine BEGIN: called CC=1 SCAR=0.0
3  
4 =pod
5
6 =encoding utf-8
7
8 =head1 NAME
9
10 Web::PerlDistSite - generate fairly flashy websites for CPAN distributions
11
12 =head1 DESCRIPTION
13
14 Basically a highly specialized static site generator.
15
16 =head2 Prerequisites
17
18 You will need B<cpanm>.
19
20 You will need B<nodejs> with B<npm>.
21
22 You will need B<make>.
23
24 =head2 Setup
25
26 Create a directory and copy the example F<Makefile> and F<project.json>
27 files from this distribution into it. Then run C<< make install >> to
28 install additional Nodejs and Perl dependencies.
29
30 =head2 Site Configuration
31
32 Configuration is via a file F<config.yaml>. This is a YAML file containing
33 a hash with the following keys. Each key is optional, unless noted as required.
34 An example F<config.yaml> is included in this distribution.
35
36 =over
37
38 =item C<< theme >> I<< (required) >>
39
40 A hashref of colour codes. You need at least "primary", "secondary", "light",
41 and "dark". "info", "success", "warning", and "danger" are also allowed.
42
43 theme:
44 light: "#e4e3e1"
45 dark: "#32201D"
46 primary: "#763722"
47 secondary: "#E4A042"
48
49 =item C<< name >> I<< (required) >>
50
51 The name of the project you're building a website for. This is assumed
52 to be a CPAN distribution name, like "Exporter-Tiny" or "Foo-Bar-Baz".
53
54 =item C<< abstract >> I<< (required) >>
55
56 A short plain-text summary of the project.
57
58 =item C<< abstract_html >>
59
60 A short HTML summary of the project.
61
62 =item C<< copyright >> I<< (required) >>
63
64 A short plain-text copyright statement for the website footers.
65
66 =item C<< github >>
67
68 Link to a GitHub repo for the site. Expected to be of the form
69 "https://github.com/username/reponame".
70
71 =item C<< issues >>
72
73 Link to an issue tracker.
74
75 =item C<< sponsor >>
76
77 Hashref containing project sponsorship info. The "html" key is required.
78 The "href" key is optional.
79
80 sponsor:
81 html: "<strong>Please sponsor us!</strong> Blah blah blah."
82 href: https://paypal.example/foo-bar-baz
83
84 =item C<< menu >>
85
86 A list of files to include in the navbar. If this key is missing, will
87 be loaded from F<menu.yaml> instead.
88
89 =item C<< homepage >>
90
91 Hashref of options for the homepage (index.html). May contain keys
92 "animation", "banner", "banner_fixation", "banner_position_x", and
93 "banner_position_y".
94
95 The "animation" may be "waves1", "waves2", "swirl1", "attract1", or "circles1".
96 Each of these will create a pretty animation on the homepage. Some
97 day I'll add support for more animations.
98
99 If "animation" is not defined, then "banner" can be used to supply
100 the URL of a static image to use instead of an animation.
101
102 "banner_fixation" can be "scroll" or "fixed", and defaults to the latter.
103 "banner_position_x" can be "left", "center", or "right". "banner_position_y"
104 can be "top", "center", or "bottom". These each default to "center".
105
106 "hero_options" is I<itself> a hashref and allows various parts of the
107 banner/animation to be overridden. In particular, "title" and "abstract".
108
109 homepage:
110 animation: waves1
111 hero_options:
112 title: "Blah"
113 abstract: "Blah blah blah"
114
115 In the future, more homepage options may be available.
116
117 =item C<< dist_dir >>
118
119 Directory for output. Defaults to a subdirectory called "docs".
120
121 =item C<< root_url >>
122
123 URL for the output. Can be an absolute URL, but something like "/"
124 is probably okay. (That's the default.)
125
126 =item C<< codestyle >>
127
128 Name of a highlight.js theme, used for code syntax highlighting.
129 Defaults to "github".
130
131 =item C<< pod_filter >>
132
133 A list of section names which will be filtered out of pages generated
134 from pod files. Uses "|" as a separator. Defaults to:
135 "NAME|BUGS|AUTHOR|THANKS|COPYRIGHT AND LICENCE|DISCLAIMER OF WARRANTIES".
136
137 =item C<< pod_titlecase >>
138
139 Boolean. Should ALL CAPS "=head1" headings from pod be converted to
140 Title Case? Defaults to true.
141
142 =item C<< pod_downgrade_headings >>
143
144 Converts pod "=head1" to C<< <h2> >> tags in HTML, etc.
145
146 =back
147
148 =head2 Menu Configuration
149
150 The menu can be configured under the C<menu> key of F<config.yaml>, or in
151 a separate file F<menu.yaml>. This is a list of menu items. For example:
152
153 - name: installation
154 title: Installation
155 source: _pages/installation.md
156 - name: hints
157 title: Hints and Tips
158 source: _pages/hints.pod
159 - name: manual
160 title: Manual
161 children:
162 - name: Foo-Bar
163 pod: Foo::Bar
164 - name: Foo-Bar-Baz
165 pod: Foo::Bar::Baz
166
167 The C<name> key is used for the output filename. ".html" is automatically
168 appended.
169
170 The C<title> key is the title of the document generated. It is used in the
171 navbar and in the page's C<< C<title> >> element.
172
173 Each entry needs a C<source> which is an input filename. The input may
174 be pod or markdown. (At some future point, HTML input will also be supported.)
175
176 If the C<pod> key is found, we'll find the pod via C<< @INC >>, like
177 perldoc does. This will helpfully also default C<title> and C<name> for you!
178
179 A C<children> key allows child pages to be listed. Only one level of nesting
180 is supported in the navbar, but if further levels of nesting are used, these
181 pages will still be built. (You'll just need to link to them manually somehow.)
182
183 A list item like this can be used to add dividers:
184
185 - divider: true
186
187 If the input is pod, you can also provide C<pod_filter>, C<pod_titlecase>,
188 and C<pod_downgrade_headings> settings which override the global settings.
189
190 If the input is markdown, you may use "----" (four hyphens) on a line by
191 itself to divide the page into cute sections.
192
193 =head2 Homepage
194
195 You'll need a file called F<< _pages/index.md >> for the site's homepage.
196 The filename may be configurable some day.
197
198 =head2 Custom CSS
199
200 You can create a file called F<< custom.scss >> containing custom SCSS
201 code to override or add to the defaults.
202
203 =head2 Adding Images
204
205 If you create a directory called F<images>, this will be copied to
206 F<docs/assets/images/> during the build process. This should be used
207 for things like background images, etc.
208
209 =head2 Building the Site
210
211 Running C<< make all >> will build the site.
212
213 Running C<< make clean >> will remove the C<docs> directory and
214 also any temporary SCSS files created during the build process.
215
216 =head1 EXAMPLE
217
218 Example: L<https://github.com/exportertiny/exportertiny.github.io>
219
220 Generated this site: L<https://exportertiny.github.io>
221
222 =head1 BUGS
223
224 Please report any bugs to
225 L<https://github.com/tobyink/p5-web-perldistsite/issues>.
226
227 =head1 AUTHOR
228
229 Toby Inkster E<lt>tobyink@cpan.orgE<gt>.
230
231 =head1 COPYRIGHT AND LICENCE
232
233 This software is copyright (c) 2023 by Toby Inkster.
234
235 This is free software; you can redistribute it and/or modify it under
236 the same terms as the Perl 5 programming language system itself.
237
238 =head1 DISCLAIMER OF WARRANTIES
239
240 THIS PACKAGE IS PROVIDED "AS IS" AND WITHOUT ANY EXPRESS OR IMPLIED
241 WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED WARRANTIES OF
242 MERCHANTIBILITY AND FITNESS FOR A PARTICULAR PURPOSE.
243
244 =cut
245  
246 2 ▶use Moo;
Subroutine BEGIN: called CC=1 SCAR=0.0
247 2 ▶use Web::PerlDistSite::Common -lexical, -all;
Subroutine BEGIN: called CC=1 SCAR=0.0
248  
249 our $VERSION = '0.001008';
250  
251 2 ▶use Web::PerlDistSite::MenuItem ();
Subroutine BEGIN: called CC=1 SCAR=2.6
252 use HTML::HTML5::Parser ();
253  
254 has root => (
255 is       => 'ro',
256 isa      => PathTiny,
257 required => true,
258 coerce   => true,
259 );
260  
261 has root_url => (
262 is       => 'rwp',
263 isa      => Str,
264 default  => '/',
265 );
266  
267 has dist_dir => (
268 is       => 'lazy',
269 isa      => PathTiny,
270 coerce   => true,
271 builder  => sub ( $s ) { $s->root->child( 'docs' ) },
272 );
273  
274 has name => (
275 is       => 'ro',
276 isa      => Str,
277 required => true,
278 );
279  
280 has abstract => (
281 is       => 'ro',
282 isa      => Str,
283 required => true,
284 );
285  
286 has abstract_html => (
287 is       => 'lazy',
288 isa      => Str,
289 default  => sub ( $s ) { esc_html( $s->abstract ) },
290 );
291  
292 has issues => (
293 is       => 'ro',
294 isa      => Str,
295 );
296  
297 has copyright => (
298 is       => 'ro',
299 isa      => Str,
300 );
301  
302 has github => (
303 is       => 'ro',
304 isa      => Str,
305 );
306  
307 has sponsor => (
308 is       => 'ro',
309 isa      => HashRef,
310 );
311  
312 has theme => (
313 is      => 'ro',
314 isa     => HashRef->of( Str ),
315 );
316  
317 has codestyle => (
318 is       => 'ro',
319 isa      => Str,
320 default  => 'github',
321 );
322  
323 has pod_filter => (
324 is       => 'ro',
325 isa      => Str,
326 default  => 'NAME|BUGS|AUTHOR|THANKS|COPYRIGHT AND LICENCE|DISCLAIMER OF WARRANTIES',
327 );
328  
329 has pod_titlecase => (
330 is       => 'ro',
331 isa      => Bool,
332 default  => true,
333 );
334  
335 has pod_downgrade_headings => (
336 is       => 'ro',
337 isa      => Bool,
338 default  => true,
339 );
340  
341 has menu => (
342 is      => 'ro',
343 isa      => ArrayRef->of(
344 InstanceOf
345 ->of( 'Web::PerlDistSite::MenuItem' )
346 ->plus_constructors( HashRef, 'from_hashref' )
347 ),
348 coerce   => true,
349 );
350  
351 has homepage => (
352 is       => 'ro',
353 isa      => HashRef,
354 default  => sub ( $s ) {
355 return { animation => 'waves1' };
356 },
357 );
358  
359 sub css_timestamp ( $self ) {
360 $self->dist_dir->child( 'assets/styles/main.css' )->stat->mtime;
361 }
362  
363 sub load ( $class, $filename='config.yaml' ) {
364 my $data = YAML::PP::LoadFile( $filename );
365 $data->{root} //= path( $filename )->absolute->parent;
366 $data->{menu} //= YAML::PP::LoadFile( $data->{root}->child( 'menu.yaml' ) );
367 $class->new( $data->%* );
368 }
369  
370 sub footer ( $self ) {
371 my @sections;
372
373 if ( $self->github ) {
374 push @sections, sprintf(
375 '<h2>Contributing</h2>
376 <p>%s is an open source project <a href="%s">hosted on GitHub</a> —
377 open an issue if you have an idea or find a bug.</p>',
378 esc_html( $self->name ),
379 esc_html( $self->github ),
380 );
381 if ( $self->github =~ m{^https://github.com/(.+)$} ) {
382 my $path = $1;
383 $sections[-1] .= sprintf(
384 '<p><img alt="GitHub repo stars"
385 src="https://img.shields.io/github/stars/%s?style=social"></p>',
386 $path,
387 );
388 }
389 }
390
391 if ( $self->sponsor ) {
392 push @sections, sprintf(
393 '<h2>Sponsoring</h2>
394 <p>%s</p>',
395 esc_html( $self->sponsor->{html} ),
396 );
397 if ( $self->sponsor->{href} ) {
398 $sections[-1] .= sprintf(
399 '<p><a class="btn btn-light" href="%s"><span class="text-dark">Sponsor</span></a></p>',
400 esc_html( $self->sponsor->{href} ),
401 );
402 }
403 }
404
405 my $width = int( 12 / @sections );
406 my @html;
407 push @html, '<div class="container">';
408 push @html, '<div class="row">';
409 for my $section ( @sections ) {
410 push @html, "<div class=\"col-12 col-lg-$width\">$section</div>";
411 }
412 if ( $self->copyright ) {
413 push @html, '<div class="col-12 text-center pt-3">';
414 push @html, sprintf( '<p>%s</p>', esc_html( $self->copyright ) );
415 push @html, '</div>';
416 }
417 push @html, '</div>';
418 push @html, '</div>';
419 return join qq{\n}, @html;
420 }
421  
422 sub navbar ( $self, $active_item ) {
423 my @html;
424 push @html, '<nav class="navbar navbar-expand-lg">';
425 push @html, '<div class="container">';
426 push @html, sprintf( '<a class="navbar-brand" href="%s">%s</a>', $self->root_url, $self->name );
427 push @html, '<button class="navbar-toggler" type="button" data-bs-toggle="collapse" data-bs-target="#navbarSupportedContent" aria-controls="navbarSupportedContent" aria-expanded="false" aria-label="Toggle navigation">';
428 push @html, '<span class="navbar-toggler-icon"></span>';
429 push @html, '</button>';
430 push @html, '<div class="collapse navbar-collapse" id="navbarSupportedContent">';
431 push @html, '<ul class="navbar-nav ms-auto mb-2 mb-lg-0">';
432 push @html, map $_->nav_item( $active_item ), $self->menu->@*;
433 push @html, '</ul>';
434 push @html, '</div>';
435 push @html, '</div>';
436 push @html, '</nav>';
437 return join qq{\n}, @html;
438 }
439  
440 sub BUILD ( $self, $args ) {
441 $_->project( $self ) for $self->menu->@*;
442 $self->root_url( $self->root_url . '/' ) unless $self->root_url =~ m{/$};
443 }
444  
445 sub write_pages ( $self ) {
446 for my $item ( $self->menu->@* ) {
447 $item->write_pages;
448 }
449 $self->write_homepage;
450 }
451  
452 sub write_variables_scss ( $self ) {
453 my $scss = '';
454 for my $key ( sort keys $self->theme->%* ) {
455 $scss .= sprintf( '$%s: %s;', $key, $self->theme->{$key} ) . "\n";
456 }
457 $self->root->child( '_build/variables.scss' )->spew_if_changed( $scss );
458 }
459  
460 sub write_homepage ( $self ) {
461 require Web::PerlDistSite::MenuItem::Homepage;
462 my $page = Web::PerlDistSite::MenuItem::Homepage->new(
463 $self->homepage->%*,
464 project => $self,
465 );
466 $page->write_pages;
467 }
468  
469 sub get_template_page ( $self, $item=undef ) {
470 state $template = do {
471 local $/;
472 my $html = <DATA>;
473 $html =~ s[\{\{\s*\$root\s*\}\}]{$self->root_url}ge;
474 $html =~ s[\{\{\s*\$codestyle\s*\}\}]{$self->codestyle}ge;
475 $html =~ s[\{\{\s*\$css_timestamp\s*\}\}]{$self->css_timestamp}ge;
476 $html;
477 };
478
479 state $p = HTML::HTML5::Parser->new;
480 my $dom = $p->parse_string( $template );
481
482 my $navbar = $p->parse_balanced_chunk( $self->navbar( $item ) );
483 $dom->getElementsByTagName( 'header' )->shift->appendChild( $navbar );
484
485 my $footer = $p->parse_balanced_chunk( $self->footer );
486 $dom->getElementsByTagName( 'footer' )->shift->appendChild( $footer );
487
488 $dom->getElementsByTagName( 'title' )->shift->appendText( $item ? $item->page_title : $self->name );
489
490 return $dom;
491 }
492  
493 1;
494  
495 __DATA__
496 <html>
497 <head>
498 <meta charset="utf-8">
499 <meta name="viewport" content="width=device-width, initial-scale=1">
500 <title></title>
501 <link href="{{ $root }}assets/styles/main.css?v={{ $css_timestamp }}" rel="stylesheet">
502 <link rel="stylesheet" href="//unpkg.com/@highlightjs/cdn-assets@11.7.0/styles/{{ $codestyle }}.min.css">
503 </head>
504 <body id="top">
505 <header></header>
506 <main></main>
507 <div style="height: 150px; overflow: hidden;"><svg viewBox="0 0 500 150" preserveAspectRatio="none" style="height: 100%; width: 100%;"><path d="M0.00,49.98 C138.82,121.67 349.20,-49.98 500.00,49.98 L500.00,150.00 L0.00,150.00 Z" style="stroke: none; fill: rgba(var(--bs-dark-rgb), 1);"></path></svg></div>
508 <footer id="bottom"></footer>
509 <a id="return-to-top" href="#top"><i class="fa-solid fa-circle-up"></i></a>
510 <script src="{{ $root }}assets/scripts/bootstrap.bundle.min.js"></script>
511 <script src="//kit.fontawesome.com/6d700b1a29.js" crossorigin="anonymous"></script>
512 <script src="//unpkg.com/@highlightjs/cdn-assets@11.7.0/highlight.min.js"></script>
513 <script>
514 const classy_scroll = function () {
515 const scroll = document.documentElement.scrollTop;
516 const avail = window.screen.availHeight;
517 if ( scroll > 75 ) {
518 document.body.classList.add( 'is-scrolled' );
519 document.body.classList.remove( 'at-top' );
520 }
521 else if ( scroll < 25 ) {
522 document.body.classList.remove( 'is-scrolled' );
523 document.body.classList.add( 'at-top' );
524 }
525 if ( scroll > avail ) {
526 document.body.classList.add( 'is-scrolled-deeply' );
527 }
528 else if ( scroll < avail ) {
529 document.body.classList.remove( 'is-scrolled-deeply' );
530 }
531 };
532 classy_scroll();
533 window.addEventListener( 'scroll', classy_scroll );
534
535 hljs.highlightAll();
536 </script>
537 </body>
538 </html>
« PerlDistSite/MenuItem/PodFile.pm ↑ Summary PerlDistSite/MenuItem.pm »