Class: ChefAttrdoc::AttributesFile

Inherits:
Object
  • Object
show all
Defined in:
lib/chef_attrdoc.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(content) ⇒ AttributesFile

Returns a new instance of AttributesFile.



25
26
27
28
29
30
31
32
33
# File 'lib/chef_attrdoc.rb', line 25

def initialize(content)
  @lexed = Ripper.lex(content)
  @groups = []
  @comment = false
  @code = []
  @newline = false

  self.parse
end

Instance Attribute Details

#groupsObject (readonly)

Returns the value of attribute groups.



23
24
25
# File 'lib/chef_attrdoc.rb', line 23

def groups
  @groups
end

Instance Method Details

#end_groupObject



35
36
37
38
39
40
# File 'lib/chef_attrdoc.rb', line 35

def end_group
  if @comment
    @groups << [@code.join, @comment]
  end
  new_group
end

#new_groupObject



42
43
44
45
46
# File 'lib/chef_attrdoc.rb', line 42

def new_group
  @comment = false
  @code = []
  @newline = false
end

#parseObject



48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
# File 'lib/chef_attrdoc.rb', line 48

def parse
  @lexed.each do |(lineno, column), token, content|
    case token
    # Ignored newlines occur when a newline is encountered, but
    # the statement that was expressed on that line was not
    # completed on that line.
    when :on_ignored_nl
      # end a group if we've reached an empty line after a comment
      if @comment && @newline
        end_group
      else
        @newline = true
        @code << content
      end
    # This is the first thing that exists on a new line–NOT the last!
    when :on_nl
      @newline = true
      @code << content
    when :on_comment
      if ignored_comments(content)
        # inline comments
        # go back to the existing code and remove the trailing
        # whitespace, but give it the newline which the lexer
        # considers part of the comment
        if !@code.empty?
          @code[-1].strip!
          @code << "\n"
        end

        next
      end

      if @comment
        # After the code has started, leave the inline comments
        # where we found them, but ignore the ones below the
        # code. Those are usually garbage. We do this by ending the
        # current group when we encounter them.
        if !@code.empty? && @newline
          end_group
          @comment = ''
        end
        # Since we can only have one comment per block (which we put
        # at the top, before the code), keep appending to that
        # until the code starts.
        if @code.empty?
          @comment << content
        else
          # inline comments
          @code << content
        end
      elsif column == 0
        @comment = content
        @code = []
      end

      @newline = false
    else
      @code << content
      @newline = false
    end
  end
  # when there are no newlines at the end of the file, we have to close
  # the code block manually
  unless @code.empty?
    end_group
  end
end

#to_sObject



116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
# File 'lib/chef_attrdoc.rb', line 116

def to_s
  strings = []
  # ignore the starting comments in a file, these are usually
  # shebangs, copyright statements, encoding declarations etc.
  @groups = @groups.drop_while{|code, doc| /\A[[:space:]]*\z/.match code}

  @groups.each do |code, doc|
    strings << doc.gsub(/^#[[:blank:]]*/, '')
    strings << "\n"
    unless /\A[[:space:]]*\z/.match code
      strings << "```ruby\n"
      strings << code
      strings << "```\n\n"
    end
  end
  strings.join
end