review 3.0.0 → 4.2.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (170) hide show
  1. checksums.yaml +4 -4
  2. data/.github/workflows/ruby-win.yml +39 -0
  3. data/.github/workflows/ruby.yml +27 -0
  4. data/.rubocop.yml +50 -12
  5. data/.travis.yml +17 -8
  6. data/Dockerfile +21 -5
  7. data/NEWS.ja.md +358 -0
  8. data/NEWS.md +358 -1
  9. data/README.md +11 -8
  10. data/appveyor.yml +1 -3
  11. data/bin/review-catalog-converter +4 -4
  12. data/bin/review-check +8 -8
  13. data/bin/review-checkdep +1 -1
  14. data/bin/review-compile +12 -12
  15. data/bin/review-epubmaker +3 -35
  16. data/bin/review-idgxmlmaker +16 -0
  17. data/bin/review-index +2 -89
  18. data/bin/review-preproc +14 -19
  19. data/bin/review-validate +3 -3
  20. data/bin/review-vol +4 -78
  21. data/doc/LICENSE +1 -1
  22. data/doc/config.yml.sample +46 -12
  23. data/doc/config.yml.sample-simple +4 -2
  24. data/doc/format.ja.md +37 -13
  25. data/doc/format.md +35 -20
  26. data/doc/pdfmaker.ja.md +43 -1
  27. data/doc/pdfmaker.md +42 -1
  28. data/doc/quickstart.ja.md +46 -26
  29. data/doc/quickstart.md +38 -17
  30. data/lib/epubmaker/epubcommon.rb +10 -5
  31. data/lib/epubmaker/epubv2.rb +1 -1
  32. data/lib/epubmaker/epubv3.rb +1 -0
  33. data/lib/epubmaker/producer.rb +4 -2
  34. data/lib/review/book.rb +1 -1
  35. data/lib/review/book/base.rb +38 -79
  36. data/lib/review/book/chapter.rb +18 -3
  37. data/lib/review/book/compilable.rb +6 -5
  38. data/lib/review/book/index.rb +69 -101
  39. data/lib/review/book/index/item.rb +40 -0
  40. data/lib/review/book/page_metric.rb +7 -7
  41. data/lib/review/book/part.rb +28 -5
  42. data/lib/review/book/volume.rb +3 -4
  43. data/lib/review/builder.rb +105 -44
  44. data/lib/review/catalog.rb +13 -16
  45. data/lib/review/compiler.rb +84 -72
  46. data/lib/review/configure.rb +19 -8
  47. data/lib/review/epub2html.rb +37 -4
  48. data/lib/review/epubmaker.rb +62 -7
  49. data/lib/review/extentions/string.rb +0 -4
  50. data/lib/review/htmlbuilder.rb +102 -115
  51. data/lib/review/htmlutils.rb +2 -3
  52. data/lib/review/i18n.rb +2 -2
  53. data/lib/review/i18n.yml +9 -0
  54. data/lib/review/idgxmlbuilder.rb +153 -74
  55. data/lib/review/idgxmlmaker.rb +191 -0
  56. data/lib/review/init-web/finish.html +10 -0
  57. data/lib/review/init-web/index.html +190 -0
  58. data/lib/review/init-web/review-layout-design.js +691 -0
  59. data/lib/review/init.rb +125 -34
  60. data/lib/review/latexbuilder.rb +199 -88
  61. data/lib/review/lineinput.rb +1 -1
  62. data/lib/review/location.rb +32 -0
  63. data/lib/review/logger.rb +4 -8
  64. data/lib/review/makerhelper.rb +24 -5
  65. data/lib/review/markdownbuilder.rb +31 -37
  66. data/lib/review/md2inaobuilder.rb +3 -5
  67. data/lib/review/pdfmaker.rb +44 -22
  68. data/lib/review/plaintextbuilder.rb +106 -85
  69. data/lib/review/preprocessor.rb +32 -41
  70. data/lib/review/rstbuilder.rb +33 -33
  71. data/lib/review/textmaker.rb +19 -3
  72. data/lib/review/textutils.rb +76 -2
  73. data/lib/review/tocprinter.rb +231 -102
  74. data/lib/review/topbuilder.rb +114 -61
  75. data/lib/review/update.rb +19 -19
  76. data/lib/review/version.rb +1 -1
  77. data/lib/review/volumeprinter.rb +99 -0
  78. data/lib/review/webmaker.rb +11 -4
  79. data/lib/review/webtocprinter.rb +38 -35
  80. data/lib/review/yamlloader.rb +26 -16
  81. data/review.gemspec +6 -4
  82. data/samples/sample-book/README.md +7 -2
  83. data/samples/sample-book/src/.gitignore +154 -0
  84. data/samples/sample-book/src/config-ebook.yml +4 -0
  85. data/samples/sample-book/src/config-jlreq-ebook.yml +4 -0
  86. data/samples/sample-book/src/config-jlreq.yml +6 -0
  87. data/samples/sample-book/src/config.yml +2 -2
  88. data/samples/sample-book/src/lib/tasks/review.rake +29 -14
  89. data/samples/sample-book/src/lib/tasks/z01_copy_sty.rake +14 -8
  90. data/samples/syntax-book/ch01.re +4 -2
  91. data/samples/syntax-book/ch02.re +8 -16
  92. data/samples/syntax-book/ch03.re +3 -6
  93. data/samples/syntax-book/config-jlreq-lualatex.yml +4 -0
  94. data/samples/syntax-book/config-jlreq.yml +5 -0
  95. data/samples/syntax-book/config-print.yml +3 -0
  96. data/samples/syntax-book/config.yml +1 -1
  97. data/samples/syntax-book/lib/tasks/review.rake +30 -15
  98. data/samples/syntax-book/lib/tasks/z01_copy_sty.rake +14 -8
  99. data/templates/latex/config.erb +16 -0
  100. data/templates/latex/layout.tex.erb +4 -0
  101. data/templates/latex/review-jlreq/review-base.sty +150 -61
  102. data/templates/latex/review-jlreq/review-jlreq.cls +74 -8
  103. data/templates/latex/review-jlreq/review-style.sty +4 -1
  104. data/templates/latex/review-jsbook/README.md +39 -0
  105. data/templates/latex/review-jsbook/review-base.sty +101 -23
  106. data/templates/latex/review-jsbook/review-jsbook.cls +28 -5
  107. data/templates/latex/review-jsbook/review-style.sty +5 -2
  108. data/templates/opf/epubv3.opf.erb +1 -0
  109. data/templates/web/html/layout-html5.html.erb +2 -2
  110. data/test/assets/test_template.tex +24 -3
  111. data/test/assets/test_template_backmatter.tex +24 -3
  112. data/test/test_book.rb +75 -21
  113. data/test/test_book_chapter.rb +4 -2
  114. data/test/test_book_part.rb +3 -3
  115. data/test/test_builder.rb +16 -0
  116. data/test/test_catalog.rb +24 -42
  117. data/test/test_catalog_converter_cmd.rb +1 -1
  118. data/test/test_epubmaker_cmd.rb +14 -7
  119. data/test/test_helper.rb +15 -7
  120. data/test/test_htmlbuilder.rb +909 -159
  121. data/test/test_i18n.rb +25 -25
  122. data/test/test_idgxmlbuilder.rb +395 -38
  123. data/test/test_idgxmlmaker_cmd.rb +46 -0
  124. data/test/test_image_finder.rb +52 -70
  125. data/test/test_index.rb +50 -10
  126. data/test/test_latexbuilder.rb +1194 -106
  127. data/test/test_latexbuilder_v2.rb +628 -97
  128. data/test/test_logger.rb +14 -1
  129. data/test/test_makerhelper.rb +3 -3
  130. data/test/test_markdownbuilder.rb +134 -16
  131. data/test/test_md2inaobuilder.rb +32 -9
  132. data/test/test_pdfmaker.rb +18 -1
  133. data/test/test_pdfmaker_cmd.rb +100 -6
  134. data/test/test_plaintextbuilder.rb +371 -25
  135. data/test/test_preprocessor.rb +2 -16
  136. data/test/test_rstbuilder.rb +249 -26
  137. data/test/test_textmaker_cmd.rb +54 -0
  138. data/test/test_textutils.rb +109 -2
  139. data/test/test_topbuilder.rb +546 -31
  140. data/test/test_update.rb +17 -8
  141. data/test/test_webtocprinter.rb +66 -34
  142. data/test/test_yamlloader.rb +13 -0
  143. data/vendor/jsclasses/LICENSE +1 -1
  144. data/vendor/jsclasses/jis/jsarticle.cls +53 -14
  145. data/vendor/jsclasses/jis/jsbook.cls +53 -14
  146. data/vendor/jsclasses/jis/jsclasses.dtx +84 -25
  147. data/vendor/jsclasses/jis/jslogo.dtx +4 -4
  148. data/vendor/jsclasses/jis/jslogo.sty +3 -3
  149. data/vendor/jsclasses/jis/jspf.cls +52 -13
  150. data/vendor/jsclasses/jis/jsreport.cls +53 -14
  151. data/vendor/jsclasses/jis/kiyou.cls +53 -14
  152. data/vendor/jsclasses/jis/okumacro.dtx +4 -5
  153. data/vendor/jsclasses/jis/okumacro.sty +3 -4
  154. data/vendor/jsclasses/jsarticle.cls +53 -14
  155. data/vendor/jsclasses/jsbook.cls +53 -14
  156. data/vendor/jsclasses/jsclasses.dtx +84 -25
  157. data/vendor/jsclasses/jsclasses.pdf +0 -0
  158. data/vendor/jsclasses/jslogo.dtx +4 -4
  159. data/vendor/jsclasses/jslogo.pdf +0 -0
  160. data/vendor/jsclasses/jslogo.sty +3 -3
  161. data/vendor/jsclasses/jspf.cls +52 -13
  162. data/vendor/jsclasses/jsreport.cls +53 -14
  163. data/vendor/jsclasses/kiyou.cls +53 -14
  164. data/vendor/jsclasses/okumacro.dtx +4 -5
  165. data/vendor/jsclasses/okumacro.pdf +0 -0
  166. data/vendor/jsclasses/okumacro.sty +3 -4
  167. metadata +55 -10
  168. data/lib/review/tocparser.rb +0 -271
  169. data/samples/syntax-book/review-ext.rb +0 -14
  170. data/test/test_tocparser.rb +0 -25
