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