Skip to content

Commit

Permalink
Add support for page numbers
Browse files Browse the repository at this point in the history
  • Loading branch information
georgboe committed Jul 5, 2012
1 parent 9e0a688 commit 22f1e4a
Show file tree
Hide file tree
Showing 4 changed files with 29 additions and 8 deletions.
3 changes: 2 additions & 1 deletion README.markdown
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ This library will parse these annotations/clippings and give you an array of rub

To install kindleclippings just run

% sudo gem install kindleclippings
% gem install kindleclippings

## Usage

Expand Down Expand Up @@ -73,3 +73,4 @@ You can filter the results by author, book title or date by using the methods `b
* Masatomo Nakano
* brokensandals
* fboucheros
* David Ramalho
9 changes: 5 additions & 4 deletions lib/kindleclippings/clipping.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,24 +3,25 @@ module KindleClippings
class Clipping
require 'date'

attr_accessor :book_title, :author, :type, :location, :added_on, :content
attr_accessor :book_title, :author, :type, :location, :added_on, :content, :page

def initialize()
end

def initialize(title, author, type, location, added_on, content)
def initialize(title, author, type, location, added_on, content, page = "0")
@book_title = title
@author = author
@type = type
@location = location
@added_on = DateTime.strptime(added_on, "%A, %B %d, %Y, %I:%M %p")
@content = content
@page = page.to_i
end

def to_s
"#{@book_title} (#{author})\n" +
"- #{@type} Loc. #{@location} | Added on #{@added_on.strftime('%A, %B %d, %Y, %I:%M %p')}\n\n" +
"- #{@type} #{('on Page ' + @page.to_s + ' | ') if @page > 0}Loc. #{@location} | Added on #{@added_on.strftime('%A, %B %d, %Y, %I:%M %p')}\n\n" +
"#{@content}"
end
end
end
end
6 changes: 3 additions & 3 deletions lib/kindleclippings/parser.rb
Original file line number Diff line number Diff line change
Expand Up @@ -37,19 +37,19 @@ def parse_clipping(clipping)
end

first_line = lines[0].strip.scan(/^(.+) \((.+)\)$/i).first
second_line = lines[1].strip.scan(/^- (.+?) Loc. ([0-9-]*?) +\| Added on (.+)$/i).first
second_line = lines[1].strip.scan(/^- (.+?) (?:on page (\d+) \| |)Loc. ([0-9-]*?) +\| Added on (.+)$/i).first

if first_line.nil?
title = lines[0].strip
author = ""
else
title, author = *first_line
end
type, location, date = *second_line
type, page, location, date = *second_line

content = lines[3..-1].join("")

Clipping.new(title, author, type.to_sym, location, date, content.strip)
Clipping.new(title, author, type.to_sym, location, date, content.strip, page)
end
end
end
19 changes: 19 additions & 0 deletions spec/parser_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -127,6 +127,25 @@
clipping.author.should eql("")
end

it "should support page information" do
input =<<EOF
Book title (Author)
- Highlight on Page 142 | Loc. 2170-74 | Added on Tuesday, July 03, 2012, 07:41 PM
Lorem ipsum dolor sit amet, consectetur adipiscing elit. In velit sem, blandit rhoncus iaculis interdum, ultrices sed sem. Nullam lacus urna, interdum eu fringilla et, fringilla id libero.
EOF
clipping = @parser.send(:parse_clipping, input)

clipping.should_not be_nil
clipping.book_title.should eql("Book title")
clipping.author.should eql("Author")
clipping.type.should eql(:Highlight)
clipping.added_on.should eql(DateTime.new(2012, 07, 03, 19, 41, 00))
clipping.content.should eql("Lorem ipsum dolor sit amet, consectetur adipiscing elit. In velit sem, blandit rhoncus iaculis interdum, ultrices sed sem. Nullam lacus urna, interdum eu fringilla et, fringilla id libero.")
clipping.page.should eql(142)

clipping.to_s.should eql(input.rstrip)
end
end


Expand Down

0 comments on commit 22f1e4a

Please sign in to comment.