Class: ReVIEW::VolumePrinter

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

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeVolumePrinter

Returns a new instance of VolumePrinter.



23
24
25
26
# File 'lib/review/volumeprinter.rb', line 23

def initialize
  @logger = ReVIEW.logger
  @yamlfile = 'config.yml'
end

Class Method Details

.execute(*args) ⇒ Object



19
20
21
# File 'lib/review/volumeprinter.rb', line 19

def self.execute(*args)
  new.execute(*args)
end

Instance Method Details

#execute(*args) ⇒ Object



28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
# File 'lib/review/volumeprinter.rb', line 28

def execute(*args)
  parse_options(args)
  begin
    @config = ReVIEW::Configure.create(yamlfile: @yamlfile)
    @book = ReVIEW::Book::Base.new('.', config: @config)
    unless File.readable?(@yamlfile)
      raise ReVIEW::FileNotFound, "No such fiile or can't open #{@yamlfile}."
    end

    I18n.setup(@book.config['language'])

    @book.each_part do |part|
      if part.number
        print_chapter_volume(part)
      end
      part.each_chapter do |chap|
        print_chapter_volume(chap)
      end
    end
  rescue ReVIEW::ConfigError, ReVIEW::FileNotFound, ReVIEW::CompileError, ReVIEW::ApplicationError => e
    @logger.error e.message
    exit 1
  end
  puts '============================='
  print_volume(@book.volume)
end

#parse_options(args) ⇒ Object



55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
# File 'lib/review/volumeprinter.rb', line 55

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('--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


72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
# File 'lib/review/volumeprinter.rb', line 72

def print_chapter_volume(chap)
  builder = ReVIEW::PLAINTEXTBuilder.new
  builder.bind(ReVIEW::Compiler.new(builder), chap, nil)

  vol = chap.volume
  title = chap.format_number
  unless title.empty?
    title += '  '
  end
  begin
    title += builder.compile_inline(chap.title)
  rescue ReVIEW::ApplicationError => e
    @logger.warn "#{chap.name} : #{e.message.sub(/.+error: /, '')}"
  end

  printf("%3dKB %6dC %5dL %3dP  %s %-s\n",
         vol.kbytes, vol.chars, vol.lines, vol.page,
         "#{chap.name} ".ljust(15, '.'), title)
end


92
93
94
95
# File 'lib/review/volumeprinter.rb', line 92

def print_volume(vol)
  # total
  printf("%3dKB %6dC %5dL %3dP\n", vol.kbytes, vol.chars, vol.lines, vol.page)
end