Method: Inkmake::InkFile#parse_line

Defined in:
lib/inkmake.rb

#parse_line(line) ⇒ Object

Raises:



370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
# File 'lib/inkmake.rb', line 370

def parse_line(line)
  cols = nil
  begin
    cols = parse_split_line(line)
  rescue CSV::MalformedCSVError => e
    raise SyntaxError, e.message
  end
  raise SyntaxError, "Invalid number of columns" if cols.count < 1

  if not DEST_RE.match(cols[0])
    raise SyntaxError, "Invalid destination format \"#{cols[0]}\""
  end

  opts = {}
  opts[:prefix] = $1
  variants = $2
  opts[:suffix] = $3
  opts[:format] = $1.downcase if EXT_RE.match(opts[:prefix] + opts[:suffix])

  cols[1..-1].each do |col|
    case col
    when RES_RE then opts[:res] = InkscapeResolution.new($1, $2, "px")
    when SVG_RE then opts[:svg] = col
    when AREA_SPEC_RE then opts[:area] = [$1.to_f, $2.to_f, $3.to_f, $4.to_f]
    when AREA_NAME_RE then opts[:area] = $1
    when /^drawing$/ then opts[:area] = :drawing
    when FORMAT_RE then opts[:format] = $1.downcase
    when ROTATE_RE then opts[:rotate] = Rotations[$1]
    when SCALE_RE then opts[:scale] = $1.to_f
    when DPI_RE then opts[:dpi] = $1.to_f
    when SHOWHIDE_RE
      op = $1 == "+" ? :show : :hide
      if $2.start_with? "#"
        type = :id
        name= $2[1..-1]
      else
        type = :layer
        name = $2 == "*" ? :all : $2
      end
      (opts[:showhide] ||= []).push({:op => op, :type => type, :name => name})
    else
      raise SyntaxError, "Unknown column \"#{col}\""
    end
  end

  if not opts[:format]
    raise SyntaxError, "Unknown or no output format could be determined"
  end

  variants = (variants.split("|") if variants) || []
  opts[:variants] = variants.collect do |variant|
    name, options = variant.split("=", 2)
    if options
      options = Hash[
        options.split(",").map do |option|
        case option
        when ROTATE_RE then [:rotate, Rotations[$1]]
        when RES_RE then [:res, InkscapeResolution.new($1, $2, "px")]
        when SCALE_RE then [:scale, $1.to_f]
        when DPI_RE then [:dpi, $1.to_f]
        else
          raise SyntaxError, "Invalid variant option \"#{option}\""
        end
        end
      ]
    else
      options = DefaultVariants[name]
      raise SyntaxError, "Invalid default variant \"#{name}\"" if not options
    end

    [name, options]
  end

  opts
end