cbeta 0.5.0 → 0.5.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 416026a7921c0c2bb6c602be134095f4079951fe
4
- data.tar.gz: d72055681fb1c8ff22a4317ed0310fc368381e1a
3
+ metadata.gz: 17bb523411446bf9b5c62e2f8d8f7fee94b4ece5
4
+ data.tar.gz: 5c61e23af96517ac7ebee712aa436b0a0cc7b4a8
5
5
  SHA512:
6
- metadata.gz: 0d71cdacc97c1d6111cc2dc1dc9b5190d2398f05b9a45280744b7475579dd8f612efa382c0cfd16f07429391f99e330a8fe1c3db1c48d770076f90abe20d15c4
7
- data.tar.gz: bd62c4cdfe0def5b62d7440f0ec029c2535e1384d0ac9865e2ba91a643db46b6a1fed02ed8a4c4435d9e26b878d8acfc7e3c91994bac50899b7e8ec4413b8717
6
+ metadata.gz: ed3587f8f479046027ba4eb0af8c9ad9c41e9a2e07ebc586046dfa08d8172039a636cc8d7d11edf69356c73d6c37009584f96805caf748c6c60521841e69eb32
7
+ data.tar.gz: 7cd3eb6d05c5a2acce351ebd4c8326ba1e9830413a9270d88579ca0b00819f8007235d297e099808323f0108bd901a007c88d96ba9a1555141d82c9fc2985a65
@@ -17,16 +17,31 @@ class CBETA::P5aToEPUB
17
17
  # 某版用字缺的符號
18
18
  MISSING = '-'
19
19
 
20
- NAV_TEMPLATE = File.read(File.join(File.dirname(__FILE__), '../data/epub-nav.xhtml'))
20
+ SCRIPT_FOLDER = File.dirname(__FILE__)
21
+ NAV_TEMPLATE = File.read(File.join(SCRIPT_FOLDER, '../data/epub-nav.xhtml'))
21
22
  MAIN = 'main.xhtml'
23
+ DATA = File.join(SCRIPT_FOLDER, '../data')
22
24
 
23
25
  # @param temp_folder [String] 供 EPUB 暫存工作檔案的路徑
24
- # @param graphic_base [String] 存放圖片的路徑
25
- def initialize(temp_folder, graphic_base)
26
+ # @param options [Hash]
27
+ # :epub_version [Integer] EPUB 版本,預設為 3
28
+ # :graphic_base [String] 圖檔路徑
29
+ # :readme [String] 說明檔,如果沒指定的話,就使用預設說明檔
30
+ def initialize(temp_folder, options={})
26
31
  @temp_folder = temp_folder
27
- @graphic_base = graphic_base
32
+ @settings = {
33
+ epub_version: 3,
34
+ readme: File.join(DATA, 'epub-readme.xhtml')
35
+ }
36
+ @settings.merge!(options)
37
+ puts @settings
28
38
  @cbeta = CBETA.new
29
39
  @gaijis = CBETA::Gaiji.new
40
+
41
+ # 載入 unicode 1.1 字集列表
42
+ fn = File.join(DATA, 'unicode-1.1.json')
43
+ json = File.read(fn)
44
+ @unicode1 = JSON.parse(json)
30
45
  end
31
46
 
32
47
  # 將某個 xml 轉為一個 EPUB
@@ -111,15 +126,20 @@ class CBETA::P5aToEPUB
111
126
  private
112
127
 
113
128
  def copy_static_files(src, dest)
114
- p1 = File.join(File.dirname(__FILE__), '../data', src)
115
- p2 = File.join(@temp_folder, dest)
116
- FileUtils.copy(p1, p2)
129
+ dest = File.join(@temp_folder, dest)
130
+ FileUtils.copy(src, dest)
117
131
  end
118
132
 
119
133
  def create_epub(output_path)
120
- copy_static_files('epub-readme.xhtml', 'readme.xhtml')
121
- copy_static_files('epub-donate.xhtml', 'donate.xhtml')
122
- create_main_html
134
+ copy_static_files(@settings[:readme], 'readme.xhtml')
135
+
136
+ src = File.join(DATA, 'epub-donate.xhtml')
137
+ copy_static_files(src, 'donate.xhtml')
138
+
139
+ src = File.join(DATA, 'epub.css')
140
+ copy_static_files(src, 'cbeta.css')
141
+
142
+ create_html_by_juan
123
143
  create_nav_html
