Class: Arrow::HTMLToken

Inherits:
Object
  • Object
show all
Defined in:
lib/arrow/htmltokenizer.rb

Overview

Base class for HTML tokens output by Arrow::HTMLTokenizer.

Direct Known Subclasses

DocType, HTMLComment, HTMLTag, HTMLText, ProcessingInstruction

Instance Attribute Summary

Instance Method Summary

Methods inherited from Object

deprecate_class_method, deprecate_method, inherited

Methods included from Loggable

#log

Constructor Details

- (HTMLToken) initialize(raw)

Initialize a token with the raw source of it.



100
101
102
103
# File 'lib/arrow/htmltokenizer.rb', line 100

def initialize( raw ) # :notnew:
  super()
  @raw = raw
end

Instance Attribute Details

- (Object) raw Also known as: to_s

The raw source of the token



106
107
108
# File 'lib/arrow/htmltokenizer.rb', line 106

def raw
  @raw
end

Instance Method Details

- (Object) css_class

Return the HTML element class attribute that corresponds to this node.



131
132
133
134
135
136
137
138
139
140
141
# File 'lib/arrow/htmltokenizer.rb', line 131

def css_class
  tokenclass = self.class.name.
    sub( /Arrow::(HTML)?/i, '').
    gsub( /::/, '-' ).
    gsub( /([a-z])([A-Z])/, "\\1-\\2" ).
    gsub( /[^-\w]+/, '' ).
    downcase
  tokenclass << "-token" unless /-token$/.match( tokenclass )

  return tokenclass
end

- (Object) escape_html(string)

Escape special characters in the given string for display in an HTML inspection interface. This escapes common invisible characters like tabs and carriage-returns in additional to the regular HTML escapes.



148
149
150
151
152
153
154
155
156
157
# File 'lib/arrow/htmltokenizer.rb', line 148

def escape_html( string )
  return "nil" if string.nil?
  string = string.inspect unless string.is_a?( String )
  string.
    gsub(/&/, '&amp;').
    gsub(/</, '&lt;').
    gsub(/>/, '&gt;').
    gsub(/\r?\n/, %Q{<br />\n}).
    gsub(/\t/, '&nbsp;&nbsp;&nbsp;&nbsp;')
end

- (Object) to_html

Return an HTML fragment that can be used to represent the token symbolically in a web-based introspection interface.



111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
# File 'lib/arrow/htmltokenizer.rb', line 111

def to_html
  content = nil

  if block_given? 
    content = yield
    # self.log.debug "content = %p" % content
  else
    content = self.escape_html( @raw )
  end

  tokenclass = self.css_class

  %q{<span class="token %s">%s</span>} % [
    tokenclass,
    content,
  ]
end