Class: Rabbit::ThemeBrowser::RD2DocumentVisitor

Inherits:
Parser::RD::Visitor show all
Defined in:
lib/rabbit/theme-browser/rd2document-lib.rb

Constant Summary collapse

EXTENSIONS =
{
  "block_verbatim" => Ext::BlockVerbatim,
}
@@itemize_icon =
nil

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods inherited from Parser::RD::Visitor

version

Constructor Details

#initialize(buffer, iter, logger) ⇒ RD2DocumentVisitor

Returns a new instance of RD2DocumentVisitor.



16
17
18
19
20
21
22
# File 'lib/rabbit/theme-browser/rd2document-lib.rb', line 16

def initialize(buffer, iter, logger)
  @buffer = buffer
  @iter = iter
  @logger = logger
  load_itemize_icon
  super()
end

Instance Attribute Details

#loggerObject (readonly)

Returns the value of attribute logger.



15
16
17
# File 'lib/rabbit/theme-browser/rd2document-lib.rb', line 15

def logger
  @logger
end

Instance Method Details

#apply_to_Code(element, contents) ⇒ Object



122
123
124
125
126
127
128
# File 'lib/rabbit/theme-browser/rd2document-lib.rb', line 122

def apply_to_Code(element, contents)
  Proc.new do |*args|
    tag("code") do
      insert_children(contents)
    end
  end
end

#apply_to_DescList(element, items) ⇒ Object



53
54
55
56
57
58
59
60
61
# File 'lib/rabbit/theme-browser/rd2document-lib.rb', line 53

def apply_to_DescList(element, items)
  Proc.new do |*args|
    items.each do |item|
      tag("description") do
        item.call
      end
    end
  end
end

#apply_to_DescListItem(element, term, description) ⇒ Object



71
72
73
74
75
76
77
78
79
80
81
# File 'lib/rabbit/theme-browser/rd2document-lib.rb', line 71

def apply_to_DescListItem(element, term, description)
  Proc.new do |*args|
    tag("description-term") do
      insert_children(term)
    end
    @buffer.insert(@iter, "\n")
    tag("description-content") do
      insert_children(description)
    end
  end
end

#apply_to_DocumentElement(element, contents) ⇒ Object



24
25
26
27
28
# File 'lib/rabbit/theme-browser/rd2document-lib.rb', line 24

def apply_to_DocumentElement(element, contents)
  tag("body") do
    insert_children(contents, "top-level-")
  end
end

#apply_to_Headline(element, contents) ⇒ Object



30
31
32
33
34
35
36
37
# File 'lib/rabbit/theme-browser/rd2document-lib.rb', line 30

def apply_to_Headline(element, contents)
  Proc.new do |*args|
    tag("heading#{element.level}") do
      insert_children(contents)
      insert("\n")
    end
  end
end

#apply_to_ItemList(element, items) ⇒ Object



39
40
41
42
43
44
45
46
47
48
49
50
51
# File 'lib/rabbit/theme-browser/rd2document-lib.rb', line 39

def apply_to_ItemList(element, items)
  Proc.new do |*args|
    items.each do |item|
      tag("item") do
        @buffer.insert(@iter, @@itemize_icon)
        @buffer.insert(@iter, " ")
        tag("item-content") do
          item.call
        end
      end
    end
  end
end

#apply_to_ItemListItem(element, contents) ⇒ Object



63
64
65
66
67
68
69
# File 'lib/rabbit/theme-browser/rd2document-lib.rb', line 63

def apply_to_ItemListItem(element, contents)
  Proc.new do |*args|
    contents.each do |content|
      content.call
    end
  end
end

#apply_to_Reference_with_RDLabel(element, contents) ⇒ Object



107
108
109
110
111
112
113
114
115
116
117
118
119
120
# File 'lib/rabbit/theme-browser/rd2document-lib.rb', line 107

def apply_to_Reference_with_RDLabel(element, contents)
  source = []
  contents.each do |elem|
    if elem.respond_to?(:call)
      elem.call
    else
      source << elem
    end
  end
  source = source.join("")
  Proc.new do |*args|
    insert_link(element.label.element_label, source)
  end
end

#apply_to_StringElement(element) ⇒ Object



103
104
105
# File 'lib/rabbit/theme-browser/rd2document-lib.rb', line 103

def apply_to_StringElement(element)
  element.content.gsub(/\n\s*/, '')
end

#apply_to_TextBlock(element, contents) ⇒ Object



83
84
85
86
87
88
89
90
91
# File 'lib/rabbit/theme-browser/rd2document-lib.rb', line 83

def apply_to_TextBlock(element, contents)
  Proc.new do |*args|
    prefix, = args
    tag("#{prefix}paragraph") do
      insert_children(contents)
      insert("\n")
    end
  end
end

#apply_to_Var(element, contents) ⇒ Object



130
131
132
133
134
135
136
# File 'lib/rabbit/theme-browser/rd2document-lib.rb', line 130

def apply_to_Var(element, contents)
  Proc.new do |*args|
    tag("variable") do
      insert_children(contents)
    end
  end
end

#apply_to_Verbatim(element) ⇒ Object



93
94
95
96
97
98
99
100
101
# File 'lib/rabbit/theme-browser/rd2document-lib.rb', line 93

def apply_to_Verbatim(element)
  content = []
  element.each_line do |line|
    content << line
  end
  content_str = content.join("")
  /\A#\s*([^\n]+)(?:\n)?(?m:(.*)?)\z/ =~ content_str
  apply_to_extension("block_verbatim", $1, $2.to_s, content_str)
end

#insert(text, *args) ⇒ Object



138
139
140
# File 'lib/rabbit/theme-browser/rd2document-lib.rb', line 138

def insert(text, *args)
  @buffer.insert(@iter, text, *args)
end

#insert_children(children, *args) ⇒ Object



158
159
160
161
162
163
164
165
166
# File 'lib/rabbit/theme-browser/rd2document-lib.rb', line 158

def insert_children(children, *args)
  children.each do |child|
    if child.respond_to?(:call)
      child.call(*args)
    else
      insert(child)
    end
  end
end


143
144
145
146
147
148
# File 'lib/rabbit/theme-browser/rd2document-lib.rb', line 143

def insert_link(name, text=nil)
  text ||= _(name)
  tag("theme-link-#{name}") do
    insert(text, "link")
  end
end

#tag(name) ⇒ Object



150
151
152
153
154
155
156
# File 'lib/rabbit/theme-browser/rd2document-lib.rb', line 150

def tag(name)
  start_offset = @iter.offset
  yield
  @buffer.apply_tag(name,
                    @buffer.get_iter_at_offset(start_offset),
                    @iter)
end