Class: Readme

Inherits:
Object
  • Object
show all
Defined in:
lib/readme.rb,
lib/readme/cli.rb,
lib/readme/version.rb

Overview

Readme is designed to parse a README file applying various hueristics in order to descern metadata about a project.

The heuristics are fairly simplistic at this point, but will improve with time and contribution.

Constant Summary collapse

FILE_PATTERN =

File glob for matching README file.

"README{,.*}"
VERSION =
'0.1.0'

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(text, file = nil) ⇒ Readme

Returns a new instance of Readme.



33
34
35
36
37
38
# File 'lib/readme.rb', line 33

def initialize(text, file=nil)
  @text  = text
  @file  = file
  @data = {}
  parse
end

Instance Attribute Details

#fileObject (readonly)

The ERADME file path, if provided.



43
44
45
# File 'lib/readme.rb', line 43

def file
  @file
end

#textObject (readonly)

The README text.



48
49
50
# File 'lib/readme.rb', line 48

def text
  @text
end

Class Method Details

.cli(*argv) ⇒ Object



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
# File 'lib/readme/cli.rb', line 5

def self.cli(*argv)
  format = nil
  name   = nil

  OptionParser.new do |opt|
    opt.banner = 'Usage: readme [option] [field]'
    opt.on('-y', '--yml', '--yaml', 'return in YAML format') do
      format = :yaml
    end
    opt.on('-j', '--json', 'return in JSON format') do
      format = :json
    end
    opt.on('-r', '--ruby', 'return in Ruby code format') do
      format = :ruby
    end
    opt.on_tail('-h', '--help', 'show this help message') do
      puts opt
      exit
    end
  end.parse!(argv)
 
  if argv.first
    name = argv.shift
    data = Readme.file[name]
  else
    if format
      data = Readme.file.to_h
    else
      data = Readme.file
    end
  end

  case format
  when :yaml
    require 'yaml'
    puts data.to_yaml
  when :json
    require 'json'
    puts data.to_json
  when :ruby
    data = {name => data} if name
    data.each do |k,v|
      case v
      when Hash
        puts "#{k} #{v.inspect[1...-1]}"
      else
        puts "#{k} #{v.inspect}"
      end
    end
  else
    puts data #Readme.file.to_s
  end
end

.file(path = Dir.pwd) ⇒ Object



21
22
23
24
25
26
27
28
29
30
# File 'lib/readme.rb', line 21

def self.file(path=Dir.pwd)
  if File.directory?(path)
    path = Dir.glob(File.join(path, FILE_PATTERN), File::FNM_CASEFOLD).first
  end
  if path
    new(File.read(path), path)
  else
    raise IOError, "no such README -- #{path}"
  end
end

Instance Method Details

#[](name) ⇒ Object

Access to underlying parse table.



71
72
73
74
75
76
77
78
79
# File 'lib/readme.rb', line 71

def [](name)
  @data[name.to_s]
  #return nil unless file
  #if respond_to?(name)
  #  __send__(name)
  #else
  #  nil
  #end
end

#authorsObject



107
108
109
# File 'lib/readme.rb', line 107

def authors
  @data['authors']
end


102
103
104
# File 'lib/readme.rb', line 102

def copyright
  @data['copyright']
end

#descriptionObject



92
93
94
# File 'lib/readme.rb', line 92

def description
  @data['description']
end

#extnameString

TODO:

Improve type heuristics.

Return file extension of README. Even if the file has no extension, this method will look at the contents and try to determine it.

Returns:

  • (String)

    Extension type, e.g. ‘.md`.



139
140
141
142
143
144
145
146
# File 'lib/readme.rb', line 139

def extname
  ext = File.extname(file)
  if ext.empty?
    ext = '.rdoc' if /^\=/ =~ text
    ext = '.md'   if /^\#/ =~ text
  end
  return ext
end

#homepageObject



117
118
119
# File 'lib/readme.rb', line 117

def homepage
  resources['home']
end

#issuesObject



127
128
129
# File 'lib/readme.rb', line 127

def issues
  resources['issues']
end

#licenseObject



97
98
99
# File 'lib/readme.rb', line 97

def license
  @data['license']
end

#nameObject



82
83
84
# File 'lib/readme.rb', line 82

def name
  @data['name']
end

#resourcesObject



112
113
114
# File 'lib/readme.rb', line 112

def resources
  @data['resources'] ||= {}
end

#rootString

Location of README file, if file was provided.

Returns:

  • (String)

    Directory of README file



55
56
57
# File 'lib/readme.rb', line 55

def root
  File.dirname(file) if file
end

#titleObject



87
88
89
# File 'lib/readme.rb', line 87

def title
  @data['title']
end

#to_hHash

Access to a copy of the underlying parse table.

Returns:

  • (Hash)

    Copy of the underlying table.



153
154
155
# File 'lib/readme.rb', line 153

def to_h
  @data.dup
end

#to_sString

The full README text.

Returns:

  • (String)

    The complete README text.



64
65
66
# File 'lib/readme.rb', line 64

def to_s
  text.to_s
end

#wikiObject



122
123
124
# File 'lib/readme.rb', line 122

def wiki
  resources['wiki']
end