@@ -1,4 +1,4 @@
1
- # Copyright (c) 2008-2017 Minero Aoki, Kenshi Muto
1
+ # Copyright (c) 2008-2019 Minero Aoki, Kenshi Muto
2
2
  # 2002-2006 Minero Aoki
3
3
  #
4
4
  # This program is free software.
@@ -67,13 +67,13 @@ module ReVIEW
67
67
  end
68
68
  private :builder_init_file
69
69
 
70
- def print(s)
70
+ def print(*s)
71
71
  @blank_seen = false
72
72
  super
73
73
  end
74
74
  private :print
75
75
 
76
- def puts(s)
76
+ def puts(*s)
77
77
  @blank_seen = false
78
78
  super
79
79
  end
@@ -103,13 +103,13 @@ module ReVIEW
103
103
  blank
104
104
  end
105
105
  puts '=' * caption.size * 2
106
- when 2 then
106
+ when 2
107
107
  p = '='
108
- when 3 then
108
+ when 3
109
109
  p = '-'
110
- when 4 then
110
+ when 4
111
111
  p = '`'
112
- when 5 then
112
+ when 5
113
113
  p = '~'
114
114
  end
115
115
 
@@ -124,7 +124,7 @@ module ReVIEW
124
124
  end
125
125
 
126
126
  def ul_item(lines)
