Class: Elisp::Sidebar

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

Instance Method Summary collapse

Constructor Details

#initialize(headings) ⇒ Sidebar

Returns a new instance of Sidebar.



14
15
16
# File 'lib/elisp.rb', line 14

def initialize(headings)
  @headings = headings
end

Instance Method Details

#htmlObject



18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
# File 'lib/elisp.rb', line 18

def html
  rest = @headings.dup
  result = +""
  while (head = rest.shift)
    level = head.level
    subheadings = []
    while (heading = rest.shift)
      if heading.level > level
        subheadings << heading
      else
        rest.unshift(heading)
        break
      end
    end
    sub_sidebar =
      subheadings.empty? ? nil
        : (html = self.class.new(subheadings).html
          "<details open>#{html}</details>")
    content = CGI.escape_html(head.content)
    result << <<~END_HTML
      <li>
        <a href="##{head.html_id}">#{content}</a>
        #{sub_sidebar}
      </li>
    END_HTML
  end
  "<ul>#{result}</ul>"
end