1*b0d17251Schristos#! /usr/bin/env perl 2*b0d17251Schristos# Copyright 2016-2021 The OpenSSL Project Authors. All Rights Reserved. 3*b0d17251Schristos# 4*b0d17251Schristos# Licensed under the Apache License 2.0 (the "License"). You may not use 5*b0d17251Schristos# this file except in compliance with the License. You can obtain a copy 6*b0d17251Schristos# in the file LICENSE in the source distribution or at 7*b0d17251Schristos# https://www.openssl.org/source/license.html 8*b0d17251Schristos 9*b0d17251Schristos# Implements the functionality to read one or more template files and run 10*b0d17251Schristos# them through Text::Template 11*b0d17251Schristos 12*b0d17251Schristospackage OpenSSL::Template; 13*b0d17251Schristos 14*b0d17251Schristos=head1 NAME 15*b0d17251Schristos 16*b0d17251SchristosOpenSSL::Template - a private extension of Text::Template 17*b0d17251Schristos 18*b0d17251Schristos=head1 DESCRIPTION 19*b0d17251Schristos 20*b0d17251SchristosThis provides exactly the functionality from Text::Template, with the 21*b0d17251Schristosfollowing additions: 22*b0d17251Schristos 23*b0d17251Schristos=over 4 24*b0d17251Schristos 25*b0d17251Schristos=item * 26*b0d17251Schristos 27*b0d17251SchristosThe template perl code delimiters (given with the C<DELIMITER> option) 28*b0d17251Schristosare set to C<{-> and C<-}> by default. 29*b0d17251Schristos 30*b0d17251Schristos=item * 31*b0d17251Schristos 32*b0d17251SchristosA few extra functions are offered to be used by the template perl code, see 33*b0d17251SchristosL</Functions>. 34*b0d17251Schristos 35*b0d17251Schristos=back 36*b0d17251Schristos 37*b0d17251Schristos=cut 38*b0d17251Schristos 39*b0d17251Schristosuse File::Basename; 40*b0d17251Schristosuse File::Spec::Functions; 41*b0d17251Schristosuse Text::Template 1.46; 42*b0d17251Schristos 43*b0d17251Schristosour @ISA = qw(Text::Template); # parent 44*b0d17251Schristos 45*b0d17251Schristossub new { 46*b0d17251Schristos my $class = shift; 47*b0d17251Schristos 48*b0d17251Schristos # Call the constructor of the parent class. 49*b0d17251Schristos my $self = $class->SUPER::new(DELIMITERS => [ '{-', '-}'], 50*b0d17251Schristos @_ ); 51*b0d17251Schristos 52*b0d17251Schristos # Add few more attributes 53*b0d17251Schristos $self->{_output_off} = 0; # Default to output hunks 54*b0d17251Schristos 55*b0d17251Schristos return bless $self, $class; 56*b0d17251Schristos} 57*b0d17251Schristos 58*b0d17251Schristossub fill_in { 59*b0d17251Schristos my $self = shift; 60*b0d17251Schristos my %opts = @_; 61*b0d17251Schristos my %hash = ( %{$opts{HASH}} ); 62*b0d17251Schristos delete $opts{HASH}; 63*b0d17251Schristos 64*b0d17251Schristos $self->SUPER::fill_in(HASH => { quotify1 => \"ify1, 65*b0d17251Schristos quotify_l => \"ify_l, 66*b0d17251Schristos output_on => sub { $self->output_on() }, 67*b0d17251Schristos output_off => sub { $self->output_off() }, 68*b0d17251Schristos %hash }, 69*b0d17251Schristos %opts); 70*b0d17251Schristos} 71*b0d17251Schristos 72*b0d17251Schristos=head2 Functions 73*b0d17251Schristos 74*b0d17251Schristos=cut 75*b0d17251Schristos 76*b0d17251Schristos# Override Text::Template's append_text_to_result, as recommended here: 77*b0d17251Schristos# 78*b0d17251Schristos# http://search.cpan.org/~mjd/Text-Template-1.46/lib/Text/Template.pm#Automatic_postprocessing_of_template_hunks 79*b0d17251Schristossub append_text_to_output { 80*b0d17251Schristos my $self = shift; 81*b0d17251Schristos 82*b0d17251Schristos if ($self->{_output_off} == 0) { 83*b0d17251Schristos $self->SUPER::append_text_to_output(@_); 84*b0d17251Schristos } 85*b0d17251Schristos 86*b0d17251Schristos return; 87*b0d17251Schristos} 88*b0d17251Schristos 89*b0d17251Schristos=begin comment 90*b0d17251Schristos 91*b0d17251SchristosWe lie about the OO nature of output_on() and output_off(), 'cause that's 92*b0d17251Schristosnot how we pass them, see the HASH option used in fill_in() above 93*b0d17251Schristos 94*b0d17251Schristos=end comment 95*b0d17251Schristos 96*b0d17251Schristos=over 4 97*b0d17251Schristos 98*b0d17251Schristos=item output_on() 99*b0d17251Schristos 100*b0d17251Schristos=item output_off() 101*b0d17251Schristos 102*b0d17251SchristosSwitch on or off template output. Here's an example usage: 103*b0d17251Schristos 104*b0d17251Schristos=over 4 105*b0d17251Schristos 106*b0d17251Schristos {- output_off() if CONDITION -} 107*b0d17251Schristos whatever 108*b0d17251Schristos {- output_on() if CONDITION -} 109*b0d17251Schristos 110*b0d17251Schristos=back 111*b0d17251Schristos 112*b0d17251SchristosIn this example, C<whatever> will only become part of the template output 113*b0d17251Schristosif C<CONDITION> is true. 114*b0d17251Schristos 115*b0d17251Schristos=back 116*b0d17251Schristos 117*b0d17251Schristos=cut 118*b0d17251Schristos 119*b0d17251Schristossub output_on { 120*b0d17251Schristos my $self = shift; 121*b0d17251Schristos if (--$self->{_output_off} < 0) { 122*b0d17251Schristos $self->{_output_off} = 0; 123*b0d17251Schristos } 124*b0d17251Schristos} 125*b0d17251Schristos 126*b0d17251Schristossub output_off { 127*b0d17251Schristos my $self = shift; 128*b0d17251Schristos $self->{_output_off}++; 129*b0d17251Schristos} 130*b0d17251Schristos 131*b0d17251Schristos# Helper functions for the templates ################################# 132*b0d17251Schristos 133*b0d17251Schristos=head1 SEE ALSO 134*b0d17251Schristos 135*b0d17251SchristosL<Text::Template> 136*b0d17251Schristos 137*b0d17251Schristos=head1 AUTHORS 138*b0d17251Schristos 139*b0d17251SchristosRichard Levitte E<lt>levitte@openssl.orgE<gt> 140*b0d17251Schristos 141*b0d17251Schristos=head1 COPYRIGHT 142*b0d17251Schristos 143*b0d17251SchristosCopyright 2016-2021 The OpenSSL Project Authors. All Rights Reserved. 144*b0d17251Schristos 145*b0d17251SchristosLicensed under the Apache License 2.0 (the "License"). You may not use 146*b0d17251Schristosthis file except in compliance with the License. You can obtain a copy 147*b0d17251Schristosin the file LICENSE in the source distribution or at 148*b0d17251SchristosL<https://www.openssl.org/source/license.html>. 149*b0d17251Schristos 150*b0d17251Schristos=cut 151