cbeta 3.7.10 → 4.0.0

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
  SHA256:
3
- metadata.gz: fa8c0001a52f96df16ac52d46416bcf1630f841ea306940a3196e81ece5c1fac
4
- data.tar.gz: 655f8c0438ff07cfd0b73f3ad1f749cde2b44c38c7567a11dc2dcd9d31b5219f
3
+ metadata.gz: 346145ac7ac767e90b736501a231bc7fb92d70eeff529dbc64f0d05c59bb4714
4
+ data.tar.gz: 9dd75ba7fbb1280e2cede956f6694193eb6f7e4d22d667a3c055dd35a7ced5c1
5
5
  SHA512:
6
- metadata.gz: a4d29529c018af7658c30da3350e75e295181df0ea86693be3ed8f63257fdbe7d1efcf6fbf311965de718fbf589ea8971c20eca3f8a296cad5a1c4b2d8205871
7
- data.tar.gz: b2f23a65ebc368bd5b7d19c21b1da96ba84fa4e21cebd7f2a32651f9ba571cc6fd76463d2483af619550a2cfcfafc2342ff07438a39e64246900b907904d7a24
6
+ metadata.gz: 521d9a7d25265961a650d7c1d8171bc3742872cca2bfdaa46d8fd93c2d07bd2ba10dd1f67822c6358c21a4cd050683ca1f63151998aac36c4a425e9e24dc6e57
7
+ data.tar.gz: e30e68df19b306e9472faa679da95ba4fc61862756748e0bebde6422cdeb3d4975b78b3850953dd0ef7af806b9e1c992daa6d4f293b59accc1ec7716d68763df
checksums.yaml.gz.sig CHANGED
Binary file
@@ -1,7 +1,7 @@
1
1
  module CbetaShare
2
2
  def each_canon(xml_root)
3
3
  Dir.entries(xml_root).sort.each do |c|
