Class: Docter::ToC
Overview
Table of contents.
A ToC is an array of entries, each entry providing a link to and a title, and may itself be a ToC.
Supports the Enumerable methods for operating on the entries, in addition to the methods each, first/last, size, empty? and index/include?. Use #add to create new entries.
Use #to_html to transform to an HTML ordered list.
Direct Known Subclasses
Constant Summary collapse
- ARRAY_METHODS =
['each', 'first', 'last', 'size', 'empty?', 'include?', 'index', '[]']
Instance Attribute Summary collapse
-
#entries ⇒ Object
readonly
Array of entries.
-
#title ⇒ Object
readonly
The title of this entry (optional).
Class Method Summary collapse
-
.from_yaml(yaml, collection = nil) ⇒ Object
Loads the ToC from a YAML file.
Instance Method Summary collapse
-
#add(*args) ⇒ Object
:call-seq: add(title, url?) => entry add(entry) => entry.
-
#initialize(title = nil) ⇒ ToC
constructor
Create new ToC with no entries.
-
#to_html(*args) ⇒ Object
:call-seq: to_html(options) => html.
Constructor Details
#initialize(title = nil) ⇒ ToC
Create new ToC with no entries.
246 247 248 249 |
# File 'lib/docter/common.rb', line 246 def initialize(title = nil) @title = title @entries = [] end |
Instance Attribute Details
#entries ⇒ Object (readonly)
Array of entries.
243 244 245 |
# File 'lib/docter/common.rb', line 243 def entries @entries end |
#title ⇒ Object (readonly)
The title of this entry (optional)
240 241 242 |
# File 'lib/docter/common.rb', line 240 def title @title end |
Class Method Details
.from_yaml(yaml, collection = nil) ⇒ Object
Loads the ToC from a YAML file.
231 232 233 |
# File 'lib/docter/common.rb', line 231 def from_yaml(yaml, collection = nil) new.send(:initialize_from, YAML.load(yaml), collection) end |
Instance Method Details
#add(*args) ⇒ Object
:call-seq:
add(title, url?) => entry
add(entry) => entry
Adds (and returns) a new entry. The first form creates an entry with a link (must be a valid URL, use CGI.escape if necessary) and HTML-encoded title. The second form adds an existing entry, for example to a page.
263 264 265 266 267 268 269 270 271 272 |
# File 'lib/docter/common.rb', line 263 def add(*args) if ToCEntry === args.first entry = args.shift raise ArgumentError, 'Can only accept a ToCEntry argument.' unless args.empty? else entry = ToCEntry.new(*args) end entries << entry entry end |
#to_html(*args) ⇒ Object
:call-seq:
to_html() => html
Transforms this ToC into an HTML ordered list (OL) by calling to_html on each ToC entry.
You can use the following options:
-
:nested – For entries that are also ToC, expands them as well. You can specify how many levels (e.g. 1 to expand only once), or true to expand all levels.
-
:class – Class to apply to the OL element.
The options
argument can take the form of a Hash, list of symbols or both. Symbols are treated as true
for example:
to_html(:nested, :class=>'toc')
Is the same as:
to_html(:nested=>true, :class=>'toc')
289 290 291 292 293 294 |
# File 'lib/docter/common.rb', line 289 def to_html(*args) = Hash === args.last ? args.pop.clone : {} args.each { |arg| [arg.to_sym] = true } cls = %{ class="#{[:class]}"} if [:class] %{<ol #{cls}>#{map { |entry| entry.to_html() }}</ol>} unless empty? end |