class Nexmo::Markdown::CodeSnippetsFilter

Public Instance Methods

call(input) click to toggle source
# File lib/nexmo_markdown_renderer/filters/code_snippets_filter.rb, line 4
def call(input)
  input.gsub(/^(\s*)```code_snippets(.+?)```/m) do |_s|
    @indentation = $1
    @config = YAML.safe_load($2)
    validate_config
    html
  end
end

Private Instance Methods

content_from_source() click to toggle source
# File lib/nexmo_markdown_renderer/filters/code_snippets_filter.rb, line 91
def content_from_source
  source_path = "#{Nexmo::Markdown::Config.docs_base_path}/#{@config['source']}/*.yml"
    
  files = Dir[source_path]
  raise "No .yml files found for #{@config['source']} code snippets" if files.empty?
    
  generate_content(files)
end
contents() click to toggle source
# File lib/nexmo_markdown_renderer/filters/code_snippets_filter.rb, line 68
def contents
  list = content_from_source
  list ||= []
    
  list = sort_contents(list)
  resolve_active_tab(list)
    
  list
end
create_content(content) click to toggle source
# File lib/nexmo_markdown_renderer/filters/code_snippets_filter.rb, line 35
def create_content(content)
  element = Nokogiri::XML::Element.new 'div', @document
  element['id'] = content['id']
  element['class'] = 'Vlt-tabs__panel'
  element['class'] += ' Vlt-tabs__panel_active' if content[:active]
  element.inner_html = content[:body]
    
  @tabs_content.add_child(element)
end
create_tabs(content) click to toggle source
# File lib/nexmo_markdown_renderer/filters/code_snippets_filter.rb, line 15
def create_tabs(content)
  tab = Nokogiri::XML::Element.new 'li', @document
  tab['class'] = 'Vlt-tabs__link'
  tab['class'] += ' Vlt-tabs__link_active' if content[:active]
  tab['aria-selected'] = 'true' if content[:active]
    
  if content['language']
    tab['data-language'] = content['language']
    tab['data-language-type'] = content['language_type']
    tab['data-language-linkable'] = true
  end
    
  tab_link = Nokogiri::XML::Element.new 'a', @document
  tab_link.inner_html = "<svg><use xlink:href=\"/assets/images/brands/#{content['icon']}.svg##{content['icon']}\" /></svg><span>" + content['title'] + '</span>'
  tab_link['class'] = 'tab-link'
    
  tab.add_child(tab_link)
  @tabs.add_child(tab)
end
generate_content(files) click to toggle source
# File lib/nexmo_markdown_renderer/filters/code_snippets_filter.rb, line 100
def generate_content(files)
  files.map do |content_path|
    source = File.read(content_path)
    
    # Load the defaults for this language
    filename = File.basename(content_path, '.yml')
    defaults = Nexmo::Markdown::CodeLanguage.find(filename)
    
    content = YAML.safe_load(source)
    content['source'] = source
    content['id'] = SecureRandom.hex
    content['title'] ||= defaults.label
    content['language'] ||= defaults.key
    content['language_type'] ||= defaults.type
    content['dependencies'] ||= defaults.dependencies
    content['icon'] = defaults.icon
    content['weight'] ||= defaults.weight
    content['run_command'] ||= defaults.run_command
    content['unindent'] = defaults.unindent || false
    content['version'] ||= defaults.version

    # If we don't have a file_name in config, use the one in the repo
    content['file_name'] ||= File.basename(content['code']['source'])
    
    parent_config = { 'code_only' => @config['code_only'], 'source' => @config['source'].gsub('_examples/', '') }
    if @config['application']
      parent_config = parent_config.merge({ 'application' => @config['application'] })
    end
    
    parent_config = parent_config.to_yaml.lines[1..-1].join
    
    source = render_single_snippet(content, parent_config)
    
    content[:body] = Nexmo::Markdown::Renderer.new(options).call(source)
    
    content
  end
end
generate_html_content(html) click to toggle source
# File lib/nexmo_markdown_renderer/filters/code_snippets_filter.rb, line 56
def generate_html_content(html)
  @document = Nokogiri::HTML::DocumentFragment.parse(html)
  @tabs = @document.at_css('.Vlt-tabs__header--bordered')
  @tabs_content = @document.at_css('.Vlt-tabs__content')
    
  tab_maker
    
  source = @document.to_html
    
  "#{@indentation}FREEZESTART#{Base64.urlsafe_encode64(source)}FREEZEEND"
end
html() click to toggle source
# File lib/nexmo_markdown_renderer/filters/code_snippets_filter.rb, line 45
      def html
        html = <<~HEREDOC
          <div class="Vlt-tabs">
            <div class="Vlt-tabs__header--bordered"></div>
            <div class="Vlt-tabs__content"></div>
          </div>
        HEREDOC
    
        generate_html_content(html)
      end
render_single_snippet(content, parent_config) click to toggle source
# File lib/nexmo_markdown_renderer/filters/code_snippets_filter.rb, line 139
      def render_single_snippet(content, parent_config)
        <<~HEREDOC
          ```single_code_snippet
          #{content.to_yaml}\n#{parent_config}
          ```
        HEREDOC
      end
resolve_active_tab(contents) click to toggle source
# File lib/nexmo_markdown_renderer/filters/code_snippets_filter.rb, line 153
def resolve_active_tab(contents)
  active_index = nil
    
  if options[:code_language]
    contents.each_with_index do |content, index|
      active_index = index if content['language'] == options[:code_language].key
    end
  end
    
  @tabs['data-has-initial-tab'] = active_index.present?
  @tabs['class'] += ' skip-pushstate' if @tabs['data-has-initial-tab']
  active_index ||= 0
    
  contents[active_index][:active] = true
end
sort_contents(contents) click to toggle source
# File lib/nexmo_markdown_renderer/filters/code_snippets_filter.rb, line 147
def sort_contents(contents)
  contents.sort_by do |content|
    content['weight']
  end
end
tab_maker() click to toggle source
# File lib/nexmo_markdown_renderer/filters/code_snippets_filter.rb, line 78
def tab_maker
  contents.each do |content|
    create_tabs(content)
    create_content(content)
  end
end
validate_config() click to toggle source
# File lib/nexmo_markdown_renderer/filters/code_snippets_filter.rb, line 85
def validate_config
  return if @config && @config['source']
    
  raise 'A source key must be present in this building_blocks config'
end