Welcome to mirror list, hosted at ThFree Co, Russian Federation.

badges.rb « filters « lib - gitlab.com/gitlab-org/gitlab-docs.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: e14b24e656dcc677f839f4b49037c449f1ae7c0c (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
# frozen_string_literal: true

# GitLab price / tiers badge
#
# This allows us to add visual Badges to our documentation using standard Markdown
# that will render in any markdown editor.
#
# The available pattern is either:
#  - `**(<BADGE_TYPE> <MODIFIER>)**` (preferred)
#  - `**[<BADGE_TYPE> <MODIFIER>]**` (deprecated)
#
# The following TIERS are supported: CORE, STARTER, PREMIUM, ULTIMATE
# The following MODIFIERS are supported: ONLY
#
# When you have ONLY as MODIFIER, it means, it applies only for on premise instances
# so we are not going to expand "STARTER" to "BRONZE" as well.
class BadgesFilter < Nanoc::Filter
  identifier :badges

  BADGES_HTML_PATTERN = %r{
    <strong>
    [\[|(]
    (?<tier>CORE|STARTER|PREMIUM|ULTIMATE|FREE|BRONZE|SILVER|GOLD)(?:\s+(?<type>ONLY|SAAS|SELF))?
    [\]|)]
    </strong>
  }x.freeze

  BADGES_MARKDOWN_PATTERN = %r{
    (?:^|[^`]) # must be start of the line or anything except backtick
    \*\*(\[|\()
    (?<tier>CORE|STARTER|PREMIUM|ULTIMATE|FREE|BRONZE|SILVER|GOLD)(?:\s+(?<type>ONLY|SAAS|SELF))
    ?(\]|\))\*\*
    (?:$|[^`]) # must end of line or anything except backtick
  }x.freeze

  def run(content, params = {})
    content.gsub(BADGES_HTML_PATTERN) { generate(Regexp.last_match[:tier].downcase, Regexp.last_match[:type]) }
  end

  def run_from_markdown(content)
    content.gsub(BADGES_MARKDOWN_PATTERN) { generate(Regexp.last_match[:tier].downcase, Regexp.last_match[:type]) }
  end

  def generate(tier, type)
    if !type.nil?
      %(<span class="badge-trigger #{tier}-#{type.downcase}"></span>)
    else
      %(<span class="badge-trigger #{tier}"></span>)
    end
  end
end