Sha256: f11f021d20b5819bd67b1ff3c496939e519f4c73eec468edc2bd4ed50076ef8c

Contents?: true

Size: 1.71 KB

Versions: 28

Compression:

Stored size: 1.71 KB

Contents

require 'pdoc/error'

module PDoc
  class Fragment
    PREFIX_REGEXP = /^\s*\*?\s*/
    
    attr_reader :line_number
    def initialize(string, line_number)
      @line_number = line_number
      @string = string
    end
    
    def normalize
      # Cleans up the text by removing opening and closing comment tags
      # and removing the prefix off of every line.
      # Raises and error if prefix are inconsistent. (Prefix is calculated
      # by looking at line 2 of the comment)
      results = []
      lines.each_with_index do |line, index|
        if index == 0                              # first line
          results << line.sub(/\s*\/\*\*\s*/, '')  # remove opening /**
        elsif index == lines.size - 1              # last line
          results << line.sub(/\s*\*\*?\/\s*/, '') # remove closing **/
        elsif line =~ prefix_regexp                # correctly prefixed lines
          results << line.sub(prefix_regexp, '')   # remove optional prefix
        else
          raise InconsistentPrefixError.new(self, line, index)
        end
      end
      results
    end
    
    def prefix
      PREFIX_REGEXP.match(lines[1])[0] if lines.size > 2
    end
    
    def prefix_regexp
      @prefix_regexp ||= Regexp.new('^' << Regexp.escape(prefix))
    end
    
    def lines
      @lines ||= @string.split("\n")
    end
    
    class InconsistentPrefixError < PDocError
      def initialize(fragment, line, index)
        @fragment = fragment
        actual = PREFIX_REGEXP.match(line)[0].inspect
        msg = "Inconsistent prefixing at line ##{fragment.line_number + index}."
        msg << "Expected prefix is: #{fragment.prefix.inspect}, actual prefix is: #{actual}"
        super(msg)
      end
    end
  end
end

Version data entries

28 entries across 28 versions & 2 rubygems

Version Path
distil-0.12.6 vendor/pdoc/lib/pdoc/parser/fragment.rb
distil-0.12.4 vendor/pdoc/lib/pdoc/parser/fragment.rb
distil-0.12.3 vendor/pdoc/lib/pdoc/parser/fragment.rb
distil-0.12.2 vendor/pdoc/lib/pdoc/parser/fragment.rb
distil-0.12.1 vendor/pdoc/lib/pdoc/parser/fragment.rb
pdoc-0.2.0 lib/pdoc/parser/fragment.rb
distil-0.12.0 vendor/pdoc/lib/pdoc/parser/fragment.rb
distil-0.11.8 vendor/pdoc/lib/pdoc/parser/fragment.rb