Class: DrawIOConverter

Inherits:
Liquid::Tag
  • Object
show all
Defined in:
lib/jekyll-drawio.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(tagName, markup, tokens) ⇒ DrawIOConverter

Returns a new instance of DrawIOConverter.



33
34
35
36
37
38
39
40
# File 'lib/jekyll-drawio.rb', line 33

def initialize(tagName, markup, tokens)
  super
  @path_to_diagram = ""
  @selected_diagram_num = 0
  @diagram_height = 200
  @markup = markup
  @diagram_extractor = DiagramExtractor.new()
end

Instance Attribute Details

#diagram_extractorObject

Returns the value of attribute diagram_extractor.



27
28
29
# File 'lib/jekyll-drawio.rb', line 27

def diagram_extractor
  @diagram_extractor
end

#diagram_heightObject

Returns the value of attribute diagram_height.



27
28
29
# File 'lib/jekyll-drawio.rb', line 27

def diagram_height
  @diagram_height
end

#markupObject

Returns the value of attribute markup.



27
28
29
# File 'lib/jekyll-drawio.rb', line 27

def markup
  @markup
end

#path_to_diagramObject

Returns the value of attribute path_to_diagram.



27
28
29
# File 'lib/jekyll-drawio.rb', line 27

def path_to_diagram
  @path_to_diagram
end

#selected_diagram_numObject

Returns the value of attribute selected_diagram_num.



27
28
29
# File 'lib/jekyll-drawio.rb', line 27

def selected_diagram_num
  @selected_diagram_num
end

Instance Method Details

#parse_content(context) ⇒ Object



42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
# File 'lib/jekyll-drawio.rb', line 42

def parse_content(context)
  parameters_line = Liquid::Template.parse(@markup).render context
  parameters_line.strip!
  parameters = split_parameters_line(parameters_line)

  parameters.each do |param|
    param_value_pair = param.split "="
    if param_value_pair[0] == "path"
      # TODO: find appropriate method to trim the string
      @path_to_diagram = param_value_pair[1].tr('\"', "")
    elsif param_value_pair[0] == "page_number"
      @selected_diagram_num = param_value_pair[1].to_i
    elsif param_value_pair[0] == "height"
      @diagram_height = param_value_pair[1].to_i
    end
  end

  puts "path=\"#{@path_to_diagram}\" page_num=#{@selected_diagram_num}"
end

#render(context) ⇒ Object



62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
# File 'lib/jekyll-drawio.rb', line 62

def render(context)
  if @markup.empty?
    return "Error processing input, incorrect syntax"
  end
  parse_content context

  diagram_name, diagram_content = self.diagram_extractor.extract_diagram @path_to_diagram, @selected_diagram_num
  encoded_diagram = ERB::Util.url_encode diagram_content
  encoded_diagram_name = ERB::Util.url_encode diagram_name

  puts "Parsed diagram: name=#{diagram_name} length=#{diagram_content.length} height=#{@diagram_height}"

  viewer_url_base = "https://viewer.diagrams.net/?highlight=0000ff&edit=_blank&layers=1&nav=1"
  viewer_url = "#{viewer_url_base}&title=#{encoded_diagram_name}\#R#{encoded_diagram}"
  viewer_style = "style=\"width:100%;height:#{@diagram_height}px\""

  %Q{<iframe frameborder="0" #{viewer_style} src="#{viewer_url}"></iframe>}
end