4
- next unless c.match(/^#{CBETA::CANON}$/)
4
+ next unless c.match(CBETA::CANON_ID)
5
5
  yield(c)
6
6
  end
7
7
  end
@@ -192,9 +192,9 @@ class CBETA::P5aChecker
192
192
 
193
193
  def e_graphic(e)
194
194
  url = File.basename(e['url'])
195
- fn = File.join(@figures, @canon, url)
196
- unless File.exist? fn
197
- error "[E05] 圖檔 不存在, url: #{url}"
195
+ path = File.join(@figures, @canon, url)
196
+ unless File.exist? path
197
+ error "[E05] 圖檔 不存在, url: #{e['url']}, path: #{path}"
198
198
  end
199
199
  end
200
200
 
@@ -124,7 +124,7 @@ class CBETA::P5aToText
124
124
 
125
125
  def convert_all
126
126
  Dir.entries(@xml_root).sort.each do |c|
127
- next unless c.match(/^#{CBETA::CANON}$/)
127
+ next unless c.match(CBETA::CANON_ID)
128
128
  handle_canon(c)
129
129
  end
130
130
  end
data/lib/cbeta.rb CHANGED
@@ -6,22 +6,36 @@
6
6
  require 'csv'
7
7
 
8
8
  class CBETA
9
- CANON = 'CC|DA|GA|GB|LC|TX|YP|ZS|ZW|[A-Z]'
9
+ # 藏經 ID, 例如 T, ZS
10
+ #
11
+ # 本身是 alternation, 已經包在 non-capturing group 裡,
12
+ # 所以插值到 regexp 時不需要 (也不應該) 再包一層 group:
13
+ # 正確: /\A#{CBETA::CANON}\z/
14
+ # 多餘: /\A(?:#{CBETA::CANON})\z/
15
+ # 若需要取出藏經 ID, 才用 capturing group 包起來: /^(#{CBETA::CANON})/
16
+ CANON = '(?:CC|DA|GA|GB|LC|TX|YP|ZS|ZW|[A-Z])'
10
17
  SORT_ORDER = %w(T X A K S F C D U P J L G M N ZS I ZW B GA GB Y LC TX YP CC)
11
18
  VOL3 = %w[A CC C G GA GB L M P U]
12
19
  DATA = File.join(File.dirname(__FILE__), 'data')
13
20
  PUNCS = ',.()[] 。‧.,、;?!:︰/()「」『』《》<>〈〉〔〕[]【】〖〗〃…—─ ~│┬▆△*+-='
14
-
21
+
15
22
  # 經號 (不含 Canon ID):
16
23
  # 四碼數字 T0001
17
24
  # 四碼數字 + 英文字母 T0150A, T0128a
18
25
  # 英文字母 + 三碼數字 JA041, ZWa073
19
- WORK_PART = '\d{4}[a-zA-Z]?|[ABa]\d{3}'
26
+ # CANON 一樣已自帶 non-capturing group。
27
+ WORK_PART = '(?:\d{4}[a-zA-Z]?|[ABa]\d{3})'
20
28
 
21
29
  # XML file 主檔名
22
30
  # GA010n0009
23
- BASENAME = "(?:#{CANON})\\d{2,3}n(?:#{WORK_PART})"
24
-
31
+ BASENAME = "#{CANON}\\d{2,3}n#{WORK_PART}"
32
+
33
+ # 整個字串就是 藏經 ID, 例如 "T", "ZS"
34
+ CANON_ID = /\A#{CANON}\z/
35
+
36
+ # 整個字串就是 典籍編號, 例如 "T0001", "JB271", "ZWa073"
37
+ WORK_ID = /\A#{CANON}#{WORK_PART}\z/
38
+
25
39
  # 由 行首資訊 取得 藏經 ID
26
40
  # @param linehead[String] 行首資訊, 例如 "T01n0001_p0001a01" 或 "GA009n0008_p0003a01"
27
41
  # @return [String] 藏經 ID,例如 "T" 或 "GA"
@@ -142,7 +156,7 @@ class CBETA
142
156
  # ex: J36nB348_p0284c01
143
157
  # @return [String] XML檔相對路徑,例如 "GA/GA009/GA009n0008.xml"
144
158
  def self.linehead_to_xml_file_path(linehead)
145
- if m = linehead.match(/^(?<work>(?<vol>(?<canon>#{CANON})\d+)n(?:#{WORK_PART})).*$/)
159
+ if m = linehead.match(/^(?<work>(?<vol>(?<canon>#{CANON})\d+)n#{WORK_PART}).*$/)
146
160
  File.join(m[:canon], m[:vol], m[:work]+'.xml')
147
161
  else
148
162
  nil
@@ -172,7 +186,7 @@ class CBETA
172
186
  # CBETA.linehead_to_s('T85n2838_p1291a03')
173
187
  # # return "T85, no. 2838, p. 1291, a03"
174
188
  def self.linehead_to_s(linehead)
175
- linehead.match(/^((?:#{CANON})\d+)n(.*)_p(\d+)([a-z]\d+)$/) {
189
+ linehead.match(/^(#{CANON}\d+)n(.*)_p(\d+)([a-z]\d+)$/) {
176
190
  return "#{$1}, no. #{$2}, p. #{$3}, #{$4}"
177
191
  }
178
192
  nil
@@ -298,14 +312,8 @@ require 'cbeta/bm_to_text'
298
312
  require 'cbeta/canon'
299
313
  require 'cbeta/char_count'
300
314
  require 'cbeta/char_freq'
301
- require 'cbeta/html_to_pdf'
302
315
  require 'cbeta/p5a_checker'
303
- require 'cbeta/p5a_to_html'
304
- require 'cbeta/p5a_to_html_for_every_edition'
305
- require 'cbeta/p5a_to_html_for_pdf'
306
- require 'cbeta/p5a_to_simple_html'
307
316
  require 'cbeta/p5a_to_text'
308
317
  require 'cbeta/p5a_validator'
309
- require 'cbeta/html_to_text'
310
318
  require 'cbeta/unicode_service'
311
319
  require 'cbeta/xml_document'
data.tar.gz.sig CHANGED
Binary file
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: cbeta
3
3
  version: !ruby/object:Gem::Version
4
- version: 3.7.10
4
+ version: 4.0.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Ray Chou
@@ -109,13 +109,7 @@ files:
109
109
  - lib/cbeta/char_count.rb
110
110
  - lib/cbeta/char_freq.rb
111
111
  - lib/cbeta/gaiji.rb
112
- - lib/cbeta/html_to_pdf.rb
113
- - lib/cbeta/html_to_text.rb
114
112
  - lib/cbeta/p5a_checker.rb
115
- - lib/cbeta/p5a_to_html.rb
116
- - lib/cbeta/p5a_to_html_for_every_edition.rb
117
- - lib/cbeta/p5a_to_html_for_pdf.rb
118
- - lib/cbeta/p5a_to_simple_html.rb
119
113
  - lib/cbeta/p5a_to_text.rb
120
114
  - lib/cbeta/p5a_validator.rb
121
115
  - lib/cbeta/unicode_service.rb
@@ -124,9 +118,6 @@ files:
124
118
  - lib/data/categories.json
125
119
  - lib/data/cbeta_gaiji.json
126
120
  - lib/data/cbeta_sanskrit.json
127
- - lib/data/html-for-pdf.css
128
- - lib/data/pdf-template.htm
129
- - lib/data/unicode-1.1.json
130
121
  homepage: https://github.com/RayCHOU/ruby-cbeta
131
122
  licenses:
132
123
  - MIT
metadata.gz.sig CHANGED
Binary file
@@ -1,75 +0,0 @@
1
- require 'fileutils'
2
-
3
- class CBETA::HTMLToPDF
4
- # @param input [String] folder of source HTML, HTML can be produced by CBETA::P5aToHTMLForPDF.
5
- # @param output [String] output folder
6
- # @param converter [String] shell command to convert HTML to PDF
7
- # * suggestion: http://www.princexml.com/
8
- # * wkhtmltopdf has font problem to display unicode extb characters
9
- #
10
- # @example
11
- # c = CBETA::HTMLToPDF.new('/temp/cbeta-html', '/temp/cbeta-pdf', "prince %{in} -o %{out}")
12
- def initialize(input, output, converter)
13
- @input = input
14
- @output = output
15
- @converter = converter
16
- end
17
-
18
- # Convert CBETA HTML to PDF
19
- #
20
- # @example for convert Taisho (大正藏) Volumn 1:
21
- #
22
- # c = CBETA::HTMLToPDF.new('/PATH/TO/CBETA/XML/P5a', '/OUTPUT/FOLDER')
23
- # c.convert('T01')
24
- #
25
- # @example for convert all in Taisho (大正藏):
26
- #
27
- # c = CBETA::HTMLToPDF.new('/PATH/TO/CBETA/XML/P5a', '/OUTPUT/FOLDER')
28
- # c.convert('T')
29
- #
30
- # @example for convert Taisho Vol. 5~7:
31
- #
32
- # c = CBETA::P5aToHTMLForPDF.new('/PATH/TO/CBETA/XML/P5a', '/OUTPUT/FOLDER')
33
- # c.convert('T05..T07')
34
- #
35
- # T 是大正藏的 ID, CBETA 的藏經 ID 系統請參考: http://www.cbeta.org/format/id.php
36
- def convert(target=nil)
37
- return convert_all if target.nil?
38
-
39
- arg = target.upcase
40
- if arg.size <= 2
41
- convert_collection(arg)
42
- else
43
- if arg.include? '..'
44
- arg.match(/^([^\.]+?)\.\.([^\.]+)$/) {
45
- convert_vols($1, $2)
46
- }
47
- else
48
- convert_vol(arg)
49
- end
50
- end
51
- end
52
-
53
- def convert_collection(c)
54
- @canon = c
55
- puts 'convert_collection ' + c
56
-
57
- output_folder = File.join(@output, @canon)
58
- FileUtils.mkdir_p(output_folder) unless Dir.exist? output_folder
59
-
60
- folder = File.join(@input, @canon)
61
- Dir.foreach(folder) { |f|
62
- next if f.start_with? '.'
63
- src = File.join(folder, f, 'main.htm')
64
- dest = File.join(output_folder, "#{f}.pdf")
65
- convert_file(src, dest)
66
- }
67
- end
68
-
69
- def convert_file(html_fn, pdf_fn)
70
- puts "convert file: #{html_fn} to #{pdf_fn}"
71
- cmd = @converter % { in: html_fn, out: pdf_fn}
72
- `#{cmd}`
73
- end
74
-
75
- end
@@ -1,151 +0,0 @@
1
- require 'fileutils'
2
- require 'nokogiri'
3
-
4
- # 將 CBETA HTML 轉為 純文字(含行首資訊)
5
- #
6
- # Example:
7
- #
8
- # h2t = CBETA::HTMLToText.new('/temp/cbeta-html', '/temp/cbeta-text')
9
- # h2t.convert("T01") # 轉換大正藏第一冊
10
- class CBETA::HTMLToText
11
- # @param html_root [String] 來源 HTML 路徑
12
- # @param out_root [String] 輸出路徑
13
- def initialize(html_root, out_root)
14
- @html_root = html_root
15
- @out_root = out_root
16
- end
17
-
18
- # @param arg [String] 要執行轉換的冊數
19
- # @example
20
- # convert("T01")
21
- def convert(arg)
22
- @dirty = false
23
- @vol = arg.upcase
24
- @corpus = @vol[0]
25
- handle_vol
26
- end
27
-
28
- private
29
-
30
- def traverse(e)
31
- r = ''
32
- e.children.each { |c|
33
- r += handle_node(c)
34
- }
35
- r.gsub(' ', '')
36
- end
37
-
38
- def handle_text(e)
39
- s = e.content().chomp
40
- return '' if s.empty?
41
- s.gsub(/[\n,、—!。:「]/, '')
42
- end
43
-
44
- def handle_span(e)
45
- r = ''
46
- case e['class']
47
- when 'doube-line-note'
48
- r = traverse(e)
49
- unless r.start_with? '('
50
- r = "(#{r})"
51
- end
52
- when 'lb'
53
- if @dirty
54
- r += "\n"
55
- else
56
- @dirty = true
57
- end
58
- # 行首資訊 T05n0220a 改為 T05n0220
59
- lb = e['id'].sub(/^(T0\dn0220)[a-z](.*)$/, '\1\2')
60
- r += lb + '║'
61
- when 'lineInfo'
62
- when 'ranja'
63
- r = '【◇】'
64
- when 'siddam'
65
- r = '【◇】'
66
- when 'star'
67
- else
68
- r = traverse(e)
69
- end
70
- r
71
- end
72
-
73
- def handle_node(e)
74
- return '' if e.comment?
75
- return handle_text(e) if e.text?
76
- r = ''
77
- case e.name
78
- when 'a'
79
- if e['class'] == 'gaijiAnchor'
80
- id = e['href'][1..-1]
81
- r = @gaiji[id]
82
- else
83
- r = traverse(e)
84
- end
85
- when 'div'
86
- if e['id'] != 'back'
87
- r = traverse(e)
88
- end
89
- when 'head'
90
- when 'p'
91
- if e['class'] == 'figure'
92
- r = '【圖】'
93
- else
94
- r = traverse(e)
95
- end
96
- when 'span'
97
- r = handle_span(e)
98
- else
99
- r = traverse(e)
100
- end
101
- r
102
- end
103
-
104
- def prepare_folder()
105
- folder = File.join(@out_root, @corpus, @vol)
106
- FileUtils.remove_dir(folder, true)
107
- FileUtils.mkdir_p(folder)
108
- folder
109
- end
110
-
111
- def handle_file(path)
112
- sutra = File.basename(path, ".*")
113
- sutra.sub!(/^(.*)_.*$/, '\1')
114
- sutra.sub!(/(T\d\dn0220).*$/, '\1') # T0220 BM 沒有分 a, b, c...
115
-
116
- if sutra != @last_sutra
117
- txt_fn = sutra + '.txt'
118
- txt_path = File.join(@folder_out, txt_fn)
119
- puts "h2t #{txt_path}"
120
- @fo = File.open(txt_path, 'w')
121
- @last_sutra = sutra
122
- @dirty = false
123
- end
124
-
125
- f = File.open(path)
126
- doc = Nokogiri::HTML(f)
127
- f.close
128
-
129
- @gaiji = {}
130
- doc.css("span.gaijiInfo").each { |e|
131
- @gaiji[e['id']] = e['zzs']
132
- }
133
-
134
- text = traverse(doc.root)
135
-
136
- # 悉曇字
137
- text.gsub!(/(\((【◇】)+\)|(【◇】)|【◇】)+/, '【◇】')
138
-
139
- @fo.write(text)
140
- end
141
-
142
- def handle_vol()
143
- folder_in = File.join(@html_root, @corpus, @vol)
144
- @folder_out = prepare_folder
145
- @last_sutra = ''
146
- Dir["#{folder_in}/*"].each { |f|
147
- handle_file(f)
148
- }
149
- end
150
-
151
- end