Class: Bcat::HeadParser
- Inherits:
-
Object
- Object
- Bcat::HeadParser
- Defined in:
- lib/bcat/html.rb
Overview
Parses HTML until the first displayable body character and provides methods for accessing head and body contents.
Constant Summary collapse
- HEAD_TOKS =
[ /\A(<!DOCTYPE.*?>)/m, /\A(<title.*?>.*?<\/title>)/mi, /\A(<script.*?>.*?<\/script>)/mi, /\A(<style.*?>.*?<\/style>)/mi, /\A(<(?:html|head|meta|link|base).*?>)/mi, /\A(<\/(?:html|head|meta|link|base|script|style|title)>)/mi, /\A(<!--(.*?)-->)/m ]
- BODY_TOKS =
[ /\A[^<]/, /\A<(?!html|head|meta|link|base|script|style|title).*?>/ ]
Instance Attribute Summary collapse
-
#buf ⇒ Object
Returns the value of attribute buf.
Instance Method Summary collapse
-
#body(inject = nil) ⇒ Object
The current body contents.
-
#complete? ⇒ Boolean
Truthy once the first displayed character of the body has arrived.
-
#feed(data) ⇒ Object
Called to parse new data as it arrives.
-
#head ⇒ Object
The head contents without any DOCTYPE, , or
tags. -
#html? ⇒ Boolean
Determine if the input is HTML.
-
#initialize ⇒ HeadParser
constructor
A new instance of HeadParser.
-
#parse(buf = @buf) ⇒ Object
Parses buf into head and body parts.
Constructor Details
#initialize ⇒ HeadParser
Returns a new instance of HeadParser.
8 9 10 11 12 13 |
# File 'lib/bcat/html.rb', line 8 def initialize @buf = '' @head = [] @body = nil @html = nil end |
Instance Attribute Details
#buf ⇒ Object
Returns the value of attribute buf.
6 7 8 |
# File 'lib/bcat/html.rb', line 6 def buf @buf end |
Instance Method Details
#body(inject = nil) ⇒ Object
The current body contents. The
tag is guaranteed to be present. If a was included in the input, it's preserved with original attributes; otherwise, a tag is inserted. The inject argument can be used to insert a string as the immediate descendant of the tag.
49 50 51 52 53 54 55 |
# File 'lib/bcat/html.rb', line 49 def body(inject=nil) if @body =~ /\A\s*(<body.*?>)(.*)/mi [$1, inject, $2].compact.join("\n") else ["<body>", inject, @body].compact.join("\n") end end |
#complete? ⇒ Boolean
Truthy once the first displayed character of the body has arrived.
27 28 29 |
# File 'lib/bcat/html.rb', line 27 def complete? !@body.nil? end |
#feed(data) ⇒ Object
Called to parse new data as it arrives.
16 17 18 19 20 21 22 23 24 |
# File 'lib/bcat/html.rb', line 16 def feed(data) if complete? @body << data else @buf << data parse(@buf) end complete? end |
#head ⇒ Object
The head contents without any DOCTYPE, , or
tags. This should consist of only