Class: ReVIEW::TOCPrinter

Inherits:
Object show all
Defined in:
lib/review/tocprinter.rb

Direct Known Subclasses

WEBTOCPrinter

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeTOCPrinter

Returns a new instance of TOCPrinter.



49
50
51
52
53
54
55
56
57
58
# File 'lib/review/tocprinter.rb', line 49

def initialize
  @logger = ReVIEW.logger
  @config = ReVIEW::Configure.values
  @yamlfile = 'config.yml'
  @book = ReVIEW::Book::Base.load
  @upper = 4
  @indent = true
  @buildonly = nil
  @detail = nil
end

Class Method Details

.execute(*args) ⇒ Object



39
40
41
42
43
44
45
46
47
# File 'lib/review/tocprinter.rb', line 39

def self.execute(*args)
  Signal.trap(:INT) { exit 1 }
  if RUBY_PLATFORM !~ /mswin(?!ce)|mingw|cygwin|bccwin/
    Signal.trap(:PIPE, 'IGNORE')
  end
  new.execute(*args)
rescue Errno::EPIPE
  exit 0
end

Instance Method Details

#build_chap(chap) ⇒ Object



238
239
240
241
242
243
244
245
246
# File 'lib/review/tocprinter.rb', line 238

def build_chap(chap)
  compiler = ReVIEW::Compiler.new(ReVIEW::PLAINTEXTTocBuilder.new)
  begin
    compiler.compile(@book.chapter(chap.name))
  rescue ReVIEW::ApplicationError => e
    @logger.error e
    exit 1
  end
end

#build_result_arrayObject



83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
# File 'lib/review/tocprinter.rb', line 83

def build_result_array
  result_array = []
  begin
    @book.parts.each do |part|
      if part.name.present? && (@buildonly.nil? || @buildonly.include?(part.name))
        result_array.push({ part: 'start' })
        if part.file?
          result = build_chap(part)
          result_array += parse_contents(part.name, @upper, result)
        else
          title = part.format_number + I18n.t('chapter_postfix') + part.title
          result_array += [
            { name: '', lines: 1, chars: title.size, list_lines: 0, text_lines: 1 },
            { level: 0, headline: title, lines: 1, chars: title.size, list_lines: 0, text_lines: 1 }
          ]
        end
      end

      part.chapters.each do |chap|
        if @buildonly.nil? || @buildonly.include?(chap.name)
          result = build_chap(chap)
          result_array += parse_contents(chap.name, @upper, result)
        end
      end
      if part.name.present? && (@buildonly.nil? || @buildonly.include?(part.name))
        result_array.push({ part: 'end' })
      end
    end
  rescue ReVIEW::FileNotFound => e
    @logger.error e
    exit 1
  end

  result_array
end

#calc_linesize(l) ⇒ Object



153
154
155
156
157
158
159
160
161
162
163
164
165
# File 'lib/review/tocprinter.rb', line 153

def calc_linesize(l)
  return l.size unless @calc_char_width
  w = 0
  l.split('').each do |c|
    # XXX: should include A also?
    if %i[Na H N].include?(Unicode::Eaw.property(c))
      w += 0.5 # halfwidth
    else
      w += 1
    end
  end
  w
end

#calc_pages(result) ⇒ Object



146
147
148
149
150
151
# File 'lib/review/tocprinter.rb', line 146

def calc_pages(result)
  p = 0
  p += result[:list_lines].to_f / @book.page_metric.list.n_lines
  p += result[:text_lines].to_f / @book.page_metric.text.n_lines
  p
end

#execute(*args) ⇒ Object



60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
# File 'lib/review/tocprinter.rb', line 60

def execute(*args)
  parse_options(args)
  @book.config = ReVIEW::Configure.values
  unless File.readable?(@yamlfile)
    @logger.error("No such fiile or can't open #{@yamlfile}.")
    exit 1
  end
  @book.load_config(@yamlfile)
  I18n.setup(@config['language'])

  if @detail
    begin
      require 'unicode/eaw'
      @calc_char_width = true
    rescue LoadError
      @logger.warn('not found unicode/eaw library. page volume may be unreliable.')
      @calc_char_width = nil
    end
  end

  print_result(build_result_array)
