user guide: add Page class to prepare for multi-page output

This commit is contained in:
Josh Holtrop 2019-06-14 16:09:44 -04:00
parent 5f71781806
commit 46294b2a88

View File

@ -34,6 +34,17 @@ class Generator
end end
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) def initialize(input, output_file, multi_page)
current_page = current_page =
if multi_page if multi_page
@ -68,15 +79,17 @@ class Generator
@markdown_renderer = Redcarpet::Markdown.new(renderer) @markdown_renderer = Redcarpet::Markdown.new(renderer)
changelog = @markdown_renderer.render(File.read("CHANGELOG.md")) 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| @sections.each do |section|
pages[section.page] ||= "" unless pages.last.name == section.page
pages[section.page] += render_section(section) pages << Page.new(section.page, section.title)
end
pages.last.contents += render_section(section)
end end
pages.each do |title, contents| pages.each do |page|
contents.gsub!("${changelog}", changelog) page.contents.gsub!("${changelog}", changelog)
contents.gsub!(%r{\$\{#(.+?)\}}) do |match| page.contents.gsub!(%r{\$\{#(.+?)\}}) do |match|
section_name = $1 section_name = $1
href = get_link_to_section(section_name) href = get_link_to_section(section_name)
%[<a href="#{href}">#{section_name}</a>] %[<a href="#{href}">#{section_name}</a>]
@ -90,8 +103,8 @@ class Generator
# TODO # TODO
else else
subpage_title = "" subpage_title = ""
content = pages.reduce("") do |result, (title, contents)| content = pages.reduce("") do |result, page|
result + contents result + page.contents
end end
html_result = erb.result(binding.clone) html_result = erb.result(binding.clone)
File.open(output_file, "w") do |fh| File.open(output_file, "w") do |fh|