Perl file hashes

FossilOrigin-Name: 99a43c7224138245ccd8270c04aefcc3b47cdf23e1eecb65abae6913e3b9105e
This commit is contained in:
nekobit 2022-07-25 05:12:54 +00:00
parent 2e21b6fea3
commit 5b9c7d75f5
7 changed files with 96 additions and 15 deletions

View file

@ -3,18 +3,20 @@ use warnings;
# Modules
use Template;
use l10n qw( %L10N );
use notifications qw( &notifs_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'});
}

11
perl/notifications.pm Normal file
View file

@ -0,0 +1,11 @@
package Notifications;
use Exporter 'import';
our @EXPORT = qw( &notifs_compact );
sub notifs_compact
{
# my ($template, ) = @_;
1;
}

28
perl/template_helpers.pm Normal file
View file

@ -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;
}

View file

@ -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))

34
src/global_perl.c Normal file
View file

@ -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 <https://www.gnu.org/licenses/>.
*/
#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);
}

View file

@ -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 */

View file

@ -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();