Skip to content

Commit

Permalink
first take of book rebuild
Browse files Browse the repository at this point in the history
  • Loading branch information
Zachary Scott committed Sep 7, 2012
1 parent 304dcbc commit fcc74b3
Show file tree
Hide file tree
Showing 7 changed files with 78 additions and 14 deletions.
6 changes: 4 additions & 2 deletions Gemfile
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
source 'http://rubygems.org'

gem 'thor', '~> 0.14'
gem 'maruku', '~> 0.6'
gem 'pdfkit'
gem 'rake'
gem 'redcarpet', '~> 2.0'
gem 'sinatra'
10 changes: 10 additions & 0 deletions Rakefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
require File.join(File.dirname(__FILE__), "book.rb")

namespace :book do
desc "build the book into pdf"
task :build do
include Book
build(true)
#File.open("#{OUTPUT_DIR}/sinatra-book.html", "w") { |f| f.write(doc) }
end
end
23 changes: 23 additions & 0 deletions app.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
require File.join(File.dirname(__FILE__), "book.rb")
require 'erb'
require 'sinatra'
include Book

get('/'){ erb build }

get('/book.css') { send_file "#{ASSETS_DIR}/book.css", :type => 'text/css' }

get('/logo.png') { send_file "#{ASSETS_DIR}/images/logo.png", :type => :png }

__END__
@@layout
<html>
<head>
<title>Sinatra Book</title>
<link rel="stylesheet" type="text/css" href="/book.css" />
</head>
<body>
<p><img src="/logo.png" /></p>
<%= yield %>
</body>
</html>
File renamed without changes
1 change: 0 additions & 1 deletion book-order.txt
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@
# Use this approach rather than than naming the files with 1_, 2_, and so on to force
# a sort order.

TOC.markdown
Introduction.markdown
Getting_to_know_Sinatra.markdown
Organizing_your_application.markdown
Expand Down
41 changes: 41 additions & 0 deletions book.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
require 'pdfkit'
require 'redcarpet'

module Book
ASSETS_DIR = File.join(File.dirname(__FILE__), "assets")
BOOK_DIR = File.join(File.dirname(__FILE__), "book")
OUTPUT_DIR = File.join(File.dirname(__FILE__), "output")

def build(pdf=false)
renderer = Redcarpet::Markdown.new(Redcarpet::Render::HTML,
#: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))
doc << renderer.render(complete_markdown)
if pdf
kit = PDFKit.new(doc, :page_size=>'Letter')
kit.stylesheets << "#{ASSETS_DIR}/book.css"
pdf = kit.to_pdf
mkdir_p OUTPUT_DIR
file = kit.to_file("#{OUTPUT_DIR}/sinatra-book.pdf")
end
return doc
end

private
def complete_markdown(toc=false)
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|
s << f.read
end
end
return s.join("\n\n* * *\n\n")
end
end
11 changes: 0 additions & 11 deletions book/TOC.markdown

This file was deleted.

0 comments on commit fcc74b3

Please sign in to comment.