124
144
 
125
145
  title = @title
@@ -136,10 +156,12 @@ class CBETA::P5aToEPUB
136
156
  date Date.today.to_s
137
157
  }
138
158
 
159
+ juan_dir = File.join(@temp_folder, 'juans')
139
160
  # in resources block, you can define resources by its relative path and datasource.
140
161
  # item creator methods are: files, file.
141
162
  builder.resources(:workdir => @temp_folder) {
142
163
  glob 'img/*'
164
+ file 'cbeta.css'
143
165
 
144
166
  # this is navigation document.
145
167
  nav 'nav.xhtml'
@@ -147,28 +169,54 @@ class CBETA::P5aToEPUB
147
169
  # ordered item. will be added to spine.
148
170
  ordered {
149
171
  file 'readme.xhtml'
150
- file 'main.xhtml'
172
+ Dir.entries(juan_dir).sort.each do |f|
173
+ next if f.start_with? '.'
174
+ file "juans/#{f}"
175
+ end
151
176
  file 'donate.xhtml'
152
177
  }
153
178
  }
179
+ builder.book.version = @settings[:epub_version]
154
180
  builder.generate_epub(output_path)
155
181
  puts "output: #{output_path}"
156
182
  end
157
183
 
158
- def create_main_html
159
- fn = File.join(@temp_folder, MAIN)
160
- s = <<eos
184
+ def create_html_by_juan
185
+ juans = @main_text.split(/(<juan \d+>)/)
186
+ open = false
187
+ fo = nil
188
+ juan_no = nil
189
+ fn = ''
190
+ buf = ''
191
+ # 一卷一檔
192
+ juans.each do |j|
193
+ if j =~ /<juan (\d+)>$/
194
+ juan_no = $1.to_i
195
+ fn = "%03d.xhtml" % juan_no
196
+ output_path = File.join(@temp_folder, 'juans', fn)
197
+ fo = File.open(output_path, 'w')
198
+ open = true
199
+ s = <<eos
161
200
  <html xmlns="http://www.w3.org/1999/xhtml">
162
201
  <head>
163
202
  <meta charset="utf-8" />
164
203
  <title>#{@title}</title>
204
+ <link rel="stylesheet" type="text/css" href="../cbeta.css" />
165
205
  </head>
166
206
  <body>
167
207
  <div id='body'>
168
208
  eos
169
- s += @main_text + "\n</div><!-- end of div[@id='body'] -->\n"
170
- s += "<div id='back'>\n" + @back + "</div></body></html>\n"
171
- File.write(fn, s)
209
+ fo.write(s)
210
+ fo.write(buf)
211
+ buf = ''
212
+ elsif open
213
+ fo.write(j + "\n</div><!-- end of div[@id='body'] -->\n")
214
+ fo.write('</body></html>')
215
+ fo.close
216
+ else
217
+ buf = j
218
+ end
219
+ end
172
220
  end
173
221
 
174
222
  def create_nav_html
@@ -180,19 +228,6 @@ eos
180
228
  end
181
229
 
182
230
  def handle_anchor(e)
183
- id = e['id']
184
- if e.has_attribute?('id')
185
- if id.start_with?('nkr_note_orig')
186
- note = @notes[id]
187
- note_text = traverse(note)
188
- n = id[/^nkr_note_orig_(.*)$/, 1]
189
- @back += "<span class='footnote' id='n#{n}'>#{note_text}</span>\n"
190
- return "<a class='noteAnchor' href='#n#{n}'></a>"
191
- elsif id.start_with? 'fx'
192
- return "<span class='star'>[*]</span>"
193
- end
194
- end
195
-
196
231
  if e.has_attribute?('type')
197
232
  if e['type'] == 'circle'
198
233
  return '◎'
@@ -203,12 +238,7 @@ eos
203
238
  end
204
239
 
205
240
  def handle_app(e)
206
- r = ''
207
- if e['type'] == 'star'
208
- c = e['corresp'][1..-1]
209
- r = "<a class='noteAnchor star' href='#n#{c}'></a>"
210
- end
211
- r + traverse(e)
241
+ traverse(e)
212
242
  end