127
- puts ' ' * (@ul_indent - 1) + "* #{lines.join}"
127
+ puts ' ' * (@ul_indent - 1) + "* #{join_lines_to_paragraph(lines)}"
128
128
  end
129
129
 
130
130
  def ul_end
@@ -138,7 +138,7 @@ module ReVIEW
138
138
  end
139
139
 
140
140
  def ol_item(lines, _num)
141
- puts ' ' * (@ol_indent - 1) + "#. #{lines.join}"
141
+ puts ' ' * (@ol_indent - 1) + "#. #{join_lines_to_paragraph(lines)}"
142
142
  end
143
143
 
144
144
  def ol_end
@@ -167,7 +167,7 @@ module ReVIEW
167
167
  if @in_role
168
168
  pre = ' '
169
169
  end
170
- puts pre + lines.join
170
+ puts pre + join_lines_to_paragraph(lines)
171
171
  puts "\n"
172
172
  end
173
173
 
@@ -575,87 +575,87 @@ module ReVIEW
575
575
  end
576
576
 
577
577
  def flushright(lines)
578
- base_parablock 'flushright', lines, nil
578
+ base_parablock('flushright', lines, nil)
579
579
  end
580
580
 
581
581
  def centering(lines)
582
- base_parablock 'centering', lines, nil
582
+ base_parablock('centering', lines, nil)
583
583
  end
584
584
 
585
585
  def note(lines, caption = nil)
586
- base_parablock 'note', lines, caption
586
+ base_parablock('note', lines, caption)
587
587
  end
588
588
 
589
589
  def memo(lines, caption = nil)
590
- base_parablock 'memo', lines, caption
590
+ base_parablock('memo', lines, caption)
591
591
  end
592
592
 
593
593
  def tip(lines, caption = nil)
594
- base_parablock 'tip', lines, caption
594
+ base_parablock('tip', lines, caption)
595
595
  end
596
596
 
597
597
  def info(lines, caption = nil)
598
- base_parablock 'info', lines, caption
598
+ base_parablock('info', lines, caption)
599
599
  end
600
600
 
601
601
  def planning(lines, caption = nil)
602
- base_parablock 'planning', lines, caption
602
+ base_parablock('planning', lines, caption)
603
603
  end
604
604
 
605
605
  def best(lines, caption = nil)
606
- base_parablock 'best', lines, caption
606
+ base_parablock('best', lines, caption)
607
607
  end
608
608
 
609
609
  def important(lines, caption = nil)
