bookie 0.0.3 → 0.0.4

Sign up to get free protection for your applications and to get access to all the features.
data/CHANGELOG.md CHANGED
@@ -14,3 +14,8 @@ anything else.
14
14
  Added some basic emitters for PDF and HTML paragraphs. Note these are proofs of
15
15
  concept ONLY, not meant for practical use. See examples/simple_emitters.rb to
16
16
  see how they're used.
17
+
18
+ ## 0.0.4 (2011.04.24)
19
+
20
+ Removed HTML emitter for now. Expanded parser and PDF emitter to support
21
+ preformatted text. See examples/preformatted.rb for to see how it works.
Binary file
@@ -0,0 +1,11 @@
1
+ require_relative "../lib/bookie"
2
+ Prawn.debug = true
3
+
4
+ file = "#{File.dirname(__FILE__)}/../test/fixtures/preformatted_blocks.md"
5
+
6
+ pdf_document = Bookie::Document.new(file, Bookie::Emitters::PDF.new)
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
@@ -1,10 +1,11 @@
1
1
  require_relative "../lib/bookie"
2
2
  Prawn.debug = true
3
3
 
4
- test_file = "#{File.dirname(__FILE__)}/../test/fixtures/multi_paragraph_document.md"
4
+ file = "#{File.dirname(__FILE__)}/../test/fixtures/multi_paragraph_document.md"
5
5
 
6
- html_document = Bookie::Document.new(test_file, Bookie::Emitters::HTML.new)
7
- File.open("output.html", "w") { |f| f << html_document.render }
6
+ pdf_document = Bookie::Document.new(file, Bookie::Emitters::PDF.new)
8
7
 
9
- pdf_document = Bookie::Document.new(test_file, Bookie::Emitters::PDF.new)
10
- File.open("output.pdf", "w") { |f| f << pdf_document.render }
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
@@ -7,8 +7,8 @@ module Bookie
7
7
 
8
8
  attr_reader :emitter, :contents
9
9
 
10
- def render
11
- @emitter.render
10
+ def render(*args)
11
+ @emitter.render(*args)
12
12
  end
13
13
  end
14
14
  end
@@ -3,20 +3,8 @@ module Bookie
3
3
  class Null
4
4
  def build_paragraph(paragraph)
5
5
  end
6
- end
7
-
8
- class HTML
9
-
10
- def initialize
11
- @output = ""
12
- end
13
-
14
- def build_paragraph(paragraph)
15
- @output << "<p>#{paragraph.children.first}</p>"
16
- end
17
6
 
18
- def render
19
- "<html><body>#{@output}</body></html>"
7
+ def build_raw_text(paragraphs)
20
8
  end
21
9
  end
22
10
 
@@ -25,18 +13,49 @@ module Bookie
25
13
 
26
14
  def initialize
27
15
  @document = new_prawn_document
16
+ @document.extend(Prawn::Measurements)
28
17
  @paragraphs = []
29
18
  end
30
19
 
31
20
  def build_paragraph(paragraph)
32
- @paragraphs << paragraph.children.first.gsub(/\s+/," ")
21
+ @paragraphs << { text: paragraph.children.first.strip }
33
22
  end
34
23
 
35
- def render
36
- @document.text(@paragraphs.join("\n\n"), align: :justify, size: 10)
24
+ def build_raw_text(raw_text)
25
+ sanitized_text = raw_text.children.first.
26
+ gsub(" ", Prawn::Text::NBSP).strip
27
+
28
+ @paragraphs << { text: sanitized_text,
29
+ font: "Courier",
30
+ size: 8 }
31
+ end
32
+
33
+ def render(params)
34
+ render_header(params)
35
+
36
+ # there needs to be a better way
37
+ @paragraphs = @paragraphs.reduce([]) { |s,r| s + [r, { text: "\n\n" }] }
38
+ @paragraphs.pop
39
+
40
+ @document.formatted_text(@paragraphs, align: :justify, size: 10)
37
41
  @document.render
38
42
  end
39
43
 
44
+ def render_header(params)
45
+ @document.instance_eval do
46
+ text "<b>#{params[:header]}</b>",
47
+ size: 12, align: :right, inline_format: true
48
+ stroke_horizontal_rule
49
+
50
+ move_down in2pt(0.1)
51
+
52
+ text "<b>#{params[:title]}</b>",
53
+ size: 18, align: :right, inline_format: true
54
+
55
+ move_down in2pt(1.25)
56
+ end
57
+ end
58
+
40
59
  private
41
60
 
42
61
  def new_prawn_document
data/lib/bookie/parser.rb CHANGED
@@ -1,6 +1,7 @@
1
1
  module Bookie
2
- ContentTree = Struct.new(:children)
3
- Paragraph = Struct.new(:children)
2
+ ContentTree = Struct.new(:children)
3
+ Paragraph = Struct.new(:children)
4
+ RawText = Struct.new(:children)
4
5
 
5
6
  class Parser
6
7
  def self.parse(contents, emitter=Bookie::Emitters::Null.new)
@@ -15,19 +16,60 @@ module Bookie
15
16
  generate_document_tree(contents)
16
17
  end
17
18
 
18
- def extract_paragraphs(contents)
19
- contents.split(/\n\n+/).each do |e|
20
- paragraph = Paragraph.new([e])
21
- @emitter.build_paragraph(paragraph)
22
- document_tree.children << paragraph
23
- end
19
+ def extract_paragraph(contents)
20
+ paragraph = Paragraph.new([contents.gsub(/\s+/," ")])
21
+ @emitter.build_paragraph(paragraph)
22
+ document_tree.children << paragraph
23
+ end
24
+
25
+ def extract_raw_text(contents)
26
+ raw_text = RawText.new([contents])
27
+ @emitter.build_raw_text(raw_text)
28
+ document_tree.children << raw_text
24
29
  end
