localhots.xyz fb2 / master
Regenerate gemspec for version 0.1.3 magnolia-fan 10 years ago
12 changed file(s) with 249 addition(s) and 14 deletion(s). Raw diff Collapse all Expand all
99 gem "bundler", "~> 1.0.0"
1010 gem "jeweler", "~> 1.6.4"
1111 gem "rcov", ">= 0"
12 gem "rdoc"
1213 end
14
15 gem "nokogiri"
0 GEM
1 remote: http://rubygems.org/
2 specs:
3 git (1.2.5)
4 jeweler (1.6.4)
5 bundler (~> 1.0)
6 git (>= 1.2.5)
7 rake
8 nokogiri (1.5.0)
9 rake (0.9.2)
10 rcov (0.9.9)
11 rdoc (3.8)
12 shoulda (2.11.3)
13
14 PLATFORMS
15 ruby
16
17 DEPENDENCIES
18 bundler (~> 1.0.0)
19 jeweler (~> 1.6.4)
20 nokogiri
21 rcov
22 rdoc
23 shoulda
0 Copyright (c) 2011 magnolia-fan
0 Copyright (c) 2011 Gregory Eremin
11
22 Permission is hereby granted, free of charge, to any person obtaining
33 a copy of this software and associated documentation files (the
0 = fb2
0 == Usage
11
2 Description goes here.
2 require 'fb2'
3 @book = Fb2::parse(open('mybook.fb2'))
34
4 == Contributing to fb2
5 == Contributing
56
67 * Check out the latest master to make sure the feature hasn't been implemented or the bug hasn't been fixed yet
78 * Check out the issue tracker to make sure someone already hasn't requested it and/or contributed it
1314
1415 == Copyright
1516
16 Copyright (c) 2011 magnolia-fan. See LICENSE.txt for
17 further details.
18
17 Copyright (c) 2011 Gregory Eremin. See LICENSE.txt for further details.
1616 gem.name = "fb2"
1717 gem.homepage = "http://github.com/magnolia-fan/fb2"
1818 gem.license = "MIT"
19 gem.summary = %Q{TODO: one-line summary of your gem}
20 gem.description = %Q{TODO: longer description of your gem}
19 gem.summary = %Q{FB2 util written in ruby}
20 gem.description = %Q{Parses FB2 (FictionBook 2) xml format into nice objects. Uses nokogiri.}
2121 gem.email = "magnolia_fan@me.com"
22 gem.authors = ["magnolia-fan"]
22 gem.authors = ["Gregory Eremin"]
23 gem.files += Dir['lib/**/*.rb']
2324 # dependencies defined in Gemfile
2425 end
2526 Jeweler::RubygemsDotOrgTasks.new
4142
4243 task :default => :test
4344
44 require 'rake/rdoctask'
45 Rake::RDocTask.new do |rdoc|
45 require 'rdoc/task'
46 RDoc::Task.new do |rdoc|
4647 version = File.exist?('VERSION') ? File.read('VERSION') : ""
4748
4849 rdoc.rdoc_dir = 'rdoc'
0 0.1.3
0 # Generated by jeweler
1 # DO NOT EDIT THIS FILE DIRECTLY
2 # Instead, edit Jeweler::Tasks in Rakefile, and run 'rake gemspec'
3 # -*- encoding: utf-8 -*-
4
5 Gem::Specification.new do |s|
6 s.name = %q{fb2}
7 s.version = "0.1.3"
8
9 s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
10 s.authors = [%q{Gregory Eremin}]
11 s.date = %q{2011-07-17}
12 s.description = %q{Parses FB2 (FictionBook 2) xml format into nice objects. Uses nokogiri.}
13 s.email = %q{magnolia_fan@me.com}
14 s.extra_rdoc_files = [
15 "LICENSE.txt",
16 "README.rdoc"
17 ]
18 s.files = [
19 ".document",
20 "Gemfile",
21 "Gemfile.lock",
22 "LICENSE.txt",
23 "README.rdoc",
24 "Rakefile",
25 "VERSION",
26 "fb2.gemspec",
27 "lib/fb2.rb",
28 "lib/models/fb2_book.rb",
29 "lib/models/fb2_section.rb",
30 "test/fixtures/sample.fb2",
31 "test/helper.rb",
32 "test/test_fb2.rb"
33 ]
34 s.homepage = %q{http://github.com/magnolia-fan/fb2}
35 s.licenses = [%q{MIT}]
36 s.require_paths = [%q{lib}]
37 s.rubygems_version = %q{1.8.5}
38 s.summary = %q{FB2 util written in ruby}
39
40 if s.respond_to? :specification_version then
41 s.specification_version = 3
42
43 if Gem::Version.new(Gem::VERSION) >= Gem::Version.new('1.2.0') then
44 s.add_runtime_dependency(%q<nokogiri>, [">= 0"])
45 s.add_development_dependency(%q<shoulda>, [">= 0"])
46 s.add_development_dependency(%q<bundler>, ["~> 1.0.0"])
47 s.add_development_dependency(%q<jeweler>, ["~> 1.6.4"])
48 s.add_development_dependency(%q<rcov>, [">= 0"])
49 s.add_development_dependency(%q<rdoc>, [">= 0"])
50 else
51 s.add_dependency(%q<nokogiri>, [">= 0"])
52 s.add_dependency(%q<shoulda>, [">= 0"])
53 s.add_dependency(%q<bundler>, ["~> 1.0.0"])
54 s.add_dependency(%q<jeweler>, ["~> 1.6.4"])
55 s.add_dependency(%q<rcov>, [">= 0"])
56 s.add_dependency(%q<rdoc>, [">= 0"])
57 end
58 else
59 s.add_dependency(%q<nokogiri>, [">= 0"])
60 s.add_dependency(%q<shoulda>, [">= 0"])
61 s.add_dependency(%q<bundler>, ["~> 1.0.0"])
62 s.add_dependency(%q<jeweler>, ["~> 1.6.4"])
63 s.add_dependency(%q<rcov>, [">= 0"])
64 s.add_dependency(%q<rdoc>, [">= 0"])
65 end
66 end
67
0 require 'nokogiri'
1 require 'models/fb2_book'
2 require 'models/fb2_section'
3
4 class Fb2
5 def self.parse data
6 xml = Nokogiri::XML(data)
7
8 # building book
9 description = xml.css('description')
10 book = Fb2Book.new
11 book.title = description.css('title-info > book-title').text
12 book.author_first_name = description.css('title-info > author > first-name').text
13 book.author_last_name = description.css('title-info > author > last-name').text
14 book.annotation = description.css('title-info > annotation > p').text
15 book.genre = description.css('title-info > genre').text
16 book.lang = description.css('title-info > lang').text
17 book.keywords = description.css('title-info > keywords').text.split(', ')
18 book.sections = []
19
20 # building sections
21 xml.css('body > section').each do |s|
22 section = Fb2Section.new
23 section.title = s.css('title > p').text
24 section.contents = s.css('p').map{ |e| e.text }.drop(1)
25 book.sections << section
26 end
27
28 book
29 end
30 end
0 class Fb2Book
1 attr_accessor :title, :author_first_name, :author_last_name, :annotation, :genre, :lang, :keywords, :sections
2 end
0 class Fb2Section
1 attr_accessor :title, :contents
2 end
0 <?xml version="1.0" encoding="utf8"?>
1 <FictionBook xmlns:l="http://www.w3.org/1999/xlink" xmlns:xlink="http://www.w3.org/1999/xlink" xmlns="http://www.gribuser.ru/xml/fictionbook/2.0">
2 <description>
3 <title-info>
4 <genre>fiction</genre>
5 <author>
6 <first-name>John</first-name>
7 <last-name>Doe</last-name>
8 </author>
9 <book-title>Fiction Book</book-title>
10 <annotation>
11 <p>Hello</p>
12 </annotation>
13 <keywords>john, doe, fiction</keywords>
14 <date value="2011-07-18">18.07.2011</date>
15 <coverpage></coverpage>
16 <lang>en</lang>
17 </title-info>
18 <document-info>
19 <author>
20 <first-name></first-name>
21 <last-name></last-name>
22 <nickname></nickname>
23 </author>
24 <program-used>Fb2 Gem</program-used>
25 <date value="2011-07-18">18.07.2011</date>
26 <src-url></src-url>
27 <src-ocr></src-ocr>
28 <id></id>
29 <version>1.0</version>
30 </document-info>
31 <publish-info>
32 </publish-info>
33 </description>
34 <body>
35 <title>
36 <p>John Doe</p>
37 <empty-line/>
38 <p>Fiction Book</p>
39 </title>
40 <section>
41 <title>
42 <p>Chapter 1</p>
43 </title>
44 <p>Line one of the first chapter</p>
45 <p>Line two of the first chapter</p>
46 <p>Line three of the first chapter</p>
47 </section>
48 <section>
49 <title>
50 <p>Chapter 2</p>
51 </title>
52 <p>Line one of the second chapter</p>
53 <p>Line two of the second chapter</p>
54 <p>Line three of the second chapter</p>
55 <p>Line four of the second chapter</p>
56 </section>
57 </body>
58 </FictionBook>
00 require 'helper'
11
22 class TestFb2 < Test::Unit::TestCase
3 should "probably rename this file and start testing for real" do
4 flunk "hey buddy, you should probably rename this file and start testing for real"
3 context "parsing" do
4 setup do
5 @book = Fb2::parse(open('test/fixtures/sample.fb2'))
6 end
7
8 should "parse books" do
9 assert_instance_of(Book, @book)
10 end
11
12 should "load correct title" do
13 assert_equal('Fiction Book', @book.title)
14 end
15
16 should "load correct author" do
17 assert_equal('John Doe', [@book.author_first_name, @book.author_last_name].join(' '))
18 end
19
20 should "load correct annotation" do
21 assert_equal('Hello', @book.annotation)
22 end
23
24 should "load correct genre" do
25 assert_equal('fiction', @book.genre)
26 end
27
28 should "load correct language" do
29 assert_equal('en', @book.lang)
30 end
31
32 should "load correct keywords" do
33 assert_equal(['john', 'doe', 'fiction'], @book.keywords)
34 end
35
36 should "load all two chapters" do
37 assert_equal(2, @book.sections.length)
38 end
39
40 should "load correct second chapter's title" do
41 assert_equal('Chapter 2', @book.sections[1].title)
42 end
43
44 should "load all strings in each section" do
45 assert_equal(3, @book.sections[0].contents.length)
46 assert_equal(4, @book.sections[1].contents.length)
47 end
548 end
649 end