Sha256: 416f535ae6e4e6c53b0a32f8f59a823b38353044560a74d6984f526f4184ffdd

Contents?: true

Size: 1.78 KB

Versions: 1

Compression:

Stored size: 1.78 KB

Contents

require 'cgi'

module ChartHelpers
  module Parsers
    class Gantt
      # This should match "title" :a1, 0.000, 0.001
      GANTT_LINE_REGEX = %r{
        (?<title>.+)
        (?<group>:\w+)
        ,\s+(?<start>[[^,]+]+)
        ,\s+(?<end>[[^,]+]+)
      }x

      def self.parse(lines)
        title = 'Gantt Chart'
        date_format = nil
        number_format = nil
        data = []

        while line = lines.shift
          next if line.empty? || line.nil?
          line.strip!

          case line
          when /\Atitle/
            if title == 'Gantt Chart'
              # Line will be like:
              # `title THIS IS MY TITLE`
              # We would want "THIS IS MY TITLE"
              title = line.split(' ')[1..-1].join(' ')
            else
              data << parse_line(line)
            end
          when /\AdateFormat/
            # Line will be like:
            # `dateFormat s.SSS`
            # We would want "s.SSS"
            date_format = line.split(' ')[1..-1].join(' ')
          when /\AnumberFormat/
            # Line will be like:
            # `numberFormat s.SSS`
            # We would want "s.SSS"
            number_format = line.split(' ')[1..-1].join(' ')
          else
            data << parse_line(line, date: !date_format.nil?)
          end
        end

        [title, date_format, number_format, data]
      end

      def self.parse_line(line, date: false)
        match_data = line.match(GANTT_LINE_REGEX)
        start_val, end_val = if date
          [DateTime.parse(match_data[:start]).to_time.to_f, DateTime.parse(match_data[:end]).to_time.to_f]
        else
          [match_data[:start].to_f, match_data[:end].to_f]
        end
        { title: CGI.escapeHTML(match_data[:title]).strip, start: start_val, end: end_val }
      end
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
chart_helpers-0.0.2 lib/chart_helpers/parsers/gantt.rb