Class: NanDoc::ParseReadme

Inherits:
Object
  • Object
show all
Defined in:
lib/nandoc/parse-readme.rb

Overview

@dependencies: none

Defined Under Namespace

Classes: DeferredParse

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(path = nil, &block) ⇒ ParseReadme

If you are really really crazy then you don’t like repeating yourself between your gemspec and your README with regard to the project summary and description.

This is a quick and dirty hack to parse your README (or similary formatted file) in a really rough way for when you are building your gemspec – it doesn’t use a markdown parser or whatever. It scans the README file for a line like “## Summary” and reads the next line with content after it. (ditto “## Description” etc.)

See the Rakefile of this project for example usage.

See Also:

  • below near 'jeweler'


23
24
25
26
27
28
# File 'lib/nandoc/parse-readme.rb', line 23

def initialize(path=nil, &block)
  @parsed_content = {}
  @lines = nil
  @path = path and get_lines!
  instance_eval(&block) if block_given?
end

Instance Attribute Details

#linesObject

Returns the value of attribute lines.



38
39
40
# File 'lib/nandoc/parse-readme.rb', line 38

def lines
  @lines
end

Class Method Details

.description(file) ⇒ Object



31
32
33
# File 'lib/nandoc/parse-readme.rb', line 31

def description file
  DeferredParse.new(file, :description)
end

.summary(file) ⇒ Object



34
35
36
# File 'lib/nandoc/parse-readme.rb', line 34

def summary file
  DeferredParse.new(file, :summary)
end

Instance Method Details

#parse_section(*names) ⇒ Object Also known as: parse_sections



44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
# File 'lib/nandoc/parse-readme.rb', line 44

def parse_section *names
  names.each do |name|
    re = /\A#* ?#{Regexp.escape(name.to_s)}\Z/i
    idx = (@lines.index{|x| re =~ x } or
      fail("#{name.to_s.inspect} section not found in #{path}")) + 1
    idx += 1 while @lines[idx] && /\A[[:space:]]+\Z/ =~ @lines[idx]
    line = @lines[idx] or
      fail("couldn't find content following #{name.to_s} section")
    @parsed_content[name] = line.strip
    class << self; self end.send(:define_method, name) do
      @parsed_content[name]
    end
  end
  nil
end

#path(name) ⇒ Object



39
40
41
42
43
# File 'lib/nandoc/parse-readme.rb', line 39

def path name
  @path = name
  get_lines!
  name
end