bookie 0.0.4 → 0.0.5
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- data/CHANGELOG.md +6 -0
- data/README.md +7 -2
- data/examples/preformatted.rb +13 -4
- data/examples/simple_emitters.rb +3 -4
- data/lib/bookie.rb +3 -0
- data/lib/bookie/emitters.rb +54 -6
- data/lib/bookie/parser.rb +18 -19
- data/lib/bookie/version.rb +1 -1
- data/test/units/document_test.rb +1 -1
- data/test/units/parser_test.rb +7 -10
- metadata +1 -1
data/CHANGELOG.md
CHANGED
@@ -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
|
-
|
22
|
-
the
|
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
|
|
data/examples/preformatted.rb
CHANGED
@@ -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
|
-
|
9
|
-
|
10
|
-
|
11
|
-
|
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")
|
data/examples/simple_emitters.rb
CHANGED
@@ -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
|
-
|
9
|
-
|
10
|
-
|
11
|
-
end
|
8
|
+
pdf_document.render(header: "Majestic Sea Creature Blog",
|
9
|
+
title: "Why does RbMU exist?",
|
10
|
+
file: "output.pdf")
|
data/lib/bookie.rb
CHANGED
data/lib/bookie/emitters.rb
CHANGED
@@ -4,7 +4,56 @@ module Bookie
|
|
4
4
|
def build_paragraph(paragraph)
|
5
5
|
end
|
6
6
|
|
7
|
-
def build_raw_text(
|
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.
|
70
|
+
@paragraphs << { text: paragraph.contents.strip }
|
22
71
|
end
|
23
72
|
|
24
73
|
def build_raw_text(raw_text)
|
25
|
-
sanitized_text = raw_text.
|
26
|
-
|
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.
|
89
|
+
@document.render_file(params[:file])
|
42
90
|
end
|
43
91
|
|
44
92
|
def render_header(params)
|
data/lib/bookie/parser.rb
CHANGED
@@ -1,39 +1,38 @@
|
|
1
1
|
module Bookie
|
2
|
-
|
3
|
-
|
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(
|
8
|
-
parser = new(
|
9
|
-
parser.
|
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 :
|
11
|
+
attr_reader :parsed_content
|
13
12
|
|
14
|
-
def initialize(
|
15
|
-
@emitter
|
16
|
-
|
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(
|
20
|
-
paragraph = Paragraph.new(
|
20
|
+
def extract_paragraph(paragraph_text)
|
21
|
+
paragraph = Paragraph.new(paragraph_text.gsub(/\s+/," "))
|
21
22
|
@emitter.build_paragraph(paragraph)
|
22
|
-
|
23
|
+
parsed_content << paragraph
|
23
24
|
end
|
24
25
|
|
25
26
|
def extract_raw_text(contents)
|
26
|
-
raw_text = RawText.new(
|
27
|
+
raw_text = RawText.new(contents)
|
27
28
|
@emitter.build_raw_text(raw_text)
|
28
|
-
|
29
|
+
parsed_content << raw_text
|
29
30
|
end
|
30
31
|
|
31
32
|
private
|
32
33
|
|
33
|
-
def
|
34
|
-
|
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?
|
data/lib/bookie/version.rb
CHANGED
data/test/units/document_test.rb
CHANGED
@@ -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.
|
8
|
+
refute document.contents.empty?, "contents should not be empty"
|
9
9
|
end
|
10
10
|
end
|
data/test/units/parser_test.rb
CHANGED
@@ -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,
|
12
|
+
assert_equal 8, parsed_content.length
|
14
13
|
|
15
|
-
actual_paragraph =
|
16
|
-
actual_paragraph_text = actual_paragraph.
|
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
|
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
|
24
|
+
assert_equal 6, parsed_content.length
|
27
25
|
end
|
28
|
-
|
29
26
|
end
|