Template.pm 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150
  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 new {
  30. my $class = shift;
  31. # Call the constructor of the parent class.
  32. my $self = $class->SUPER::new(DELIMITERS => [ '{-', '-}'],
  33. @_ );
  34. # Add few more attributes
  35. $self->{_output_off} = 0; # Default to output hunks
  36. return bless $self, $class;
  37. }
  38. sub fill_in {
  39. my $self = shift;
  40. my %opts = @_;
  41. my %hash = ( %{$opts{HASH}} );
  42. delete $opts{HASH};
  43. $self->SUPER::fill_in(HASH => { quotify1 => \&quotify1,
  44. quotify_l => \&quotify_l,
  45. output_on => sub { $self->output_on() },
  46. output_off => sub { $self->output_off() },
  47. %hash },
  48. %opts);
  49. }
  50. =head2 Functions
  51. =cut
  52. # Override Text::Template's append_text_to_result, as recommended here:
  53. #
  54. # http://search.cpan.org/~mjd/Text-Template-1.46/lib/Text/Template.pm#Automatic_postprocessing_of_template_hunks
  55. sub append_text_to_output {
  56. my $self = shift;
  57. if ($self->{_output_off} == 0) {
  58. $self->SUPER::append_text_to_output(@_);
  59. }
  60. return;
  61. }
  62. =begin comment
  63. We lie about the OO nature of output_on() and output_off(), 'cause that's
  64. not how we pass them, see the HASH option used in fill_in() above
  65. =end comment
  66. =over 4
  67. =item output_on()
  68. =item output_off()
  69. Switch on or off template output. Here's an example usage:
  70. =over 4
  71. {- output_off() if CONDITION -}
  72. whatever
  73. {- output_on() if CONDITION -}
  74. =back
  75. In this example, C<whatever> will only become part of the template output
  76. if C<CONDITION> is true.
  77. =back
  78. =cut
  79. sub output_on {
  80. my $self = shift;
  81. if (--$self->{_output_off} < 0) {
  82. $self->{_output_off} = 0;
  83. }
  84. }
  85. sub output_off {
  86. my $self = shift;
  87. $self->{_output_off}++;
  88. }
  89. # Helper functions for the templates #################################
  90. =head1 SEE ALSO
  91. L<Text::Template>
  92. =head1 AUTHORS
  93. Richard Levitte E<lt>levitte@openssl.orgE<gt>
  94. =head1 COPYRIGHT
  95. Copyright 2016-2021 The OpenSSL Project Authors. All Rights Reserved.
  96. Licensed under the Apache License 2.0 (the "License"). You may not use
  97. this file except in compliance with the License. You can obtain a copy
  98. in the file LICENSE in the source distribution or at
  99. L<https://www.openssl.org/source/license.html>.
  100. =cut