end

#parse_contents(name, upper, content) ⇒ Object



167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
# File 'lib/review/tocprinter.rb', line 167

def parse_contents(name, upper, content)
  headline_array = []
  counter = { lines: 0, chars: 0, list_lines: 0, text_lines: 0 }
  listmode = nil

  content.split("\n").each do |l|
    if l.start_with?("\x01STARTLIST\x01")
      listmode = true
      next
    elsif l.start_with?("\x01ENDLIST\x01")
      listmode = nil
      next
    elsif l =~ /\A\x01H(\d)\x01/
      # headline
      level = $1.to_i
      l = $'
      if level <= upper
        if counter[:chars] > 0
          headline_array.push(counter)
        end
        headline = l
        counter = {
          level: level,
          headline: headline,
          lines: 1,
          chars: headline.size,
          list_lines: 0,
          text_lines: 1
        }
        next
      end
    end

    counter[:lines] += 1
    counter[:chars] += l.size

    if listmode
      # code list: calculate line wrapping
      if l.size == 0
        counter[:list_lines] += 1
      else
        counter[:list_lines] += (calc_linesize(l) - 1) / @book.page_metric.list.n_columns + 1
      end
    else
      # normal paragraph: calculate line wrapping
      if l.size == 0
        counter[:text_lines] += 1
      else
        counter[:text_lines] += (calc_linesize(l) - 1) / @book.page_metric.text.n_columns + 1
      end
    end
  end
  headline_array.push(counter)

  total_lines = 0
  total_chars = 0
  total_list_lines = 0
  total_text_lines = 0

  headline_array.each do |h|
    next unless h[:lines]
    total_lines += h[:lines]
    total_chars += h[:chars]
    total_list_lines += h[:list_lines]
    total_text_lines += h[:text_lines]
  end

  headline_array.delete_if(&:empty?).
    unshift({ name: name, lines: total_lines, chars: total_chars, list_lines: total_list_lines, text_lines: total_text_lines })
end

#parse_options(args) ⇒ Object



248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
# File 'lib/review/tocprinter.rb', line 248

def parse_options(args)
  opts = OptionParser.new
  opts.version = ReVIEW::VERSION
  opts.on('--yaml=YAML', 'Read configurations from YAML file.') { |yaml| @yamlfile = yaml }
  opts.on('-y', '--only file1,file2,...', 'list only specified files.') do |v|
    @buildonly = v.split(/\s*,\s*/).map { |m| m.strip.sub(/\.re\Z/, '') }
  end
  opts.on('-l', '--level N', 'list upto N level (default=4)') do |n|
    @upper = n.to_i
  end
  opts.on('-d', '--detail', 'show characters and lines of each section.') do
    @detail = true
  end
  opts.on('--noindent', "don't indent headlines.") do
    @indent = nil
  end
  opts.on('--help', 'print this message and quit.') do
    puts opts.help
    exit 0
  end
  begin
    opts.parse!(args)
  rescue OptionParser::ParseError => e
    @logger.error e.message
    $stderr.puts opts.help
    exit 1
  end
end


119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
# File 'lib/review/tocprinter.rb', line 119

def print_result(result_array)
  result_array.each do |result|
    if result[:part]
      next
    end

    if result[:name]
      # file information
      if @detail
        puts '============================='
        printf("%6dC %5dL %5dP  %s\n", result[:chars], result[:lines], calc_pages(result).ceil, result[:name])
        puts '-----------------------------'
      end
      next
    end

    # section information
    if @detail
      printf('%6dC %5dL %5.1fP  ', result[:chars], result[:lines], calc_pages(result))
    end
    if @indent && result[:level]
      print '  ' * (result[:level] == 0 ? 0 : result[:level] - 1)
    end
    puts result[:headline]
  end
end