610
- base_parablock 'important', lines, caption
610
+ base_parablock('important', lines, caption)
611
611
  end
612
612
 
613
613
  def security(lines, caption = nil)
614
- base_parablock 'security', lines, caption
614
+ base_parablock('security', lines, caption)
615
615
  end
616
616
 
617
617
  def caution(lines, caption = nil)
618
- base_parablock 'caution', lines, caption
618
+ base_parablock('caution', lines, caption)
619
619
  end
620
620
 
621
621
  def term(lines)
622
- base_parablock 'term', lines, nil
622
+ base_parablock('term', lines, nil)
623
623
  end
624
624
 
625
625
  def link(lines, caption = nil)
626
- base_parablock 'link', lines, caption
626
+ base_parablock('link', lines, caption)
627
627
  end
628
628
 
629
629
  def notice(lines, caption = nil)
630
- base_parablock 'notice', lines, caption
630
+ base_parablock('notice', lines, caption)
631
631
  end
632
632
 
633
633
  def point(lines, caption = nil)
634
- base_parablock 'point', lines, caption
634
+ base_parablock('point', lines, caption)
635
635
  end
636
636
 
637
637
  def shoot(lines, caption = nil)
638
- base_parablock 'shoot', lines, caption
638
+ base_parablock('shoot', lines, caption)
639
639
  end
640
640
 
641
641
  def reference(lines)
642
- base_parablock 'reference', lines, nil
642
+ base_parablock('reference', lines, nil)
643
643
  end
644
644
 
645
645
  def practice(lines)
646
- base_parablock 'practice', lines, nil
646
+ base_parablock('practice', lines, nil)
647
647
  end
648
648
 
649
649
  def expert(lines)
650
- base_parablock 'expert', lines, nil
650
+ base_parablock('expert', lines, nil)
651
651
  end
652
652
 
653
653
  def insn(lines, caption = nil)
654
- base_block 'insn', lines, caption
654
+ base_block('insn', lines, caption)
655
655
  end
656
656
 
657
657
  def warning(lines, caption = nil)
658
- base_parablock 'warning', lines, caption
658
+ base_parablock('warning', lines, caption)
659
659
  end
660
660
 
661
661
  alias_method :box, :insn
@@ -682,7 +682,7 @@ module ReVIEW
682
682
  end
683
683
 
684
684
  def bpo(lines)
685
- base_block 'bpo', lines, nil
685
+ base_block('bpo', lines, nil)
686
686
  end
687
687
 
688
688
  def inline_dtp(_str)
@@ -714,7 +714,7 @@ module ReVIEW
714
714
  end
715
715
 
716
716
  def source(lines, caption = nil, _lang = nil)
717
- base_block 'source', lines, caption
717
+ base_block('source', lines, caption)
718
718
  end
719
719
 
720
720
  def inline_ttibold(str)
@@ -1,4 +1,4 @@
1
- # Copyright (c) 2018 Kenshi Muto
1
+ # Copyright (c) 2018-2020 Kenshi Muto
2
2
  #
3
3
  # This program is free software.
4
4
  # You can distribute or modify this program under the terms of
@@ -15,9 +15,12 @@ require 'review/book'
15
15
  require 'review/yamlloader'
16
16
  require 'review/topbuilder'
17
17
  require 'review/version'
18
+ require 'review/makerhelper'
18
19
 
19
20
  module ReVIEW
20
21
  class TEXTMaker
22
+ include MakerHelper
23
+
21
24
  attr_accessor :config, :basedir
22
25
 
23
26
  def initialize
@@ -27,12 +30,12 @@ module ReVIEW
27
30
  end
28
31
 
29
32
  def error(msg)
30
- @logger.error "#{File.basename($PROGRAM_NAME, '.*')}: #{msg}"
33
+ @logger.error msg
31
34
  exit 1
32
35
  end
33
36
 
34
37
  def warn(msg)
35
- @logger.warn "#{File.basename($PROGRAM_NAME, '.*')}: #{msg}"
38
+ @logger.warn msg
36
39
  end
37
40
 
38
41
  def self.execute(*args)
@@ -42,10 +45,12 @@ module ReVIEW
42
45
  def parse_opts(args)
43
46
  cmd_config = {}
44
47
  opts = OptionParser.new
48
+ @buildonly = nil
45
49
 
46
50
  opts.banner = 'Usage: review-textmaker [-n] configfile'
47
51
  opts.version = ReVIEW::VERSION
48
52
  opts.on('-n', 'No decoration.') { @plaintext = true }
53
+ opts.on('-y', '--only file1,file2,...', 'Build only specified files.') { |v| @buildonly = v.split(/\s*,\s*/).map { |m| m.strip.sub(/\.re\Z/, '') } }
49
54
  opts.on('--help', 'Prints this message and quit.') do
50
55
  puts opts.help
51
56
  exit 0