25
30
 
26
31
  private
27
32
 
28
33
  def generate_document_tree(contents)
29
34
  @document_tree = ContentTree.new([])
30
- extract_paragraphs(contents)
35
+
36
+ lines = contents.lines.to_a
37
+ mode = nil
38
+
39
+ until lines.empty?
40
+ line = lines.shift
41
+ case
42
+ when mode == nil
43
+ if line[/^ {4,}/]
44
+ mode = :raw
45
+ chunk = line[4..-1]
46
+ else
47
+ mode = :paragraph
48
+ chunk = line
49
+ end
50
+ when mode == :raw
51
+ chunk << (line.chomp.empty? ? "\n" : line[4..-1].to_s)
52
+
53
+ if lines.first =~ /^ {0,3}\S/
54
+ mode = nil
55
+ extract_raw_text(chunk)
56
+ end
57
+ when mode == :paragraph
58
+ if line.chomp.empty?
59
+ mode = nil
60
+ extract_paragraph(chunk)
61
+ else
62
+ chunk << line
63
+ end
64
+ end
65
+ end
66
+
67
+ case mode
68
+ when :paragraph
69
+ extract_paragraph(chunk)
70
+ when :raw
71
+ extract_raw_text(chunk)
72
+ end
31
73
  end
32
74
  end
33
75
  end
@@ -1,3 +1,3 @@
1
1
  module Bookie
2
- VERSION = "0.0.3"
2
+ VERSION = "0.0.4"
3
3
  end
@@ -0,0 +1,39 @@
1
+ I decided to start off this newsletter with one of the most basic but
2
+ essential pieces of knowledge you can have about Ruby's object model:
3
+ the way it looks up methods. Let's do a little exploration by working
4
+ through a few examples.
5
+
6
+ Below we have a simple report class who's job is to perform some basic
7
+ data manipulations and then produce some text output.
8
+
9
+ class Report
10
+ def initialize(ledger)
11
+ @balance = ledger.inject(0) { |sum, (k,v)| sum + v }
12
+ @credits, @debits = ledger.partition { |k,v| v > 0 }
13
+ end
14
+
15
+ attr_reader :credits, :debits, :balance
16
+
17
+ def formatted_output
18
+ "Current Balance: #{balance}\n\n" +
19
+ "Credits:\n\n#{formatted_line_items(credits)}\n\n" +
20
+ "Debits:\n\n#{formatted_line_items(debits)}"
21
+ end
22
+
23
+ def formatted_line_items(items)
24
+ items.map { |k, v| "#{k}: #{'%.2f' % v.abs}" }.join("\n")
25
+ end
26
+ end
27
+
28
+ The following code demonstrates how we'd make use of this newly created class.
29
+
30
+ ledger = [ ["Deposit Check #123", 500.15],
31
+ ["Fancy Shoes", -200.25],
32
+ ["Fancy Hat", -54.40],
33
+ ["ATM Deposit", 1200.00],
34
+ ["Kitteh Litteh", -5.00] ]
35
+
36
+ report = Report.new(ledger)
37
+ puts report.formatted_output
38
+
39
+ Amazing, right? (Yadda... yadda.. yadda)
@@ -5,7 +5,8 @@ context "A Parser" do
5
5
  sample_text = File.read(fixture("multi_paragraph_document.md"))
6
6
 
7
7
  # NOTE: Is this the behavior we'd expect?
8
- sample_paragraph_text = File.read(fixture("single_paragraph.md")).chomp
8
+ sample_paragraph_text = File.read(fixture("single_paragraph.md"))
9
+ .gsub(/\s+/," ")
9
10
 
10
11
  tree = Bookie::Parser.parse(sample_text)
11
12
 
@@ -16,4 +17,13 @@ context "A Parser" do
16
17
 
17
18
  assert_equal sample_paragraph_text, actual_paragraph_text
18
19
  end
20
+
21
+ test "should know about preformatted text" do
22
+ sample_text = File.read(fixture("preformatted_blocks.md"))
23
+
24
+ tree = Bookie::Parser.parse(sample_text)
25
+
26
+ assert_equal 7, tree.children.length
27
+ end
28
+
19
29
  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.3
5
+ version: 0.0.4
6
6
  platform: ruby
7
7
  authors:
8
8
  - Gregory Brown
@@ -10,7 +10,7 @@ autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
12
 
13
- date: 2011-04-23 00:00:00 -04:00
13
+ date: 2011-04-24 00:00:00 -04:00
14
14
  default_executable:
15
15
  dependencies:
16
16
  - !ruby/object:Gem::Dependency
@@ -40,12 +40,15 @@ files:
40
40
  - lib/bookie/version.rb
41
41
  - lib/bookie.rb
42
42
  - test/fixtures/multi_paragraph_document.md
43
+ - test/fixtures/preformatted_blocks.md
43
44
  - test/fixtures/single_paragraph.md
44
45
  - test/suite.rb
45
46
  - test/test_helper.rb
46
47
  - test/units/document_test.rb
47
48
  - test/units/parser_test.rb
49
+ - examples/preformatted.rb
48
50
  - examples/simple_emitters.rb
51
+ - doc/markdown_syntax.pdf
49
52
  - GPLv3
50
53
  - README.md
51
54
  - CHANGELOG.md