Qgoda/Migrator/Jekyll/LiquidConverter.pm untested

filter: Statementstmt 0.0% 0 / 103 Branchbran 0.0% 0 / 36 Conditioncond 0.0% 0 / 11 MC/DCmcdc n/a Subroutinesub 0.0% 0 / 11 Podpod 0.0% 0 / 7 totaltotal 0.0% 0 / 168 CC 19 SCAR 45.6
CC
19
Cov
0%
convert
182.0
parse
56.0
addOutput
2.0
CRAP 95.7
1 #! /bin/false
2  
3 # Copyright (C) 2016-2018 Guido Flohr <guido.flohr@cantanea.com>,
4 # all rights reserved.
5  
6 # This program is free software: you can redistribute it and/or modify
7 # it under the terms of the GNU General Public License as published by
8 # the Free Software Foundation; either version 3 of the License, or
9 # (at your option) any later version.
10  
11 # This program is distributed in the hope that it will be useful,
12 # but WITHOUT ANY WARRANTY; without even the implied warranty of
13 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 # GNU General Public License for more details.
15  
16 # You should have received a copy of the GNU General Public License
17 # along with this program. If not, see <http://www.gnu.org/licenses/>.
18  
19 package Qgoda::Migrator::Jekyll::LiquidConverter;
20 $Qgoda::Migrator::Jekyll::LiquidConverter::VERSION = 'v0.9.2';
21 use strict;
22  
23 use Locale::TextDomain qw(qgoda);
24  
25 use Qgoda;
26 use Qgoda::Util qw(empty trim);
27  
28 #use base qw (Qgoda::Migrator::Jekyll::LiquidParser);
29  
30 sub convert {
31 my ($self, $filename, $input, $logger, %options) = @_;
32  
33 $options{tt2_start} ||= '[%';
34 $options{tt2_end} ||= '%]';
35  
36 $self->{__options} = \%options;
37 $self->{__logger} = $logger;
38 $self->{__output} = '';
39  
40 my $lineno = 1 + $options{offset};
41 # No need for a stack because there is no nesting.
42 my $state = 'INITIAL';
43 my $last_token = __"beginning of file";
44 my $whitespace = sub {
45 while ($input =~ s/^[ \x09-\x0d]*\n//) {
46 ++$lineno;
47 }
48 $input =~ s/^[ \x09-\x0d]*//;
49 return if empty $input;
50  
51 return 1;
52 };
53  
54 my $next_char = sub {
55 $input =~ s/(.)//;
56 return CDATA => $1;
57 };
58  
59 # Re-quote a double-quoted string.
60 my $drequote = sub {
61 my ($string) = @_;
62  
63 $string =~ s/\\/\\\\/g;
64  
65 return qq{"$string"};
66 };
67  
68 # Re-quote a single-quoted string;
69 my $srequote = sub {
70 my ($string) = @_;
71  
72 $string =~ s/\\/\\\\/g;
73  
74 return qq{'$string'};
75 };
76  
77 my $lexer = sub {
78 return '', undef if empty $input;
79  
80 if ('INITIAL' eq $state) {
81 $input =~ s/([^\n\{]*)//;
82 return CDATA => $1 if !empty $1;
83  
84 if ($input =~ s/^\n//) {
85 ++$lineno;
86 return CDATA => "\n";
87 } elsif ($input =~ s/^\{\{//) {
88 $state = 'OBJECT';
89 return SO => '{{';
90 } elsif ($input =~ s/^\{\%//) {
91 $state = 'TAG';
92 return ST => '{%';
93 } else {
94 return $next_char->();
95 }
96 } elsif ('TAG' eq $state) {
97 $whitespace->() or return '', undef;
98  
99 if ($input =~ s/^([-a-zA-Z0-9_]+)//) {
100 $state = 'IN-TAG';
101 return DIRECTIVE => $1;
102 } else {
103 return $next_char->();
104 }
105 } elsif ('IN-TAG' eq $state) {
106 $whitespace->() or return '', undef;
107  
108 if ($input =~ s/^"(.*?)"//) {
109 return DQUOTE => $drequote->($1);
110 } elsif ($input =~ s/^'(.*?)'//) {
111 return SQUOTE => $srequote->($1);
112 } else {
113 return $next_char->();
114 }
115 } else {
116 die "unhandled state $state";
117 }
118 };
119  
120 my $lexer_wrapper = sub {
121 my ($token, $content) = $lexer->();
122 $last_token = $content;
123  
124 return $token, $content;
125 };
126  
127 my $error = sub {
128 $state = 0;
129  
130 my $location = $last_token;
131 $self->logger->error(__x("{filename}:{lineno}: Syntax error at or"
132 . " near '{location}'!",
133 filename => $filename,
134 lineno => $lineno,
135 location => $location));
136 };
137  
138 $self->YYParse(yylex => $lexer_wrapper, yyerror => $error);
139  
140 # FIXME! Check for errors!
141 return $self->{__output};
142 }
143  
144 sub addOutput {
145 my ($self, $chunk) = @_;
146  
147 $self->{__output} .= $chunk;
148  
149 return $self;
150 }
151  
152 sub startTag {
153 my ($self) = @_;
154  
155 return $self->{__options}->{tt2_start};
156 }
157  
158 sub endTag {
159 my ($self) = @_;
160  
161 return $self->{__options}->{tt2_end};
162 }
163  
164 sub addPlugin {
165 my ($self, $plugin) = @_;
166  
167 $self->{__plugins}->{$plugin} = 1;
168  
169 return $self;
170 }
171  
172 sub parse {
173 my ($self, $filename) = @_;
174  
175 my $logger = $self->logger;
176 my $input = read_file $filename
177 or $logger->fatal(__x("cannot read '{filename}': {error}"));
178  
179 my $lineno = 1;
180 my $state = 0;
181  
182 my $lexer = sub {
183 return '', undef if empty $input;
184  
185 if ($state == 0) {
186 $input =~ s/([^\n\{]*)//;
187 return CDATA => $1 if !empty $1;
188  
189 if ("\n" eq $1) {
190 ++$lineno;
191 return CDATA => "\n";
192 } elsif ($input =~ s/^\{\{//) {
193 return SE => '{{';
194 } elsif ($input =~ s/^\{\%//) {
195 return SE => '{%';
196 } else {
197 die;
198 }
199 }
200 };
201 my $error = sub {
202 $logger->error(__"Syntax error!\n");
203 };
204  
205 $self->YYParse(yylex => $lexer, yyerror => $error);
206 }
207  
208 sub logger {
209 shift->{__logger};
210 }
211  
212 1;