@@ -65,6 +70,7 @@ module ReVIEW
65
70
  end
66
71
 
67
72
  def remove_old_files(path)
73
+ cleanup_mathimg('_review_math_text')
68
74
  FileUtils.rm_rf(path)
69
75
  end
70
76
 
@@ -80,6 +86,7 @@ module ReVIEW
80
86
  rescue => e
81
87
  error "yaml error #{e.message}"
82
88
  end
89
+
83
90
  # YAML configs will be overridden by command line options.
84
91
  @config.deep_merge!(cmd_config)
85
92
  I18n.setup(@config['language'])
@@ -89,6 +96,11 @@ module ReVIEW
89
96
  raise if @config['debug']
90
97
  error(e.message)
91
98
  end
99
+
100
+ math_dir = "./#{@config['imagedir']}/_review_math_text"
101
+ if @config['imgmath'] && File.exist?(File.join(math_dir, '__IMGMATH_BODY__.map'))
102
+ make_math_images(math_dir)
103
+ end
92
104
  end
93
105
 
94
106
  def generate_text_files(yamlfile)
@@ -144,6 +156,10 @@ module ReVIEW
144
156
  filename = Pathname.new(chap.path).relative_path_from(base_path).to_s
145
157
  end
146
158
  id = File.basename(filename).sub(/\.re\Z/, '')
159
+ if @buildonly && !@buildonly.include?(id)
160
+ warn "skip #{id}.re"
161
+ return
162
+ end
147
163
 
148
164
  textfile = "#{id}.txt"
149
165
 
@@ -1,4 +1,13 @@
1
+ # Copyright (c) 2008-2020 Minero Aoki, Kenshi Muto, Masayoshi Takahashi,
2
+ # KADO Masanori
3
+ # 2002-2007 Minero Aoki
4
+ #
5
+ # This program is free software.
6
+ # You can distribute or modify this program under the terms of
7
+ # the GNU LGPL, Lesser General Public License version 2.1.
8
+ #
1
9
  require 'nkf'
10
+ require 'digest'
2
11
 
3
12
  module ReVIEW
4
13
  module TextUtils
@@ -26,10 +35,75 @@ module ReVIEW
26
35
  end
27
36
  end
28
37
 
38
+ blocked_lines.map! { |i| join_lines_to_paragraph(i) }
39
+
29
40
  if pre && post
30
- blocked_lines.map! { |i| [pre] + i + [post] }
41
+ blocked_lines.map! { |i| pre + i + post }
42
+ end
43
+
44
+ blocked_lines
45
+ end
46
+
47
+ def add_space?(line1, line2, lang, lazy = nil)
48
+ # https://drafts.csswg.org/css-text-3/#line-break-transform
49
+ tail = line1[-1]
50
+ head = line2[0]
51
+ if tail.nil? || head.nil?
52
+ return nil
53
+ end
54
+ space = true
55
+ # rule 2
56
+ if %i[F W H].include?(Unicode::Eaw.property(tail)) &&
57
+ %i[F W H].include?(Unicode::Eaw.property(head)) &&
58
+ tail !~ /\p{Hangul}/ && head !~ /\p{Hangul}/
59
+ space = nil
60
+ end
61
+
62
+ if %w[ja zh zh_CN zh_TW yi].include?(lang)
63
+ # rule 3
64
+ if (%i[F W H].include?(Unicode::Eaw.property(tail)) &&
65
+ tail !~ /\p{Hangul}/ &&
66
+ (head =~ /\p{P}/ || head =~ /\p{S}/ || Unicode::Eaw.property(head) == :A)) ||
67
+ (%i[F W H].include?(Unicode::Eaw.property(head)) &&
68
+ head !~ /\p{Hangul}/ &&
69
+ (tail =~ /\p{P}/ || head =~ /\p{S}/ || Unicode::Eaw.property(tail) == :A))
70
+ space = nil
71
+ end
72
+
73
+ # lazy than rule 3, but it looks better
74
+ if lazy &&
75
+ (%i[F W H].include?(Unicode::Eaw.property(tail)) &&
76
+ tail !~ /\p{Hangul}/) ||
77
+ (%i[F W H].include?(Unicode::Eaw.property(head)) &&
78
+ head !~ /\p{Hangul}/)
79
+ space = nil
80
+ end
81
+ end
82
+ space
83
+ end
84
+
85
+ def join_lines_to_paragraph(lines)
86
+ unless @book.config['join_lines_by_lang']
87
+ return lines.join
88
+ end
89
+ lazy = true
90
+ lang = 'ja'
91
+ 0.upto(lines.size - 2) do |n|
92
+ if add_space?(lines[n], lines[n + 1], lang, lazy)
93
+ lines[n] += ' '
94
+ end
95
+ end
96
+ lines.join
97
+ end
98
+
99
+ def defer_math_image(str, path, key)
100
+ # for Re:VIEW >3
101
+ File.open(File.join(File.dirname(path), "__IMGMATH_BODY__#{key}.tex"), 'w') do |f|
102
+ f.puts str
103
+ end
104
+ File.open(File.join(File.dirname(path), '__IMGMATH_BODY__.map'), 'a+') do |f|
105
+ f.puts key
31
106
  end
