diff --git a/perl/main.pl b/perl/main.pl index 0666e0d..31e886a 100644 --- a/perl/main.pl +++ b/perl/main.pl @@ -3,18 +3,20 @@ use warnings; # Modules use Template; use l10n qw( %L10N ); +use notifications qw( ¬ifs_compact ); +use template_helpers qw( &to_template ); -my $template = Template->new( - { - INTERPOLATE => 1, - POST_CHOMP => 1, - EVAL_PERL => 1, - TRIM => 1 - }); +# my $template = Template->new( +# { +# INTERPOLATE => 1, +# POST_CHOMP => 1, +# EVAL_PERL => 1, +# TRIM => 1 +# }); sub base_page { - my ($ssn, $data, $main) = @_; + my ($ssn, $data, $main, $notifs) = @_; my $result; my %vars = ( @@ -24,13 +26,11 @@ sub base_page lang => sub { $L10N{'EN_US'}->{shift(@_)} }, main => $main, sidebar_opacity => $ssn->{config}->{sidebar_opacity} / 255, - acct => $ssn->{account} + acct => $ssn->{account}, + data => $data, + notifs => $notifs ); - - $template->process(\$data, \%vars, \$result) || - return $template->error(); - - $result; + to_template(\%vars, \$data->{'main.tt'}); } diff --git a/perl/notifications.pm b/perl/notifications.pm new file mode 100644 index 0000000..622f1fe --- /dev/null +++ b/perl/notifications.pm @@ -0,0 +1,11 @@ +package Notifications; + +use Exporter 'import'; + +our @EXPORT = qw( ¬ifs_compact ); + +sub notifs_compact +{ + # my ($template, ) = @_; + 1; +} diff --git a/perl/template_helpers.pm b/perl/template_helpers.pm new file mode 100644 index 0000000..a31a11c --- /dev/null +++ b/perl/template_helpers.pm @@ -0,0 +1,28 @@ +package template_helpers; + +use Exporter 'import'; + +our @EXPORT = qw( &to_template ); + +my $template = Template->new( + { + INTERPOLATE => 1, + POST_CHOMP => 1, + EVAL_PERL => 1, + TRIM => 1 + }); + +sub to_template +{ + my ($vars, $data) = @_; + my $result; + + 0 unless ref $data; + 0 unless ref $vars; + + # TODO HTML error formatting + $template->process($data, $vars, \$result) || + return $template->error(); + + $result; +} diff --git a/src/base_page.c b/src/base_page.c index 6d90256..c85db19 100644 --- a/src/base_page.c +++ b/src/base_page.c @@ -49,7 +49,7 @@ void render_base_page(struct base_page* page, FCGX_Request* req, struct session* HV* session_hv = perlify_session(ssn); XPUSHs(sv_2mortal(newRV_inc((SV*)session_hv))); - XPUSHs(sv_2mortal(newSVpv(data_main_tt, 0))); + XPUSHs(sv_2mortal(newRV_inc((SV*)template_files))); XPUSHs(sv_2mortal(newSVpv(page->content, 0))); if (keystr(ssn->cookies.logged_in) && keystr(ssn->cookies.access_token)) diff --git a/src/global_perl.c b/src/global_perl.c new file mode 100644 index 0000000..5bf5567 --- /dev/null +++ b/src/global_perl.c @@ -0,0 +1,34 @@ +/* + * Treebird - Lightweight frontend for Pleroma + * Copyright (C) 2022 Nekobit + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU Affero General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Affero General Public License for more details. + * + * You should have received a copy of the GNU Affero General Public License + * along with this program. If not, see . + */ + +#include "global_perl.h" +#include "../templates/main.ctt" + +const HV* template_files; + +void init_template_files() +{ + template_files = newHV(); + + hv_stores(template_files, "main.tt", newSVpv(data_main_tt, data_main_tt_size)); +} + +void cleanup_template_files() +{ + hv_undef(template_files); +} diff --git a/src/global_perl.h b/src/global_perl.h index 7615cf3..ebc9634 100644 --- a/src/global_perl.h +++ b/src/global_perl.h @@ -28,5 +28,9 @@ ((val) ? newRV_inc((SV* const)(val)) : &PL_sv_undef)) static PerlInterpreter* perl; +extern const HV* template_files; + +void init_template_files(); +void cleanup_template_files(); #endif /* GLOBAL_PERL_H */ diff --git a/src/main.c b/src/main.c index c9c1ebb..7d390ea 100644 --- a/src/main.c +++ b/src/main.c @@ -221,6 +221,8 @@ int main(int argc, char **argv, char **env) PL_exit_flags |= PERL_EXIT_DESTRUCT_END; perl_run(perl); + init_template_files(); + // Initiate mastodont library mastodont_t api; mastodont_init(&api); @@ -240,6 +242,8 @@ int main(int argc, char **argv, char **env) mastodont_cleanup(&api); mastodont_global_curl_cleanup(); + cleanup_template_files(); + perl_destruct(perl); perl_free(perl); PERL_SYS_TERM();