Sha256: 52d14eed8fca3a424782a9001847dcabd05b93997dec1da55731d16b121ea825

Contents?: true

Size: 1.48 KB

Versions: 1

Compression:

Stored size: 1.48 KB

Contents

class Ld::Document

  attr_accessor :doc, :class_name

  #= 作用 空的实例方法,Ld::Document的大部分功能都由实例提供(个人习惯调用对象方法而不是类方法)
  def initialize file
    @class_name = file.split('.rb')[0].camelize
    @doc = {}
    lines = file.lines
    lines.each_with_index do |line, i|
      arr = line.split(' ')
      if arr.delete_at(0) == 'def'
        notes = get_notes(lines, i)
        if notes.size > 0
          @doc[arr.delete_at(0)] = {
              params:arr.join(' '),
              notes:notes
          }
        end
      end
    end
  end

  def get_notes lines, i
    notes = []
    (i-1).downto(0) do |j|
      arr = lines[j].split(' ')
      if arr[0] == '#='
        notes << {title:arr[1], note:arr[2..(arr.size)].join(' ')}
      else
        return notes
      end
    end
    notes
  end


  def class_name

  end

  def self.write_readme readme_path = '/Users/liudong/ruby/my_gems/ld/README2.md'
    docs = Ld::File.open('/Users/liudong/ruby/my_gems/ld/lib/ld').search_regexp(/.rb$/).map{|f| Ld::Document.new f}
    arr = ["# API"]
    docs.map do |doc|
      arr << "## #{doc.class_name}"
      arr << "```ruby"
      doc.doc.each do |k, v|
        v[:notes].each do |note|
          arr << "#{note[:title]}:#{note[:note]}"
        end
        arr << "#{k} #{v[:params]}"
      end
      arr << "```"
    end
    File.open readme_path,'w' do |file|
      arr.each do |line|
        arr.puts line
      end
      file.colse
    end
  end

end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
ld-0.3.6 lib/ld/document/document.rb