213
243
 
214
244
  def handle_byline(e)
@@ -228,24 +258,7 @@ eos
228
258
  end
229
259
 
230
260
  def handle_corr(e)
231
- r = ''
232
- if e.parent.name == 'choice'
233
- sic = e.parent.at_xpath('sic')
234
- unless sic.nil?
235
- @dila_note += 1
236
- r = "<a class='noteAnchor dila' href='#dila_note#{@dila_note}'></a>"
237
-
238
- note = @orig
239
- sic_text = traverse(sic, 'back')
240
- if sic_text.empty?
241
- note += MISSING
242
- else
243
- note += sic_text
244
- end
245
- @back += "<span class='footnote_dila' id='dila_note#{@dila_note}'>#{note}</span>\n"
246
- end
247
- end
248
- r + "<span class='cbeta'>%s</span>" % traverse(e)
261
+ traverse(e)
249
262
  end
250
263
 
251
264
  def handle_div(e)
@@ -296,56 +309,34 @@ eos
296
309
  when 'SD-E35B'
297
310
  return ')'
298
311
  else
299
- return "<span class='siddam' roman='#{g['roman']}' code='#{gid}' char='#{g['sd-char']}'/>"
312
+ return g['roman']
300
313
  end
301
314
  end
302
315
 
303
316
  if gid.start_with?('RJ')
304
- return "<span class='ranja' roman='#{g['roman']}' code='#{gid}' char='#{g['rj-char']}'/>"
317
+ return g['roman']
305
318
  end
306
319
 
307
320
  default = ''
308
321
  if g.has_key?('unicode')
309
- #if @unicode1.include?(g['unicode'])
310
- # 如果在 unicode ext-C, ext-D, ext-E 範圍內
311
- if (0x2A700..0x2CEAF).include? g['unicode'].hex
312
- default = g['unicode-char']
313
- else
314
- return g['unicode-char'] # 直接採用 unicode
322
+ if @unicode1.include?(g['unicode'])
323
+ return g['unicode-char'] # unicode 1.1 直接用
315
324
  end
316
325
  end
317
-
318
- nor = ''
319
- if g.has_key?('normal_unicode')
320
- nor = g['normal_unicode']
321
- default = nor if default.empty?
322
- end
323
-
324
- if g.has_key?('normal')
325
- nor += ', ' unless nor==''
326
- nor += g['normal']
327
- default = g['normal'] if default.empty?
328
- end
329
-
330
- default = zzs if default.empty?
331
326
 
332
- href = 'http://dict.cbeta.org/dict_word/gaiji-cb/%s/%s.gif' % [gid[2, 2], gid]
333
- unless @back.include?(href)
334
- @back += "<span id='#{gid}' class='gaijiInfo' figure_url='#{href}' zzs='#{zzs}' nor='#{nor}'>#{default}</span>\n"
335
- end
336
- "<a class='gaijiAnchor' href='##{gid}'>#{default}</a>"
327
+ zzs
337
328
  end
338
329
 
339
330
  def handle_graphic(e)
340
331
  url = e['url']
341
332
  url.sub!(/^.*figures\/(.*)$/, '\1')
342
333
 
343
- src = File.join(@graphic_base, url)
334
+ src = File.join(@settings[:graphic_base], url)
344
335
  basename = File.basename(src)
345
336
  dest = File.join(@temp_folder, 'img', basename)
346
337
  FileUtils.copy(src, dest)
347
338
 
348
- "<img src='img/#{basename}' />"
339
+ "<img src='../img/#{basename}' />"
349
340
  end
350
341
 
351
342
  def handle_head(e)
@@ -419,20 +410,7 @@ eos
419
410
  end
420
411
 
421
412
  def handle_lem(e)
422
- r = ''
423
- w = e['wit']
424
- if w.include? 'CBETA' and not w.include? @orig
425
- @dila_note += 1
426
- r = "<a class='noteAnchor dila' href='#dila_note#{@dila_note}'></a>"
427
- r += "<span class='cbeta'>%s</span>" % traverse(e)
428
-
429
- note = lem_note_cf(e)
430
- note += lem_note_rdg(e)
431
- @back += "<span class='footnote_dila' id='dila_note#{@dila_note}'>#{note}</span>\n"
432
- else
433
- r = traverse(e)
434
- end
435
- r
413
+ r = traverse(e)
436
414
  end
