Class: BayeuxTextGen

Inherits:
Object
  • Object
show all
Defined in:
lib/bayeux/text_gen.rb

Instance Method Summary collapse

Constructor Details

#initialize(syntax_tree) ⇒ BayeuxTextGen

Default Constructor



6
7
8
9
10
# File 'lib/bayeux/text_gen.rb', line 6

def initialize(syntax_tree)
  @syntax_tree = syntax_tree
  @text_string = String.new
  @text = String.new
end

Instance Method Details

#generateObject

Return the AST as an HTML string (stored in @text_string)



13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
# File 'lib/bayeux/text_gen.rb', line 13

def generate
  
  # Create a walker for the trees in the forest
  walker = TreeWalker.new
  
  walker.on_before_down = self.method(:generate_node_start)
  walker.on_after_up = self.method(:generate_node_end)
  
  walker.on_no_siblings = self.method(:generate_full_node)
  walker.on_no_children = self.method(:generate_full_node)
      
  # Clear the internal HTML representation
  @text_string.clear
  
  # Walk the forest
  @syntax_tree.block_forest.each{|tree|
    walker.walk_tree(tree)
  }

  return @text_string 
end

#generate_full_node(block) ⇒ Object

Output the full contents of the node, properly bracketed as an HTML expression. This is only called if we have no sub-nodes to deal with



43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
# File 'lib/bayeux/text_gen.rb', line 43

def generate_full_node(block)
  
  # Check if we need to add a space
  # Check if we need to add a space
  if not block.content[0] =~ /\s[:;.,?!]/
    @text_string << " "
  end
              
  case block.type
    
    # Headers
    when :h1, :h2, :h3, :h4, :h5, :h6
      @text_string << "#{block.content.strip}\n"
    
    # Ordinary paragraphs
    when :paragraph
      @text_string << "#{block.content.word_wrap(70)}\n\n"

    when :none
      @text_string << "#{block.content.word_wrap(70)}"
      
    # Special paragraphs
    when :block_quote
      @text_string << "#{block.content.word_wrap(60).indent(5)}\n\n"
    when :single_quote
      @text_string << "\'#{block.content}\'"
    when :double_quote
      @text_string << "\"#{block.content}\""
      
    when :note
      @text_string << "Note: #{block.content}\n\n"
      
    when :command
      @text_string << "#{block.content.strip.indent(5)}\n\n"
      
    when :code_language
      @code_language = block.content
    when :code_start_number
      @code_start_number = block.content  
      
    when :file
      @text_string << "#{block.content}\n\n"
    when :output
      70.downto(0){|index|
        @text_string << "-"
      }        
      @text_string << "\n#{block.content.strip}\n"
      70.downto(0){|index|
        @text_string << "-"
      }
      @text_string << "\n\n"
      
    # Special Characters
    when :em_dash
      @text_string << " - "
    when :en_dash
      @text_string << "-"
    when :elipses
      @text_string << "..."
    when :elipses_stop
      @text_string << "...."
      
    # Links
    when :link_target
      @link_target = block.content
    when :link_text
      @link_text = block.content
      
    # Lists
    when :item
      @text_string << " * #{block.content}"
    
    when :dl_header
      @text_string << "#{block.content}: "
    when :dl_text
      @text_string << "#{block.content}"
            
    # Tags
    when :ac
      # Do we know anything about this acronymn?
      if $reference.acronym_list.include?(block.content) then
        
        # Assemble from the reference list        
        if $reference.acronym_list[block.content].include?('text_text') then
          acronym = $reference.acronym_list[block.content]['text_text']
        else
          acronym = $reference.acronym_list[block.content]['text']
        end
        
        definition = $reference.acronym_list[block.content]['def']
        
        @text_string << "#{acronym}"
      else
        # Do what we can
        @text_string << "#{block.content}"
      end
          
    when :emph 
      @text_string << "#{block.content}"
    
    when :tt
      @text_string << "#{block.content}"
      
  end
end

#generate_node_end(block) ⇒ Object



243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
# File 'lib/bayeux/text_gen.rb', line 243

def generate_node_end(block)
      
  case block.type
    
    # Headers
    when :h1, :h2, :h3, :h4, :h5, :h6
      @text_string << @text.word_wrap(70)
      @text = ""
    
    # Ordinary paragraphs
    when :paragraph
      @text_string << @text.indent(2).word_wrap(70) + "\n"
      @text = ""
    
    # Special paragraphs
    when :block_quote
      @text_string << @text.indent(5).word_wrap(60) + "\n"
      @text = ""
    when :single_quote
      @text_string << "'"
    when :double_quote
      @text_string << "\""
      
    when :note
      @text_string << @text.indent(2).word_wrap(70) + "\n"
      @text = ""
      
    # Special Characters
    when :em_dash
      @text << " - "
    when :en_dash
      @text << "-"
    when :elipses
      @text << "..."
    when :elipses_stop
      @text << "...."
      
    # Links
    when :link
      # Assemble the link from the sub-tree nodes
      @text << "#{@link_text}"
    when :link_target
      @link_target = block.content
    when :link_text
      @link_text = block.content
        
    # Lists
    when :item
      @text_string << @text.indent(2).word_wrap(70) + "\n" 
      @text = ""
    
    when :dl
      @text_string << "\n" 
      @text = ""
    when :dl_text
      @text_string << @text.indent(2).word_wrap(70) + "\n" 
      @text = ""
  end
end

#generate_node_start(block) ⇒ Object

Output only the start of a node



150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
# File 'lib/bayeux/text_gen.rb', line 150

def generate_node_start(block)
  
  # Check if we need to add a space
  if not block.content[0] =~ /\s[:;.,?!]/
    @text << " "
  end
  
  case block.type
    
    # Headers
    when :h1, :h2, :h3, :h4, :h5, :h6
      @text_string << "#{block.content.strip}"
    
    # Ordinary paragraphs
    when :paragraph
      @text << "#{block.content}"
      
    when :none
      @text << "#{block.content}"
      
    # Special paragraphs
    when :block_quote
      @text << "#{block.content}"
    when :single_quote
      @text << "'#{block.content}"
    when :double_quote
      @text << "\"#{block.content}"
      
    when :note
      @text << "Note: #{block.content}"
      
    when :command
      @text_string << "#{block.content.lstrip.indent(5)}\n"
      
    when :code_language
      @code_language = block.content
    when :code_start_number
      @code_start_number = block.content
      
    # Special Characters
    when :em_dash
      @text << " - "
    when :en_dash
      @text << "-"
    when :elipses
      @text << "..."
    when :elipses_stop
      @text << "...."
      
    # Links
    when :link_target
      @link_target = block.content
    when :link_text
      @link_text = block.content
      
    # Lists
    when :item
      @text << " * #{block.content}"
    
    when :dl_header
      @text << "#{block.content}: "
    when :dl_text
      @text << "#{block.content}"
    
    # Tags        
    when :ac
      # Do we know anything about this acronymn?
      if $reference.acronym_list.include?(block.content) then
      
        # Assemble from the reference list        
        if $reference.acronym_list[block.content].include?('text_text') then
          acronym = $reference.acronym_list[block.content]['text_text']
        else
          acronym = $reference.acronym_list[block.content]['text']
        end
      
        definition = $reference.acronym_list[block.content]['def']
      
        @text_string << "#{acronym}"
      else
        # Do what we can
        @text_string << "#{block.content}"
      end
              
    when :emph 
      @text << "#{block.content}"
      
    when :tt
      @text << "#{block.content}"
      
  end
end