basic porting work for SCSite
[scpubgit/App-SCS.git] / lib / App / SCS / PageSet.pm
CommitLineData
632f0e07 1package App::SCS::PageSet;
2
3use Text::MultiMarkdown 'markdown';
4use HTML::Zoom;
5use Sub::Quote;
6use Syntax::Keyword::Gather;
7use App::SCS::Page;
8use IO::All;
9use Try::Tiny;
10use List::Util qw(reduce);
0034f151 11use Module::Runtime qw(use_module);
632f0e07 12use JSON;
13use Moo;
14
15with 'App::SCS::Role::PageChildren';
16
17{
18 my $j = JSON->new;
19 sub _json { $j }
20}
21
22has top_dir => (is => 'ro', lazy => 1, builder => 'base_dir');
23has base_dir => (is => 'ro', required => 1);
24has plugin_config => (is => 'ro', required => 1);
25has max_depth => (is => 'ro', default => quote_sub q{ 0 });
26has min_depth => (is => 'ro', default => quote_sub q{ 1 });
27
28has rel_path => (is => 'lazy');
29
30sub _build_rel_path {
31 my ($self) = @_;
32 io->dir('/')
33 ->catdir(File::Spec->abs2rel($self->base_dir->name, $self->top_dir->name))
34}
35
f50b4a35 36sub _page_set { $_[0] }
632f0e07 37sub _page_set_class { ref($_[0]) }
38sub _top_dir { shift->top_dir }
39sub _my_path { shift->base_dir }
40
41sub get {
42 my ($self, $spec) = @_;
43 $spec->{path} or die "path is required to get";
44 my ($dir, $file) = $spec->{path} =~ m{^(?:(.*)/)?([^/]+)$};
45 my $type;
46 my @poss = io->dir($self->base_dir)->${\sub {
47 my $io = shift;
48 defined($dir) ? $io->catdir($dir) : $io
49 }}->filter(sub {
50 $_->filename =~ /^\Q${file}\E${\$self->_types_re}$/ and $type = $1
51 })
52 ->${\sub { -e "$_[0]" ? $_[0]->all_files : () }};
53 die "multiple files found for ${\$spec->{path}}:\n".join "\n", @poss
54 if @poss > 1;
55 return undef unless @poss;
56 $self->_inflate(
57 $type, $self->rel_path->catdir($spec->{path}), $poss[0]
58 );
59}
60
61sub _inflate {
62 my ($self, $type, $path, $io) = @_;
63 (my $cache_name = $io->name) =~ s/\/([^\/]+)$/\/.htcache.$1.json/;
64 my $cache = io($cache_name);
65 if (-f $cache_name) {
66 if ($cache->mtime >= $io->mtime) {
67 return try {
68 $self->_new_page($path, $self->_json->decode($cache->all));
69 } catch {
70 die "Error inflating ${path} from cache: $_\n";
71 }
72 }
73 }
74 my $raw = $io->all;
75 try {
76 my $extracted = $self->${\"_extract_from_${type}"}($raw);
77 try { $cache->print($self->_json->encode($extracted)); };
78 $self->_new_page($path, $extracted);
79 } catch {
80 die "Error inflating ${path} as ${type}: $_\n";
81 }
82}
83
84sub map {
85 my ($self, $mapper) = @_;
86 [ map $mapper->($_), $self->flatten ]
87}
88
89sub _depth_under_base {
90 my ($self, $path) = @_;
91 File::Spec->splitdir(File::Spec->abs2rel($path, $self->base_dir->name))
92}
93
94sub flatten {
95 my ($self) = @_;
96 my $slash = io->dir('/');
97 map {
98 my ($path, $type) = $_->name =~ /^(.*)${\$self->_types_re}$/;
99 $self->_inflate(
100 $type,
101 $slash->catdir(File::Spec->abs2rel($path, $self->top_dir->name)),
102 $_
103 );
104 } $self->_all_files;
105}
106
107sub all_paths {
108 my ($self) = @_;
109 my $slash = io->dir('/');
110 map {
111 my ($path, $type) = $_->name =~ /^(.*)${\$self->_types_re}$/;
112 $slash->catdir(File::Spec->abs2rel($path, $self->top_dir->name))->name,
113 } $self->_all_files;
114}
115
116sub _all_files {
117 my ($self) = @_;
118 return unless (my $base = $self->base_dir)->exists;
119 my %seen;
120 my $min = $self->min_depth;
121 map {
122 $_->filter(sub { $_->filename =~ /${\$self->_types_re}$/ })
123 ->all_files($self->max_depth - ($min-1))
124 } map
125 $min > 1
126 ? do {
127 # can't use ->all_dirs($min-1) since we only want the final level
128 my @x = ($_); @x = map $_->all_dirs, @x for 1..$min-1; @x
129 }
130 : $_,
131 $base;
132}
133
134sub latest {
135 my ($self, $max) = @_;
0034f151 136 use_module('App::SCS::LatestPageSet')->new(
632f0e07 137 parent => $self,
138 max_entries => $max,
139 );
140}
141
142sub _new_page {
0034f151 143 use_module('App::SCS::Page')->new(
144 path => $_[1], page_set => $_[0], %{$_[2]}
145 );
632f0e07 146}
147
148sub _types_re { qw/\.(html|md)/ }
149
150sub _extract_from_html {
151 my ($self, $html) = @_;
152 my %meta;
153 HTML::Zoom->from_html($html)
154 ->select('title')->collect_content({ into => \my @title })
155 ->${\sub {
156 my $z = shift;
157 return reduce {
158 $a->collect("meta[name=${b}]", { into => ($meta{$b}=[]) })
159 } $z, qw(subtitle description keywords created plugins)
160 }}
161 ->run;
162 +{
163 title => $title[0]->{raw}||'',
164 (map +($_ => $meta{$_}[0]->{attrs}{content}||''), keys %meta),
165 html => $html,
166 }
167}
168
169sub _extract_from_md {
170 my ($self, $md) = @_;
632f0e07 171 $self->_extract_from_html(markdown($md, { document_format => 'complete' }));
172}
173
1741;