Class: Jekyll::Spaceship::MermaidProcessor
- Inherits:
-
Processor
- Object
- Processor
- Jekyll::Spaceship::MermaidProcessor
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
#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
Class Method Details
.config ⇒ Object
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_mermaid_img_data(url) ⇒ Object
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
|
# File 'lib/jekyll-spaceship/processors/mermaid-processor.rb', line 100
def get_mermaid_img_data(url)
data = ''
begin
res = Net::HTTP.get_response URI(url)
raise res.body unless res.is_a?(Net::HTTPSuccess)
data = Base64.encode64(res.body)
content_type = res.['Content-Type']
raise 'Unknown content type!' if content_type.nil?
data = "data:#{content_type};base64, #{data}"
rescue StandardError => msg
data = url
logger.log msg
end
data
end
|
#get_url(code) ⇒ Object
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
|
# File 'lib/jekyll-spaceship/processors/mermaid-processor.rb', line 79
def get_url(code)
src = self.config['src']
code = {
'code' => code.gsub(/^\s*|\s*$/, ''),
'mermaid' => config['config']
}.to_json
src += '{code}' if src.match(/\{.*\}/).nil?
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
|
# File 'lib/jekyll-spaceship/processors/mermaid-processor.rb', line 62
def handle_mermaid(code)
code = code.encode('UTF-8')
url = get_url(code)
case self.config['mode']
when 'pre-fetch'
url = self.get_mermaid_img_data(url)
end
css_class = self.config['css']['class']
"<img class=\"#{css_class}\" src=\"#{url}\">"
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)
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
content.gsub(/\\(#{custom[0]}|#{custom[1]})/, '\1')
end
|