Class: Jekyll::PlantumlBlock

Inherits:
Liquid::Block
  • Object
show all
Defined in:
lib/jekyll-plantuml-url.rb

Instance Method Summary collapse

Constructor Details

#initialize(tag_name, markup, tokens) ⇒ PlantumlBlock

Returns a new instance of PlantumlBlock.



32
33
34
35
# File 'lib/jekyll-plantuml-url.rb', line 32

def initialize(tag_name, markup, tokens)
  super
  @html = (markup or '').strip
end

Instance Method Details

#render(context) ⇒ Object



37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
# File 'lib/jekyll-plantuml-url.rb', line 37

def render(context)
  site = context.registers[:site]
  name = Digest::MD5.hexdigest(super)
  if site.config['plantuml']
    uml_config=site.config['plantuml']
  else
    puts "** No configuration for plantuml in present in _config.yml, using default"
    #Jekyll.logger.debug "No configuration for plantuml in present in _config.yml, using default";
    uml_config={
      'type'         => 'svg',
      'url'          => 'http://www.plantuml.com/plantuml',
      'ssl_noverify' => '0',
      'http_debug'   => '0'
    }
  end
  if uml_config['type'] == ""
    uml_type="svg"
  else
    uml_type=uml_config['type']
  end
  if !File.exists?(File.join(site.dest, "uml/#{name}.#{uml_type}"))
    uml_out_file = File.join(site.source, "uml/#{name}.#{uml_type}")
    if File.exists?(uml_out_file)
      puts "File #{uml_out_file} already exists (#{File.size(uml_out_file)} bytes)"
    else
      if uml_config['url'] == ""
        uml_url="http://www.plantuml.com/plantuml"
        puts "using default url: #{uml_url}"
      else
        uml_url=uml_config['url']
        puts "using config url: #{uml_url}"
      end
      uri = URI.parse(uml_url)
      http = Net::HTTP.new(uri.host, uri.port)
      if uri.scheme == "https"
        http.use_ssl = true
        if uml_config['ssl_noverify'] == "1"
          http.verify_mode = OpenSSL::SSL::VERIFY_NONE
        end
      end
      if uml_config['http_debug'] == "1"
        puts "*** http debug on ***"
        http.set_debug_output $stderr
      end

      request = Net::HTTP::Post.new("#{uri.path}/form")
      request.add_field('Content-Type', 'application/x-www-form-urlencoded')
      request.body = "text=" + URI::encode("@startuml\n"+super+"\n@enduml\n".force_encoding('ASCII-8BIT'))
      response = http.request(request)

      if response.code == "302" or response.code == "301"
        # expected redirect of 302 with a new url with code-hash
        newlocation=response["Location"]
        newlocation["/uml/"]= "/#{uml_type}/"
        begin # loop for 301 and 302 of the code request
          newuri = URI.parse(newlocation)
          if newuri.host != uri.host or newuri.port != uri.port or newuri.scheme != uri.scheme
            http = Net::HTTP.new(newuri.host, newuri.port)
            if newuri.scheme == "https"
              http.use_ssl = true
              if uml_config['ssl_noverify'] == "1"
                http.verify_mode = OpenSSL::SSL::VERIFY_NONE
              end
            end
            if uml_config['http_debug'] == "1"
              puts "*** http debug on ***"
              http.set_debug_output $stderr
            end
          end
          request_img = Net::HTTP::Get.new(newuri.path)
          response_img = http.request(request_img)
          if response_img.code == "200"
            #puts response_img.body
            FileUtils.mkdir_p(File.dirname(uml_out_file))
            File.open(uml_out_file, 'w') { |f|
              f.write(response_img.body)
            }
            site.static_files << Jekyll::StaticFile.new( site, site.source, 'uml', "#{name}.#{uml_type}")
            puts "File #{uml_out_file} created (#{File.size(uml_out_file)} bytes)"
          elsif response_img.code == "302" or response_img.code == "301"
            # not expected www.plantuml.com redirect to plantuml.com, what a waste of time
            # it is what it is
            newlocation=response_img["Location"]
          else
            puts "error #{response_img.code} getting #{uri.host}#{request_img}"
          end
        end while  response_img.code == "302" or response_img.code == "301"
      else
        puts "Error #{response.code} getting #{uri.host}/plantuml/form "
      end
    end
  end
  "<p><img src='#{site.baseurl}/uml/#{name}.#{uml_type}' #{@html}
    alt='PlantUML #{uml_type} diagram' class='plantuml'/></p>"
end