class Document

Attributes

lines[RW]
sections[RW]

Public Class Methods

new(lines) click to toggle source
# File lib/mark_set_go/document.rb, line 4
def initialize(lines)
  @sections = []
  @lines = lines

  @current_section = nil
  @lines.each do |line|
    if @current_section.nil?
      if line.strip == "" # skip initial whitespace
        next
      elsif comment_check(line)
        @current_section = Comment.new(line)
      else
        @current_section = Code.new(line)
      end

    elsif @current_section.class == Code
      if comment_check(line)
        push_current_section
        @current_section = Comment.new(line)
      else
        @current_section.lines << line
      end

    elsif @current_section.class == Comment
      if @current_section.type == "block"
        if check_block_end(line)
          push_current_section
        else
          @current_section.lines << line
        end
      elsif @current_section.type == "line"
        if check_block_start(line)
          push_current_section
          @current_section = Comment.new(line)
        else
          if comment_check(line)
            @current_section.lines << line
          elsif line.strip == ""
            @current_section.lines << line
          else
            push_current_section
            @current_section = Code.new(line)
          end
        end
      end
    end
  end
  if !@current_section.nil?
    push_current_section
  end
end

Public Instance Methods

check_block_end(line) click to toggle source
# File lib/mark_set_go/document.rb, line 64
def check_block_end(line)
  line.strip.end_with? "*/"
end
check_block_start(line) click to toggle source
# File lib/mark_set_go/document.rb, line 60
def check_block_start(line)
  line.strip.start_with? "/*"
end
comment_check(line) click to toggle source
# File lib/mark_set_go/document.rb, line 56
def comment_check(line)
  ["/*", "//"].map { |str| line.strip.start_with? str }.include? true
end
push_current_section() click to toggle source
# File lib/mark_set_go/document.rb, line 68
def push_current_section
  @sections << @current_section.dup
  @current_section = nil
end
to_s() click to toggle source
# File lib/mark_set_go/document.rb, line 73
def to_s
  @sections.map(&:to_s).join("\n").strip
end