npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

cray

v2.1.6

Published

Epub parser

Downloads

146

Readme

cray npm version

Stream Based EPUB Parser.

What It Does

Cray is a simple way to make working with epub files more programmer-friendly by converting its data structures to JSON and providing some simplified structures for convenient access to that data.

Specifically, it:

  • takes a lot of essential data from any valid Epub file and makes it available in an "easy" JSON namespace
  • takes care of a lot of dirty work in determining the primary ID of a file, the location of the NAV file, the OPS root, etc

Installing As Package

npm install cray

Usage

Single Epub File

In a nutshell though, it's as simple as this:


var cray = require('cray')('./test1.epub');

// Triggered when the epub contents have been parsed.
cray.queue[0].on('finish', function() {
  console.log(parser[0]); // Entire EPUB properties accessible in this json
});

// Triggered when there is something wrong with the file stream
cray.queue[0].on('error', function(err) {
  console.log(err);
});

Multiple Epub Files

//The code example above can be modified to use the parser for multiple files.
var cray = require('cray')(['test1.epub', './test2.epub']);

// It will return an array of Epubs with Event that you can watch as 
// described in previous example.
// cray.queue[0] --> 'test1.epub' epub object
// cray.queue[1] --> 'test2.epub' epub object

Data Structure

Metadata

Important - Accessible only after finish event

Example

// Available din the array object.
// --> cray.queue[0].metadata

// Structure
// Sample -->
/* { language: 'en',
     title: 'Accessible EPUB 3',
     date: '2012-02-20',
     creator: 'Matt Garrish',
     contributor:
      [ 'O'Reilly Production Services',
        'David Futato',
        'Robert Romano',
        'Brian Sawyer',
        'Dan Fauxsmith',
        'Karen Montgomery' ],
     publisher: 'O'Reilly Media, Inc.',
     rights: 'Copyright c 2012 O'Reilly Media, Inc' } */

NAV

Important - Accessible only after finish event

Example

// Available in the array object.
// --> cray.queue[0].nav

// Structure
// Sample -->
/* { id: 'htmltoc',
        properties: 'nav',
        'media-type': 'application/xhtml+xml',
        href: 'bk01-toc.xhtml' } */

Cover

Important - Accessible only after finish event

Example

// Available in the array object.
// --> cray.queue[0].cover

// Structure
// Sample -->
/* { imagePath: 'covers/9781449328030_lrg.jpg' } */

Manifest

Important - Accessible only after finish event

Example

// Available in the array object.
// --> cray.queue[0].cover

// Structure {Array}
// Sample -->
/* [ { 'media-type': 'text/css',
          id: 'epub-css',
          href: 'css/epub.css' },
        { 'media-type': 'text/css',
          id: 'epub-tss-css',
          href: 'css/synth.css' } ] */

Spines

Important - Accessible only after finish event

Example

// Available in the array object.
// --> cray.queue[0].spines

// Structure {Array}
// Sample -->
/* [ { 'media-type': 'text/css',
          id: 'epub-css',
          href: 'css/epub.css' },
        { 'media-type': 'text/css',
          id: 'epub-tss-css',
          href: 'css/synth.css' } ] */

OPF Root

Important - Accessible only after finish event

Contains the folder name where the opf file is located.

Example

// Available in the array object.
// --> cray.queue[0].opfRoot

Events

  1. EPUB:opf:parsed DEPRECATED This event is triggered when the opf file has been completely traversed by cray. At this point the epub object will contain all the populated data structures regarding the respective epub file.

  2. EPUB:error DEPRECATED Triggered when there is a node fs error w.r.t to the epub file that is parsed.

  3. EPUB:invalid DEPRECATED Triggered when there is an invalid file structure present within the EPUB.

  4. READY Triggered when all epubs in the queue are processed.

// Example
var cray = require('cray')(['./test1.epub', 'test2.epub']);
cray.on('READY', function() {
  console.log("All processed", cray.queue /* This is an array */);
});

Command Line Tool

Cray can also be used as a command line utility by installing it globally. As described below.

npm install -g cray

Usage

cray -e test1.epub,fail.epub

┌────────────────────┬───────────┬────────────────────────────────┐
│ Epub Name          │ Directory │ Status                         │
├────────────────────┼───────────┼────────────────────────────────┤
│ test1.epub         │ CWD       │ Valid                          │
├────────────────────┼───────────┼────────────────────────────────┤
│ containerFail.epub │ CWD       │ Container XML file is missing! │
└────────────────────┴───────────┴────────────────────────────────┘

Important Using -v or --verbose, Checks the epub file using IDPF epubcheck tool, requires java to be installed.

Help

You can check out the list of options that come along with using Cray by typing the following command.

cray --help

Examples

Check out the examples folder for more info on usage.