437
415
 
438
416
  def handle_lg(e)
@@ -465,7 +443,16 @@ eos
465
443
  end
466
444
 
467
445
  def handle_milestone(e)
468
- ''
446
+ r = ''
447
+ if e['unit'] == 'juan'
448
+ r += "</div>" * @open_divs.size # 如果有 div 跨卷,要先結束, ex: T55n2154, p. 680a29, 跨 19, 20 兩卷
449
+ @juan = e['n'].to_i
450
+ r += "<juan #{@juan}>"
451
+ @open_divs.each { |d|
452
+ r += "<div class='#{d['type']}'>"
453
+ }
454
+ end
455
+ r
469
456
  end
470
457
 
471
458
  def handle_mulu(e)
@@ -478,7 +465,8 @@ eos
478
465
 
479
466
  label = traverse(e, 'txt')
480
467
  @mulu_count += 1
481
- li = @current_nav.last.add_child("<li><a href='#{@main_html}#mulu#{@mulu_count}'>#{label}</a></li>").first
468
+ fn = "juans/%03d.xhtml" % @juan
469
+ li = @current_nav.last.add_child("<li><a href='#{fn}#mulu#{@mulu_count}'>#{label}</a></li>").first
482
470
  ol = li.add_child('<ol></ol>').first
483
471
  @current_nav << ol
484
472
  "<a id='mulu#{@mulu_count}' />"
@@ -532,17 +520,13 @@ eos
532
520
  when 'equivalent'
533
521
  return ''
534
522
  when 'orig'
535
- return handle_note_orig(e)
523
+ return ''
536
524
  when 'orig_biao'
537
- return handle_note_orig(e, 'biao')
525
+ return ''
538
526
  when 'orig_ke'
539
- return handle_note_orig(e, 'ke')
527
+ return ''
540
528
  when 'mod'
541
- @pass << false
542
- s = traverse(e)
543
- @pass.pop
544
- @back += "<span class='footnote_cb' id='n#{n}'>#{s}</span>\n"
545
- return "<a class='noteAnchor' href='#n#{n}'></a>"
529
+ return ""
546
530
  when 'rest'
547
531
  return ''
548
532
  else
@@ -556,31 +540,12 @@ eos
556
540
 
557
541
  if e.has_attribute?('place') && e['place']=='inline'
558
542
  r = traverse(e)
559
- return "<span class='doube-line-note'>#{r}</span>"
543
+ return "(#{r})"
560
544
  else
561
545
  return traverse(e)
562
546
  end
563
547
  end
564
548
 
565
- def handle_note_orig(e, anchor_type=nil)
566
- n = e['n']
567
- @pass << false
568
- s = traverse(e)
569
- @pass.pop
570
- @back += "<span class='footnote_orig' id='n#{n}'>#{s}</span>\n"
571
-
572
- if @mod_notes.include? n
573
- return ''
574
- else
575
- label = case anchor_type
576
- when 'biao' then " data-label='標#{n[-2..-1]}'"
577
- when 'ke' then " data-label='科#{n[-2..-1]}'"
578
- else ''
579
- end
580
- return "<a class='noteAnchor' href='#n#{n}'#{label}></a>"
581
- end
582
- end
583
-
584
549
  def handle_p(e)
585
550
  r = "<div class='p'>\n"
586
551
  r += traverse(e)
@@ -705,10 +670,10 @@ eos
705
670
 
706
671
  @mulu_count = 0
707
672
  @main_text = ''
708
- @back = ''
709
673
  @dila_note = 0
710
674
 
711
675
  FileUtils::mkdir_p File.join(@temp_folder, 'img')
676
+ FileUtils::mkdir_p File.join(@temp_folder, 'juans')
712
677
  end
713
678
 
714
679
  def open_xml(fn)
@@ -763,7 +728,7 @@ eos
763
728
  end
764
729
  end
765
730
  end
766
-
731
+
767
732
  def to_html(e)