32
- blocked_lines.map(&:join)
33
107
  end
34
108
 
35
109
  private
@@ -1,148 +1,277 @@
1
- # Copyright (c) 2008-2017 Minero Aoki, Kenshi Muto
2
- # 2002-2007 Minero Aoki
1
+ # Copyright (c) 2008-2020 Minero Aoki, Kenshi Muto
2
+ # 1999-2007 Minero Aoki
3
3
  #
4
4
  # This program is free software.
5
5
  # You can distribute or modify this program under the terms of
6
6
  # the GNU LGPL, Lesser General Public License version 2.1.
7
- # For details of LGPL, see the file "COPYING".
7
+ # For details of the GNU LGPL, see the file "COPYING".
8
8
  #
9
9
 
10
- require 'review/htmlutils'
11
- require 'review/tocparser'
10
+ require 'review/book'
11
+ require 'review/version'
12
+ require 'optparse'
13
+ require 'review/plaintextbuilder'
12
14
 
13
15
  module ReVIEW
14
- class TOCPrinter
15
- def self.default_upper_level
16
- 99 # no one use 99 level nest
16
+ class PLAINTEXTTocBuilder < PLAINTEXTBuilder
17
+ def headline(level, label, caption)
18
+ if @chapter.is_a?(ReVIEW::Book::Part)
19
+ print "\x01H0\x01" # XXX: don't modify level value. level value will be handled in sec_counter#prefix()
20
+ else
21
+ print "\x01H#{level}\x01"
22
+ end
23
+ # embed header information for tocparser
24
+ super(level, label, caption)
17
25
  end
18
26
 
19
- def initialize(print_upper, param, out = $stdout)
20
- @print_upper = print_upper
21
- @config = param
22
- @out = out
27
+ def base_block(type, lines, caption = nil)
28
+ puts "\x01STARTLIST\x01"
29
+ super(type, lines, caption)
30
+ puts "\x01ENDLIST\x01"
23
31
  end
24
32
 
25
- def print_book(book)
26
- book.each_part { |part| print_part(part) }
33
+ def blank
34
+ @blank_seen = true
27
35
  end
36
+ end
28
37
 
29
- def print_part(part)
30
- part.each_chapter { |chap| print_chapter(chap) }
38
+ class TOCPrinter
39
+ def self.execute(*args)
40
+ Signal.trap(:INT) { exit 1 }
41
+ if RUBY_PLATFORM !~ /mswin(?!ce)|mingw|cygwin|bccwin/
42
+ Signal.trap(:PIPE, 'IGNORE')
43
+ end
44
+ new.execute(*args)
45
+ rescue Errno::EPIPE
46
+ exit 0
31
47
  end
32
48
 
33
- def print_chapter(chap)
34
- chap_node = TOCParser.chapter_node(chap)
35
- print_node 1, chap_node
36
- print_children chap_node
49
+ def initialize
50
+ @logger = ReVIEW.logger
51
+ @config = ReVIEW::Configure.values
52
+ @yamlfile = 'config.yml'
53
+ @book = ReVIEW::Book::Base.load
54
+ @upper = 4
55
+ @indent = true
56
+ @buildonly = nil
57
+ @detail = nil
37
58
  end
38
59
 
39
- def print?(level)
40
- level <= @print_upper
60
+ def execute(*args)
61
+ parse_options(args)
62
+ @book.config = ReVIEW::Configure.values
63
+ unless File.readable?(@yamlfile)
64
+ @logger.error("No such fiile or can't open #{@yamlfile}.")
65
+ exit 1
66
+ end
67
+ @book.load_config(@yamlfile)
68
+ I18n.setup(@config['language'])
69
+
70
+ if @detail
71
+ begin
72
+ require 'unicode/eaw'
73
+ @calc_char_width = true
74
+ rescue LoadError
75
+ @logger.warn('not found unicode/eaw library. page volume may be unreliable.')
76
+ @calc_char_width = nil
77
+ end
78
+ end
79
+
80
+ print_result(build_result_array)
41
81
  end
42
- end
43
82
 
