sub BUILD {
my ($self) = @_;
$self->load_plugin(Core => {});
- foreach my $spec (@{$self->config->{plugins}||[]}) {
- $self->load_plugin(@$spec);
+ my @plist = @{$self->config->{plugins}||[]};
+ while (my ($name, $conf) = splice @plist, 0, 2) {
+ $self->load_plugin($name, $conf);
}
}
return;
}
+sub run_if_script {
+ my $self = shift;
+ if (caller(1)) {
+ return 1;
+ } else {
+ return $self->run;
+ }
+}
+
+sub run {
+ my $self = shift;
+ my $env = {
+ argv => \@ARGV,
+ stdin => \*STDIN,
+ stdout => \*STDOUT,
+ stderr => \*STDERR,
+ };
+ foreach my $p (@{$self->plugins}) {
+ return if $p->run_cli($env);
+ }
+ $self->web->run;
+}
+
1;
--- /dev/null
+package App::SCS::LatestPageSet;
+
+use Moo;
+
+has _parent => (is => 'ro', required => 1, init_arg => 'parent');
+has _max_entries => (is => 'ro', required => 1, init_arg => 'max_entries');
+
+sub flatten {
+ my ($self) = @_;
+ my @sorted = sort {
+ $b->created cmp $a->created
+ } $self->_parent->flatten;
+ my $max = $self->_max_entries||0;
+ $max && @sorted > $max ? @sorted[0..$max-1] : @sorted;
+}
+
+sub map {
+ my ($self, $mapper) = @_;
+ [ map $mapper->($_), $self->flatten ]
+}
+
+1;
use Module::Runtime qw(use_module);
use Moo;
+has "_$_" => (is => 'ro', init_arg => $_) for qw(page_set);
+
+sub _page_set_class { ref($_[0]->_page_set) }
+sub _top_dir { $_[0]->_page_set->top_dir }
+sub _my_path { io->dir($_[0]->_top_dir)->catdir($_[0]->path) }
+
with 'App::SCS::Role::PageChildren';
has $_ => (is => 'ro') for qw(
: ''
}
-has "_$_" => (is => 'ro', init_arg => $_) for qw(page_set);
-
-sub _page_set_class { ref($_[0]->_page_set) }
-sub _top_dir { $_[0]->_page_set->top_dir }
-sub _my_path { io->dir($_[0]->_top_dir)->catdir($_[0]->path) }
-
sub to_app {
my ($self) = @_;
return sub { $self->to_psgi_response(@_) };
->catdir(File::Spec->abs2rel($self->base_dir->name, $self->top_dir->name))
}
+sub _page_set { $_[0] }
sub _page_set_class { ref($_[0]) }
sub _top_dir { shift->top_dir }
sub _my_path { shift->base_dir }
sub _extract_from_md {
my ($self, $md) = @_;
- #warn markdown($md, { document_format => 'complete' });
$self->_extract_from_html(markdown($md, { document_format => 'complete' }));
}
->then
->replace(sub {
my $sel = $to_replace[0]->{attrs}{'data-replace'};
- $orig->collect($sel, { into => \my @replace_with })->run;
+ my $content = $sel =~ s/^content-of:\s*//;
+ $orig->collect($sel, {
+ into => \my @replace_with,
+ content => $content
+ })->run;
HTML::Zoom::ArrayStream->new({ array => \@replace_with })
})
->to_stream;
my $base = $self->mount_at;
"/${base}/**/" => sub {
if (my $conf = $self->config->{$_[1]}) {
+ $conf = { base => $_[1], %$conf };
$self->_feed_http_response(200 => $conf => $_[-1]);
}
},
use HTML::Tags;
join '', HTML::Tags::to_html_string(<p>, $_->description, </p>)
},
- content_html => $self->_absolutify_html($_->body, $base_url, $page_url),
+ content_html => $self->_content_html($_, $base_url, $page_url),
created => join('T', split(' ',$_->created)).'Z',
web_url => $page_url,
}
' ', </entry>, "\n";
}
-sub _absolutify_html {
- my ($self, $html, $base_url, $page_url) = @_;
- $html;
+sub _content_html {
+ my ($self, $page, $base_url, $page_url) = @_;
+ my @ev;
+ HTML::Zoom->from_html($page->html)
+ ->collect(body => { into => \@ev })
+ ->run;
+ HTML::Zoom->from_events(\@ev)->to_html;
}
1;
--- /dev/null
+package App::SCS::Plugin::Server;
+
+use Module::Runtime qw(use_module);
+use Moo;
+no warnings::illegalproto;
+
+with 'App::SCS::Role::Plugin';
+
+has _static_handler => (is => 'lazy');
+
+sub _build__static_handler {
+ my ($self) = @_;
+ use_module('Plack::App::File')->new(
+ root => $self->app->share_dir->catdir('static')
+ );
+}
+
+sub page_dispatchers {
+ my ($self) = @_;
+ sub (/**.*) {
+ my $path = $_[1];
+ return unless $path =~ s/\/-/\//;
+ App::SCS::Web::redispatch_to("/static/${path}");
+ },
+ sub (/static/...) { $self->_static_handler },
+ sub (/favicon + .ico) { $self->_static_handler },
+}
+
+sub run_command_server {
+ my ($self, $env) = @_;
+ my @args = @{$env->{argv}};
+ my $r = use_module('Plack::Runner')->new(
+ server => 'Starman',
+ app => $self->app->web->to_psgi_app
+ );
+ $r->parse_options(@args);
+ $r->set_options(argv => \@args);
+ $r->run;
+}
+
+1;
use Moo::Role;
+requires '_page_set';
requires '_page_set_class';
requires '_top_dir';
requires '_my_path';
top_dir => $self->_top_dir,
base_dir => $self->_my_path,
max_depth => 1,
+ plugin_config => $self->_page_set->plugin_config,
%args,
);
}
sub register { return }
+sub run_cli {
+ my ($self, $env) = @_;
+ my ($command, @argv) = @{$env->{argv}};
+ return unless $command;
+ return unless $self->can(my $meth = "run_command_${command}");
+ $self->$meth({ argv => \@argv, %$env });
+ return 1;
+}
+
1;