class Schizm::Markup

Attributes

attrs[RW]
elems[RW]
tag[R]

Public Class Methods

new(tag = nil) click to toggle source
# File lib/schizm/markup.rb, line 34
def initialize tag = nil
  @tag = tag
  @attrs = {}
  @elems = []
end

Public Instance Methods

<<(param) click to toggle source

Add element(s).

# File lib/schizm/markup.rb, line 74
def << param
  case param
  when nil
    # Ignore silently.
  when ::Array
    for entry in param
      self << entry
    end
  when ::Integer
    self << '&#' << param.to_s << ';'
  when ::String
    target = @elems.last
    target = @elems unless target.is_a? ::String
    target << param
  else
    @elems << param
  end
  return self
end
[](key) click to toggle source

Attribute accessor.

# File lib/schizm/markup.rb, line 45
def [] key
  return @attrs[key]
end
[]=(key, value) click to toggle source

Attribute accessor.

# File lib/schizm/markup.rb, line 50
def []= key, value
  return @attrs[key] = value
end
add_attrs(pairs) click to toggle source

Add attributes from hash pairs.

# File lib/schizm/markup.rb, line 66
def add_attrs pairs
  pairs.each do |key, value|
    @attrs[key] = value 
  end
  return self
end
add_elems(param) click to toggle source

Wrapper for +self << param+.

# File lib/schizm/markup.rb, line 95
def add_elems param
  return self << param
end
find_first_of(*tags, &block) click to toggle source
# File lib/schizm/markup.rb, line 113
def find_first_of *tags, &block
  if @tag and tags.include? @tag.to_s
    return self if not block or (block.call(self) == true)
  end
  for elem in @elems
      temp = elem.find_first_of *tags, &block if elem.is_a? Markup
      return temp if temp
  end
  nil
end
get_attr(name) click to toggle source

Attribute name or nil if name does not exist.

# File lib/schizm/markup.rb, line 55
def get_attr name
  return @attrs[name] if @attrs.has_key? name
  return nil
end
guess_description() click to toggle source
# File lib/schizm/markup.rb, line 173
def guess_description
  elem = find_first_of "p"
  text = elem.to_plain_text if elem
  return "description: \"#{text.truncate 80}\"" if text
  return "description: \"\""
end
guess_parts() click to toggle source
# File lib/schizm/markup.rb, line 180
def guess_parts
  tab = "  "
  parts = "parts:\n"
  yield_each "h2" do |elem|
    if elem.get_attr("class") == "title" and
       elem.get_attr("id") != nil
      parts.concat "#{tab}-\n"
      parts.concat "#{tab}#{tab}id: \"#{elem.get_attr "id"}\"\n"
      parts.concat "#{tab}#{tab}title: \"#{elem.to_plain_text}\"\n"
    end
  end
  parts
end
guess_title() click to toggle source
# File lib/schizm/markup.rb, line 165
def guess_title
  elem = find_first_of "h1" do |temp|
    true if temp.has_attr? "class" and temp["class"] == "title"
  end
  return "title: \"#{elem.to_plain_text}\"" if elem
  return "title: \"\""
end
has_attr?(name) click to toggle source

Wrapper for +@attrs.has_key?+.

# File lib/schizm/markup.rb, line 61
def has_attr? name 
  return @attrs.has_key? name
end
last() click to toggle source

Wrapper for +@elems.last+.

# File lib/schizm/markup.rb, line 100
def last
  return @elems.last
end
to_plain_text() click to toggle source
# File lib/schizm/markup.rb, line 124
def to_plain_text
  text = String.new
  @elems.each do |elem|
    case elem
    when Markup 
      if elem.tag == "script" and 
        (elem["type"] == "math/tex; mode=inline" or
         elem["type"] == "math/tex; mode=display")
        temp = elem.elems[0].dump
        text << String.new(temp[1..(temp.size-2)]).html
      else
        text << elem.to_plain_text
      end
    else
      text << elem.to_s if elem.respond_to? :to_s
    end
  end
  text
end
to_s() click to toggle source
# File lib/schizm/markup.rb, line 144
def to_s
  s = String.new
  unless @tag == nil
    s << "<" << @tag
    @attrs.each do |k, v|
      if k and k.to_s != ""
        s << " #{k.to_s}" if v == nil
        s << " #{k.to_s}=\"#{v.to_s}\"" if v != nil and v.to_s != ""
      end
    end
    s << ">"
  end
  @elems.each do |elem|
    s << elem.to_s if elem.respond_to? :to_s
  end
  unless @tag == nil
    s << "</" << @tag << ">" 
  end
  s
end
yield_each(*tags) { |self| ... } click to toggle source
# File lib/schizm/markup.rb, line 104
def yield_each *tags, &block
  if @tag and tags.include? @tag.to_s
    yield self
  end
  for elem in @elems
    elem.yield_each *tags, &block if elem.is_a? Markup
  end
end