44
- class TextTOCPrinter < TOCPrinter
45
- private
83
+ def build_result_array
84
+ result_array = []
85
+ begin
86
+ @book.parts.each do |part|
87
+ if part.name.present? && (@buildonly.nil? || @buildonly.include?(part.name))
88
+ result_array.push({ part: 'start' })
89
+ if part.file?
90
+ result = build_chap(part)
91
+ result_array += parse_contents(part.name, @upper, result)
92
+ else
93
+ title = part.format_number + I18n.t('chapter_postfix') + part.title
94
+ result_array += [
95
+ { name: '', lines: 1, chars: title.size, list_lines: 0, text_lines: 1 },
96
+ { level: 0, headline: title, lines: 1, chars: title.size, list_lines: 0, text_lines: 1 }
97
+ ]
98
+ end
99
+ end
46
100
 
47
- def print_children(node)
48
- return unless print?(node.level + 1)
49
- node.each_section_with_index do |section, idx|
50
- unless section.blank?
51
- print_node idx + 1, section
52
- print_children section
101
+ part.chapters.each do |chap|
102
+ if @buildonly.nil? || @buildonly.include?(chap.name)
103
+ result = build_chap(chap)
104
+ result_array += parse_contents(chap.name, @upper, result)
105
+ end
106
+ end
107
+ if part.name.present? && (@buildonly.nil? || @buildonly.include?(part.name))
108
+ result_array.push({ part: 'end' })
109
+ end
53
110
  end
111
+ rescue ReVIEW::FileNotFound => e
112
+ @logger.error e
113
+ exit 1
54
114
  end
115
+
116
+ result_array
55
117
  end
56
118
 
57
- def print_node(number, node)
58
- if node.chapter?
59
- vol = node.volume
60
- @out.printf "%3s %3dKB %6dC %5dL %s (%s)\n",
61
- chapnumstr(node.number),
62
- vol.kbytes, vol.chars, vol.lines,
63
- node.label, node.chapter_id
64
- else ## for section node
65
- @out.printf "%17s %5dL %s\n",
66
- '', node.estimated_lines,
67
- " #{' ' * (node.level - 1)}#{number} #{node.label}"
119
+ def print_result(result_array)
120
+ result_array.each do |result|
121
+ if result[:part]
122
+ next
123
+ end
124
+
125
+ if result[:name]
126
+ # file information
127
+ if @detail
128
+ puts '============================='
129
+ printf("%6dC %5dL %5dP %s\n", result[:chars], result[:lines], calc_pages(result).ceil, result[:name])
130
+ puts '-----------------------------'
131
+ end
132
+ next
133
+ end
134
+
135
+ # section information
136
+ if @detail
137
+ printf('%6dC %5dL %5.1fP ', result[:chars], result[:lines], calc_pages(result))
138
+ end
139
+ if @indent && result[:level]
140
+ print ' ' * (result[:level] == 0 ? 0 : result[:level] - 1)
141
+ end
142
+ puts result[:headline]
68
143
  end
69
144
  end
70
145
 
71
- def chapnumstr(n)
72
- n ? sprintf('%2d.', n) : ' '
146
+ def calc_pages(result)
147
+ p = 0
148
+ p += result[:list_lines].to_f / @book.page_metric.list.n_lines
149
+ p += result[:text_lines].to_f / @book.page_metric.text.n_lines
150
+ p
73
151
  end
74
152
 
75
- def volume_columns(level, volstr)
76
- cols = ['', '', '', nil]
77
- cols[level - 1] = volstr
78
- cols[0, 3] # does not display volume of level-4 section
153
+ def calc_linesize(l)
154
+ return l.size unless @calc_char_width
155
+ w = 0
156
+ l.split('').each do |c|
157
+ # XXX: should include A also?
158
+ if %i[Na H N].include?(Unicode::Eaw.property(c))
159
+ w += 0.5 # halfwidth
160
+ else
161
+ w += 1
162
+ end
163
+ end
164
+ w
79
165
  end
80
- end
81
166
 
82
- class HTMLTOCPrinter < TOCPrinter
83
- include HTMLUtils
167
+ def parse_contents(name, upper, content)
168
+ headline_array = []
169
+ counter = { lines: 0, chars: 0, list_lines: 0, text_lines: 0 }
170
+ listmode = nil
84
171
 
85
- def print_book(book)
86
- @out.puts '<ul class="book-toc">'
87
- book.each_part { |part| print_part(part) }
88
- @out.puts '</ul>'
89
- end
172
+ content.split("\n").each do |l|
173
+ if l.start_with?("\x01STARTLIST\x01")
174
+ listmode = true
175
+ next
176
+ elsif l.start_with?("\x01ENDLIST\x01")
177
+ listmode = nil
178
+ next
179
+ elsif l =~ /\A\x01H(\d)\x01/
180
+ # headline
181
+ level = $1.to_i
182
+ l = $'
183
+ if level <= upper
184
+ if counter[:chars] > 0
185
+ headline_array.push(counter)
186
+ end
187
+ headline = l
188
+ counter = {
189
+ level: level,
190
+ headline: headline,
191
+ lines: 1,
192
+ chars: headline.size,
193
+ list_lines: 0,
194
+ text_lines: 1
195
+ }
196
+ next
197
+ end
198
+ end
90
199
 
