custom_helpers.rb 3.27 KB
Newer Older
1 2 3
require 'open-uri'
require 'nokogiri'

4 5 6 7 8 9 10
module CustomHelpers
  def icon(icon, cssclass = "", attrs = {})
    width = attrs[:width] || 76
    height = attrs[:height] || 76
    viewbox_width = attrs[:viewbox_width] || width
    viewbox_height = attrs[:viewbox_height] || height
    label = attrs[:label] || ""
Matija Čupić's avatar
Matija Čupić committed
11
    content_tag :svg, viewbox: "0 0 #{viewbox_width} #{viewbox_height}", width: width, height: height, class: cssclass, aria: { label: label }, role: "img" do
12 13 14 15
      partial "includes/icons/#{icon}.svg"
    end
  end

16 17 18 19 20 21 22
  def svg_image(icon, cssclass = "", attrs = {})
    data = attrs[:data] || ""
    content_tag :svg, class: cssclass, role: "img", data: data do
      partial icon
    end
  end

23 24 25
  def xml_feed_content(article)
    content = article.body

Matija Čupić's avatar
Matija Čupić committed
26 27 28
    content << if article.data.image_title
                 "<img src='#{data.site.url}#{article.data.image_title}' class='webfeedsFeaturedVisual' style='display: none;' />"
               else
Matija Čupić's avatar
Matija Čupić committed
29
                 "<img src='#{data.site.url}#{image_path('default-blog-image.png')}' class='webfeedsFeaturedVisual' style='display: none;' />"
Matija Čupić's avatar
Matija Čupić committed
30
               end
31 32 33 34 35

    h(content)
  end

  def markdown(text)
36 37 38
    # Scope parameter is necessary to make Markdown in YAML work properly
    # See: https://github.com/middleman/middleman/issues/653#issuecomment-9954111
    Tilt['markdown'].new { text }.render(scope: self)
39 40
  end

Matija Čupić's avatar
Matija Čupić committed
41 42
  def kramdown(text)
    Kramdown::Document.new(text).to_html
Victor Wu's avatar
Victor Wu committed
43 44
  end

45
  def team_size
46
    data.team.count { |entry| entry['type'] == 'person' }
47 48
  end

Clement Ho's avatar
Clement Ho committed
49 50
  def open_roles
    data.roles.select(&:open).sort_by(&:title)
51 52 53
  end

  def salary_avail
Clement Ho's avatar
Clement Ho committed
54
    data.roles.select(&:salary).sort_by(&:title)
55 56
  end

57 58
  def current_role_for_salary_calculator
    current_role = salary_avail.detect do |role|
Clement Ho's avatar
Clement Ho committed
59
      role.description.start_with?("/#{File.dirname(current_page.request_path)}")
60
    end
61

62
    if current_role&.levels&.is_a? String
63
      current_role.levels = data.role_levels.send(current_role.levels)
64 65 66
    end

    current_role
67 68 69
  end

  def font_url(current_page)
70
    fonts = ["Source+Sans+Pro:200,300,400,500,600,700"]
71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92

    if current_page.data.extra_font
      fonts = fonts.concat current_page.data.extra_font
    end
    fonts = fonts.join("|")

    "//fonts.googleapis.com/css?family=#{fonts}"
  end

  def highlight_active_nav_link(link_text, url, options = {})
    options[:class] ||= ""
    options[:class] << " active" if url == current_page.url
    link_to(link_text, url, options)
  end

  def full_url(current_page)
    "#{data.site.url}#{current_page.url}"
  end

  def current_version
    ReleaseList.new.release_posts.first.version
  end
93 94 95 96 97 98 99 100 101 102 103 104 105 106

  def copy_btn_options(copy_text, tooltip_text = nil, button_class = nil)
    tooltip_text = 'Copy to clipboard' if tooltip_text.nil?
    button_class = 'copy-btn js-copy-btn' if button_class.nil?

    {
      class: "btn #{button_class}", type: 'button',
      title: tooltip_text, 'aria-label' => tooltip_text,
      data: {
        'clipboard-text' => copy_text,
        toggle: 'tooltip', placement: 'top'
      }
    }
  end
107 108 109 110

  def production?
    ENV['MIDDLEMAN_ENV'] == 'production'
  end
Sean McGivern's avatar
Sean McGivern committed
111 112 113 114 115 116 117 118 119 120

  def add_extra_css(*files)
    current_page.data.extra_css ||= []
    current_page.data.extra_css |= files
  end

  def add_extra_js(*files)
    current_page.data.extra_js ||= []
    current_page.data.extra_js |= files
  end
121
end