Class: JsDuck::Format::HtmlStack

Inherits:
Object
  • Object
show all
Defined in:
lib/jsduck/format/html_stack.rb

Overview

Tracks opening and closing of HTML tags, with the purpose of closing down the unfinished tags.

See Format::Doc#replace for the use of this class.

Instance Method Summary collapse

Constructor Details

#initialize(ignore_html = {}, doc_context = {}) ⇒ HtmlStack

Initializes the stack with two optional parameters:

Parameters:

  • ignore_html (defaults to: {})

    A hash of additional HTML tags that don’t need closing.

  • doc_context (defaults to: {})

    Filename and linenr of the current doc-comment.



16
17
18
19
20
# File 'lib/jsduck/format/html_stack.rb', line 16

def initialize(ignore_html={}, doc_context={})
  @ignore_html = ignore_html
  @doc_context = doc_context
  @open_tags = []
end

Instance Method Details

#close(s) ⇒ Object

Scans a closing tag in HTML using the passed in StringScanner.



28
29
30
31
32
33
34
# File 'lib/jsduck/format/html_stack.rb', line 28

def close(s)
  s.scan(/<\//)
  tag = s.scan(/\w+/)
  s.scan(/>/)

  pop_tags(tag).map {|t| "</#{t}>" }.join
end

#open(s) ⇒ Object

Scans an opening tag in HTML using the passed in StringScanner.



23
24
25
# File 'lib/jsduck/format/html_stack.rb', line 23

def open(s)
  s.scan(/</) + push_tag(s.scan(/\w+/)) + s.scan_until(/>|\Z/)
end

#open?(tag) ⇒ Boolean

True when the tag is currently open.

Returns:

  • (Boolean)


43
44
45
# File 'lib/jsduck/format/html_stack.rb', line 43

def open?(tag)
  @open_tags.include?(tag)
end

#push_tag(tag) ⇒ Object

Registers opening of a tag. Returns the tag.



37
38
39
40
# File 'lib/jsduck/format/html_stack.rb', line 37

def push_tag(tag)
  @open_tags.push(tag) unless void?(tag)
  tag
end