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

admonition.rb « filters « lib - gitlab.com/gitlab-org/gitlab-docs.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 141b93d725f2d73708985aa1142983e2b323527b (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
# encoding: utf-8

# Adapted from the admonition code on http://nanoc.ws/
class AdmonitionFilter < Nanoc::Filter

  identifier :admonition

  BOOTSTRAP_MAPPING = {
    'tip'     => 'success',
    'note'    => 'info',
    'caution' => 'warning',
    'danger'  => 'danger',
  }

  FONT_AWESOME_MAPPING = {
    'note'    =>  'info-circle',
    'tip'     =>  'pencil',
    'caution' =>  'exclamation-triangle',
    'danger'  =>  'bolt',
  }

  def run(content, params = {})
    # `#dup` is necessary because `.fragment` modifies the incoming string. Ew!
    # See https://github.com/sparklemotion/nokogiri/issues/1077
    doc = Nokogiri::HTML.fragment(content.dup)
    doc.css('p').each do |para|
      content = para.inner_html
      next if content !~ /\A(TIP|NOTE|CAUTION|DANGER): (.*)\Z/m
      new_content = generate($1.downcase, $2)
      para.replace(new_content)
    end
    doc.to_s
  end

  def generate(kind, content)
    %[<div class="admonition-wrapper #{kind}">] +
    %[<div class="admonition alert alert-#{BOOTSTRAP_MAPPING[kind]}">] +
    %[<i class="fa fa-#{FONT_AWESOME_MAPPING[kind]} fa-fw" aria-hidden="true"></i>] +
    content +
    %[</div></div>]
  end

end