Class: PlantUmlEncode64

Inherits:
Object
  • Object
show all
Defined in:
lib/plantuml-encode64.rb

Overview

Encode the uml code into a string to provide for building the image with a remote provider.

This code is inspired by the page of plantuml website plantuml.sourceforge.net/codephp.html

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(input) ⇒ PlantUmlEncode64

Returns a new instance of PlantUmlEncode64.



27
28
29
# File 'lib/plantuml-encode64.rb', line 27

def initialize(input)
    @input = input;
end

Class Method Details

.append3bytes(b1, b2, b3) ⇒ Object



79
80
81
82
83
84
85
86
87
88
# File 'lib/plantuml-encode64.rb', line 79

def self.append3bytes(b1, b2, b3)
    c1 = (b1 >> 2)
    c2 = ((b1 & 0x3) << 4) | (b2 >> 4)
    c3 = ((b2 & 0xF) << 2) | (b3 >> 6)
    c4 = b3 & 0x3F;
    r = encode6bit(c1 & 0x3F)
    r += encode6bit(c2 & 0x3F);
    r += encode6bit(c3 & 0x3F);
    return r + encode6bit(c4 & 0x3F);
end

.encode64(input) ⇒ Object

Internal : Encode is some special base 64.

 @param a deflate string Returns a encoded string



45
46
47
48
49
50
51
52
53
54
# File 'lib/plantuml-encode64.rb', line 45

def self.encode64(input)
    len, i, out =  input.length, 0, "";
    while i < len do
        i1 = (i+1 < len) ? input[i+1].ord : 0;
        i2 = (i+2 < len) ? input[i+2].ord : 0;
        out += append3bytes(input[i].ord, i1, i2);
        i += 3;
    end
    return out
end

.encode6bit(b) ⇒ Object



56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
# File 'lib/plantuml-encode64.rb', line 56

def self.encode6bit(b)
    if b < 10 then
    return (48 + b).chr;
    end

    b -= 10;
    if b < 26 then
    return (65 + b).chr;
    end

    b -= 26;
    if b < 26 then
    return (97 + b).chr;
    end

    b -= 26;
    if b == 0 then
        return '-';
    end

    return (b == 1) ? '_' : '?';
end

Instance Method Details

#encodeObject

Public : proceed to the encoding for plantuml servlet

Returns the encoded uml to send to the servlet



34
35
36
37
38
39
# File 'lib/plantuml-encode64.rb', line 34

def encode()
    require 'zlib';
    o = @input.force_encoding("utf-8");
    o = Zlib::Deflate.new(nil, -Zlib::MAX_WBITS).deflate(o, Zlib::FINISH)
    return PlantUmlEncode64.encode64(o);
end