Class: Hermeneutics::Tags

Inherits:
Object
  • Object
show all
Defined in:
lib/hermeneutics/tags.rb

Overview

Example

This parses a table and outputs it as a CSV.

t = Tags.compile "<table><tr><td> ... </table>", "iso-8859-15"
t.table.each :tr do |row|
  if row.has? :th then
    l = row.map :th do |h| h.data end.join ";"
  else
    l = row.map :td do |c| c.data end.join ";"
  end
  puts l
end

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(name, attrs = nil, *elems) ⇒ Tags

Returns a new instance of Tags.



238
239
240
241
242
243
# File 'lib/hermeneutics/tags.rb', line 238

def initialize name, attrs = nil, *elems
  @name = name.to_sym if name
  @attrs = {}.update attrs if attrs
  @list = []
  @list.concat elems.flatten
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(sym, *args) ⇒ Object (private)



293
294
295
296
297
# File 'lib/hermeneutics/tags.rb', line 293

def method_missing sym, *args
  (tag sym, *args) or super
rescue
  super
end

Instance Attribute Details

#attrsObject (readonly)

Returns the value of attribute attrs.



236
237
238
# File 'lib/hermeneutics/tags.rb', line 236

def attrs
  @attrs
end

#listObject (readonly)

Returns the value of attribute list.



236
237
238
# File 'lib/hermeneutics/tags.rb', line 236

def list
  @list
end

#nameObject (readonly)

Returns the value of attribute name.



236
237
238
# File 'lib/hermeneutics/tags.rb', line 236

def name
  @name
end

Class Method Details

.compile(str, parser = nil) ⇒ Object



196
197
198
199
200
201
# File 'lib/hermeneutics/tags.rb', line 196

def compile str, parser = nil
  p = (parser||Parser).new str
  enc = p.find_encoding||str.encoding
  l = lex p, enc
  new nil, nil, l
end

.lex(parser, encoding = nil) ⇒ Object



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
# File 'lib/hermeneutics/tags.rb', line 203

def lex parser, encoding = nil
  r = []
  while parser.list.any? do
    e = parser.list.shift
    case e.type
      when :tag
        a = {}
        e.attrs.each { |k,v|
          v.force_encoding encoding if encoding
          a[ k.downcase.to_sym] = Entities.new.decode v
        }
        i = new e.tag, a
        if e.data then
          f = lex e.data, encoding
          i.concat f
        end
        r.push i
      when nil
        d = e.data
        d.force_encoding encoding if encoding
        c = Entities.new.decode d
        r.push c
      when :instr then
      when :comm  then
      when :bang  then
      when :cmd   then
    end
  end
  r
end

Instance Method Details

#concat(elems) ⇒ Object



249
250
251
# File 'lib/hermeneutics/tags.rb', line 249

def concat elems
  @list.concat elems
end

#dataObject



301
302
303
304
305
# File 'lib/hermeneutics/tags.rb', line 301

def data
  d = ""
  gather_data self, d
  d
end

#each(t = nil) ⇒ Object



257
258
259
260
261
262
263
264
265
# File 'lib/hermeneutics/tags.rb', line 257

def each t = nil
  if t then
    @list.each { |e|
      yield e if Tags === e and e.name == t
    }
  else
    @list.each { |e| yield e }
  end
end

#has_tag?(t) ⇒ Boolean Also known as: has?

Returns:

  • (Boolean)


273
274
275
276
277
# File 'lib/hermeneutics/tags.rb', line 273

def has_tag? t
  @list.find { |e|
    Tags === e and e.name == t
  } and true
end

#inspectObject



253
254
255
# File 'lib/hermeneutics/tags.rb', line 253

def inspect
  "<##@name [#{@list.length}]>"
end

#map(t) ⇒ Object



267
268
269
270
271
# File 'lib/hermeneutics/tags.rb', line 267

def map t
  @list.map { |e|
    yield e if Tags === e and e.name == t
  }.compact
end

#push(elem) ⇒ Object



245
246
247
# File 'lib/hermeneutics/tags.rb', line 245

def push elem
  @list.push elem
end

#tag(t, n = nil) ⇒ Object



280
281
282
283
284
285
286
287
288
289
# File 'lib/hermeneutics/tags.rb', line 280

def tag t, n = nil
  n ||= 0
  @list.each { |e|
    if Tags === e and e.name == t then
      return e if n.zero?
      n -= 1
    end
  }
  nil
end