Class: Jekyll::Archimate::CatalogTag

Inherits:
Liquid::Tag
  • Object
show all
Defined in:
lib/jekyll/archimate/catalog_tag.rb

Overview

Insert a diagram from the ArchiMate model.

{% element_catalog type:"Principle" | caption:"Principles Catalog"  %}

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(tag_name, markup, tokens) ⇒ CatalogTag

Returns a new instance of CatalogTag.



13
14
15
16
17
18
19
# File 'lib/jekyll/archimate/catalog_tag.rb', line 13

def initialize(tag_name, markup, tokens)
  @markup = markup
  @context = nil
  @caption = nil
  @element_types = []
  super
end

Instance Attribute Details

#captionObject (readonly)

Returns the value of attribute caption.



9
10
11
# File 'lib/jekyll/archimate/catalog_tag.rb', line 9

def caption
  @caption
end

#contextObject (readonly)

Returns the value of attribute context.



8
9
10
# File 'lib/jekyll/archimate/catalog_tag.rb', line 8

def context
  @context
end

#element_typesObject (readonly)

Returns the value of attribute element_types.



10
11
12
# File 'lib/jekyll/archimate/catalog_tag.rb', line 10

def element_types
  @element_types
end

#markupObject (readonly)

Returns the value of attribute markup.



11
12
13
# File 'lib/jekyll/archimate/catalog_tag.rb', line 11

def markup
  @markup
end

Instance Method Details

#converter(context) ⇒ Object



87
88
89
90
91
# File 'lib/jekyll/archimate/catalog_tag.rb', line 87

def converter(context)
  # Gather settings
  site = context.registers[:site]
  site.find_converter_instance(::Jekyll::Converters::Markdown)
end

#elements(catalog) ⇒ Object



45
46
47
# File 'lib/jekyll/archimate/catalog_tag.rb', line 45

def elements(catalog)
  @element_types.map { |element_type| catalog[element_type] }.flatten.compact
end

#render(context) ⇒ Object



21
22
23
24
25
# File 'lib/jekyll/archimate/catalog_tag.rb', line 21

def render(context)
  @context = context
  scan_attributes(context)
  render_table
end

#render_properties(props) ⇒ Object



62
63
64
65
66
# File 'lib/jekyll/archimate/catalog_tag.rb', line 62

def render_properties(props)
  return "" if props.empty?

  "<dl>" + props.map { |k, v| "<dt>#{k}</dt><dd>#{v}</dd>" }.join("") + "</dl>"
end

#render_rows(elements) ⇒ Object



49
50
51
52
53
54
55
56
57
58
59
60
# File 'lib/jekyll/archimate/catalog_tag.rb', line 49

def render_rows(elements)
  return "<tr><td colspan=\"3\">No Items</td></tr>" if elements.empty?
  elements.map do |element|
    <<-END
    <tr>
      <td><span class="badge badge-primary">#{element["type"]}</span> #{element["name"]}</td>
      <td>#{@converter.convert(element["documentation"]).gsub(/<\/?p[^>]*>/, '').chomp if element["documentation"]}</td>
      <td>#{render_properties(element["properties"])}</td>
    </tr>
    END
  end.join("")
end

#render_tableObject



27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
# File 'lib/jekyll/archimate/catalog_tag.rb', line 27

def render_table
  <<-END.gsub(/^ {6}/, '')
  <table>
    <caption>#{caption}</caption>
    <thead>
      <tr>
        <th>Name</th>
        <th>Documentation</th>
        <th>Properties</th>
      </tr>
    </thead>
    <tbody>
    #{render_rows(elements(site.data["archimate"]["catalog"]))}
    </tbody>
  </table>
  END
end

#scan_attributes(context) ⇒ Object



68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
# File 'lib/jekyll/archimate/catalog_tag.rb', line 68

def scan_attributes(context)
  @converter = converter(context)

  # Render any liquid variables
  markup = Liquid::Template.parse(@markup).render(context)

  # Extract tag attributes
  attributes = {}
  markup.scan(Liquid::TagAttributes) do |key, value|
    attributes[key] = value
  end

  caption = attributes['caption']&.gsub!(/\A"|"\Z/, '')
  @caption = @converter.convert(caption).gsub(/<\/?p[^>]*>/, '').chomp if @caption
  element_type = attributes['type']
  element_type = element_type.gsub!(/\A"|"\Z/, '') if element_type
  @element_types = element_type.split(",").map(&:strip)
end

#siteObject



93
94
95
# File 'lib/jekyll/archimate/catalog_tag.rb', line 93

def site
  @site ||= context.registers[:site]
end