Class: ReVIEW::TEXTMaker

Inherits:
Object show all
Includes:
MakerHelper
Defined in:
lib/review/textmaker.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods included from MakerHelper

bindir, #cleanup_mathimg, copy_images_to_dir, #default_imgmath_preamble, #make_math_images, #make_math_images_dvipng, #make_math_images_pdfcrop

Constructor Details

#initializeTEXTMaker

Returns a new instance of TEXTMaker.



26
27
28
29
30
# File 'lib/review/textmaker.rb', line 26

def initialize
  @basedir = nil
  @logger = ReVIEW.logger
  @plaintext = nil
end

Instance Attribute Details

#basedirObject

Returns the value of attribute basedir.



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

def basedir
  @basedir
end

#configObject

Returns the value of attribute config.



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

def config
  @config
end

Class Method Details

.execute(*args) ⇒ Object



41
42
43
# File 'lib/review/textmaker.rb', line 41

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

Instance Method Details

#build_body(basetmpdir, _yamlfile) ⇒ Object



116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
# File 'lib/review/textmaker.rb', line 116

def build_body(basetmpdir, _yamlfile)
  base_path = Pathname.new(@basedir)
  builder = nil
  if @plaintext
    builder = ReVIEW::PLAINTEXTBuilder.new
  else
    builder = ReVIEW::TOPBuilder.new
  end
  @converter = ReVIEW::Converter.new(@book, builder)
  @book.parts.each do |part|
    if part.name.present?
      if part.file?
        build_chap(part, base_path, basetmpdir, true)
      else
        textfile = "part_#{part.number}.txt"
        build_part(part, basetmpdir, textfile)
      end
    end

    part.chapters.each { |chap| build_chap(chap, base_path, basetmpdir, false) }
  end
end

#build_chap(chap, base_path, basetmpdir, ispart) ⇒ Object



148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
# File 'lib/review/textmaker.rb', line 148

def build_chap(chap, base_path, basetmpdir, ispart)
  filename = ''

  if ispart.present?
    filename = chap.path
  else
    filename = Pathname.new(chap.path).relative_path_from(base_path).to_s
  end
  id = File.basename(filename).sub(/\.re\Z/, '')

  textfile = "#{id}.txt"

  begin
    @converter.convert(filename, File.join(basetmpdir, textfile))
  rescue => e
    warn "compile error in #{filename} (#{e.class})"
    warn e.message
  end
end

#build_part(part, basetmpdir, textfile) ⇒ Object



139
140
141
142
143
144
145
146
# File 'lib/review/textmaker.rb', line 139

def build_part(part, basetmpdir, textfile)
  File.open(File.join(basetmpdir, textfile), 'w') do |f|
    f.print '■H1■' unless @plaintext
    f.print ReVIEW::I18n.t('part', part.number)
    f.print " #{part.name.strip}" if part.name.strip.present?
    f.puts
  end
end

#build_pathObject



66
67
68
# File 'lib/review/textmaker.rb', line 66

def build_path
  "#{@config['bookname']}-text"
end

#error(msg) ⇒ Object



32
33
34
35
# File 'lib/review/textmaker.rb', line 32

def error(msg)
  @logger.error msg
  exit 1
end

#execute(*args) ⇒ Object



75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
# File 'lib/review/textmaker.rb', line 75

def execute(*args)
  @config = ReVIEW::Configure.values
  @config.maker = 'textmaker'
  cmd_config, yamlfile = parse_opts(args)
  error "#{yamlfile} not found." unless File.exist?(yamlfile)

  begin
    loader = ReVIEW::YAMLLoader.new
    @config.deep_merge!(loader.load_file(yamlfile))
  rescue => e
    error "yaml error #{e.message}"
  end

  # YAML configs will be overridden by command line options.
  @config.deep_merge!(cmd_config)
  I18n.setup(@config['language'])
  begin
    generate_text_files(yamlfile)
  rescue ApplicationError => e
    raise if @config['debug']
    error(e.message)
  end

  math_dir = "./#{@config['imagedir']}/_review_math_text"
  if @config['imgmath'] && File.exist?(File.join(math_dir, '__IMGMATH_BODY__.tex'))
    make_math_images(math_dir)
  end
end

#generate_text_files(yamlfile) ⇒ Object



104
105
106
107
108
109
110
111
112
113
114
# File 'lib/review/textmaker.rb', line 104

def generate_text_files(yamlfile)
  @basedir = File.dirname(yamlfile)
  @path = build_path
  remove_old_files(@path)
  Dir.mkdir(@path)

  @book = ReVIEW::Book.load(@basedir)
  @book.config = @config

  build_body(@path, yamlfile)
end

#parse_opts(args) ⇒ Object



45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
# File 'lib/review/textmaker.rb', line 45

def parse_opts(args)
  cmd_config = {}
  opts = OptionParser.new

  opts.banner = 'Usage: review-textmaker [-n] configfile'
  opts.version = ReVIEW::VERSION
  opts.on('-n', 'No decoration.') { @plaintext = true }
  opts.on('--help', 'Prints this message and quit.') do
    puts opts.help
    exit 0
  end

  opts.parse!(args)
  if args.size != 1
    puts opts.help
    exit 0
  end

  [cmd_config, args[0]]
end

#remove_old_files(path) ⇒ Object



70
71
72
73
# File 'lib/review/textmaker.rb', line 70

def remove_old_files(path)
  cleanup_mathimg('_review_math_text')
  FileUtils.rm_rf(path)
end

#warn(msg) ⇒ Object



37
38
39
# File 'lib/review/textmaker.rb', line 37

def warn(msg)
  @logger.warn msg
end