SiteGenerator_Tag.rb

Path: SiteGenerator_Tag.rb
Last Update: Mon Mar 28 19:25:59 Westeuropäische Sommerzeit 2005

SiteGeneratorTag.rb

Based on Version 1.0.4 of Tag.rb from MoonWolf <moonwolf-ruby@moonwolf.com>. www.moonwolf.com/ruby/tag.html raa.ruby-lang.org/project/tag/

I missed some features of the tag.rb-code. So I extended the Tag-class of the original tag.rb, later I rewrote it.

Changes:

  • Allows usage of symbols instead strings for tagname and attributes.
  • Check tagname and attribute (don’t allow unknown Tags).
  • Delete minor bugs

Example of usage:

  require "tag"
  doc = []
  html = Tag('html')                #<html>
  doc << html                       #doc
  html << head = Tag('head')        #html
  head << (Tag('title') << "TITLE") #head
  html << body = Tag('body',{'bgcolor'=>'#FFFFFF'})
                                    #<body bgcolor="#FFFFFF">
  print doc.to_s                    #HTML

Result is:

  <html><head><title>TITLE</title></head><body bgcolor="#FFFFFF"></body></html>

Example2:

  img = Tag('img/')
  img['src']='xxx.png'  #<img src="xxx.png">

  o = Tag('option')
  o['selected']=true    #<option selected>

Differences between tag.rb and WebSiteTag.rb:

  • changed and extended documentation

-

 This part of teh Sitegenerator should be replaced by DocGenerator.

Required files

cgi  

Methods

Tag  

Classes and Modules

Class Tag

Public Instance methods

Create a Tag directly without a new. This feature allows also the usage of a block.

Usage:

       Tag(tagname [, attr [, item]])
       Tag(tagname [, attr [, item]]) {...}
  • tagname: The Tag. Can be a string or an symbol. If it is a symbol, the symbol is checked.
  • attr: A Hash, containing all attributes.
  • item: (list) of the content of the Tag. Can be defined also in th block.

Example:

       Tag('a', {'href'=> 'url'} )
       Tag('a', {'href'=> 'url'}, 'content' )
       Tag('a', {'href'=> 'url'} ){'content' }
       Tag('a', {'href'=> 'url'}, ['x'] ){'content' }

returns:

       <a href="url"></a>
       <a href="url">content</a>
       <a href="url">content</a>
       <a href="url">xcontent</a>

If a tag without content is required (e.g. img has only attributes, or br) the tagname must contain a ’/’ in the end. Example:

       Tag('br/')

[Validate]