Class: Arrow::HTMLToken

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

Overview

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods inherited from Object

deprecate_class_method, deprecate_method, inherited

Constructor Details

#initialize(raw) ⇒ HTMLToken

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

#rawObject 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

#css_classObject

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

#escape_html(string) ⇒ Object

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

#to_htmlObject

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