Class: Reacco::Readme
- Inherits:
-
Object
- Object
- Reacco::Readme
- 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
- #api_blocks ⇒ Object
-
#doc(options = {}) ⇒ Object
### doc() [method] Returns HTML as a Nokogiri document with all the transformations applied.
-
#exists? ⇒ Boolean
### exists? [method] Returns true if the file (given in the constructor) exists.
-
#file ⇒ Object
### file [method] The path to the README file.
- #file=(file) ⇒ Object
-
#github ⇒ Object
### github [method] Returns the GitHub URL, or nil if not applicable.
-
#html ⇒ Object
### html [method] Returns body’s inner HTML with transformatinos.
-
#initialize(options = {}) ⇒ Readme
constructor
A new instance of Readme.
-
#inject_api_block(html) ⇒ Object
### inject_api_block [method] Adds an API block.
-
#locals ⇒ Object
### locals [method] Returns a hash with the local variables to be used for the ERB template.
-
#raw ⇒ Object
### raw [method] Returns raw Markdown markup.
-
#raw_html ⇒ Object
### raw_html [method] Returns a string of the raw HTML data built from the markdown source.
- #raw_html=(str) ⇒ Object
-
#switches ⇒ Object
### switches [method] The switches, like literate and toc.
-
#title ⇒ Object
### title [method] Returns a string of the title of the document.
- #title? ⇒ Boolean
Methods included from Filters::TOC
#linkify, #make_section, #make_toc
Methods included from Filters::HeadingID
Methods included from Filters::PreLang
Methods included from Filters::Literate
Methods included from Filters::Hgroup
Methods included from Filters::Sections
Methods included from Filters::Brief
Constructor Details
#initialize(options = {}) ⇒ Readme
Returns a new instance of Readme.
21 22 23 24 |
# File 'lib/reacco/readme.rb', line 21 def initialize(={}) @file = .delete(:file) if [:file] @options = end |
Instance Method Details
#api_blocks ⇒ Object
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(={}) @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
54 55 56 |
# File 'lib/reacco/readme.rb', line 54 def exists? file && File.exists?(file) end |
#file ⇒ Object
### 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 |
#github ⇒ Object
### 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 |
#html ⇒ Object
### 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 |
#locals ⇒ Object
### 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 |
#raw ⇒ Object
### 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_html ⇒ Object
### 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 |
#switches ⇒ Object
### 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 |
#title ⇒ Object
### 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
83 84 85 |
# File 'lib/reacco/readme.rb', line 83 def title? title.to_s.size > 0 end |