Class: Docster::Parser

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

Class Method Summary collapse

Class Method Details

.parse_files_for_doc(file_names) ⇒ Object



3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
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
# File 'lib/docster/doc_parser.rb', line 3

def self.parse_files_for_doc file_names
  blocks = []
  doc_items = {}
  items = []
  
  file_names.each do |file_name|
    File.open(file_name, "r") do |file|

      current_block = nil
      current_scope = nil

      lines = file.readlines
      lines.each_with_index do |line, index|

        if current_block
          match = line.match(/# @([^\s\t\r\n\f\[\]]+)(\[.+\])?\s+(.*)$/)
    
          if match
            entry = {
              :element =>  match[1],
              :specification =>  match[2],
              :description =>  match[3].strip
            }
      
            if entry[:specification]
              entry[:specification] = entry[:specification][1..-2]
            end
      
            # check next lines for multiline description
            i = index + 1
            while (i < lines.size && next_match = lines[i].match(/# [\s\t\r\n\f\@]+ (.+)$/))
              new_line = "<br>"
              new_line = "\n" if entry[:element] == 'example'
              entry[:description] = entry[:description] + new_line + next_match[1]
              i = i + 1
            end
      
            puts entry.inspect
      
            current_block << entry
          end
        end

        if line =~ /^\s*# -- doc --/
          current_block = []
        end

        # end of doc item
        if line =~ /^\s*# -- \/doc --/
          if current_scope == nil     
            # create new scope   
            for entry in current_block
              if entry[:element] == 'object'
                current_scope = entry[:specification].to_sym
                doc_items[current_scope] = []
          
                item = {
                  :key => entry[:specification].to_sym,
                  :description => entry[:description]
                }
          
                items << item
          
                break
              end    
            end
          else
            # create object in scope
            obj = {}
            for entry in current_block
              if entry[:specification] == nil
                obj[entry[:element].to_sym] = entry[:description]
              else
                obj[entry[:element].to_sym] ||= []
                e = { entry[:specification].to_sym  => entry[:description] }
                obj[entry[:element].to_sym] << e
              end
            end
            doc_items[current_scope] << obj
          end  
      
          blocks << current_block
          current_block = nil
        end
      end

    end
  end
  
  { :doc_items =>  doc_items, :items => items }
end