Class: Jekyll::Spaceship::MermaidProcessor

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

Constant Summary

Constants inherited from Processor

Processor::DEFAULT_PRIORITY, 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

class_name, #converter, #dispatch, escape_html, exclude, #exclusion_regexs, #ext, fetch_img_data, #filename, #get_exclusion, handle_bang_link, #initialize, #initialize_exclusions, #initialize_priority, #initialize_register, make_img_tag, #name, #next?, #on_handle_html, #on_handle_html_block, #on_handled, #output_ext, #post_exclude, #pre_exclude, priority, #process?, register

Constructor Details

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

Class Method Details

.configObject



10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
# File 'lib/jekyll-spaceship/processors/mermaid-processor.rb', line 10

def self.config
  {
    'mode' => 'default',
    'syntax' => {
      'code' => 'mermaid!',
      'custom' => ['@startmermaid', '@endmermaid']
    },
    'css' => {
      'class' => 'mermaid'
    },
    'config': {
      'theme' => 'default'
    },
    'src' => 'https://mermaid.ink/svg/'
  }
end

Instance Method Details

#get_url(code) ⇒ Object



84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
# File 'lib/jekyll-spaceship/processors/mermaid-processor.rb', line 84

def get_url(code)
  src = self.config['src']

  # wrap code
  code = {
    'code' => code.gsub(/^\s*|\s*$/, ''),
    'mermaid' => config['config']
  }.to_json

  # set default method
  src += '{code}' if src.match(/\{.*\}/).nil?

  # encode to base64 string
  if src.include?('{code}')
    code = Base64.urlsafe_encode64(code, padding: false)
    return src.gsub('{code}', code)
  else
    raise "No supported src ! #{src}"
  end
end

#handle_mermaid(code) ⇒ Object



62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
# File 'lib/jekyll-spaceship/processors/mermaid-processor.rb', line 62

def handle_mermaid(code)
  # Handle extra empty lines, otherwise it would cause error
  code = code.gsub(/\n\s*\n/, "\n%%-\n")

  # encode to UTF-8
  code = code.encode('UTF-8')
  url = get_url(code)

  # render mode
  case self.config['mode']
  when 'pre-fetch'
    data = self.class.fetch_img_data(url)
  end
  if data.nil?
    data = { 'type' => 'url', 'body' => url }
  end

  # return img tag
  data['class'] = self.config['css']['class']
  self.class.make_img_tag(data)
end

#handle_mermaid_block(pattern, content) ⇒ Object



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

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

    self.handled = true

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

#on_handle_markdown(content) ⇒ Object



27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
# File 'lib/jekyll-spaceship/processors/mermaid-processor.rb', line 27

def on_handle_markdown(content)
  # match custom mermaid block and code block
  syntax = self.config['syntax']
  code_name = syntax['code']
  custom = syntax['custom'][-2, 2]

  patterns = [
    /((`{3,})\s*#{code_name}((?:.|\n)*?)\2)/,
    /((?<!\\)(#{custom[0]})((?:.|\n)*?)(?<!\\)(#{custom[1]}))/
  ]

  patterns.each do |pattern|
    content = handle_mermaid_block(pattern, content)
  end

  # handle escape custom mermaid block
  content.gsub(/\\(#{custom[0]}|#{custom[1]})/, '\1')
end