class HDSL

Attributes

result[R]

Public Class Methods

new(*args, &block) click to toggle source
# File lib/hdsl.rb, line 3
def initialize(*args, &block)
  instance_eval(&block)
  if args.length == 1
    self.save args.first
  end
end

Public Instance Methods

par(*args, &block) click to toggle source

BEGIN special tags p is shorthand for “puts”, so this must be used instead

# File lib/hdsl.rb, line 21
def par(*args, &block)
  tagstart 'p'
  do_content *args, &block
  tagend 'p'
end
save(filename) click to toggle source
# File lib/hdsl.rb, line 10
def save(filename)
  file = File.open(filename, 'w')
  file.write @result
  rescue IOError => e
    puts "Could not open file for writing."
  ensure
    file.close unless file.nil?
end
stylesheet(href) click to toggle source
# File lib/hdsl.rb, line 27
def stylesheet(href)
  @result ||= ''
  @result << "<link rel=\"stylesheet\" type=\"text/css\" href=\"#{href}\">"
end

Private Instance Methods

do_content(*args, &block) click to toggle source
# File lib/hdsl.rb, line 53
def do_content *args, &block
  content = args.first
  if block_given?
    instance_eval(&block)
  else
    @result << content unless !content
  end
end
method_missing(name, *args, &block) click to toggle source
# File lib/hdsl.rb, line 66
def method_missing(name, *args, &block)
  tag = name.to_s

  tagstart tag
  do_content *args, &block
  tagend tag
end
tagend(tagname) click to toggle source
# File lib/hdsl.rb, line 62
def tagend tagname
  @result << "</#{tagname}>"
end
tagstart(tagname) click to toggle source
# File lib/hdsl.rb, line 48
def tagstart tagname
  @result ||= ''
  @result << "<#{tagname}>"
end
with_attr(*args, &block) click to toggle source

END special tags

# File lib/hdsl.rb, line 34
def with_attr *args, &block
  if block_given?
    tagname, *attrs = *args
  else
    tagname, *attrs, content = *args
  end

  content = '' if !content

  tagstart tagname + ' ' + attrs.join(' ')
  do_content content, &block
  tagend tagname
end