IBM®
跳转到主要内容
    中国 [选择]    使用条款
 
 
Select a scope: Search for:    
    首页    产品    服务与解决方案     支持与下载    个性化服务    
跳转到主要内容

developerWorks 中国  >  XML  >

使用 EPUB 制作数字图书

基于 XML 的开放式 eBook 格式

developerWorks
前一页第 5 页,共 10 页后一页

文档选项

样例代码


对本教程的评价

帮助我们改进这些内容


从 DocBook 到 EPUB

DocBook 是需要维护大型技术文档的开发人员常用的选择。与传统字处理程序生成的文件不同,可以使用基于文本的版本控制系统管理 DocBook 输出。由于 DocBook 是 XML,很容易将其转换成不同输出格式。2008 年夏天出现了正式的 DocBook XSL 项目,将 EPUB 作为一种输出格式。

使用 XSLT 运行 DocBook-to-EPUB 管道

从一个简单 DocBook 文档开始,如 清单 14 所示。 该文档的类型为 book,包括前言、两个章节以及标题页面中内联显示的图像。图像和 DocBook 源文件的目录相同。可以自己创建该文件和标题页,也可 下载 本文提供的例子。


清单 14. 简单的 DocBook 图书
					
<?xml version="1.0" encoding="utf-8"?`>  
<book>
  <bookinfo>
    <title>My EPUB book</title>
    <author><firstname>Liza</firstname>
            <surname>Daly</surname></author>
    <volumenum>1234</volumenum>
  </bookinfo>
  <preface id="preface">  
    <title>Title page</title>
    <figure id="cover-image">
      <title>Our EPUB cover image icon</title>
      <graphic fileref="cover.png"/>
    </figure>
  </preface>
  <chapter id="chapter1"> 
    <title>This is a pretty simple DocBook example</title>
    <para>
      Not much to see here. 
    </para>
  </chapter>
  <chapter id="end-notes"> 
    <title>End notes</title>
    <para>
      This space intentionally left blank.
    </para>
  </chapter>
</book>

然后从 参考资料 下载最新版本的 DocBook XSL 样式表,并安装 xsltproc 或 Saxon 之类的 XSLT 处理程序。本文使用 xsltproc,大多数类 UNIX 系统上都能找到。转换 DocBook 文件,只需要用 DocBook XSL 中包含的 EPUB 模块运行该文件即可,如 清单 15 所示。


清单 15. 将 DocBook 转化成 EPUB
					
$ xsltproc /path/to/docbook-xsl-1.74.0/epub/docbook.xsl docbook.xml
Writing OEBPS/bk01-toc.html for book
Writing OEBPS/pr01.html for preface(preface)
Writing OEBPS/ch01.html for chapter(chapter1)
Writing OEBPS/ch02.html for chapter(end-notes)
Writing OEBPS/index.html for book
Writing OEBPS/toc.ncx
Writing OEBPS/content.opf
Writing META-INF/container.xml

定制 DocBook XSL

DocBook-to-EPUB 转换管道仍然比较新,可能需要定制 XSLT 以得到需要的结果。

然后添加 mimetype 文件并建立 epub+zip 档案。清单 16 显示了这三个命令和通过 EpubCheck 验证程序的结果。


清单 16. 从 DocBook 创建 EPUB 档案
					
$ echo "application/epub+zip" > mimetype
$ zip -0Xq  my-book.epub mimetype
$ zip -Xr9D my-book.epub *
$ java -jar epubcheck.jar my-book.epub 
No errors or warnings detected

太简单了!图 3 显示了 ADE 中的结果。


图 3. ADE 显示了从 DocBook 转化得到的 EPUB
ADE 显示了从 DocBook 转化得到的 EPUB




回页首


利用 Python 和 lxml 实现 DocBook-to-EPUB 转换自动化

DocBook XSL 大大降低了生成 EPUB 的复杂性,但是在 XSLT 之外还有几个步骤要执行。最后一节给出的 Python 示例程序能够生成有效的 EPUB 包。本教程显示了不同的方法,可 下载 完整的 docbook2epub.py 程序。

可使用不同的 Python XSLT 库,我喜欢 lxml。它不但提供了 XSLT 1.0 必要的功能,而且解析效率高,完全支持 XPath 1.0,提供了专门处理 HTML 的扩展。如果喜欢不同的库或者使用 Python 以外的编程语言,修改这些例子也很简单。

用 lxml 调用 DocBook XSL

使用 lxml 调用 XSLT 的最有效办法是事先解析 XSLT,然后创建反复使用的转换器。这样很方便,因为我的 DocBook-to-EPUB 需要转换多个 DocBook 文件。如 清单 17 所示。


清单 17. 使用 lxml 运行 DocBook XSL
					
import os.path
from lxml import etree

def convert_docbook(docbook_file):
    docbook_xsl = os.path.abspath('docbook-xsl/epub/docbook.xsl')
    # Give the XSLT processor the ability to create new directories
    xslt_ac = etree.XSLTAccessControl(read_file=True, 
                                      write_file=True, 
                                      create_dir=True, 
                                      read_network=True, 
                                      write_network=False)
    transform = etree.XSLT(etree.parse(docbook_xsl), access_control=xslt_ac)
    transform(etree.parse(docbook_file))

DocBook XSL 中的 EPUB 模块创建输出文件本身,因此转换过程什么也不返回。相反,DocBook 在当前工作目录中创建了两个文件夹(META-INF 和 OEBPS),包含转换的结果。

将图片和其他资源复制到档案中

DocBook XSL 不会对文档中使用的任何图片执行操作,仅仅创建元数据文件和要呈现的 XHTML。由于 EPUB 规范要求 content.opf manifest 列出所有资源,可以预料到 manifest 将寻找原始 DocBook 文件引用的任何图片。清单 18 显示了这种技术,其中假定 path 变量包含 DocBook XSLT 生成的、当前所处理的 EPUB 的路径。


清单 18. 解析 OPF 内容文件以寻找缺失的资源
					
import os.path, shutil
from lxml import etree

def find_resources(path='/path/to/our/epub/directory'):
    opf = etree.parse(os.path.join(path, 'OEBPS', 'content.opf'))

    # All the opf:item elements are resources
    for item in opf.xpath('//opf:item', 
                          namespaces= { 'opf': 'http://www.idpf.org/2007/opf' }):

        # If the resource was not already created by DocBook XSL itself, 
        # copy it into the OEBPS folder
        href = item.attrib['href']
        referenced_file = os.path.join(path, 'OEBPS', href):
        if not os.path.exists(referenced_file):
            shutil.copy(href, os.path.join(path, 'OEBPS'))

自动创建 mimetype

DocBook XSL 不会创建 mimetype 文件,不过 清单 19 中所示的代码可以完成这项任务。


清单 19. 创建 mimetype 文件
					
def create_mimetype(path='/path/to/our/epub/directory'):
    f = '%s/%s' % (path, 'mimetype')
    f = open(f, 'w')
    # Be careful not to add a newline here
    f.write('application/epub+zip')
    f.close()

用 Python 创建 EPUB 包

现在只需要将文件打包成有效的 EPUB ZIP 包。需要分两步:将未经压缩的 mimetype 文件作为第一个文件加进去,然后添加其他目录。如 清单 20 所示。


清单 20. 使用 Python zipfile 模块创建 EPUB 包
					
import zipfile, os

def create_archive(path='/path/to/our/epub/directory'):
    '''Create the ZIP archive.  The mimetype must be the first file in the archive 
    and it must not be compressed.'''

    epub_name = '%s.epub' % os.path.basename(path)

    # The EPUB must contain the META-INF and mimetype files at the root, so 
    # we'll create the archive in the working directory first and move it later
    os.chdir(path)    

    # Open a new zipfile for writing
    epub = zipfile.ZipFile(epub_name, 'w')

    # Add the mimetype file first and set it to be uncompressed
    epub.write(MIMETYPE, compress_type=zipfile.ZIP_STORED)
    
    # For the remaining paths in the EPUB, add all of their files
    # using normal ZIP compression
    for p in os.listdir('.'):
        for f in os.listdir(p):
            epub.write(os.path.join(p, f)), compress_type=zipfile.ZIP_DEFLATED)
    epub.close()

好了!切记要进行验证。





回页首



前一页第 5 页,共 10 页后一页
    关于 IBM 隐私条约 联系 IBM 使用条款