user guide: add table of contents

This commit is contained in:
Josh Holtrop 2019-06-09 22:31:47 -04:00
parent bbf2319da6
commit f74d8202d3

View File

@ -17,13 +17,15 @@ end
class Generator class Generator
class Section class Section
attr_reader :name attr_reader :title
attr_reader :new_page attr_reader :new_page
attr_reader :contents attr_reader :contents
def initialize(name, new_page) attr_reader :anchor
@name = name def initialize(title, new_page)
@title = title
@new_page = new_page @new_page = new_page
@contents = "" @contents = ""
@anchor = "s" + title.gsub(/[^a-zA-Z0-9]/, "_")
end end
def append(contents) def append(contents)
@contents += contents @contents += contents
@ -31,7 +33,7 @@ class Generator
end end
def initialize(input, output_file, multi_file) def initialize(input, output_file, multi_file)
@sections = [Section.new("index", false)] @sections = []
@current_section_number = [0] @current_section_number = [0]
@lines = input.lines @lines = input.lines
while @lines.size > 0 while @lines.size > 0
@ -46,16 +48,21 @@ class Generator
section_title = "#{section_number} #{title_text}" section_title = "#{section_number} #{title_text}"
@sections << Section.new(section_title, new_page) @sections << Section.new(section_title, new_page)
@sections.last.append("#{level_text} #{section_title}") @sections.last.append("#{level_text} #{section_title}")
else elsif @sections.size > 0
@sections.last.append(line) @sections.last.append(line)
end end
end end
renderer = Redcarpet::Render::HTML.new renderer = Redcarpet::Render::HTML.new
markdown = Redcarpet::Markdown.new(renderer) markdown = Redcarpet::Markdown.new(renderer)
content = @sections.map do |section| content = %[<h1>Table of Contents</h1>\n]
markdown.render(section.contents) @sections.each do |section|
end.join("\n") content += %[<a href="##{section.anchor}">#{section.title}</a><br/>\n]
end
@sections.each do |section|
content += %[<a name="#{section.anchor}" />]
content += markdown.render(section.contents)
end
template = File.read("rb/assets/user_guide.html.erb") template = File.read("rb/assets/user_guide.html.erb")
erb = ERB.new(template, nil, "<>") erb = ERB.new(template, nil, "<>")