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.



5
6
7
8
# File 'lib/reacco/readme.rb', line 5

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

Instance Method Details

#api_blocksObject



67
68
69
# File 'lib/reacco/readme.rb', line 67

def api_blocks
  @api_blocks ||= ""
end

#doc(options = {}) ⇒ Object

### doc [method] Returns HTML as a Nokogiri document.



73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
# File 'lib/reacco/readme.rb', line 73

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.

Returns:

  • (Boolean)


28
29
30
# File 'lib/reacco/readme.rb', line 28

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

#fileObject

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



12
13
14
# File 'lib/reacco/readme.rb', line 12

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

#file=(file) ⇒ Object



16
17
18
# File 'lib/reacco/readme.rb', line 16

def file=(file)
  @file = file
end

#githubObject

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



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

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

#htmlObject

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



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

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.



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

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

#localsObject

Returns locals for the template.



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

def locals
  { :title      => title,
    :body_class => switches.join(' '), 
    :github     => github }
end

#rawObject

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



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

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

#raw_htmlObject

### raw_html [method] Raw HTML data.



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

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

#raw_html=(str) ⇒ Object



57
58
59
# File 'lib/reacco/readme.rb', line 57

def raw_html=(str)
  @raw_html = str
end

#switchesObject

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



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

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

#titleObject

### title [method] Returns a string of the title of the document (the first h1).



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

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

#title?Boolean

Returns:

  • (Boolean)


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

def title?
  title.to_s.size > 0
end