Template.pm 3.4 KB

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