Class: PuppetDocLint::Parser

Inherits:
Object
  • Object
show all
Defined in:
lib/puppet-doc-lint/parser.rb

Instance Method Summary collapse

Constructor Details

#initialize(file) ⇒ Parser

Returns a new instance of Parser.



4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
# File 'lib/puppet-doc-lint/parser.rb', line 4

def initialize(file)
  # Read file and return parsed object
  pparser = Puppet::Parser::Parser.new(Puppet::Node::Environment.new('production'))
  if File.exists?(file)
    @file = File.expand_path(file)
    pparser.import(@file)

    # Find object in list of hostclasses
    pparser.environment.known_resource_types.hostclasses.each do |x|
      @object = x.last if x.last.file == @file
    end
    # Find object in list of definitions
    pparser.environment.known_resource_types.definitions.each do |x|
      @object = x.last if x.last.file == @file
    end

  else
    'File does not exist'
  end
end

Instance Method Details

#authorsObject

def docs



58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
# File 'lib/puppet-doc-lint/parser.rb', line 58

def authors
  if !@object.doc.nil?
    rdoc            = RDoc::Markup.parse(@object.doc)
    authors         = []

    author_docs = rdoc.parts.chunk{|i|i.class == RDoc::Markup::Heading && i.text == 'Authors'}.reject{|sep,ans| sep}.map{|sep,ans| ans}

    author_docs.each do | doc_chunk |
      unless doc_chunk[1].class == RDoc::Markup::BlankLine || doc_chunk[1].class == RDoc::Markup::Heading || !doc_chunk[1].respond_to?(:items)
        doc_chunk[1].items.each do |chunk|
          authors << chunk.parts.first.parts
        end
      end
    end

    authors
  end # if nil?
end

#docsObject

Read RDOC contents from parsed object, returns hash of paragraph headings and the following paragraph contents (i.e. parameter and parameter documentation)



40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
# File 'lib/puppet-doc-lint/parser.rb', line 40

def docs
  if !@object.doc.nil?
    rdoc            = RDoc::Markup.parse(@object.doc)
    docs            = {}

    rdoc.parts.each do |part|
      if part.respond_to?(:items)
        part.items.each do |item|
          next if item.label.nil? || item.parts.first.class == RDoc::Markup::BlankLine
          key       = item.label.to_s.tr('^A-Za-z0-9_-', '')
          docs[key] = item.parts.first.parts
        end # do item
      end # endif
    end # do parm
    docs
  end # if nil?
end

#klassObject

Read class from parsed object, returns string containing class



33
34
35
# File 'lib/puppet-doc-lint/parser.rb', line 33

def klass
  @object.name if (defined? @object.class.name)
end

#parametersObject

Read parameters from parsed object, returns hash of parameters and default values



27
28
29
30
# File 'lib/puppet-doc-lint/parser.rb', line 27

def parameters
  result = (defined? @object.arguments) ? @object.arguments : {}
  result
end