Class TagTreeScanner::TagFactory
In: TagTreeScanner.rb
Parent: Object

A TagFactory holds the information about a specific kind of tag:

  • the name of the tag
  • what to look for to open and close the tag
  • what genre of tags it may contain
  • whether the tag permits raw text
  • additional code to run when creating the tag

See the documentation about the @tag_genres hash inside the TagTreeScanner class for information on how to add factories for use.

Utilizing :autoclose

Occasionally you will want to create a tag and allow no other tags inside it. An example might be a tag containing preformatted code.

Rather than opening the tag and slowly spinning through all the text, the combination of the :autoclose and :setup options allow you to create the tag, fill it with content, and then immediately continute with the parent tag.

See the new method for how to use the :setup function, and an example usage.

Methods

new  

Attributes

allowed_genre  [RW]  A symbol with the genre of tags that are allowed inside the tag. (See @tag_genres in the TagTreeScanner documentation.)
allows_text  [RW]  May tags created by this factory have text added to them?
autoclose  [RW]  Should this tag stay open when created, or automatically close?
close_match  [RW]  The regexp which causes the tag to automatically close.
close_requires_bol  [RW]  Does the open_match regexp require beginning of line?
open_match  [RW]  A regexp to match (and consume) that causes a new tag to be started.
open_requires_bol  [RW]  Does the open_match regexp require beginning of line?
tag_name  [RW]  The type of tag this factory produces.

Public Class methods

tag_name:A symbol with the name of the tag to create
options:A hash including one or more of :open_match,

:open_requires_bol, :close_match, :close_requires_bol, :autoclose, :allows_text, :allowed_genre, and :setup.

Due to the way the StringScanner class works, placing a ^ (beginning of line) marker in your :open_match or :close_match regular expressions will not behave as desired. Instead, set the :open_requires_bol and/or :close_requires_bol properties to true if desired.

A factory should either be set to :autoclose => true, or supply a :close_match. (Otherwise, it will never close.)

Further, a factory should either be set to :autoclose => true or specify an :allowed_genre. (See below for how to efficiently create a tag that cannot contain other tags.)

The :setup option is used to run code during the tag creation. The value of this option should be a lambda/Proc that accepts three parameters:

  • the Tag being created
  • the StringScanner instance that matched the tag opening
  • the TagTreeScanner instance creating the tag.

Example:

 # Shove URLs as HTML anchors, without the protocol prefix shown
 @tag_genres[ :inline ] << TagFactory.new( :a,
   :open_match => %r{http://(\S+)},
   :setup => lambda{ |tag, ss, tagtree|
     tag.attributes[ :href ] = ss[0]
     tag << ss[1]
   },
   :autoclose => true
 )

[Validate]