Class: HTree::GenCode

Inherits:
Object
  • Object
show all
Defined in:
lib/htree/gencode.rb

Defined Under Namespace

Classes: CDATABuffer

Constant Summary collapse

ChRef =
{
  '&' => '&',
  '>' => '>',
  '<' => '&lt;',
  '"' => '&quot;',
}

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(outvar, contextvar, internal_encoding = Encoder.internal_charset) ⇒ GenCode

Returns a new instance of GenCode.



19
20
21
22
23
24
25
26
27
# File 'lib/htree/gencode.rb', line 19

def initialize(outvar, contextvar, internal_encoding=Encoder.internal_charset)
  @outvar = outvar
  @contextvar = contextvar
  @state = :none
  @buffer = ''
  @internal_encoding = internal_encoding
  @code = ''
  @html_output = nil
end

Instance Attribute Details

#contextvarObject (readonly)

Returns the value of attribute contextvar.



28
29
30
# File 'lib/htree/gencode.rb', line 28

def contextvar
  @contextvar
end

#outvarObject (readonly)

Returns the value of attribute outvar.



28
29
30
# File 'lib/htree/gencode.rb', line 28

def outvar
  @outvar
end

Instance Method Details

#finishObject



186
187
188
189
# File 'lib/htree/gencode.rb', line 186

def finish
  flush_buffer
  @code
end

#flush_bufferObject



174
175
176
177
178
179
180
181
182
183
184
# File 'lib/htree/gencode.rb', line 174

def flush_buffer
  return if @buffer.empty?
  case @state
  when :string
    @code << "#{@outvar}.output_string #{@buffer.dump}\n"
    @buffer = ''
  when :text
    @code << "#{@outvar}.output_text #{@buffer.dump}\n"
    @buffer = ''
  end
end

#html_output=(flag) ⇒ Object



34
35
36
# File 'lib/htree/gencode.rb', line 34

def html_output=(flag)
  @html_output = flag
end

#html_output?Boolean

Returns:

  • (Boolean)


30
31
32
# File 'lib/htree/gencode.rb', line 30

def html_output?
  @html_output
end

#output_cdata_content(content, context) ⇒ Object



89
90
91
92
93
94
95
96
97
# File 'lib/htree/gencode.rb', line 89

def output_cdata_content(content, context)
  tmp_outvar = @outvar + '_tmp'
  output_logic_line "#{@outvar} = #{@outvar}.output_cdata_content_do(#{@outvar},"
  output_logic_line "lambda { #{@outvar} = HTree::GenCode::CDATABuffer.new },"
  output_logic_line "lambda {"
  content.each {|n| n.output(self, context) }
  output_logic_line "},"
  output_logic_line "lambda {|#{tmp_outvar}| #{tmp_outvar}.output_string(#{@outvar}.result) })"
end

#output_dynamic_attvalue(expr) ⇒ Object



113
114
115
116
# File 'lib/htree/gencode.rb', line 113

def output_dynamic_attvalue(expr)
  flush_buffer
  @code << "#{@outvar}.output_dynamic_attvalue((#{expr}))\n"
end

#output_dynamic_text(expr) ⇒ Object



103
104
105
106
# File 'lib/htree/gencode.rb', line 103

def output_dynamic_text(expr)
  flush_buffer
  @code << "#{@outvar}.output_dynamic_text((#{expr}))\n"
end

#output_dynamic_tree(expr, context_expr) ⇒ Object



108
109
110
111
# File 'lib/htree/gencode.rb', line 108

def output_dynamic_tree(expr, context_expr)
  flush_buffer
  @code << "(#{expr}).output(#{@outvar}, #{context_expr})\n"
end

#output_logic_line(line) ⇒ Object



118
119
120
121
# File 'lib/htree/gencode.rb', line 118

def output_logic_line(line)
  flush_buffer
  @code << line << "\n"
end

#output_slash_if_xmlObject



99
100
101
# File 'lib/htree/gencode.rb', line 99

def output_slash_if_xml
  output_logic_line "#{@outvar}.output_slash_if_xml"
end

#output_string(str) ⇒ Object



123
124
125
126
127
128
129
130
# File 'lib/htree/gencode.rb', line 123

def output_string(str)
  return if str.empty?
  if @state != :string
    flush_buffer
    @state = :string
  end
  @buffer << str
end

#output_text(str) ⇒ Object



132
133
134
135
136
137
138
139
140
141
142
143
144
145
# File 'lib/htree/gencode.rb', line 132

def output_text(str)
  return if str.empty?
  if /\A[\s\x21-\x7e]+\z/ =~ str && @state == :string
    # Assumption: external charset can represent white spaces and
    # ASCII printable.
    output_string(str)
    return 
  end
  if @state != :text
    flush_buffer
    @state = :text
  end
  @buffer << str
end

#output_xmlns(namespaces) ⇒ Object



153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
# File 'lib/htree/gencode.rb', line 153

def output_xmlns(namespaces)
  unless namespaces.empty?
    flush_buffer
    namespaces.each {|k, v|
      if k
        ks = k.dump
        aname = "xmlns:#{k}"
      else
        ks = "nil"
        aname = "xmlns"
      end
      @code << "if #{@contextvar}.namespace_uri(#{ks}) != #{v.dump}\n"
      output_string " #{aname}=\""
      output_text v.gsub(/[&<>"]/) {|s| ChRef[s] }
      output_string '"'
      flush_buffer
      @code << "end\n"
    }
  end
end