Class: Jekyll::Spaceship::PlantumlProcessor

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

Constant Summary collapse

PLANTUML_PATTERNS =
[
  /(\\?(@startuml)((?:.|\n)*?)@enduml)/,
  /((`{3,})\s*plantuml((?:.|\n)*?)\2)/
]

Constants inherited from Processor

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

Instance Attribute Summary

Attributes inherited from Processor

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

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from Processor

#after_exclude, #converter, #dispatch, escape_html, exclude, #ext, #filename, #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

Class Method Details

.configObject



15
16
17
# File 'lib/jekyll-spaceship/processors/plantuml-processor.rb', line 15

def self.config
  { 'src' => 'http://www.plantuml.com/plantuml/png/' }
end

Instance Method Details

#get_plantuml_img_data(code) ⇒ Object



62
63
64
65
66
67
68
69
70
71
72
73
74
# File 'lib/jekyll-spaceship/processors/plantuml-processor.rb', line 62

def get_plantuml_img_data(code)
  data = ''
  url = "#{config['src']}#{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



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

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 class=\"plantuml\" src=\"#{data}\">"
end

#handle_plantuml_block(pattern, content) ⇒ Object



29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
# File 'lib/jekyll-spaceship/processors/plantuml-processor.rb', line 29

def handle_plantuml_block(pattern, content)
  content.scan pattern do |match|
    match = match.select { |m| not m.nil? }
    block = match[0]
    code = match[2]

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

    self.handled = true

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

#on_handle_markdown(content) ⇒ Object



19
20
21
22
23
24
25
26
27
# File 'lib/jekyll-spaceship/processors/plantuml-processor.rb', line 19

def on_handle_markdown(content)
  # match default plantuml block and code block
  PLANTUML_PATTERNS.each do |pattern|
    content = handle_plantuml_block(pattern, content)
  end

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