91
- def print_part(part)
92
- if part.number
93
- @out.puts li(part.title)
94
- end
95
- super
96
- end
200
+ counter[:lines] += 1
201
+ counter[:chars] += l.size
97
202
 
98
- def print_chapter(chap)
99
- chap_node = TOCParser.chapter_node(chap)
100
- ext = chap.book.config['htmlext'] || 'html'
101
- path = chap.path.sub(/\.re/, '.' + ext)
102
- label = if chap_node.number && chap.on_chaps?
103
- "#{chap.number} #{chap.title}"
104
- else
105
- chap.title
106
- end
107
- @out.puts li(a_name(path, escape_html(label)))
108
- return unless print?(2)
109
- if print?(3)
110
- @out.puts chap_sections_to_s(chap_node)
111
- else
112
- @out.puts chapter_to_s(chap_node)
203
+ if listmode
204
+ # code list: calculate line wrapping
205
+ if l.size == 0
206
+ counter[:list_lines] += 1
207
+ else
208
+ counter[:list_lines] += (calc_linesize(l) - 1) / @book.page_metric.list.n_columns + 1
209
+ end
210
+ else
211
+ # normal paragraph: calculate line wrapping
212
+ if l.size == 0
213
+ counter[:text_lines] += 1
214
+ else
215
+ counter[:text_lines] += (calc_linesize(l) - 1) / @book.page_metric.text.n_columns + 1
216
+ end
217
+ end
113
218
  end
114
- end
219
+ headline_array.push(counter)
115
220
 
116
- private
221
+ total_lines = 0
222
+ total_chars = 0
223
+ total_list_lines = 0
224
+ total_text_lines = 0
117
225
 
118
- def chap_sections_to_s(chap)
119
- return '' if chap.section_size < 1
120
- res = []
121
- res << '<ol>'
122
- chap.each_section { |sec| res << li(escape_html(sec.label)) }
123
- res << '</ol>'
124
- res.join("\n")
125
- end
226
+ headline_array.each do |h|
227
+ next unless h[:lines]
228
+ total_lines += h[:lines]
229
+ total_chars += h[:chars]
230
+ total_list_lines += h[:list_lines]
231
+ total_text_lines += h[:text_lines]
232
+ end
126
233
 
127
- def chapter_to_s(chap)
128
- res = []
129
- chap.each_section do |sec|
130
- res << li(escape_html(sec.label))
131
- next unless print?(4)
132
- next unless sec.section_size > 0
133
- res << '<ul>'
134
- sec.each_section { |node| res << li(escape_html(node.label)) }
135
- res << '</ul>'
136
- end
137
- res.join("\n")
234
+ headline_array.delete_if(&:empty?).
235
+ unshift({ name: name, lines: total_lines, chars: total_chars, list_lines: total_list_lines, text_lines: total_text_lines })
138
236
  end
139
237
 
140
- def li(content)
141
- "<li>#{content}</li>"
238
+ def build_chap(chap)
239
+ compiler = ReVIEW::Compiler.new(ReVIEW::PLAINTEXTTocBuilder.new)
240
+ begin
241
+ compiler.compile(@book.chapter(chap.name))
242
+ rescue ReVIEW::ApplicationError => e
243
+ @logger.error e
244
+ exit 1
245
+ end
142
246
  end
143
247
 
144
- def a_name(name, label)
145
- %Q(<a name="#{name}">#{label}</a>)
248
+ def parse_options(args)
249
+ opts = OptionParser.new
250
+ opts.version = ReVIEW::VERSION
251
+ opts.on('--yaml=YAML', 'Read configurations from YAML file.') { |yaml| @yamlfile = yaml }
252
+ opts.on('-y', '--only file1,file2,...', 'list only specified files.') do |v|
253
+ @buildonly = v.split(/\s*,\s*/).map { |m| m.strip.sub(/\.re\Z/, '') }
254
+ end
255
+ opts.on('-l', '--level N', 'list upto N level (default=4)') do |n|
256
+ @upper = n.to_i
257
+ end
258
+ opts.on('-d', '--detail', 'show characters and lines of each section.') do
259
+ @detail = true
260
+ end
261
+ opts.on('--noindent', "don't indent headlines.") do
262
+ @indent = nil
263
+ end
264
+ opts.on('--help', 'print this message and quit.') do
265
+ puts opts.help
266
+ exit 0
267
+ end
268
+ begin
269
+ opts.parse!(args)
270
+ rescue OptionParser::ParseError => e
271
+ @logger.error e.message
272
+ $stderr.puts opts.help
273
+ exit 1
274
+ end
146
275
  end
147
276
  end
148
277
  end