Class: Arrow::HTMLToken
Overview
Base class for HTML tokens output by Arrow::HTMLTokenizer.
Direct Known Subclasses
DocType, HTMLComment, HTMLTag, HTMLText, ProcessingInstruction
Instance Attribute Summary collapse
-
#raw ⇒ Object
(also: #to_s)
The raw source of the token.
Instance Method Summary collapse
-
#css_class ⇒ Object
Return the HTML element class attribute that corresponds to this node.
-
#escape_html(string) ⇒ Object
Escape special characters in the given
string
for display in an HTML inspection interface. -
#initialize(raw) ⇒ HTMLToken
constructor
Initialize a token with the
raw
source of it. -
#to_html ⇒ Object
Return an HTML fragment that can be used to represent the token symbolically in a web-based introspection interface.
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
#raw ⇒ Object 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_class ⇒ Object
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(/&/, '&'). gsub(/</, '<'). gsub(/>/, '>'). gsub(/\r?\n/, %Q{<br />\n}). gsub(/\t/, ' ') end |
#to_html ⇒ Object
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 |