Class: SGML

Inherits:
Tree show all
Defined in:
lib/rwd/sgml.rb

Direct Known Subclasses

XML

Instance Attribute Summary

Attributes inherited from Tree

#children, #data, #parent, #visible

Instance Method Summary collapse

Methods inherited from Tree

#buildchildren, #buildparents, #deletedummies, file, #inspect, location, #markclosed, new_from_cache, new_from_cache2, #parse, #path

Methods included from ParseTree

#parsetree

Methods included from TextArray

#textarray

Constructor Details

#initialize(*args) ⇒ SGML

Returns a new instance of SGML.



119
120
121
122
123
# File 'lib/rwd/sgml.rb', line 119

def initialize(*args)
  @tagcache	= {}

  super
end

Instance Method Details

#buildobjects(string) ⇒ Object



125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
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
# File 'lib/rwd/sgml.rb', line 125

def buildobjects(string)
  @objects = []

  verwerk3	=
  lambda do |string|
    a	= []

    string[1..-2].splitblocks(["'", "'"], ['"', '"']).collect do |type, s|
      case type
      when 0
        if self.class.to_s == "HTML"
          s.splitwords.each do |w|
            d	= w.split("=", 2)

            if d.length == 1
              a << d[0]
            else
              a << d[0]	if not d[0].nil? and not d[0].empty?
              a << "="
              a << d[1]	if not d[1].nil? and not d[1].empty?
            end
          end
        else
          a.concat s.splitwords(["/", "="])
        end
      when 1, 2	then a << s
      end
    end

    open	= false
    close	= false

    a = a[0].splitwords("/") + a[1..-1]

    if a[0] == "/"
      close	= true
      a.shift
    else
      open	= true
    end

    if a[-1] == "/"
      close	= true
      a.pop
    end

    tag	= a.shift.downcase
    args	= {}

    while not a.length.zero?
      if a.length >= 3 and a[1] == "="
        key	= a.shift.downcase
        dummy	= a.shift
        value	= a.shift.noquotes
        args[key]	= value
      else
        key	= a.shift.downcase
        args[key]	= ""
      end
    end

    [tag, args, open, close]
  end

  verwerk2	=
  lambda do |string|
    if @tagcache.include? string
      res	= @tagcache[string]
    else
      res	= verwerk3.call(string)

      @tagcache[string] = res
    end

    res
  end

  verwerk1 =
  lambda do |string|
    tag, args, open, close	= verwerk2.call(string)

    @objects << OpenTag.new(tag.dup, args.dup)	if open
    @objects << CloseTag.new(tag.dup, args.dup)	if close
  end

  string.splitblocks(["<!--", "-->"], ["<!", ">"], ["<?", "?>"], ["<", ">"]).each do |type, s|
    case type
    when 0		then @objects << Text.new(s)
    when 1		then @objects << Comment.new(s)
    when 2		then @objects << Special.new(s)
    when 3		then @objects << Instruction.new(s)
    when 4		then verwerk1.call(s)
    end
  end
end

#to_hObject



229
230
231
232
233
234
235
# File 'lib/rwd/sgml.rb', line 229

def to_h
  res	= ""

  parsetree("prechildren_to_sgml", "postchildren_to_sgml", res)

  res
end

#to_sObject



221
222
223
224
225
226
227
# File 'lib/rwd/sgml.rb', line 221

def to_s
  res	= ""

  parsetree("prechildren_to_s", "postchildren_to_s", res)

  res
end