Class: Coradoc::Markdown::TocGenerator
- Inherits:
-
Object
- Object
- Coradoc::Markdown::TocGenerator
- Defined in:
- lib/coradoc/markdown/toc_generator.rb
Overview
Table of Contents Generator
Generates a table of contents from document headings. Supports Kramdown-style TOC with options for levels, depth, etc.
Defined Under Namespace
Classes: Entry
Constant Summary collapse
- DEFAULT_OPTIONS =
Default options for TOC generation
{ min_level: 1, max_level: 6, numbered: false, styled: false, link_headings: true }.freeze
Class Method Summary collapse
-
.generate(document, options = {}) ⇒ Entry?
Generate a TOC from a document.
-
.generate_array(document, options = {}) ⇒ Array<Hash>
Generate TOC as array structure.
-
.generate_markdown(document, options = {}) ⇒ String
Generate TOC as Markdown string.
Instance Method Summary collapse
-
#generate(document) ⇒ Entry?
Generate TOC from document.
-
#initialize(options = {}) ⇒ TocGenerator
constructor
A new instance of TocGenerator.
Methods included from Coradoc::Markdown::Transform::TextExtraction
Constructor Details
#initialize(options = {}) ⇒ TocGenerator
Returns a new instance of TocGenerator.
108 109 110 111 112 113 |
# File 'lib/coradoc/markdown/toc_generator.rb', line 108 def initialize( = {}) @options = DEFAULT_OPTIONS.merge() @min_level = @options[:min_level] @max_level = @options[:max_level] @numbered = @options[:numbered] end |
Class Method Details
.generate(document, options = {}) ⇒ Entry?
Generate a TOC from a document
82 83 84 |
# File 'lib/coradoc/markdown/toc_generator.rb', line 82 def self.generate(document, = {}) new().generate(document) end |
.generate_array(document, options = {}) ⇒ Array<Hash>
Generate TOC as array structure
101 102 103 104 105 106 |
# File 'lib/coradoc/markdown/toc_generator.rb', line 101 def self.generate_array(document, = {}) toc = generate(document, ) return [] unless toc toc.children.map(&:to_h) end |
.generate_markdown(document, options = {}) ⇒ String
Generate TOC as Markdown string
91 92 93 94 |
# File 'lib/coradoc/markdown/toc_generator.rb', line 91 def self.generate_markdown(document, = {}) toc = generate(document, ) toc ? toc.to_markdown : '' end |
Instance Method Details
#generate(document) ⇒ Entry?
Generate TOC from document
119 120 121 122 123 124 125 126 |
# File 'lib/coradoc/markdown/toc_generator.rb', line 119 def generate(document) headings = extract_headings(document) return nil if headings.empty? root = Entry.new(id: nil, text: 'Table of Contents', level: 0) build_toc_tree(root, headings) root end |