From 46294b2a8869a4fedb6a7ffb89452a89a3531476 Mon Sep 17 00:00:00 2001 From: Josh Holtrop Date: Fri, 14 Jun 2019 16:09:44 -0400 Subject: [PATCH] user guide: add Page class to prepare for multi-page output --- rb/gen_user_guide.rb | 29 +++++++++++++++++++++-------- 1 file changed, 21 insertions(+), 8 deletions(-) diff --git a/rb/gen_user_guide.rb b/rb/gen_user_guide.rb index 798ba9a..a6a5202 100644 --- a/rb/gen_user_guide.rb +++ b/rb/gen_user_guide.rb @@ -34,6 +34,17 @@ class Generator end end + class Page + attr_reader :name + attr_reader :title + attr_accessor :contents + def initialize(name, title, contents = "") + @name = name + @title = title + @contents = contents + end + end + def initialize(input, output_file, multi_page) current_page = if multi_page @@ -68,15 +79,17 @@ class Generator @markdown_renderer = Redcarpet::Markdown.new(renderer) changelog = @markdown_renderer.render(File.read("CHANGELOG.md")) - pages = {"toc" => render_toc} + pages = [Page.new("toc", "Table of Contents", render_toc)] @sections.each do |section| - pages[section.page] ||= "" - pages[section.page] += render_section(section) + unless pages.last.name == section.page + pages << Page.new(section.page, section.title) + end + pages.last.contents += render_section(section) end - pages.each do |title, contents| - contents.gsub!("${changelog}", changelog) - contents.gsub!(%r{\$\{#(.+?)\}}) do |match| + pages.each do |page| + page.contents.gsub!("${changelog}", changelog) + page.contents.gsub!(%r{\$\{#(.+?)\}}) do |match| section_name = $1 href = get_link_to_section(section_name) %[#{section_name}] @@ -90,8 +103,8 @@ class Generator # TODO else subpage_title = "" - content = pages.reduce("") do |result, (title, contents)| - result + contents + content = pages.reduce("") do |result, page| + result + page.contents end html_result = erb.result(binding.clone) File.open(output_file, "w") do |fh|