Class: Gollum::Filter::Tags

Inherits:
Gollum::Filter show all
Defined in:
lib/gollum-lib/filter/tags.rb

Overview

Render all tags (things in double-square-brackets). This one’s a biggie.

Instance Method Summary collapse

Methods inherited from Gollum::Filter

#initialize

Methods included from Helpers

#trim_leading_slash

Constructor Details

This class inherits a constructor from Gollum::Filter

Instance Method Details

#extract(data) ⇒ Object

Extract all tags into the tagmap and replace with placeholders.



6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
# File 'lib/gollum-lib/filter/tags.rb', line 6

def extract(data)
  return data if @markup.format == :txt || @markup.format == :asciidoc
  data.gsub!(/(.?)\[\[(.+?)\]\]([^\[]?)/m) do
    if Regexp.last_match[1] == "'" && Regexp.last_match[3] != "'"
      "[[#{Regexp.last_match[2]}]]#{Regexp.last_match[3]}"
    elsif Regexp.last_match[2].include?('][')
      if Regexp.last_match[2][0..4] == 'file:'
        pre            = Regexp.last_match[1]
        post           = Regexp.last_match[3]
        parts          = Regexp.last_match[2].split('][')
        parts[0][0..4] = ""
        link           = "#{parts[1]}|#{parts[0].sub(/\.org/, '')}"
        id             = register_tag(link)
        "#{pre}#{id}#{post}"
      else
        Regexp.last_match[0]
      end
    else
      id = register_tag(Regexp.last_match[2])
      "#{Regexp.last_match[1]}#{id}#{Regexp.last_match[3]}"
    end
  end
  data
end

#process(rendered_data) ⇒ Object

Process all text nodes from the doc and replace the placeholders with the final markup.



39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
# File 'lib/gollum-lib/filter/tags.rb', line 39

def process(rendered_data)
  doc  = Nokogiri::HTML::DocumentFragment.parse(rendered_data)
  doc.traverse do |node|
    if node.text? then
      content = node.content
      content.gsub!(/TAG[a-f0-9]+TAG/) do |id|
        if (tag = @map[id]) then
          if is_preformatted?(node) then
            "[[#{tag}]]"
          else
            process_tag(tag).gsub('%2f', '/')
          end
        end
      end
      node.replace(content) if content != node.content
    end
  end

  doc.to_html
end

#register_tag(tag) ⇒ Object



31
32
33
34
35
# File 'lib/gollum-lib/filter/tags.rb', line 31

def register_tag(tag)
  id       = "TAG#{Digest::SHA1.hexdigest(tag)}TAG"
  @map[id] = tag
  id
end