Code Monkey home page Code Monkey logo

fb2's Introduction

Fb2 Build Status

FictionBook parser / reader.

Installation

Add this line to your application's Gemfile:

gem 'fb2'

Usage

In Ruby

require 'open-uri'

file = open("https://raw.githubusercontent.com/localhots/fb2/master/test/fixtures/sample.fb2")

# get all elements
Fb2::Book.new(file).elements
# => #<Enumerator::Lazy: #<Enumerator: #<Enumerator::Generator:0x007fa3ebb0b5f8>:each>>

Fb2::Book.new(file).elements.count
# => 2

# get only description elements
Fb2::Book.new(file).description
# =>  #<Fb2::Description:0x007fa3edc452c8 @title ... >

# get title info
Fb2::Book.new(file).description.title_info
# => #<Fb2::TitleInfo:0x007fbfb9971438 @genres=[ ... >

# get document info
Fb2::Book.new(file).description.document_info
# => #<Fb2::DocumentInfo:0x007fbfb904fb98 @id=nil ... >

# get publish info
Fb2::Book.new(file).description.publish_info
# => #<Fb2::PublishInfo:0x007fbfb936bf70 ... >

# get custom info
Fb2::Book.new(file).description.custom_info
# => nil

# get only body sections
Fb2::Book.new(file).bodies
# => #<Enumerator::Lazy: #<Enumerator::Lazy: #<Enumerator: #<Enumerator::Generator:0x007fa3edc5f420>:each>>:select>

# get first body element
Fb2::Book.new(file).bodies.first
# =>  #<Fb2::Body:0x007fbfbac45cd0 @name=nil, ... >

# get text in first body element
Fb2::Book.new(file).bodies.first.text
# [#<Fb2::Title:0x007fbfb93d6d70 @text=[#<Fb2::Paragraph:0x007fbfb93d4e58 @id=nil, @style=nil, @lang=nil, @text=[#<Fb2::Text value="John Doe">]>, #<Fb2::EmptyLine:0x007fbfb93c75c8>, #<Fb2::Paragraph:0x007fbfb93bf530 @id=nil, @style=nil, @lang=nil, @text=[#<Fb2::Text value="Fiction Book">]>], @lang=nil>, #<Fb2::Section:0x007fbfb93afd60 @id=nil, @lang=nil, @text=[#< ... >

Fb2::Book.new(file).bodies.first.text.map(&:class)
# => [Fb2::Title, Fb2::Section, Fb2::Section]

# get all binay files
Fb2::Book.new(file).binaries
# => #<Enumerator::Lazy: #<Enumerator::Lazy: #<Enumerator: ... >

In console

fb2binary -f 'tolstoy_port.png' spec/support/example.fb2 > test.png

ls -al  | grep test.png
-rw-r--r--   1 username  group  17281 Mar 31 22:02 test.png

open test.png # open file in image viewer

Development

After checking out the repo, run bin/setup to install dependencies. Then, run rake spec to run the tests. You can also run bin/console for an interactive prompt that will allow you to experiment.

To install this gem onto your local machine, run bundle exec rake install. To release a new version, update the version number in version.rb, and then run bundle exec rake release, which will create a git tag for the version, push git commits and tags, and push the .gem file to rubygems.org.

Contributing

Bug reports and pull requests are welcome on GitHub at https://github.com/zinenko/fb2.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.