Class: Svg2Slides

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

Constant Summary collapse

VERSION =
'0.1.0'
OPTIONS =
[]

Instance Method Summary collapse

Instance Method Details

#optionsObject



55
56
57
58
59
60
61
62
63
64
# File 'lib/svg_2_slides.rb', line 55

def options
  @options ||=
    begin
      opts = {}
      OPTIONS.each do |item|
        opts[item[:name]] = item[:default]
      end
      opts
    end
end

#process(inputfile) ⇒ Object



8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
# File 'lib/svg_2_slides.rb', line 8

def process(inputfile)
  doc = REXML::Document.new(File.new(inputfile))
  layers = doc.elements.each('descendant::g[attribute::inkscape:groupmode="layer"]') { |e| e }
  n = layers.size
  while n > 0
    filename = inputfile.sub('.svg', options['--suffix'] + '.svg') % n
    File.open(filename, 'w') do |f|
      say "Writing #{filename} ..."
      doc.write(f)
      layers.last.remove
      layers.pop
      n = n - 1
    end
  end
end

#process_argsObject



89
90
91
92
93
94
95
96
97
98
# File 'lib/svg_2_slides.rb', line 89

def process_args
  opts = GetoptLong.new(*OPTIONS.map { |item| [item[:name]] + (item[:aliases] || []) + [item[:arg]] })
  opts.each do |opt, arg|
    options[opt] = arg
  end
  if options['--version']
    puts "svg2slides version #{VERSION}"
    exit(0)
  end
end

#runObject



100
101
102
103
104
105
106
107
108
# File 'lib/svg_2_slides.rb', line 100

def run
  process_args
  if ARGV.empty?
    usage
  end
  ARGV.each do |item|
    process(item)
  end
end

#say(msg) ⇒ Object



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

def say(msg)
  puts(msg) unless options['--quiet']
end

#usageObject



66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
# File 'lib/svg_2_slides.rb', line 66

def usage
  puts "Usage: svg2slides [OPTIONS] FILE [FILE ...]"
  OPTIONS.each do |opt|
    spec = [opt[:name]] + (opt[:aliases] || [])
    opt_usage =
      case opt[:arg]
      when GetoptLong::NO_ARGUMENT
        spec.join(', ')
      when GetoptLong::REQUIRED_ARGUMENT
        spec.map { |variant| [variant, opt[:arg_name]].join(' ')}.join(', ')
      else
        raise 'unsupported command line option spec: %s' % opt.inspect
      end
    puts 
    puts '  ' + opt_usage
    puts
    puts '    ' + opt[:description]
    if opt[:default]
      puts '    ' + '(default: "%s")' % opt[:default]
    end
  end
end