Class: Cdoc::CDoc

Inherits:
Object
  • Object
show all
Includes:
Helpers
Defined in:
lib/cdoc.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Helpers

#highlight, #to_id

Constructor Details

#initializeCDoc

Returns a new instance of CDoc.



177
178
179
180
181
# File 'lib/cdoc.rb', line 177

def initialize
  @files = Rake::FileList.new('app/controllers/**/*.rb')
  @files = @files.select { |file| file.end_with?('_controller.rb') }
  @doc = DocString.new
end

Instance Attribute Details

#docstringObject

Returns the value of attribute docstring.



175
176
177
# File 'lib/cdoc.rb', line 175

def docstring
  @docstring
end

#filesObject

Returns the value of attribute files.



175
176
177
# File 'lib/cdoc.rb', line 175

def files
  @files
end

Instance Method Details

#extract_documentation(file) ⇒ Object



205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
# File 'lib/cdoc.rb', line 205

def extract_documentation(file)
  begin
    lines = File.readlines(file)
  rescue => e
    puts "#{e.class}: Error reading file #{file}. Skip"
    return
  end

  sections  = []
  section_doc_lines = []
  recording = false

  lines.each do |line|
    if !recording && (line.strip == '#doc')
      recording = true
      next
    end

    if recording
      line = line.strip

      if line.empty?
        next
      elsif line.start_with?('#')
        section_doc_lines << line.strip.sub('#', '')
      else
        recording = false
        sections << section_doc_lines.join("\n")
        section_doc_lines = []
      end
    end
  end

  if recording && (section_doc_lines.length != 0)
    sections << section_doc_lines.join("\n")
  end

  sections
end

#generateObject



183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
# File 'lib/cdoc.rb', line 183

def generate
  @doc.title(ENV['TITLE'] || 'Chillr API Documentaion')

  file_groups = files.group_by { |f| File.basename(f, '_controller.rb') }

  # Generates sidebar
  @doc.sidebar(file_groups.keys)

  # Generates body
  file_groups.each do |group, files|
    @doc.section(group.capitalize)
    files.each do |file|
      docs = extract_documentation(file)
      docs.each do |doc|
        @doc.subsection(doc)
      end
    end
  end

  @doc.finish
end