Hatena::ブログ(Diary)

PARAGRAPHS

 | 

2010-07-05

JavaScript で ePub を生成するライブラリ (というか EeePub を JavaScript写経してみた)

http://github.com/tily/js-eeepub

JavaScript で ePub を読むだけじゃなくて作成できたら面白いだろうなと思って、勉強も兼ねて jugyo さんの EeePubJavaScript に移植してみた。

不完全なところが色々あるし、もっと便利にできそうな気がするんだけど、だいぶ飽きてきたんで一旦 github で公開してみる。

zip.jsxmlbuilder.js に依存している。あと、JSSpec でスペックを書いてみた。スペックに書いてるまんまだけど、こんな感じで使う。

var easy = new EeePub.Easy({
    title:'sample',
    creator:'tily',
    identifiers:'http://example.com/book/foo',
    uid:'http://example.com/book/foo',
})
easy.sections.push(['1. foo', [
    '<?xml version="1.0" encoding="UTF-8"?>', "\n",
    '<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN" "http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">', "\n",
    '<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="ja">', "\n",
    '  <head>', "\n",
    '    <title>foo</title>', "\n",
    '  </head>', "\n",
    '  <body>', "\n",
    '    <p>', "\n",
    '    foo foo foo foo foo foo', "\n",
    '    </p>', "\n",
    '  </body>', "\n",
    '</html>', "\n"
].join('')])
easy.sections.push(['2. bar', [
    '<?xml version="1.0" encoding="UTF-8"?>', "\n",
    '<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN" "http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">', "\n",
    '<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="ja">', "\n",
    '  <head>', "\n",
    '    <title>bar</title>', "\n",
    '  </head>', "\n",
    '  <body>', "\n",
    '    <p>', "\n",
    '    bar bar bar bar bar bar', "\n",
    '    </p>', "\n",
    '  </body>', "\n",
    '</html>', "\n"
].join('')])
console.log(easy.save())

こんな感じのが出力される。

data:application/zip;base64,UEsDBAoAAAAAANwG5TzeiCCNNQEAADUBAAAOAAAAc2VjdGlvbl8wLmh0bWw8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1JTkYvY29udGFpbmVyLnhtbDw/eG1sIHZlcnNpb249IjEuMCIgZW5jb2Rpbmc9IlVURi04Ij8+Cjxjb250YWluZXIgeG1sbnM9InVybjpvYXNpczpuYW1lczp0YzpvcGVuZG9jdW1lbnQ6eG1sbnM6Y29udGFpbmVyIiB2ZXJzaW9uPSIxLjAiPjxyb290ZmlsZXM+PHJvb3RmaWxlIGZ1bGwtcGF0aD0iY29udGVudC5vcGYiIG1lZGlhLXR5cGU9ImFwcGxpY2F0aW9uL29lYnBzLXBhY2thZ2UreG1sIj48L3Jvb3RmaWxlPjwvcm9vdGZpbGVzPjwvY29udGFpbmVyPlBLAQIXAwoAAAAAANwG5TzeiCCNNQEAADUBAAAOAAAAAAAAAAAAAACkgQAAAABzZWN0aW9uXzAuaHRtbFBLAQIXAwoAAAAAANwG5TzrI298NQEAADUBAAAOAAAAAAAAAAAAAACkgWEBAABzZWN0aW9uXzEuaHRtbFBLAQIXAwoAAAAAANwG5TyyTvYhjQIAAI0CAAAHAAAAAAAAAAAAAACkgcICAAB0b2MubmN4UEsBAhcDCgAAAAAA3AblPAK00/tuAwAAbgMAAAsAAAAAAAAAAAAAAKSBdAUAAGNvbnRlbnQub3BmUEsBAhcDCgAAAAAA3AblPG9hqywUAAAAFAAAAAgAAAAAAAAAAAAAAKSBCwkAAG1pbWV0eXBlUEsBAhcDCgAAAAAA3AblPAAAAAAAAAAAAAAAAAkAAAAAAAAAAAAQAO1BRQkAAE1FVEEtSU5GL1BLAQIXAwoAAAAAANwG5Ty6T6vP8wAAAPMAAAAWAAAAAAAAAAAAAACkgWwJAABNRVRBLUlORi9jb250YWluZXIueG1sUEsFBgAAAAAHAAcAlwEAAJMKAAAAAA==

FirefoxRhino (jgate) で動くことを確認しています。生成された ePub ファイルについては ibis readerEPUBReader で読めることを確認している。スペックは E4X を使っているので Firefox でしか動かない。

これを使って「あとで読みたい」と思ったページを ePub にしたり LDR のピンを ePub にするやつとか作りたいな。というかその前にまともに ePub を読めるモバイル端末が欲しい。

 |