Class: Jekyll::Spaceship::PlantUMLProcessor

Inherits:
Processor
  • Object
show all
Defined in:
lib/jekyll-spaceship/processors/plantuml-processor.rb

Constant Summary collapse

PLANT_UML_HOST =
'http://www.plantuml.com/plantuml/png/'

Constants inherited from Processor

Jekyll::Spaceship::Processor::DEFAULT_PRIORITY, Jekyll::Spaceship::Processor::PRIORITY_MAP

Instance Attribute Summary

Attributes inherited from Processor

#exclusions, #handled, #logger, #page, #priority, #registers

Instance Method Summary collapse

Methods inherited from Processor

#after_exclude, #converter, #dispatch, exclude, #ext, #initialize, #initialize_exclusions, #initialize_priority, #initialize_register, #name, #on_handle_html, #on_handle_html_block, #on_handled, #output_ext, #pre_exclude, priority, #process?, register

Constructor Details

This class inherits a constructor from Jekyll::Spaceship::Processor

Instance Method Details

#get_plantuml_img_data(code) ⇒ Object



52
53
54
55
56
57
58
59
60
61
62
63
64
# File 'lib/jekyll-spaceship/processors/plantuml-processor.rb', line 52

def get_plantuml_img_data(code)
  data = ''
  url = "#{PLANT_UML_HOST}#{code}"
  begin
    data = Net::HTTP.get URI(url)
    data = Base64.encode64(data)
    data = "data:image/png;base64, #{data}"
  rescue StandardError => msg
    data = url
    logger.log msg
  end
  data
end

#handle_plantuml(code) ⇒ Object



40
41
42
43
44
45
46
47
48
49
50
# File 'lib/jekyll-spaceship/processors/plantuml-processor.rb', line 40

def handle_plantuml(code)
  # wrap plantuml code
  code = "@startuml#{code}@enduml".encode('UTF-8')

  # encode to hex string
  code = '~h' + code.unpack("H*").first
  data = self.get_plantuml_img_data(code)

  # return img tag
  "<img src=\"#{data}\">"
end

#on_handle_markdown(content) ⇒ Object



11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
# File 'lib/jekyll-spaceship/processors/plantuml-processor.rb', line 11

def on_handle_markdown(content)
  # match default plantuml block and code block
  pattern = Regexp.union(
    /(\\?@startuml((?:.|\n)*?)@enduml)/,
    /(`{3}\s*plantuml((?:.|\n)*?)`{3})/
  )

  content.scan pattern do |match|
    match = match.select { |m| not m.nil? }
    block = match[0]
    code = match[1]

    # skip escape default plantuml block
    if block.match(/(^\\@startuml|\\@enduml$)/)
      next
    end

    self.handled = true

    content = content.gsub(
      block,
      handle_plantuml(code)
    )
  end

  # handle escape default plantuml block
  content.gsub(/\\(@startuml|@enduml)/, '\1')
end