Class: Utopia::Project::Sidebar
- Inherits:
-
Object
- Object
- Utopia::Project::Sidebar
- Defined in:
- lib/utopia/project/sidebar.rb
Overview
Generates a sidebar navigation from markdown document headings.
Defined Under Namespace
Classes: Entry
Instance Attribute Summary collapse
-
#entries ⇒ Object
readonly
The navigation entries.
Class Method Summary collapse
-
.build(document) ⇒ Object
Build a sidebar from a markdown document by extracting headings.
Instance Method Summary collapse
-
#any? ⇒ Boolean
Check if there are any navigation entries.
-
#initialize(entries) ⇒ Sidebar
constructor
Initialize with an array of entries.
-
#to_html(sidebar: false, title: "Table of Contents") ⇒ Object
Generate HTML markup for the sidebar navigation.
Constructor Details
#initialize(entries) ⇒ Sidebar
Initialize with an array of entries.
43 44 45 |
# File 'lib/utopia/project/sidebar.rb', line 43 def initialize(entries) @entries = entries end |
Instance Attribute Details
#entries ⇒ Object (readonly)
The navigation entries.
49 50 51 |
# File 'lib/utopia/project/sidebar.rb', line 49 def entries @entries end |
Class Method Details
.build(document) ⇒ Object
Build a sidebar from a markdown document by extracting headings.
36 37 38 39 |
# File 'lib/utopia/project/sidebar.rb', line 36 def self.build(document) entries = extract_headings_from_document(document) new(entries) end |
Instance Method Details
#any? ⇒ Boolean
Check if there are any navigation entries.
53 54 55 |
# File 'lib/utopia/project/sidebar.rb', line 53 def any? !entries.empty? end |
#to_html(sidebar: false, title: "Table of Contents") ⇒ Object
Generate HTML markup for the sidebar navigation.
61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 |
# File 'lib/utopia/project/sidebar.rb', line 61 def to_html(sidebar: false, title: "Table of Contents") return XRB::Markup.raw("") unless any? XRB::Builder.fragment do |builder| builder.tag :nav do builder.tag :heading do builder.text title end builder.tag :ul do entries.each do |entry| if entry.level > 2 builder.tag :li, {class: "level-#{entry.level}"} do builder.tag :a, {href: "##{entry.anchor}"} do builder << entry.title_html end end else builder.tag :li do builder.tag :a, {href: "##{entry.anchor}"} do builder << entry.title_html end end end end end end end end |