Class: CbetaEpub

Inherits:
Object
  • Object
show all
Defined in:
lib/cbeta_epub.rb

Overview

Convert CBETA XML P5a to EPUB

CBETA XML P5a 可由此取得: github.com/cbeta-git/xml-p5a

Instance Method Summary collapse

Constructor Details

#initialize(temp_folder, opts = {}) ⇒ CbetaEpub

Returns a new instance of CbetaEpub.

Examples:

options = {
  epub_version: 3,
  front_page: '/path/to/front_page.xhtml',
  front_page_title: '編輯說明',
  back_page: '/path/to/back_page.xhtml',
  back_page_title: '贊助資訊',
  graphic_base: '/path/to/grphic/files/root'
}
c = CBETA::P5aToEPUB.new('/path/to/temp/working/folder', options)
c.convert_folder('/path/to/xml/roo', '/path/for/output/epubs')  

Parameters:

  • temp_folder (String)

    供 EPUB 暫存工作檔案的路徑

  • opts (Hash) (defaults to: {})

    a customizable set of options

Options Hash (opts):

  • :epub_version (Integer) — default: 3

    EPUB 版本

  • :graphic_base (String)

    圖檔路徑

    • graphic_base/covers: 封面圖檔位置

    • graphic_base/figures: 插圖圖檔位置

    • graphic_base/sd-gif: 悉曇字圖檔位置

    • graphic_base/rj-gif: 蘭札體圖檔位置

  • :front_page (String)

    內文前可以加一份 HTML 檔,例如「編輯說明」

  • :front_page_title (String)

    加在目錄的 front_page 標題

  • :back_page (String)

    內文後可以加一份 HTML 檔,例如「版權聲明」

  • :back_page_title (String)

    加在目錄的 back_page 標題

  • :juan_toc (Boolean)

    目次中是否要有卷目次,預設為 true



52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
# File 'lib/cbeta_epub.rb', line 52

def initialize(temp_folder, opts={})
  @temp_folder = temp_folder
  @settings = {
    epub_version: 3,
    juan_toc: true
  }
  @settings.merge!(opts)
  @cbeta = CBETA.new
  @gaijis = CBETA::Gaiji.new
  
  # 載入 unicode 1.1 字集列表
  fn = File.join(DATA, 'unicode-1.1.json')
  json = File.read(fn)
  @unicode1 = JSON.parse(json)
end

Instance Method Details

#convert_file(input_path, output_path) ⇒ Object

將某個 xml 轉為一個 EPUB

Parameters:

  • input_path (String)

    輸入 XML 檔路徑

  • output_paath (String)

    輸出 EPUB 檔路徑



71
72
73
74
75
76
77
78
79
80
# File 'lib/cbeta_epub.rb', line 71

def convert_file(input_path, output_path)
  return false unless input_path.end_with? '.xml'
    
  @book_id = File.basename(input_path, ".xml")
  
  sutra_init
  
  handle_file(input_path)
  create_epub(output_path)
end

#convert_folder(input_folder, output_folder) ⇒ Object

將某個資料夾下的每部作品都轉為一個對應的 EPUB。 跨冊的作品也會合成一個 EPUB。

Examples:

require 'cbeta'

TEMP = '/temp/epub-work'
IMG = '/Users/ray/Documents/Projects/D道安/figures'

c = CBETA::P5aToEPUB.new(TEMP, IMG)
c.convert_folder('/Users/ray/Documents/Projects/D道安/xml-p5a/DA', '/temp/cbeta-epub/DA')


93
94
95
96
97
98
99
100
101
102
103
# File 'lib/cbeta_epub.rb', line 93

def convert_folder(input_folder, output_folder)
  puts "convert folder: #{input_folder} to #{output_folder}"
  @todo = {}
  
  # 先檢視整個資料夾,哪些是要多檔合一
  prepare_todo_list(input_folder, output_folder)
  
  @todo.each_pair do |k, v|
    convert_sutra(k, v[:xml_files], v[:epub])
  end
end

#convert_sutra(book_id, xml_files, out) ⇒ Object

將多個 xml 檔案合成一個 EPUB

Examples:

大般若經 跨三冊 合成一個 EPUB

require 'cbeta'

TEMP = '/temp/epub-work'

xml_files = [
  '/Users/ray/git-repos/cbeta-xml-p5a/T/05/T05n0220a.xml',
  '/Users/ray/git-repos/cbeta-xml-p5a/T/06/T06n0220b.xml',
  '/Users/ray/git-repos/cbeta-xml-p5a/T/07/T07n0220c.xml',
  '/Users/ray/git-repos/cbeta-xml-p5a/T/07/T07n0220d.xml',
  '/Users/ray/git-repos/cbeta-xml-p5a/T/07/T07n0220e.xml',
  '/Users/ray/git-repos/cbeta-xml-p5a/T/07/T07n0220f.xml',
  '/Users/ray/git-repos/cbeta-xml-p5a/T/07/T07n0220g.xml',
  '/Users/ray/git-repos/cbeta-xml-p5a/T/07/T07n0220h.xml',
  '/Users/ray/git-repos/cbeta-xml-p5a/T/07/T07n0220i.xml',
  '/Users/ray/git-repos/cbeta-xml-p5a/T/07/T07n0220j.xml',
  '/Users/ray/git-repos/cbeta-xml-p5a/T/07/T07n0220k.xml',
  '/Users/ray/git-repos/cbeta-xml-p5a/T/07/T07n0220l.xml',
  '/Users/ray/git-repos/cbeta-xml-p5a/T/07/T07n0220m.xml',
  '/Users/ray/git-repos/cbeta-xml-p5a/T/07/T07n0220n.xml',
  '/Users/ray/git-repos/cbeta-xml-p5a/T/07/T07n0220o.xml',
]

c = CBETA::P5aToEPUB.new(TEMP)
c.convert_sutra('T0220', xml_files, '/temp/cbeta-epub/T0220.epub')


132
133
134
135
136
137
138
139
140
141
142
# File 'lib/cbeta_epub.rb', line 132

def convert_sutra(book_id, xml_files, out)
  @book_id = book_id
  sutra_init
  xml_files.each { |f| handle_file(f) }
  
  if xml_files.size > 1
    @title.sub!(/^(.*)\(.*?\)$/, '\1')
    @title.sub!(/^(.*?)((.*?))+$/, '\1')
  end
  create_epub(out)
end