bookie 0.0.4 → 0.0.5

Sign up to get free protection for your applications and to get access to all the features.
@@ -19,3 +19,9 @@ see how they're used.
19
19
 
20
20
  Removed HTML emitter for now. Expanded parser and PDF emitter to support
21
21
  preformatted text. See examples/preformatted.rb for to see how it works.
22
+
23
+ ## 0.0.5 (2011.04.24)
24
+
25
+ Reinstated a skeletal HTML emitter because it's needed for EPub and Mobi. Added
26
+ pretty terrible Mobi support, will not really be useful until we flesh out
27
+ manifests and table of content generation, but looks okay on my Kindle!
data/README.md CHANGED
@@ -18,8 +18,13 @@ put together a proper release plan.
18
18
  ## Dependencies
19
19
 
20
20
  Bookie targets Ruby 1.9.2 exclusively, and will not run on Ruby 1.8.
21
- Currently, the only direct dependency we use is Prawn, but that may change in
22
- the future.
21
+ PDF generation is provided by [Prawn](http::/prawn.majesticseacreature.com),
22
+ ePUB from [eeepub](https://github.com/jugyo/eeepub), and MOBI via the command
23
+ line tool [kindlegen](http://www.amazon.com/gp/feature.html?ie=UTF8&docId=1000234621).
24
+
25
+ Prawn and eeepub are free software projects available via rubygems, kindlegen is
26
+ free-as-in-beer proprietary software from Amazon that needs to be installed
27
+ manually.
23
28
 
24
29
  ## Contributing
25
30
 
@@ -5,7 +5,16 @@ file = "#{File.dirname(__FILE__)}/../test/fixtures/preformatted_blocks.md"
5
5
 
6
6
  pdf_document = Bookie::Document.new(file, Bookie::Emitters::PDF.new)
7
7
 
8
- File.open("output.pdf", "w") do |f|
9
- f << pdf_document.render(header: "Practicing Ruby: Issue #1",
10
- title: "Ruby's Method Lookup Path")
11
- end
8
+ pdf_document.render(header: "Practicing Ruby: Issue #1",
9
+ title: "Ruby's Method Lookup Path",
10
+ file: "output.pdf")
11
+
12
+ html_document = Bookie::Document.new(file, Bookie::Emitters::HTML.new)
13
+
14
+ html_document.render(title: "Ruby's Method Lookup Path",
15
+ file: "output.html")
16
+
17
+ mobi_document = Bookie::Document.new(file, Bookie::Emitters::MOBI.new)
18
+
19
+ mobi_document.render(title: "Ruby's Method Lookup Path",
20
+ file: "output.mobi")
@@ -5,7 +5,6 @@ file = "#{File.dirname(__FILE__)}/../test/fixtures/multi_paragraph_document.md"
5
5
 
6
6
  pdf_document = Bookie::Document.new(file, Bookie::Emitters::PDF.new)
7
7
 
8
- File.open("output.pdf", "w") do |f|
9
- f << pdf_document.render(header: "Majestic Sea Creature Blog",
10
- title: "Why does RbMU exist?")
11
- end
8
+ pdf_document.render(header: "Majestic Sea Creature Blog",
9
+ title: "Why does RbMU exist?",
10
+ file: "output.pdf")
@@ -1,3 +1,6 @@
1
+ require "tempfile"
2
+ require "fileutils"
3
+
1
4
  require "prawn"
2
5
 
3
6
  require_relative "bookie/version"
@@ -4,7 +4,56 @@ module Bookie
4
4
  def build_paragraph(paragraph)
5
5
  end
6
6
 
7
- def build_raw_text(paragraphs)
7
+ def build_raw_text(raw_text)
8
+ end
9
+ end
10
+
11
+ class HTML
12
+ def initialize
13
+ @body = ""
14
+ end
15
+
16
+ attr_reader :body
17
+
18
+ def build_paragraph(paragraph)
19
+ @body << "<p>#{paragraph.contents}</p>"
20
+ end
21
+
22
+ def build_raw_text(raw_text)
23
+ @body << "<pre>#{raw_text.contents}</pre>"
24
+ end
25
+
26
+ def render(params)
27
+ File.open(params[:file], "w") do |file|
28
+ file << %{
29
+ <html>
30
+ <body><h1>#{params[:title]}</h1>#{@body}</body>
31
+ </html>
32
+ }
33
+ end
34
+ end
35
+ end
36
+
37
+ class MOBI < HTML
38
+ def render(params)
39
+ t = Tempfile.new(params[:file])
40
+ t << %{
41
+ <html>
42
+ <head>
43
+ <style type="text/css">
44
+ h1 { margin-bottom: 3em }
45
+ p { margin-bottom: 1.1em; text-indent: 0 }
46
+ pre { font-size: xx-small }
47
+ </style>
48
+ </head>
49
+ <body><h1>#{params[:title]}</h1>#{@body}</body>
50
+ </html>
51
+ }
52
+ t.close
53
+ FileUtils.mv(t.path, "#{t.path}.html")
54
+
55
+ `kindlegen #{t.path+'.html'} -o #{params[:file]}`
56
+ FileUtils.mv("#{Dir.tmpdir}/#{params[:file]}", ".")
8
57
  end
9
58
  end
10
59
 
@@ -18,13 +67,12 @@ module Bookie
18
67
  end
19
68
 
20
69
  def build_paragraph(paragraph)
21
- @paragraphs << { text: paragraph.children.first.strip }
70
+ @paragraphs << { text: paragraph.contents.strip }
22
71
  end
23
72
 
24
73
  def build_raw_text(raw_text)
25
- sanitized_text = raw_text.children.first.
26
- gsub(" ", Prawn::Text::NBSP).strip
27
-
74
+ sanitized_text = raw_text.contents.gsub(" ", Prawn::Text::NBSP).strip
75
+
28
76
  @paragraphs << { text: sanitized_text,
29
77
  font: "Courier",
30
78
  size: 8 }
@@ -38,7 +86,7 @@ module Bookie
38
86
  @paragraphs.pop
39
87
 
40
88
  @document.formatted_text(@paragraphs, align: :justify, size: 10)
41
- @document.render
89
+ @document.render_file(params[:file])
42
90
  end
43
91
 
44
92
  def render_header(params)
@@ -1,39 +1,38 @@
1
1
  module Bookie
2
- ContentTree = Struct.new(:children)
3
- Paragraph = Struct.new(:children)
4
- RawText = Struct.new(:children)
2
+ Paragraph = Struct.new(:contents)
3
+ RawText = Struct.new(:contents)
5
4
 
6
5
  class Parser
7
- def self.parse(contents, emitter=Bookie::Emitters::Null.new)
8
- parser = new(contents, emitter)
9
- parser.document_tree
6
+ def self.parse(raw_data, emitter=Bookie::Emitters::Null.new)
7
+ parser = new(raw_data, emitter)
8
+ parser.parsed_content
10
9
  end
11
10
 
12
- attr_reader :document_tree
11
+ attr_reader :parsed_content
13
12
 
14
- def initialize(contents, emitter)
15
- @emitter = emitter
16
- generate_document_tree(contents)
13
+ def initialize(raw_data, emitter)
14
+ @emitter = emitter
15
+ @parsed_content = []
16
+
17
+ parse_contents(raw_data)
17
18
  end
18
19
 
19
- def extract_paragraph(contents)
20
- paragraph = Paragraph.new([contents.gsub(/\s+/," ")])
20
+ def extract_paragraph(paragraph_text)
21
+ paragraph = Paragraph.new(paragraph_text.gsub(/\s+/," "))
21
22
  @emitter.build_paragraph(paragraph)
22
- document_tree.children << paragraph
23
+ parsed_content << paragraph
23
24
  end
24
25
 
25
26
  def extract_raw_text(contents)
26
- raw_text = RawText.new([contents])
27
+ raw_text = RawText.new(contents)
27
28
  @emitter.build_raw_text(raw_text)
28
- document_tree.children << raw_text
29
+ parsed_content << raw_text
29
30
  end
30
31
 
31
32
  private
32
33
 
33
- def generate_document_tree(contents)
34
- @document_tree = ContentTree.new([])
35
-
36
- lines = contents.lines.to_a
34
+ def parse_contents(raw_data)
35
+ lines = raw_data.lines.to_a
37
36
  mode = nil
38
37
 
39
38
  until lines.empty?
@@ -1,3 +1,3 @@
1
1
  module Bookie
2
- VERSION = "0.0.4"
2
+ VERSION = "0.0.5"
3
3
  end
@@ -5,6 +5,6 @@ context "A Document" do
5
5
  sample_text = fixture("multi_paragraph_document.md")
6
6
  document = Bookie::Document.new(sample_text)
7
7
 
8
- refute document.contents.children.empty?, "contents should not be empty"
8
+ refute document.contents.empty?, "contents should not be empty"
9
9
  end
10
10
  end
@@ -7,23 +7,20 @@ context "A Parser" do
7
7
  # NOTE: Is this the behavior we'd expect?
8
8
  sample_paragraph_text = File.read(fixture("single_paragraph.md"))
9
9
  .gsub(/\s+/," ")
10
-
11
- tree = Bookie::Parser.parse(sample_text)
10
+ parsed_content = Bookie::Parser.parse(sample_text)
12
11
 
13
- assert_equal 8, tree.children.length
12
+ assert_equal 8, parsed_content.length
14
13
 
15
- actual_paragraph = tree.children[4]
16
- actual_paragraph_text = actual_paragraph.children.first
14
+ actual_paragraph = parsed_content[4]
15
+ actual_paragraph_text = actual_paragraph.first
17
16
 
18
17
  assert_equal sample_paragraph_text, actual_paragraph_text
19
18
  end
20
19
 
21
20
  test "should know about preformatted text" do
22
- sample_text = File.read(fixture("preformatted_blocks.md"))
23
-
24
- tree = Bookie::Parser.parse(sample_text)
21
+ sample_text = File.read(fixture("preformatted_blocks.md"))
22
+ parsed_content = Bookie::Parser.parse(sample_text)
25
23
 
26
- assert_equal 7, tree.children.length
24
+ assert_equal 6, parsed_content.length
27
25
  end
28
-
29
26
  end
metadata CHANGED
@@ -2,7 +2,7 @@
2
2
  name: bookie
3
3
  version: !ruby/object:Gem::Version
4
4
  prerelease:
5
- version: 0.0.4
5
+ version: 0.0.5
6
6
  platform: ruby
7
7
  authors:
8
8
  - Gregory Brown