App-Dapper

 view release on metacpan or  search on metacpan

lib/App/Dapper.pm  view on Meta::CPAN


sub clean_output {
    my ($self) = @_;

    return unless App::Dapper::Publish::enabled($self->{site}, 'clean');

    my $manifest_file = catfile('.dapper', 'manifest.json');
    my $output = abs_path($self->{output});
    return unless defined $output;

    my $manifest = {};
    if (-f $manifest_file) {
        $manifest = eval { JSON->new->decode(App::Dapper::Utils::read_file($manifest_file)) } || {};
        $manifest = {} unless ref $manifest eq 'HASH';
    }

    my $old = $manifest->{$output};
    my $removed = 0;
    my %dirs;
    if (ref $old eq 'ARRAY') {
        for my $path (@$old) {
            next if $self->{written}->{$path};
            next if $path =~ m{(?:^|/)\.\.(?:/|$)} or $path =~ m{^/};
            my $file = catfile($self->{output}, $path);
            next unless -f $file;
            unlink $file or next;
            $removed++;
            (my $dir = $path) =~ s{/?[^/]*$}{};
            while (length $dir) {
                $dirs{$dir} = 1;
                $dir =~ s{/?[^/]*$}{};
            }
        }
    }

    # Remove directories the removed files leave empty, deepest first
    for my $dir (sort { length $b <=> length $a } keys %dirs) {
        rmdir catfile($self->{output}, $dir);
    }

    $self->_log("Removed $removed file" . ($removed == 1 ? '' : 's') . " left over from an earlier build") if $removed;

    $manifest->{$output} = [ sort keys %{$self->{written}} ];
    App::Dapper::Utils::write_file($manifest_file, JSON->new->canonical->pretty->encode($manifest));
}

# sub live_reload_script - The script added to pages while "dapper serve" is
# running. It asks for /.dapper-reload every second and reloads the page when
# a new build changes it.

sub live_reload_script {
    return <<'HTML';
<script>
(function () {
  var last = null;
  function poll() {
    fetch('/.dapper-reload?' + Date.now(), { cache: 'no-store' })
      .then(function (r) { return r.ok ? r.text() : last; })
      .then(function (t) { if (last !== null && t !== last) { location.reload(); } last = t; })
      .catch(function () {})
      .then(function () { setTimeout(poll, 1000); });
  }
  poll();
})();
</script>
HTML
}

# sub write_generated - Write the files Dapper makes for the whole site:
# redirects, the sitemap, the feed, and llms.txt and llms-full.txt. A page that writes the
# same file wins, with a warning.

sub write_generated {
    my ($self) = @_;

    my $site  = $self->{site};
    my @pages = @{$site->{pages}};
    my %taken = map { $_->{filename} => 1 } @pages;
    my $has_url = defined $site->{url} && length $site->{url};

    my $write = sub {
        my ($path, $content) = @_;
        my $file = canonpath(catfile($self->{output}, $path));
        if ($taken{$file}) {
            $self->problem('warning', $file, "a page in the source directory already writes $file, so Dapper did not generate one");
            return;
        }
        $self->write_output($file, $content);
        $self->_log("Wrote $file");
    };

    my $redirects = App::Dapper::Publish::redirects($site);
    for my $from (sort keys %$redirects) {
        my $file = canonpath(catfile($self->{output}, App::Dapper::Publish::redirect_path($from)));
        if ($taken{$file}) {
            $self->problem('warning', $file, "a page is still at $from, so the redirect from it to $redirects->{$from} was not written");
            next;
        }
        $self->write_output($file, App::Dapper::Publish::redirect_page($site, $redirects->{$from}));
        $self->{page_owner}->{$file} = "the redirect from $from";
        $taken{$file} = 1;
    }

    if (App::Dapper::Publish::enabled($site, 'llms_txt')) {
        my @twins = grep { $_->{markdown_url} } @pages;
        if (@twins) {
            $write->('llms.txt', App::Dapper::Publish::llms_txt($site, \@twins));
            $write->('llms-full.txt', App::Dapper::Publish::llms_full_txt($site, \@twins));
        }
    }

    if (App::Dapper::Publish::enabled($site, 'sitemap')) {
        if ($has_url) {
            $write->('sitemap.xml', App::Dapper::Publish::sitemap($site, \@pages));
        }
        elsif ($self->{configured}->{sitemap}) {
            $self->problem('warning', $self->{config}, "the sitemap needs the site's address; set url in $self->{config}");
        }
    }

    if (my $settings = App::Dapper::Publish::feed_settings($site)) {



( run in 1.130 second using v1.01-cache-2.11-cpan-234fa220139 )