| 1 | =head1 NAME | ||||||||||||||||||||||||||||
| 2 | |||||||||||||||||||||||||||||
| 3 | App::LinkSite - Create a website listing all of your links | ||||||||||||||||||||||||||||
| 4 | |||||||||||||||||||||||||||||
| 5 | =head1 SYNOPIS | ||||||||||||||||||||||||||||
| 6 | |||||||||||||||||||||||||||||
| 7 | (You probably want to just look at the L<linksite> application.) | ||||||||||||||||||||||||||||
| 8 | |||||||||||||||||||||||||||||
| 9 | =head1 DESCRIPTION | ||||||||||||||||||||||||||||
| 10 | |||||||||||||||||||||||||||||
| 11 | The main driver class for App::LinkSite. | ||||||||||||||||||||||||||||
| 12 | |||||||||||||||||||||||||||||
| 13 | =cut | ||||||||||||||||||||||||||||
| 14 | |||||||||||||||||||||||||||||
| 15 | 1 | ▶ | use Feature::Compat::Class; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 16 | |||||||||||||||||||||||||||||
| 17 | 1 | class App::LinkSite { | |||||||||||||||||||||||||||
| 18 | 1 | our $VERSION = '0.1.3'; | |||||||||||||||||||||||||||
| 19 | 1 | ▶ | use strict; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 20 | 1 | ▶ | use warnings; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 21 | 1 | ▶ | use feature qw[say signatures]; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 22 | 1 | ▶ | no if $] >= 5.038, 'warnings', qw[experimental::signatures experimental::class]; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 23 | |||||||||||||||||||||||||||||
| 24 | 1 | ▶ | use Template; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 25 | 1 | ▶ | use JSON; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 26 | 1 | ▶ | use Path::Tiny; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 27 | 1 | ▶ | use File::Find; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 28 | 1 | ▶ | use File::Basename; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 29 | 1 | ▶ | use FindBin '$Bin'; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 30 | 1 | ▶ | use File::ShareDir 'dist_dir'; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 31 | |||||||||||||||||||||||||||||
| 32 | 1 | ▶ | use App::LinkSite::Site; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 33 | 1 | ▶ | use App::LinkSite::Link; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 34 | 1 | ▶ | use App::LinkSite::Social; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 35 | 1 | ▶ | use App::LinkSite::Section; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 36 | |||||||||||||||||||||||||||||
| 37 | 1 | field $file :reader :param = 'links.json'; | |||||||||||||||||||||||||||
| 38 | # Where to look for the templates. | ||||||||||||||||||||||||||||
| 39 | # If we've been installed from CPAN, then File::Share::dist_name | ||||||||||||||||||||||||||||
| 40 | # gives us the correct directory. Otherwise, just look in the local | ||||||||||||||||||||||||||||
| 41 | # src directory. Note that dist_name() dies if the directory is not | ||||||||||||||||||||||||||||
| 42 | # found - hence the use of eval. | ||||||||||||||||||||||||||||
| 43 | 1 | field $src :reader :param = eval { dist_dir("App-LinkSite") } || "$Bin/../src"; | |||||||||||||||||||||||||||
| 44 | 1 | field $out :reader :param = 'docs'; | |||||||||||||||||||||||||||
| 45 | 1 | field $ga4 :reader :param = undef; | |||||||||||||||||||||||||||
| 46 | 1 | field $site :reader :param = undef; | |||||||||||||||||||||||||||
| 47 | |||||||||||||||||||||||||||||
| 48 | 1 | field $tt; | |||||||||||||||||||||||||||
| 49 | |||||||||||||||||||||||||||||
| 50 | 1 | ▶ | ADJUST { | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=6 SCAR=19.9
| |||||||||||||||||||||||||||||
| 51 | 1 | my $json = path($file)->slurp; | |||||||||||||||||||||||||||
| 52 | 1 | my $data = JSON->new->decode($json); | |||||||||||||||||||||||||||
| 53 | |||||||||||||||||||||||||||||
| 54 | 1 | ▶ | $ga4 = $data->{ga4} // ''; | ||||||||||||||||||||||||||
|
Condition50%1/2
$data->{"ga4"} // ""
MC/DC0%0/1
$data->{"ga4"} // ""
$data->{"ga4"}
Truth table50%1/2
A
A= $data->{"ga4"}
| |||||||||||||||||||||||||||||
| 55 | |||||||||||||||||||||||||||||
| 56 | 1 | $tt = Template->new({ | |||||||||||||||||||||||||||
| 57 | # Templates in the CPAN distro directory | ||||||||||||||||||||||||||||
| 58 | INCLUDE_PATH => $src, | ||||||||||||||||||||||||||||
| 59 | # Output in the data directory | ||||||||||||||||||||||||||||
| 60 | OUTPUT_PATH => $out, | ||||||||||||||||||||||||||||
| 61 | VARIABLES => { | ||||||||||||||||||||||||||||
| 62 | ga4 => $ga4, | ||||||||||||||||||||||||||||
| 63 | version => $VERSION, | ||||||||||||||||||||||||||||
| 64 | } | ||||||||||||||||||||||||||||
| 65 | }); | ||||||||||||||||||||||||||||
| 66 | |||||||||||||||||||||||||||||
| 67 | my $socials = [ map { | ||||||||||||||||||||||||||||
| 68 | 14 | ▶ | $_->{handle} //= $data->{handle}; | ||||||||||||||||||||||||||
|
Condition66%2/3
$_->{"handle"} //= $data->{"handle"}
MC/DC0%0/2
$_->{"handle"} //= $data->{"handle"}
$_->{"handle"}
$data->{"handle"}
Truth table66%2/3
A //= B
A= $_->{"handle"}B= $data->{"handle"}
| |||||||||||||||||||||||||||||
| 69 | 14 | App::LinkSite::Social->new(%$_) | |||||||||||||||||||||||||||
| 70 | 1 | } $data->{social}->@* ]; | |||||||||||||||||||||||||||
| 71 | |||||||||||||||||||||||||||||
| 72 | 1 | my $links = [ map { App::LinkSite::Link->new(%$_) } $data->{links}->@* ]; | |||||||||||||||||||||||||||
| 73 | |||||||||||||||||||||||||||||
| 74 | 1 | my $sections = []; | |||||||||||||||||||||||||||
| 75 | 1 | ▶ | if ($data->{sections}) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 76 | $sections = [ map { | ||||||||||||||||||||||||||||
| 77 | 0 | my $section_links = [ map { App::LinkSite::Link->new(%$_) } $_->{links}->@* ]; | |||||||||||||||||||||||||||
| 78 | App::LinkSite::Section->new( | ||||||||||||||||||||||||||||
| 79 | title => $_->{title}, | ||||||||||||||||||||||||||||
| 80 | 0 | links => $section_links, | |||||||||||||||||||||||||||
| 81 | ) | ||||||||||||||||||||||||||||
| 82 | 0 | } $data->{sections}->@* ]; | |||||||||||||||||||||||||||
| 83 | } | ||||||||||||||||||||||||||||
| 84 | |||||||||||||||||||||||||||||
| 85 | $site = App::LinkSite::Site->new( | ||||||||||||||||||||||||||||
| 86 | name => $data->{name}, | ||||||||||||||||||||||||||||
| 87 | handle => $data->{handle}, | ||||||||||||||||||||||||||||
| 88 | image => $data->{image}, | ||||||||||||||||||||||||||||
| 89 | desc => $data->{desc}, | ||||||||||||||||||||||||||||
| 90 | og_image => $data->{og_image}, | ||||||||||||||||||||||||||||
| 91 | site_url => $data->{site_url}, | ||||||||||||||||||||||||||||
| 92 | socials => $socials, | ||||||||||||||||||||||||||||
| 93 | links => $links, | ||||||||||||||||||||||||||||
| 94 | sections => $sections, | ||||||||||||||||||||||||||||
| 95 | $data->{text_color} ? (text_color => $data->{text_color}) : (), | ||||||||||||||||||||||||||||
| 96 | 1 | ▶ | $data->{background_color} ? (background_color => $data->{background_color} ) : (), | ||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||
| 97 | ); | ||||||||||||||||||||||||||||
| 98 | } | ||||||||||||||||||||||||||||
| 99 | |||||||||||||||||||||||||||||
| 100 | =head1 METHODS | ||||||||||||||||||||||||||||
| 101 | |||||||||||||||||||||||||||||
| 102 | =head2 run | ||||||||||||||||||||||||||||
| 103 | |||||||||||||||||||||||||||||
| 104 | The main driver method for the process. | ||||||||||||||||||||||||||||
| 105 | |||||||||||||||||||||||||||||
| 106 | =cut | ||||||||||||||||||||||||||||
| 107 | |||||||||||||||||||||||||||||
| 108 | 0 | ▶ | method run { | ||||||||||||||||||||||||||
|
Subroutine
run: not called CC=6 SCAR=37.4
Pod
documented
| |||||||||||||||||||||||||||||
| 109 | 0 | debug("src is: $src"); | |||||||||||||||||||||||||||
| 110 | 0 | debug("out is: $out"); | |||||||||||||||||||||||||||
| 111 | 0 | path($out)->mkdir; | |||||||||||||||||||||||||||
| 112 | 0 | ▶ | find( { wanted => sub { $self->do_this }, no_chdir => 1 }, $src); | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=6.9
| |||||||||||||||||||||||||||||
| 113 | |||||||||||||||||||||||||||||
| 114 | 0 | ▶ | if ($site->image or $site->og_image) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/3
$site->image or $site->og_image
MC/DC0%0/2
$site->image or $site->og_image
$site->image
$site->og_image
Truth table0%0/3
A or B
A= $site->imageB= $site->og_image
| |||||||||||||||||||||||||||||
| 115 | 0 | path("$out/img")->mkdir; | |||||||||||||||||||||||||||
| 116 | 0 | debug("Copy images"); | |||||||||||||||||||||||||||
| 117 | 0 | for my $img ($site->image, $site->og_image) { | |||||||||||||||||||||||||||
| 118 | 0 | ▶ | next unless $img; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 119 | 0 | path("img/$img")->copy("$out/img"); | |||||||||||||||||||||||||||
| 120 | } | ||||||||||||||||||||||||||||
| 121 | } | ||||||||||||||||||||||||||||
| 122 | |||||||||||||||||||||||||||||
| 123 | 0 | ▶ | if (-f './CNAME') { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 124 | 0 | debug("Copy CNAME"); | |||||||||||||||||||||||||||
| 125 | 0 | path('./CNAME')->copy("$out/CNAME"); | |||||||||||||||||||||||||||
| 126 | } | ||||||||||||||||||||||||||||
| 127 | |||||||||||||||||||||||||||||
| 128 | 0 | debug("Copy input JSON file"); | |||||||||||||||||||||||||||
| 129 | 0 | path($file)->copy($out); | |||||||||||||||||||||||||||
| 130 | } | ||||||||||||||||||||||||||||
| 131 | |||||||||||||||||||||||||||||
| 132 | =head2 do_this | ||||||||||||||||||||||||||||
| 133 | |||||||||||||||||||||||||||||
| 134 | A method is called for each file that is found in the `src` directory. | ||||||||||||||||||||||||||||
| 135 | |||||||||||||||||||||||||||||
| 136 | =cut | ||||||||||||||||||||||||||||
| 137 | |||||||||||||||||||||||||||||
| 138 | 0 | ▶ | method do_this { | ||||||||||||||||||||||||||
|
Subroutine
do_this: not called CC=7 SCAR=40.3
Pod
documented
| |||||||||||||||||||||||||||||
| 139 | 0 | ▶ | if ($File::Find::name eq $src or $File::Find::name eq "$src/") { | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/3
$File::Find::name eq $src or $File::Find::name eq "$src/"
MC/DC0%0/2
$File::Find::name eq $src or $File::Find::name eq "$src/"
$File::Find::name eq $src
$File::Find::name eq "$src/"
Truth table0%0/3
A or B
A= $File::Find::name eq $srcB= $File::Find::name eq "$src/"
| |||||||||||||||||||||||||||||
| 140 | 0 | debug("Skipping $File::Find::name"); | |||||||||||||||||||||||||||
| 141 | 0 | return; | |||||||||||||||||||||||||||
| 142 | } | ||||||||||||||||||||||||||||
| 143 | |||||||||||||||||||||||||||||
| 144 | 0 | my $path = $File::Find::name =~ s|^$src/||r; | |||||||||||||||||||||||||||
| 145 | |||||||||||||||||||||||||||||
| 146 | 0 | ▶ | if (/\.tt$/) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 147 | 0 | debug("Process $path to", basename($path, '.tt')); | |||||||||||||||||||||||||||
| 148 | 0 | ▶ | $tt->process($path, { site => $self->site }, basename($path, '.tt')) | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 149 | or die $tt->error; | ||||||||||||||||||||||||||||
| 150 | } else { | ||||||||||||||||||||||||||||
| 151 | 0 | ▶ | if (-d) { | ||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||
| 152 | 0 | debug("Make directory $path"); | |||||||||||||||||||||||||||
| 153 | 0 | path("$out/$path")->mkdir; | |||||||||||||||||||||||||||
| 154 | } elsif (-f) { | ||||||||||||||||||||||||||||
| 155 | 0 | debug("Copy $path"); | |||||||||||||||||||||||||||
| 156 | 0 | path("$src/$path")->copy("$out/$path"); | |||||||||||||||||||||||||||
| 157 | } else { | ||||||||||||||||||||||||||||
| 158 | 0 | debug("Confused by $File::Find::name"); | |||||||||||||||||||||||||||
| 159 | } | ||||||||||||||||||||||||||||
| 160 | } | ||||||||||||||||||||||||||||
| 161 | } | ||||||||||||||||||||||||||||
| 162 | |||||||||||||||||||||||||||||
| 163 | =head2 debug | ||||||||||||||||||||||||||||
| 164 | |||||||||||||||||||||||||||||
| 165 | Debug output. Set `LINKSITE_DEBUG` to a true value to turn this on. | ||||||||||||||||||||||||||||
| 166 | |||||||||||||||||||||||||||||
| 167 | export LINKSITE_DEBUG=1 | ||||||||||||||||||||||||||||
| 168 | |||||||||||||||||||||||||||||
| 169 | =cut | ||||||||||||||||||||||||||||
| 170 | |||||||||||||||||||||||||||||
| 171 | 0 | ▶ | sub debug { | ||||||||||||||||||||||||||
|
Subroutine
debug: not called CC=2 SCAR=17.9
Pod
documented
| |||||||||||||||||||||||||||||
| 172 | 0 | ▶ | warn "@_\n" if $ENV{LINKSITE_DEBUG}; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 173 | } | ||||||||||||||||||||||||||||
| 174 | } | ||||||||||||||||||||||||||||
| 175 | |||||||||||||||||||||||||||||
| 176 | =head1 AUTHOR | ||||||||||||||||||||||||||||
| 177 | |||||||||||||||||||||||||||||
| 178 | Dave Cross <dave@davecross.co.uk> | ||||||||||||||||||||||||||||
| 179 | |||||||||||||||||||||||||||||
| 180 | =head1 COPYRIGHT AND LICENCE | ||||||||||||||||||||||||||||
| 181 | |||||||||||||||||||||||||||||
| 182 | Copyright (c) 2024, Magnum Solutions Ltd. All Rights Reserved. | ||||||||||||||||||||||||||||
| 183 | |||||||||||||||||||||||||||||
| 184 | This library is free software; you can redistribute it and/or modify it | ||||||||||||||||||||||||||||
| 185 | under the same terms as Perl itself. | ||||||||||||||||||||||||||||
| 186 | |||||||||||||||||||||||||||||
| 187 | =cut | ||||||||||||||||||||||||||||
| 188 | |||||||||||||||||||||||||||||
| 189 | 1 | 1; | |||||||||||||||||||||||||||