blib/lib/HTML/ERuby.pm

filter: Statementstmt 86.6% 13 / 15 Branchbran n/a Conditioncond n/a MC/DCmcdc n/a Subroutinesub 100% 5 / 5 Podpod n/a totaltotal 90.0% 18 / 20 CC 1 SCAR 0.6
CC
1
Cov
87%
BEGIN
1.3
BEGIN
1.0
BEGIN
1.0
CRAP 1.1
↑ Summary
1 package HTML::ERuby;
2 # $Id: ERuby.pm,v 1.5 2002/04/14 01:04:49 ikechin Exp $
3 5 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 5 ▶use vars qw($VERSION $ERUBY_TAG_RE);
Subroutine BEGIN: called CC=1 SCAR=0.0
5 5 ▶use IO::File;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 5 ▶use Carp ();
Subroutine BEGIN: called CC=1 SCAR=0.0
7 0 ▶use Inline::Ruby qw(rb_eval);
Subroutine BEGIN: called CC=1 SCAR=2.6
8 use Data::Dumper;
9  
10 $ERUBY_TAG_RE = qr/(<%%|%%>|<%=|<%#|<%|%>|\n)/so;
11 $VERSION = '0.02';
12  
13 sub new {
14 my $class = shift;
15 my $self = bless {
16 }, $class;
17 $self;
18 }
19  
20 sub compile {
21 my ($self, %args) = @_;
22 my $data;
23 if ($args{filename}) {
24 $data = $self->_open_file($args{filename});
25 }
26 elsif ($args{scalarref}) {
27 $data = ${$args{scalarref}};
28 }
29 elsif ($args{arrayref}) {
30 $data = join('', @{$args{arrayref}});
31 }
32 else {
33 Carp::croak("please specify ERuby document");
34 }
35 my $vars = '';
36 if ($args{vars}) {
37 $vars = $self->_convert_vars($args{vars});
38 }
39 my $src = $self->_parse(\$data);
40 return rb_eval($vars. $src);
41 }
42  
43 sub _open_file {
44 my ($self, $filename) = @_;
45 local $/ = undef;
46 my $f = IO::File->new($filename, "r") or Carp::croak("can not open eruby file: $filename");
47 my $data = $f->getline;
48 $f->close;
49 return $data;
50 }
51  
52 sub _convert_vars {
53 my ($self, $vars) = @_;
54 my $code = '';
55 local $Data::Dumper::Deepcopy = 1;
56 while(my ($name, $value) = each %$vars) {
57 if (my $type = ref($value)) {
58 Carp::croak(__PACKAGE__. " supports String, Hash and Array only")
59 if $type ne 'ARRAY' && $type ne 'HASH';
60 }
61 my $dumped = Dumper $value;
62 $dumped =~ s/\$VAR1 =//; # strip Data::Dumper '$VAR1 =' string.
63 $code .= "$name = $dumped\n";
64 }
65 return $code;
66 }
67  
68 # copy from erb/compile.rb and Perlize :)
69 sub _parse {
70 my($self, $scalarref) = @_;
71 my $src = q/_erb_out = '';/;
72 my @text = split($ERUBY_TAG_RE, $$scalarref);
73 my @content = ();
74 my @cmd = ("_erb_out = ''\n");
75 my $stag = '';
76 my $token = '';
77 for my $token(@text) {
78 if ($token eq '<%%') {
79 push @content, '<%';
80 next;
81 }
82 if ($token eq '%%>') {
83 push @content, '%>';
84 next;
85 }
86 unless ($stag) {
87 if ($token eq '<%' || $token eq '<%=' || $token eq '<%#') {
88 $stag = $token;
89 my $str = join('', @content);
90 if ($str) {
91 push @cmd, qq/_erb_out.concat '$str';/;
92 }
93 @content = ();
94 }
95 elsif($token eq "\n") {
96 push @content, "\n";
97 my $str = join('', @content);
98 push @cmd, qq/_erb_out.concat '$str';/ if $str;
99 @content = ();
100 }
101 else {
102 push @content, $token;
103 }
104 }
105 else {
106 if ($token eq '%>') {
107 my $str = join('', @content);
108 if ($stag eq '<%') {
109 push @cmd, $str, "\n";
110 }
111 elsif ($stag eq '<%=') {
112 push @cmd, qq/_erb_out.concat( ($str).to_s );/;
113 }
114 elsif ($stag eq '<%#') {
115 # comment out SKIP!
116 }
117 @content = ();
118 $stag = undef;
119 }
120 else {
121 push @content, $token;
122 }
123 }
124 }
125 if (@content) {
126 my $str = join('', @content);
127 push @cmd, qq/_erb_out.concat '$str';/;
128 }
129 push @cmd, '_erb_out;';
130 return join('', @cmd);
131 }
132  
133 1;
134  
135 __END__
↑ Summary