Permalink
Browse files

fix toc renderer

  • Loading branch information...
1 parent fcc74b3 commit 75de7002c8ba223e2dde0b2e8829f5c7c5b7805c @zzak zzak committed Sep 7, 2012
Showing with 2 additions and 91 deletions.
  1. +0 −1 Rakefile
  2. +2 −2 book.rb
  3. +0 −88 sinatra-book.thor
View
1 Rakefile
@@ -5,6 +5,5 @@ namespace :book do
task :build do
include Book
build(true)
- #File.open("#{OUTPUT_DIR}/sinatra-book.html", "w") { |f| f.write(doc) }
end
end
View
4 book.rb
@@ -7,10 +7,10 @@ module Book
OUTPUT_DIR = File.join(File.dirname(__FILE__), "output")
def build(pdf=false)
- renderer = Redcarpet::Markdown.new(Redcarpet::Render::HTML,
+ renderer = Redcarpet::Markdown.new(
+ Redcarpet::Render::HTML.new(:with_toc_data => true),
#:no_links => true,
:space_after_headers => true,
- :with_toc_data => true,
:fenced_code_blocks => true)
toc_renderer = Redcarpet::Markdown.new(Redcarpet::Render::HTML_TOC)
doc = toc_renderer.render(complete_markdown(true))
View
88 sinatra-book.thor
@@ -1,88 +0,0 @@
-# module: sinatra-book
-
-require 'rubygems'
-require 'maruku'
-
-require 'fileutils'
-
-class Book < Thor
- include FileUtils
-
- SUPPORTED_FORMATS = %w{html latex pdf}
- OUTPUT_DIR = File.join(File.dirname(__FILE__), "output")
- BOOK_DIR = File.join(File.dirname(__FILE__), "book")
- BOOK_FILE_NAME = "sinatra-book"
- OUTPUT_FILE_BASE_NAME = File.join(OUTPUT_DIR, BOOK_FILE_NAME)
-
- desc "build [FORMAT]", "Build the book. FORMAT specifies what format the output should have. Defaults to html. Valid options are: #{SUPPORTED_FORMATS.join(", ")}"
- def build(format = 'html')
- doc = Maruku.new(complete_markdown)
-
- mkdir_p OUTPUT_DIR
- Dir.glob File.expand_path('../{assets,images}/*', __FILE__) do |file|
- cp file, OUTPUT_DIR
- end
-
-
- if SUPPORTED_FORMATS.include?( format )
- self.send("build_#{format}", doc)
- else
- STDERR << "Error: Don't know how to build for format '#{format}'"
- exit 1
- end
-
- end
-
- desc "clean", "Delete the output directory, along with all contents"
- def clean
- rm_rf OUTPUT_DIR, :verbose => true
- end
-
- private
-
-
- def build_html(doc)
- File.open(OUTPUT_FILE_BASE_NAME + '.html', 'w+') do |file|
- file << doc.to_html_document
- end
- end
-
- def build_latex(doc)
- File.open(OUTPUT_FILE_BASE_NAME + '.tex', 'w+') do |file|
- file << doc.to_latex_document
- end
- end
-
- def build_pdf(doc)
- build_latex(doc)
-
- Dir.chdir(OUTPUT_DIR) do |dir|
- # Run twice to get cross-references right
- 2.times { system("pdflatex #{OUTPUT_FILE_BASE_NAME + '.tex'} -output-directory=#{OUTPUT_DIR}") }
-
- # Clean up
- file_patterns = %w{*.aux *.out *.toc *.log}
- file_patterns.each do |pattern|
- FileUtils.rm(Dir.glob(pattern))
- end
- end
- end
-
- def complete_markdown
- # Collect all the markdown files in the correct order and squash them together into one big string
- s = []
- File.new("book-order.txt").each_line do |line|
- line.strip!
- next if line =~ /^#/ # Skip comments
- next if line =~ /^$/ # Skip blank lines
-
- File.open(File.join(BOOK_DIR, line)) do |f|
- # I have no idea if the double \n is needed, but seems safe
- s << f.read
- end
- end
-
- return s.join("\n\n* * *\n\n")
- end
-
-end

0 comments on commit 75de700

Please sign in to comment.