Permalink
Browse files

first take of book rebuild

  • Loading branch information...
1 parent 304dcbc commit fcc74b35fcd225080f92a6f2f366c2b342da8586 @zzak zzak committed Sep 7, 2012
Showing with 78 additions and 14 deletions.
  1. +4 −2 Gemfile
  2. +10 −0 Rakefile
  3. +23 −0 app.rb
  4. BIN { → assets}/images/logo.png
  5. +0 −1 book-order.txt
  6. +41 −0 book.rb
  7. +0 −11 book/TOC.markdown
View
@@ -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'
View
@@ -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
View
23 app.rb
@@ -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
View
@@ -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
View
41 book.rb
@@ -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
View
@@ -1,11 +0,0 @@
-CSS: ./book.css
-Title: Sinatra Book
-
-![logo](logo.png)
-
-Table of Contents
-=================
-
-* This list will contain the toc, no matter what we write here
-{:toc}
-

0 comments on commit fcc74b3

Please sign in to comment.