| 1 | package Text::MustacheTemplate::Generator; | ||||||||||||||||||||||
| 2 | 15 | ▶ | use strict; | ||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||
| 3 | 15 | ▶ | use warnings; | ||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||
| 4 | |||||||||||||||||||||||
| 5 | 15 | ▶ | use Text::MustacheTemplate::Lexer qw/:types/; | ||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||
| 6 | |||||||||||||||||||||||
| 7 | 13 | ▶ | sub generate_from_tokens { | ||||||||||||||||||||
|
Subroutine
generate_from_tokens: called CC=10 SCAR=23.0
Pod
documented
| |||||||||||||||||||||||
| 8 | 13 | my ($class, @tokens) = @_; | |||||||||||||||||||||
| 9 | |||||||||||||||||||||||
| 10 | 13 | my ($open_delimiter, $close_delimiter) = do { | |||||||||||||||||||||
| 11 | 13 | my $token = shift @tokens; | |||||||||||||||||||||
| 12 | 13 | ▶ | die 'first token must be delimiter' if $token->[0] != TOKEN_DELIMITER; # uncoverable branch true | ||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||
| 13 | 13 | @$token[3,4]; | |||||||||||||||||||||
| 14 | }; | ||||||||||||||||||||||
| 15 | |||||||||||||||||||||||
| 16 | 13 | my $buf = ''; | |||||||||||||||||||||
| 17 | 13 | for my $token (@tokens) { | |||||||||||||||||||||
| 18 | 124 | my ($type) = @$token; | |||||||||||||||||||||
| 19 | 124 | ▶ | if ($type == TOKEN_RAW_TEXT) { # uncoverable branch false count:4 | ||||||||||||||||||||
|
Branch: 4 branches
| |||||||||||||||||||||||
| 20 | 59 | my (undef, undef, $text) = @$token; | |||||||||||||||||||||
| 21 | 59 | $buf .= $text; | |||||||||||||||||||||
| 22 | } elsif ($type == TOKEN_PADDING) { | ||||||||||||||||||||||
| 23 | 9 | my (undef, undef, $padding) = @$token; | |||||||||||||||||||||
| 24 | 9 | $buf .= $padding; | |||||||||||||||||||||
| 25 | } elsif ($type == TOKEN_TAG) { | ||||||||||||||||||||||
| 26 | 54 | ▶ | if (@$token == 3) { # uncoverable branch false count:2 | ||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||
| 27 | 14 | my (undef, undef, $body) = @$token; | |||||||||||||||||||||
| 28 | 14 | $buf .= $open_delimiter.$body.$close_delimiter; | |||||||||||||||||||||
| 29 | } elsif (@$token == 4) { | ||||||||||||||||||||||
| 30 | 40 | my (undef, undef, $tag_type, $body) = @$token; | |||||||||||||||||||||
| 31 | 40 | $buf .= $open_delimiter.$tag_type.$body; | |||||||||||||||||||||
| 32 | 40 | ▶ | $buf .= '}' if $tag_type eq '{'; | ||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||
| 33 | 40 | $buf .= $close_delimiter; | |||||||||||||||||||||
| 34 | } else { | ||||||||||||||||||||||
| 35 | 0 | die "Unknown tag token size: ".scalar(@$token); # uncoverable statement | |||||||||||||||||||||
| 36 | } | ||||||||||||||||||||||
| 37 | } elsif ($type == TOKEN_DELIMITER) { | ||||||||||||||||||||||
| 38 | 2 | my (undef, undef, $body, $new_open_delimiter, $new_close_delimiter) = @$token; | |||||||||||||||||||||
| 39 | 2 | $buf .= $open_delimiter.'='.$body.'='.$close_delimiter; | |||||||||||||||||||||
| 40 | 2 | ($open_delimiter, $close_delimiter) = ($new_open_delimiter, $new_close_delimiter); | |||||||||||||||||||||
| 41 | } else { | ||||||||||||||||||||||
| 42 | 0 | die "Unknown token type: $type"; # uncoverable statement | |||||||||||||||||||||
| 43 | } | ||||||||||||||||||||||
| 44 | } | ||||||||||||||||||||||
| 45 | 13 | return $buf; | |||||||||||||||||||||
| 46 | } | ||||||||||||||||||||||
| 47 | |||||||||||||||||||||||
| 48 | 15 | 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 E<lt>karupa@cpan.orgE<gt> | ||||||||||||||||||||||
| 102 | |||||||||||||||||||||||
| 103 | =cut | ||||||||||||||||||||||
| 104 | |||||||||||||||||||||||