Class: Arrow::HTMLTag
Overview
Class for tokens output by Arrow::HTMLTokenizer for the tags in an HTML document.
Constant Summary
- AttributePattern =
The pattern for matching tag attribute key-value pairs
%r{ \s*([-A-Za-z:]+) (?:\s*=\s*( "(?:[^"]|\\.)*" | # Match strings quoted with " '(?:[^']|\\.)*' | # Match strings quoted with ' \S+ # Match non-whitespace ))? }mx
Instance Attribute Summary
-
- (Object) tagname
readonly
The name of the tag.
Instance Method Summary
-
- (Object) [](name)
Return the tag attribute with the specified name (if it exists).
-
- (Object) attrs
Return the Hash of tag attributes belonging to this token.
-
- (Boolean) endtag?
Returns true if this tag is an closing tag.
-
- (Object) escape_html(string)
Escape special characters in the given string for display in an HTML inspection interface.
-
- (HTMLTag) initialize(raw)
constructor
Create a new HTMLTag from the specified raw source.
-
- (Object) to_html
Return an HTML fragment that can be used to represent the token symbolically in a web-based introspection interface.
Methods inherited from HTMLToken
Methods inherited from Object
deprecate_class_method, deprecate_method, inherited
Methods included from Loggable
Constructor Details
- (HTMLTag) initialize(raw)
Create a new HTMLTag from the specified raw source.
216 217 218 219 220 221 222 223 224 225 226 227 228 |
# File 'lib/arrow/htmltokenizer.rb', line 216 def initialize( raw ) unless (( match = /<\s*(\/)?(\w+)\s*([^>]*)>/.match(raw) )) raise ArgumentError, "Malformed HTMLTag: %p" % raw end @endtag = !match[1].nil? @tagname = match[2] @rawattrs = match[3] || '' @attrs = nil super end |
Instance Attribute Details
- (Object) tagname (readonly)
The name of the tag
236 237 238 |
# File 'lib/arrow/htmltokenizer.rb', line 236 def tagname @tagname end |
Instance Method Details
- (Object) [](name)
Return the tag attribute with the specified name (if it exists).
269 270 271 |
# File 'lib/arrow/htmltokenizer.rb', line 269 def []( name ) self.attrs[ name.gsub(/-/, '_').downcase.to_sym ] end |
- (Object) attrs
Return the Hash of tag attributes belonging to this token.
243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 |
# File 'lib/arrow/htmltokenizer.rb', line 243 def attrs unless @attrs @attrs = {} @rawattrs.scan( AttributePattern ) {|name,value| ns = nil if /:/ =~ name ns, name = name.split(/:/, 2) if ns == 'html' then ns = nil end end cname = name.gsub(/-/, '_').downcase cval = value.nil? ? true : value.gsub(/^["']|['"]$/, '') if ns.nil? @attrs[ cname.to_sym ] = cval else @attrs[ ns.to_sym ] ||= {} @attrs[ ns.to_sym ][ name.to_sym ] = cval end } end return @attrs end |
- (Boolean) endtag?
Returns true if this tag is an closing tag
239 |
# File 'lib/arrow/htmltokenizer.rb', line 239 def endtag?; @endtag end |
- (Object) escape_html(string)
Escape special characters in the given string for display in an HTML inspection interface.
312 313 314 315 316 317 318 319 |
# File 'lib/arrow/htmltokenizer.rb', line 312 def escape_html( string ) return "nil" if string.nil? string = string.inspect unless string.is_a?( String ) string. gsub(/&/, '&'). gsub(/</, '<'). gsub(/>/, '>') end |
- (Object) to_html
Return an HTML fragment that can be used to represent the token symbolically in a web-based introspection interface.
276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 |
# File 'lib/arrow/htmltokenizer.rb', line 276 def to_html tagopen, tagbody = @raw.split( /\s+/, 2 ) # self.log.debug "tagopen = %p, tagbody = %p" % [ tagopen, tagbody ] tagopen = self.escape_html( tagopen ).sub( %r{^<(/)?(\w+)} ) {|match| %Q{<#$1<span class="tag-token-name">#$2</span>} } unless tagbody.nil? tagbody.sub!( />$/, '' ) tagbody = self.escape_html( tagbody ).gsub( AttributePattern ) {|match| name, mid, val = match.split(/(\s*=\s*)/, 2) val.gsub!( /(\[\?(?:[^\?]|\?(?!\]))+\?\])/s ) {|m| %q{<span class="%s">%s</span>} % [ 'tag-attr-directive', m ] } %q{<span class="%s">%s</span>%s<span class="%s">%s</span>} % [ 'tag-token-attr-name', name, mid, 'tag-token-attr-value', val, ] } tagbody << '>' end #self.log.debug "tagopen = %p; tagbody = %p" % # [ tagopen, tagbody ] super { [tagopen, tagbody].compact.join(" ") } end |