Class: Inkscape::Merge::Processor

Inherits:
Object
  • Object
show all
Defined in:
lib/inkscape_merge/processor.rb

Overview

Main class to initialize processing

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeProcessor

Initialize the processor, setting files and options



14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
# File 'lib/inkscape_merge/processor.rb', line 14

def initialize
  @options = OpenStruct.new
  # Default options
  @options.format = "pdf"
  @options.csv_options = {:headers => true, :col_sep => ',', :encoding => 'utf-8'}
  @options.limit = 0
  @options.dpi = 300
  @options.inkscape = %x(which inkscape).chomp
  # If no Inkscape in PATH, try to guess from platform
  if options.inkscape.empty?
    options.inkscape = case RUBY_PLATFORM
      when /darwin/
        "/Applications/Inkscape.app/Contents/Resources/bin/inkscape"
      end
  end

end

Instance Attribute Details

#optionsObject (readonly)

Returns the value of attribute options.



11
12
13
# File 'lib/inkscape_merge/processor.rb', line 11

def options
  @options
end

Instance Method Details

#runObject

Iterate over all data rows and generate output files Optionally stop when LIMIT is reached



34
35
36
37
38
39
40
41
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
67
68
69
70
71
72
73
74
75
76
77
# File 'lib/inkscape_merge/processor.rb', line 34

def run
  validate_options

  # Open the files
  @svg = File.read options.svg_file
  @data_file = DataParser.detect(options)

  count = 0
  headers = @data_file.headers
  pattern = /%VAR_(#{headers.map(&:to_s).join("|")})%/
  @data_file.each{|row|
    break if @options.limit > 0 && count >= @options.limit
    count += 1
    puts "Row: #{count}"
    tmp_file = Tempfile.new('inkscape_merge')
    begin
      (outfile,merged_svg) = [@options.output,@svg].map{|s|
          s.gsub(pattern){|m|
            puts $1 if @options.verbose
            # return corresponding value from current row
            row[$1]
        }
      }

      # Write merged SVG out
      tmp_file.puts merged_svg
      tmp_file.close

      # Sprintf outfile with current row number
      outfile %= count

      # Generate output path
      FileUtils.mkdir_p(File.dirname outfile)

      # Generate the file itself
      ink_generate tmp_file.path, Shellwords.escape(outfile), @options.format, @options.dpi
    rescue => e
      $stderr.puts "ERROR: #{e}"
      $stderr.puts e.backtrace if @options.verbose
    ensure
      tmp_file.unlink
    end
  }
end