768
733
  e.to_xml(encoding: 'UTF-8', pertty: true, :save_with => Nokogiri::XML::Node::SaveOptions::AS_XML)
769
734
  end
@@ -1,11 +1,5 @@
1
- <?xml version="1.0" encoding="utf-8" standalone="no"?>
2
- <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN"
3
- "http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">
4
-
5
1
  <html xmlns="http://www.w3.org/1999/xhtml" xml:lang="zh-TW" xmlns:xml="http://www.w3.org/XML/1998/namespace">
6
2
  <head>
7
- <link href="../Styles/stylesheet.css" rel="stylesheet" type="text/css" />
8
-
9
3
  <title>贊助</title>
10
4
  </head>
11
5
 
@@ -1,11 +1,5 @@
1
- <?xml version="1.0" encoding="utf-8" standalone="no"?>
2
- <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN"
3
- "http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">
4
-
5
1
  <html xmlns="http://www.w3.org/1999/xhtml" xml:lang="zh-TW" xmlns:xml="http://www.w3.org/XML/1998/namespace">
6
2
  <head>
7
- <link href="../Styles/stylesheet.css" rel="stylesheet" type="text/css" />
8
-
9
3
  <title>說明</title>
10
4
  </head>
11
5
 
@@ -14,13 +8,17 @@
14
8
  <h2>編輯說明</h2>
15
9
 
16
10
  <ul>
17
- <li>本電子書以<a href="http://www.seeland.org.tw/www/zhiyu/index.html">「西蓮淨苑智諭老和尚著作全集」</a>為資料來源。</li>
11
+ <li>本電子書以「CBETA 電子佛典集成 Version 2014」為資料來源。</li>
12
+
13
+ <li>漢字呈現以 Unicode 1.1 為基礎,不在此範圍的字則採用<a href="http://www.cbeta.org/format/rare-rule.php">組字式</a>表達。</li>
14
+
15
+ <li>梵文悉曇字及蘭札字均採用羅馬轉寫字,如無轉寫字則提供字型圖檔。</li>
18
16
 
19
- <li>漢字呈現以 Unicode 1.1 為基礎,不在此範圍的字則採用 <a href="http://www.cbeta.org/format/rare-rule.php">組字式</a> 表達。</li>
17
+ <li>CBETA 對底本所做的修訂用字以紅色字元表示。(部份 ePub 閱讀器可能無法呈現指定的顏色)</li>
20
18
 
21
- <li><span style="line-height: 1.6em;">若有發現任何問題,歡迎來函</span> <a href="mailto:seeland77@gmail.com" style="line-height: 1.6em;">seeland77@gmail.com</a> <span style="line-height: 1.6em;">回報。</span><br /></li>
19
+ <li>若有發現任何問題,歡迎來函 <a href="mailto:service@cbeta.org">service@cbeta.org</a> 回報。</li>
22
20
 
23
- <li>版權所有,歡迎自由流通,但禁止營利使用。</li>
21
+ <li><a href="http://www.cbeta.org/copyright.php">版權所有</a>,歡迎自由流通,但禁止營利使用。</li>
24
22
  </ul><br />
25
23
  </div>
26
24
  </body>
data/lib/data/epub.css ADDED
@@ -0,0 +1,8 @@
1
+ table {
2
+ border-collapse: collapse;
3
+ }
4
+ th, td {
5
+ border: solid;
6
+ border-width: 1px;
7
+ padding: 5px;
8
+ }
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: cbeta
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.5.0
4
+ version: 0.5.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - Ray Chou
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2015-08-22 00:00:00.000000000 Z
11
+ date: 2015-08-24 00:00:00.000000000 Z
12
12
  dependencies: []
13
13
  description: Ruby gem for use Chinese Buddhist Text resources made by CBETA (http://www.cbeta.org).
14
14
  email: zhoubx@gmail.com
@@ -28,6 +28,7 @@ files:
28
28
  - lib/data/epub-donate.xhtml
29
29
  - lib/data/epub-nav.xhtml
30
30
  - lib/data/epub-readme.xhtml
31
+ - lib/data/epub.css
31
32
  - lib/data/gaiji.json
32
33
  - lib/data/unicode-1.1.json
33
34
  homepage: https://github.com/RayCHOU/ruby-cbeta