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: a5f4ad0b31ccf1a5a9a83484f4ddf89bb90891d2 (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>
    [\[|\(]
    (?<badge_type>CORE|STARTER|PREMIUM|ULTIMATE|FREE|BRONZE|SILVER|GOLD)(?:\s+(?<only>ONLY))?
    [\]|\)]
    </strong>
  }x

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

  def run(content, params = {})
    content.gsub(BADGES_HTML_PATTERN) { generate(Regexp.last_match[:badge_type].downcase, !Regexp.last_match[:only].nil?) }
  end

  def run_from_markdown(content)
    content.gsub(BADGES_MARKDOWN_PATTERN) { generate(Regexp.last_match[:badge_type].downcase, !Regexp.last_match[:only].nil?) }
  end

  def generate(badge_type, only)
    if only
      %(<span class="badge-trigger #{badge_type}-only"></span>)
    else
      %(<span class="badge-trigger #{badge_type}"></span>)
    end
  end
end