From f74d8202d3dbffc71b997d82cb7a868c347465d0 Mon Sep 17 00:00:00 2001 From: Josh Holtrop Date: Sun, 9 Jun 2019 22:31:47 -0400 Subject: [PATCH] user guide: add table of contents --- rb/gen_user_guide.rb | 23 +++++++++++++++-------- 1 file changed, 15 insertions(+), 8 deletions(-) diff --git a/rb/gen_user_guide.rb b/rb/gen_user_guide.rb index 87b21d2..3535e36 100644 --- a/rb/gen_user_guide.rb +++ b/rb/gen_user_guide.rb @@ -17,13 +17,15 @@ end class Generator class Section - attr_reader :name + attr_reader :title attr_reader :new_page attr_reader :contents - def initialize(name, new_page) - @name = name + attr_reader :anchor + def initialize(title, new_page) + @title = title @new_page = new_page @contents = "" + @anchor = "s" + title.gsub(/[^a-zA-Z0-9]/, "_") end def append(contents) @contents += contents @@ -31,7 +33,7 @@ class Generator end def initialize(input, output_file, multi_file) - @sections = [Section.new("index", false)] + @sections = [] @current_section_number = [0] @lines = input.lines while @lines.size > 0 @@ -46,16 +48,21 @@ class Generator section_title = "#{section_number} #{title_text}" @sections << Section.new(section_title, new_page) @sections.last.append("#{level_text} #{section_title}") - else + elsif @sections.size > 0 @sections.last.append(line) end end renderer = Redcarpet::Render::HTML.new markdown = Redcarpet::Markdown.new(renderer) - content = @sections.map do |section| - markdown.render(section.contents) - end.join("\n") + content = %[

Table of Contents

\n] + @sections.each do |section| + content += %[#{section.title}
\n] + end + @sections.each do |section| + content += %[] + content += markdown.render(section.contents) + end template = File.read("rb/assets/user_guide.html.erb") erb = ERB.new(template, nil, "<>")