Class: ReVIEW::IDGXMLMaker

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

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeIDGXMLMaker

Returns a new instance of IDGXMLMaker.



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

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

Instance Attribute Details

#basedirObject

Returns the value of attribute basedir.



21
22
23
# File 'lib/review/idgxmlmaker.rb', line 21

def basedir
  @basedir
end

#configObject

Returns the value of attribute config.



21
22
23
# File 'lib/review/idgxmlmaker.rb', line 21

def config
  @config
end

Class Method Details

.execute(*args) ⇒ Object



38
39
40
# File 'lib/review/idgxmlmaker.rb', line 38

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

Instance Method Details

#apply_filter(xmlfile) ⇒ Object



114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
# File 'lib/review/idgxmlmaker.rb', line 114

def apply_filter(xmlfile)
  return unless @filter

  # pass filename information to filter by environment variable
  ENV['REVIEW_FNAME'] = File.basename(xmlfile).sub(/.xml\Z/, '.re')
  begin
    o, e, s = Open3.capture3(@filter, stdin_data: File.read(xmlfile))
    unless e.empty?
      warn("filter error for #{xmlfile}: #{e}")
    end
    if s.success?
      File.write(xmlfile, o) # override
    end
  rescue => e
    warn("filter error for #{xmlfile}: #{e.message}")
  end
end

#build_body(basetmpdir, _yamlfile) ⇒ Object



132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
# File 'lib/review/idgxmlmaker.rb', line 132

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

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

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



166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
# File 'lib/review/idgxmlmaker.rb', line 166

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/, '')
  if @buildonly && !@buildonly.include?(id)
    warn "skip #{id}.re"
    return
  end

  xmlfile = "#{id}.xml"

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

#build_part(part, basetmpdir, xmlfile) ⇒ Object



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

def build_part(part, basetmpdir, xmlfile)
  File.open(File.join(basetmpdir, xmlfile), 'w') do |f|
    title = ReVIEW::I18n.t('part', part.number)
    if part.name.strip.present?
      title << ReVIEW::I18n.t('chapter_postfix')
      title << part.name.strip
    end
    f.puts '<?xml version="1.0" encoding="UTF-8"?>'
    f.print '<doc xmlns:aid="http://ns.adobe.com/AdobeInDesign/4.0/"><title aid:pstyle="h1">'
    f.print CGI.escapeHTML(title)
    f.print '</title><?dtp level="1" section="'
    f.print CGI.escapeHTML(title)
    f.puts '"?></doc>'
  end
  apply_filter(File.join(basetmpdir, xmlfile))
end

#build_pathObject



68
69
70
# File 'lib/review/idgxmlmaker.rb', line 68

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

#error(msg) ⇒ Object



29
30
31
32
# File 'lib/review/idgxmlmaker.rb', line 29

def error(msg)
  @logger.error "#{File.basename($PROGRAM_NAME, '.*')}: #{msg}"
  exit 1
end

#execute(*args) ⇒ Object



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

def execute(*args)
  @config = ReVIEW::Configure.values
  @config.maker = 'idgxmlmaker'
  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_idgxml_files(yamlfile)
  rescue ApplicationError => e
    raise if @config['debug']
    error(e.message)
  end
end

#generate_idgxml_files(yamlfile) ⇒ Object



99
100
101
102
103
104
105
106
107
108
109
110
111
112
# File 'lib/review/idgxmlmaker.rb', line 99

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

  @book = ReVIEW::Book.load(@basedir)
  @book.config = @config
  if @table
    @book.config['tableopt'] = @table
  end

  build_body(@path, yamlfile)
end

#parse_opts(args) ⇒ Object



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

def parse_opts(args)
  cmd_config = {}
  opts = OptionParser.new
  @table = nil
  @filter = nil
  @buildonly = nil

  opts.banner = 'Usage: review-idgxmlmaker [options] configfile'
  opts.version = ReVIEW::VERSION
  opts.on('-w', '--width widthoftypepage', 'Specify the width of type page for layouting tables (mm).') { |v| @table = v }
  opts.on('-f', '--filter filterprogrampath', 'Specify the filter path.') { |v| @filter = v }
  opts.on('-y', '--only file1,file2,...', 'Build only specified files.') { |v| @buildonly = v.split(/\s*,\s*/).map { |m| m.strip.sub(/\.re\Z/, '') } }
  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



72
73
74
# File 'lib/review/idgxmlmaker.rb', line 72

def remove_old_files(path)
  FileUtils.rm_rf(path)
end

#warn(msg) ⇒ Object



34
35
36
# File 'lib/review/idgxmlmaker.rb', line 34

def warn(msg)
  @logger.warn "#{File.basename($PROGRAM_NAME, '.*')}: #{msg}"
end