Skip to content
Browse files

* build and download pdf from app

* style for app
* separate style for pdf
* remove old logo, use sinatra/resources logo
* use fileutils.rb for mkdir_p
* separate toc, content, header methods
* vendor wkhtmltopdf static binary
  • Loading branch information...
1 parent 75de700 commit 57763ae66fd0b302a4b40951175c69bfd471880f @zzak zzak committed
Showing with 130 additions and 47 deletions.
  1. +8 −2 app.rb
  2. +32 −34 assets/book.css
  3. BIN assets/images/download.png
  4. BIN assets/images/logo.png
  5. +49 −0 assets/print.css
  6. +41 −11 book.rb
  7. BIN vendor/wkhtmltopdf
View
10 app.rb
@@ -5,9 +5,11 @@
get('/'){ erb build }
+get('/download'){ build(true); send_file "#{OUTPUT_DIR}/sinatra-book.pdf", :type => 'application/pdf'; }
+
get('/book.css') { send_file "#{ASSETS_DIR}/book.css", :type => 'text/css' }
-get('/logo.png') { send_file "#{ASSETS_DIR}/images/logo.png", :type => :png }
+get('/download.png') { send_file "#{ASSETS_DIR}/images/download.png", :type => :png }
__END__
@@layout
@@ -17,7 +19,11 @@
<link rel="stylesheet" type="text/css" href="/book.css" />
</head>
<body>
- <p><img src="/logo.png" /></p>
+ <div id="download">
+ <a href="/download">
+ <img src="/download.png" alt="Download the Sinatra Book" />
+ </a>
+ </div>
<%= yield %>
</body>
</html>
View
66 assets/book.css
@@ -5,65 +5,63 @@ body
{ color: #222;
background: #fafafa;
font-size: 80%;
- min-width: 300px;
+ min-width: 800px;
font-family: 'Lucida Grande', Verdana, sans-serif;
padding: 1em 2em;
margin: auto;
- max-width: 800px;
- border: 1px solid #d5d8db;
- border-top: none; border-bottom: none; }
+ max-width: 1024px; }
-body h1,
-body h2,
-body h3,
-body h4,
-body h5
- { font-family: 'Helvetica', 'Arial', sans-serif; }
+h1, h2, h3, h4, h5
+ { font-family: serif; }
-body h1
+h1
{ color: #fff;
background: #404142;
font-size: 160%;
- padding: 0.4em 1.4em 0.3em 1.4em;
- margin: 2em -1em 0 -1em; }
+ padding: 0.4em 1.4em 0.3em 1.4em; }
-body h2
+h2
{ margin: 1.2em 0 0.2em 0; }
-body h3
+h3
{ margin: 0.8em 0 0.1em 0; }
-body h4,
-body h5
+h4, h5
{ margin: 0.4em 0 0.05em 0; }
-body code
- { color: #444;
+code
+ { color: #444;
font-size: 120%; }
-body pre
+pre
{ white-space: pre-wrap; white-space: -moz-pre-wrap !important;
padding: 1em;
border: 4px solid #dfe2e5; }
-body a
+a
{ color: #1177DD;
text-decoration: none; }
-body a:hover
+a:hover
{ text-decoration: underline; }
-body p
+p
{ margin: 0 0 1em 0; }
-body hr
+hr
{ display: none; }
-body img[src="logo.png"]
- { position: relative;
- left: -1em;
- margin-bottom: -2em; }
-
-/* TOC */
-.maruku_toc ul { font-weight: bold; padding: 0em; }
-.maruku_toc ul ul { font-weight: normal; padding: 0em 0 0.5em 1em; }
-.maruku_toc ul ul ul { font-weight: normal; padding: 0em 0 0.25em 0.5em; }
-.maruku_toc ul ul ul ul { font-weight: normal; padding: 0em 0 0.1em 0.25em; }
+#download
+ { float: right;
+ position: absolute;
+ right: 0px; top: 0px;
+ z-index: 1; }
+
+#download img
+ { border: none; }
+
+#toc
+ { float: left;
+ width: 300px; }
+
+#content
+ { float: right;
+ width: 700px; }
View
BIN assets/images/download.png
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
View
BIN assets/images/logo.png
Deleted file not rendered
View
49 assets/print.css
@@ -0,0 +1,49 @@
+html
+ { padding: 0 2em; }
+
+body
+ { color: #222;
+ background: #fafafa;
+ font-size: 80%;
+ font-family: 'Lucida Grande', Verdana, sans-serif; }
+
+h1, h2, h3, h4, h5
+ { font-family: serif; }
+
+h1
+ { color: #fff;
+ background: #404142;
+ font-size: 160%;
+ padding: 0.4em 1.4em 0.3em 1.4em; }
+
+h2
+ { margin: 1.2em 0 0.2em 0; }
+
+h3
+ { margin: 0.8em 0 0.1em 0; }
+
+h4, h5
+ { margin: 0.4em 0 0.05em 0; }
+
+code
+ { color: #444;
+ font-size: 120%; }
+pre
+ { white-space: pre-wrap; white-space: -moz-pre-wrap !important;
+ padding: 1em;
+ border: 4px solid #dfe2e5; }
+
+a
+ { color: #1177DD;
+ text-decoration: none; }
+a:hover
+ { text-decoration: underline; }
+
+p
+ { margin: 0 0 1em 0; }
+
+hr
+ { display: none; }
+
+#header img
+ { width: 296px; }
View
52 book.rb
@@ -1,5 +1,6 @@
require 'pdfkit'
require 'redcarpet'
+require 'fileutils'
module Book
ASSETS_DIR = File.join(File.dirname(__FILE__), "assets")
@@ -7,26 +8,55 @@ module Book
OUTPUT_DIR = File.join(File.dirname(__FILE__), "output")
def build(pdf=false)
- renderer = Redcarpet::Markdown.new(
- Redcarpet::Render::HTML.new(:with_toc_data => true),
- #:no_links => true,
- :space_after_headers => 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)
+ doc = header
+ doc << toc
+ doc << content
if pdf
+ PDFKit.configure { |config|
+ config.wkhtmltopdf = File.join(File.dirname(__FILE__),
+ 'vendor', 'wkhtmltopdf')
+ }
kit = PDFKit.new(doc, :page_size=>'Letter')
- kit.stylesheets << "#{ASSETS_DIR}/book.css"
+ kit.stylesheets << "#{ASSETS_DIR}/print.css"
pdf = kit.to_pdf
- mkdir_p OUTPUT_DIR
+ FileUtils.mkdir_p OUTPUT_DIR
file = kit.to_file("#{OUTPUT_DIR}/sinatra-book.pdf")
end
return doc
end
private
- def complete_markdown(toc=false)
+ def header
+ <<-header
+ <div id="header">
+ <p>
+ <img src="https://raw.github.com/sinatra/resources/master/logo/book-logo.png" />
+ </p>
+ </div>
+ header
+ end
+
+ def toc
+ toc_renderer = Redcarpet::Markdown.new(Redcarpet::Render::HTML_TOC)
+ <<-toc
+ <div id="toc">
+ #{toc_renderer.render(complete_markdown)}
+ </div>
+ toc
+ end
+
+ def content
+ renderer = Redcarpet::Markdown.new(
+ Redcarpet::Render::HTML.new(:with_toc_data => true),
+ :fenced_code_blocks => true)
+ <<-content
+ <div id="content">
+ #{renderer.render(complete_markdown)}
+ </div>
+ content
+ end
+
+ def complete_markdown
s = []
File.new("book-order.txt").each_line do |line|
line.strip!
View
BIN vendor/wkhtmltopdf
Binary file not shown.

0 comments on commit 57763ae

Please sign in to comment.
Something went wrong with that request. Please try again.