From 5b9c7d75f50f1542ce95b4f062245653ac98693d Mon Sep 17 00:00:00 2001 From: nekobit Date: Mon, 25 Jul 2022 05:12:54 +0000 Subject: [PATCH] Perl file hashes FossilOrigin-Name: 99a43c7224138245ccd8270c04aefcc3b47cdf23e1eecb65abae6913e3b9105e --- perl/main.pl | 28 ++++++++++++++-------------- perl/notifications.pm | 11 +++++++++++ perl/template_helpers.pm | 28 ++++++++++++++++++++++++++++ src/base_page.c | 2 +- src/global_perl.c | 34 ++++++++++++++++++++++++++++++++++ src/global_perl.h | 4 ++++ src/main.c | 4 ++++ 7 files changed, 96 insertions(+), 15 deletions(-) create mode 100644 perl/notifications.pm create mode 100644 perl/template_helpers.pm create mode 100644 src/global_perl.c 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();