File Coverage

blib/lib/Text/MustacheTemplate/Generator.pm
Criterion Covered Total %
statement 33 35 100.0
branch 13 16 100.0
condition n/a
subroutine 4 4 100.0
pod 1 1 100.0
total 51 56 100.0


line stmt bran cond sub pod time code
1             package Text::MustacheTemplate::Generator;
2 15     15   137337 use strict;
  15         25  
  15         563  
3 15     15   64 use warnings;
  15         26  
  15         748  
4              
5 15     15   595 use Text::MustacheTemplate::Lexer qw/:types/;
  15         29  
  15         8746  
6              
7             sub generate_from_tokens {
8 13     13 1 239842 my ($class, @tokens) = @_;
9              
10 13         24 my ($open_delimiter, $close_delimiter) = do {
11 13         50 my $token = shift @tokens;
12 13 50       51 die 'first token must be delimiter' if $token->[0] != TOKEN_DELIMITER; # uncoverable branch true
13 13         45 @$token[3,4];
14             };
15              
16 13         28 my $buf = '';
17 13         30 for my $token (@tokens) {
18 124         215 my ($type) = @$token;
19 124 100       283 if ($type == TOKEN_RAW_TEXT) { # uncoverable branch false count:4
    100          
    100          
    50          
20 59         106 my (undef, undef, $text) = @$token;
21 59         125 $buf .= $text;
22             } elsif ($type == TOKEN_PADDING) {
23 9         16 my (undef, undef, $padding) = @$token;
24 9         18 $buf .= $padding;
25             } elsif ($type == TOKEN_TAG) {
26 54 100       154 if (@$token == 3) { # uncoverable branch false count:2
    50          
27 14         26 my (undef, undef, $body) = @$token;
28 14         34 $buf .= $open_delimiter.$body.$close_delimiter;
29             } elsif (@$token == 4) {
30 40         84 my (undef, undef, $tag_type, $body) = @$token;
31 40         83 $buf .= $open_delimiter.$tag_type.$body;
32 40 100       98 $buf .= '}' if $tag_type eq '{';
33 40         102 $buf .= $close_delimiter;
34             } else {
35 0         0 die "Unknown tag token size: ".scalar(@$token); # uncoverable statement
36             }
37             } elsif ($type == TOKEN_DELIMITER) {
38 2         7 my (undef, undef, $body, $new_open_delimiter, $new_close_delimiter) = @$token;
39 2         5 $buf .= $open_delimiter.'='.$body.'='.$close_delimiter;
40 2         7 ($open_delimiter, $close_delimiter) = ($new_open_delimiter, $new_close_delimiter);
41             } else {
42 0         0 die "Unknown token type: $type"; # uncoverable statement
43             }
44             }
45 13         51 return $buf;
46             }
47              
48             1;
49              
50             =encoding utf-8
51              
52             =head1 NAME
53              
54             Text::MustacheTemplate::Generator - Template generator for Mustache templates
55              
56             =head1 SYNOPSIS
57              
58             use Text::MustacheTemplate::Lexer;
59             use Text::MustacheTemplate::Generator;
60              
61             my @tokens = Text::MustacheTemplate::Lexer->tokenize('Hello {{name}}!');
62             my $regenerated_template = Text::MustacheTemplate::Generator->generate_from_tokens(@tokens);
63            
64             # Result: 'Hello {{name}}!'
65              
66             =head1 DESCRIPTION
67              
68             Text::MustacheTemplate::Generator can regenerate a Mustache template from tokens.
69             This is primarily used for lambda functions that need access to the raw template string.
70              
71             This is a low-level interface for Text::MustacheTemplate.
72             The APIs may change without notice.
73              
74             =head1 METHODS
75              
76             =over 4
77              
78             =item generate_from_tokens($delimiter_token, @tokens)
79              
80             Regenerates a Mustache template string from lexer tokens.
81              
82             Parameters:
83             =over 8
84             =item $delimiter_token - The delimiter token containing open/close delimiters
85             =item @tokens - Array of tokens from Text::MustacheTemplate::Lexer
86             =back
87              
88             Returns a string containing the regenerated template.
89              
90             =back
91              
92             =head1 LICENSE
93              
94             Copyright (C) karupanerura.
95              
96             This library is free software; you can redistribute it and/or modify
97             it under the same terms as Perl itself.
98              
99             =head1 AUTHOR
100              
101             karupanerura Ekarupa@cpan.orgE
102              
103             =cut
104