Commit 35c138c0 authored by Rene Saarsoo's avatar Rene Saarsoo
Browse files

Extract DocParser#purify method to separate class.

parent ac39b323
Loading
Loading
Loading
Loading
+38 −0
Original line number Diff line number Diff line
module JsDuck

  # A simple helper to extract doc comment contents.
  class DocComment

    # Extracts content inside /** ... */
    def self.purify(input)
      result = []

      # We can have two types of lines:
      # - those beginning with *
      # - and those without it
      indent = nil
      input.each_line do |line|
        line.chomp!
        if line =~ /\A\s*\*\s?(.*)\Z/
          # When comment contains *-lines, switch indent-trimming off
          indent = 0
          result << $1
        elsif line =~ /\A\s*\Z/
          # pass-through empty lines
          result << line
        elsif indent == nil && line =~ /\A(\s*)(.*?\Z)/
          # When indent not measured, measure it and remember
          indent = $1.length
          result << $2
        else
          # Trim away indent if available
          result << line.sub(/\A\s{0,#{indent||0}}/, "")
        end
      end

      result.join("\n")
    end

  end

end
+2 −29
Original line number Diff line number Diff line
require 'strscan'
require 'jsduck/doc_comment'
require 'jsduck/builtins_registry'
require 'jsduck/meta_tag_registry'
require 'jsduck/logger'
@@ -36,7 +37,7 @@ module JsDuck
      @filename = filename
      @linenr = linenr
      @tags = []
      @input = StringScanner.new(purify(input))
      @input = StringScanner.new(DocComment.purify(input))

      parse_loop

@@ -45,34 +46,6 @@ module JsDuck
      @tags
    end

    # Extracts content inside /** ... */
    def purify(input)
      result = []
      # We can have two types of lines:
      # - those beginning with *
      # - and those without it
      indent = nil
      input.each_line do |line|
        line.chomp!
        if line =~ /\A\s*\*\s?(.*)\Z/
          # When comment contains *-lines, switch indent-trimming off
          indent = 0
          result << $1
        elsif line =~ /\A\s*\Z/
          # pass-through empty lines
          result << line
        elsif indent == nil && line =~ /\A(\s*)(.*?\Z)/
          # When indent not measured, measure it and remember
          indent = $1.length
          result << $2
        else
          # Trim away indent if available
          result << line.sub(/\A\s{0,#{indent||0}}/, "")
        end
      end
      return result.join("\n")
    end

    # The parsing process can leave whitespace at the ends of
    # doc-strings, here we get rid of it.
    # Additionally null all empty docs.