Class: Reacco::Readme

Inherits:
Object
  • Object
show all
Includes:
Filters::Brief, Filters::HeadingID, Filters::Hgroup, Filters::Literate, Filters::PreLang, Filters::Sections, Filters::TOC
Defined in:
lib/reacco/readme.rb

Instance Method Summary collapse

Methods included from Filters::TOC

#linkify, #make_section, #make_toc

Methods included from Filters::HeadingID

#heading_id

Methods included from Filters::PreLang

#pre_lang

Methods included from Filters::Literate

#move_pre

Methods included from Filters::Hgroup

#wrap_hgroup

Methods included from Filters::Sections

#section_wrap

Methods included from Filters::Brief

#brief_first_p

Constructor Details

#initialize(options = {}) ⇒ Readme

Returns a new instance of Readme.



21
22
23
24
# File 'lib/reacco/readme.rb', line 21

def initialize(options={})
  @file    = options.delete(:file)  if options[:file]
  @options = options
end

Instance Method Details

#api_blocksObject



109
110
111
# File 'lib/reacco/readme.rb', line 109

def api_blocks
  @api_blocks ||= ""
end

#doc(options = {}) ⇒ Object

### doc() [method] Returns HTML as a Nokogiri document with all the transformations applied.

readme.doc
#=> #<Nokogiri::HTML ...>


119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
# File 'lib/reacco/readme.rb', line 119

def doc(options={})
  @doc ||= begin
    add_api(api_blocks)
    html = Nokogiri::HTML(raw_html)

    html = pre_lang(html)
    html = heading_id(html)
    html = wrap_hgroup(html)
    html = move_pre(html)       if @options[:literate]
    html = brief_first_p(html)
    html = section_wrap(html)
    html = make_toc(html)       if @options[:toc]

    html
  end
end

#exists?Boolean

### exists? [method] Returns true if the file (given in the constructor) exists.

readme = Readme.new :file => 'non-existent-file.txt'
readme.exists?
#=> false (maybe)

Returns:

  • (Boolean)


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

def exists?
  file && File.exists?(file)
end

#fileObject

### file [method] The path to the README file. Returns ‘nil` if not available.

readme.file      #=> "README.md"


31
32
33
# File 'lib/reacco/readme.rb', line 31

def file
  @file ||= (Dir['README.*'].first || Dir['readme.*'].first || Dir['README'].first)
end

#file=(file) ⇒ Object



35
36
37
# File 'lib/reacco/readme.rb', line 35

def file=(file)
  @file = file
end

#githubObject

### github [method] Returns the GitHub URL, or nil if not applicable.

readme.github
#=> "https://github.com/rstacruz/reacco"


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

def github
  "https://github.com/#{@options[:github]}"  if @options[:github]
end

#htmlObject

### html [method] Returns body’s inner HTML with transformatinos.

readme.raw_html
#=> "<h1>My project</h1>..."


142
143
144
# File 'lib/reacco/readme.rb', line 142

def html
  doc.at_css('body').inner_html
end

#inject_api_block(html) ⇒ Object

### inject_api_block [method] Adds an API block. Takes an ‘html` argument.



105
106
107
# File 'lib/reacco/readme.rb', line 105

def inject_api_block(html)
  @api_blocks = "#{api_blocks}\n#{html}\n"
end

#localsObject

### locals [method] Returns a hash with the local variables to be used for the ERB template.

readme.locals
#=> { :title => 'My project', ... }


162
163
164
165
166
167
# File 'lib/reacco/readme.rb', line 162

def locals
  { :title      => title,
    :body_class => switches.join(' '), 
    :analytics  => @options[:analytics],
    :github     => github }
end

#rawObject

### raw [method] Returns raw Markdown markup.

readme.raw
#=> "# My project\n#### This is my project\n\n..."


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

def raw
  @raw ||= File.read(file)
end

#raw_htmlObject

### raw_html [method] Returns a string of the raw HTML data built from the markdown source. This does not have the transformations applied to it yet.

readme.raw_html
#=> "<h1>My project</h1>..."


94
95
96
# File 'lib/reacco/readme.rb', line 94

def raw_html
  @raw_html ||= markdown.render(raw)
end

#raw_html=(str) ⇒ Object



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

def raw_html=(str)
  @raw_html = str
end

#switchesObject

### switches [method] The switches, like literate and toc. Returns an array of strings.

readme.switches  #=> [ 'literate' ]


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

def switches
  @options.keys.map { |k| k.to_s }
end

#titleObject

### title [method] Returns a string of the title of the document. Often, this is the first H1, but if that’s not available, then it is inferred from the current directory’s name.

readme.title
#=> "My project"


76
77
78
79
80
81
# File 'lib/reacco/readme.rb', line 76

def title
  @title ||= begin
    h1 = (h1 = doc.at_css('h1')) && h1.text
    h1 || File.basename(Dir.pwd)
  end
end

#title?Boolean

Returns:

  • (Boolean)


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

def title?
  title.to_s.size > 0
end