File Coverage

blib/lib/UML/PlantUML/Encoder.pm
Criterion Covered Total %
statement 62 70 88.5
branch 7 14 50.0
condition n/a
subroutine 14 14 100.0
pod 6 6 100.0
total 89 104 85.5


line stmt bran cond sub pod time code
1             package UML::PlantUML::Encoder;
2              
3 2     2   133003 use 5.006;
  2         16  
4 2     2   10 use strict;
  2         5  
  2         39  
5 2     2   10 use warnings;
  2         4  
  2         54  
6              
7 2     2   1141 use Encode qw(encode);
  2         30445  
  2         147  
8 2     2   1247 use Compress::Zlib;
  2         127986  
  2         463  
9 2     2   1092 use MIME::Base64;
  2         1395  
  2         231  
10              
11             our ( @ISA, @EXPORT, @EXPORT_OK );
12              
13             BEGIN {
14 2     2   16 require Exporter;
15 2         45 @ISA = qw(Exporter);
16 2         8 @EXPORT = qw(encode_p); # symbols to export
17 2         1271 @EXPORT_OK = qw(encode_p); # symbols to export on request
18             }
19              
20             =for html  
21              
22             =head1 NAME
23              
24             UML::PlantUML::Encoder - Provides PlantUML Language's Encoding in Perl
25              
26             Encodes PlantUML Diagram Text using the PlantUML Encoding Standard described at L
27              
28             =head1 VERSION
29              
30             Version 0.03
31              
32             =cut
33              
34             our $VERSION = '0.03';
35              
36             =head1 SYNOPSIS
37              
38             use UML::PlantUML::Encoder qw(encode_p);
39              
40             my $encoded = encode_p(qq{
41             Alice -> Bob: Authentication Request
42             Bob --> Alice: Authentication Response
43             });
44              
45             print "\nhttp://www.plantuml.com/plantuml/uml/$encoded";
46             print "\nhttp://www.plantuml.com/plantuml/png/$encoded";
47             print "\nhttp://www.plantuml.com/plantuml/svg/$encoded";
48             print "\nhttp://www.plantuml.com/plantuml/txt/$encoded";
49              
50             # Output
51             http://www.plantuml.com/plantuml/uml/~169NZKe00nvpCv5G5NJi5f_maAmN7qfACrBoIpEJ4aipyF8MWrCBIrE8IBgXQe185NQ1Ii1uiYeiBylEAKy6g0HPp7700
52             http://www.plantuml.com/plantuml/png/~169NZKe00nvpCv5G5NJi5f_maAmN7qfACrBoIpEJ4aipyF8MWrCBIrE8IBgXQe185NQ1Ii1uiYeiBylEAKy6g0HPp7700
53             http://www.plantuml.com/plantuml/svg/~169NZKe00nvpCv5G5NJi5f_maAmN7qfACrBoIpEJ4aipyF8MWrCBIrE8IBgXQe185NQ1Ii1uiYeiBylEAKy6g0HPp7700
54             http://www.plantuml.com/plantuml/txt/~169NZKe00nvpCv5G5NJi5f_maAmN7qfACrBoIpEJ4aipyF8MWrCBIrE8IBgXQe185NQ1Ii1uiYeiBylEAKy6g0HPp7700
55              
56             =head1 EXPORT
57              
58             The only Subroutine that this module exports is C
59              
60             =head1 SUBROUTINES/METHODS
61              
62             =head2 utf8_encode
63              
64             Encoded in UTF-8
65              
66             =cut
67              
68             sub utf8_encode {
69 1     1 1 10 return encode( 'UTF-8', $_[0] );
70             }
71              
72             =head2 _compress_with_deflate
73              
74             Compressed using Deflate algorithm
75              
76             =cut
77              
78             sub _compress_with_deflate {
79 1     1   4 my $buffer;
80 1         8 my $d = deflateInit( -WindowBits => $_[1] );
81 1         523 $buffer = $d->deflate( $_[0] );
82 1         36 $buffer .= $d->flush();
83 1         134 return $buffer;
84             }
85              
86             =head2 encode6bit
87              
88             Transform to String of characters that contains only digits, letters, underscore and minus character
89              
90             =cut
91              
92             sub encode6bit {
93 84     84 1 105 my $b = $_[0];
94 84 100       146 if ( $b < 10 ) {
95 16         34 return chr( 48 + $b );
96             }
97 68         90 $b -= 10;
98 68 100       105 if ( $b < 26 ) {
99 41         79 return chr( 65 + $b );
100             }
101 27         48 $b -= 26;
102 27 50       44 if ( $b < 26 ) {
103 27         54 return chr( 97 + $b );
104             }
105 0         0 $b -= 26;
106 0 0       0 if ( $b == 0 ) {
107 0         0 return '-';
108             }
109 0 0       0 if ( $b == 1 ) {
110 0         0 return '_';
111             }
112 0         0 return '?';
113             }
114              
115             =head2 append3bytes
116              
117             Transform adjacent bytes
118              
119             =cut
120              
121             sub append3bytes {
122 21     21 1 32 my ( $c1, $c2, $c3, $c4, $r );
123 21         27 my $b1 = $_[0];
124 21         27 my $b2 = $_[1];
125 21         26 my $b3 = $_[2];
126 21         30 $c1 = $b1 >> 2;
127 21         33 $c2 = ( ( $b1 & 0x3 ) << 4 ) | ( $b2 >> 4 );
128 21         28 $c3 = ( ( $b2 & 0xF ) << 2 ) | ( $b3 >> 6 );
129 21         32 $c4 = $b3 & 0x3F;
130 21         26 $r = "";
131 21         35 $r .= encode6bit( $c1 & 0x3F );
132 21         38 $r .= encode6bit( $c2 & 0x3F );
133 21         37 $r .= encode6bit( $c3 & 0x3F );
134 21         40 $r .= encode6bit( $c4 & 0x3F );
135 21         57 return $r;
136             }
137              
138             =head2 encode64
139              
140             Reencoded in ASCII using a transformation close to base64
141              
142             =cut
143              
144             sub encode64 {
145 1     1 1 3 my $c = $_[0];
146 1         2 my $str = "";
147 1         3 my $len = length $c;
148 1         15 my $i;
149 1         5 for ( $i = 0; $i < $len; $i += 3 ) {
150 21 50       49 if ( $i + 2 == $len ) {
    50          
151 0         0 $str .= append3bytes( ord( substr( $c, $i, 1 ) ),
152             ord( substr( $c, $i + 1, 1 ) ), 0 );
153             }
154             elsif ( $i + 1 == $len ) {
155 0         0 $str .= append3bytes( ord( substr( $c, $i, 1 ) ), 0, 0 );
156             }
157             else {
158 21         49 $str .= append3bytes(
159             ord( substr( $c, $i, 1 ) ),
160             ord( substr( $c, $i + 1, 1 ) ),
161             ord( substr( $c, $i + 2, 1 ) )
162             );
163             }
164             }
165 1         4 return $str;
166             }
167              
168             =head2 add_header_huffman
169              
170             To Indicate that this is Huffman Encoding add an header ~1.
171              
172             =cut
173              
174             sub add_header_huffman {
175 1     1 1 5 return '~1' . $_[0];
176             }
177              
178             =head2 encode_p
179              
180             Encodes diagram text descriptions
181              
182             =cut
183              
184             sub encode_p {
185 1     1 1 911 my $data = utf8_encode( $_[0] );
186 1         239 my $compressed = _compress_with_deflate( $data, 9 );
187 1         7 return add_header_huffman(encode64($compressed));
188             }
189              
190             =head1 AUTHOR
191              
192             Rangana Sudesha Withanage, C<< >>
193              
194             =head1 BUGS
195              
196             Please report any bugs or feature requests to C, or through
197             the web interface at L. I will be notified, and then you'll
198             automatically be notified of progress on your bug as I make changes.
199              
200              
201             =head1 SUPPORT
202              
203             You can find documentation for this module with the perldoc command.
204              
205             perldoc UML::PlantUML::Encoder
206              
207             You can also look for information at:
208              
209             =over 4
210              
211             =item * RT: CPAN's request tracker (report bugs here)
212              
213             L
214              
215             =item * GitHub Repository
216              
217             L
218              
219             =item * CPAN Ratings
220              
221             L
222              
223             =item * Search CPAN
224              
225             L
226              
227             =back
228              
229             =head1 LICENSE AND COPYRIGHT
230              
231             This software is copyright (c) 2019 by Rangana Sudesha Withanage.
232              
233             This is free software; you can redistribute it and/or modify it under
234             the same terms as the Perl 5 programming language system itself.
235              
236              
237             =cut
238              
239             1; # End of UML::PlantUML::Encoder