Class: Emmett::Document
- Inherits:
-
Struct
- Object
- Struct
- Emmett::Document
- Defined in:
- lib/emmett/document.rb
Instance Attribute Summary collapse
-
#content ⇒ Object
Returns the value of attribute content.
-
#file_name ⇒ Object
Returns the value of attribute file_name.
-
#type ⇒ Object
Returns the value of attribute type.
Class Method Summary collapse
Instance Method Summary collapse
- #code_blocks ⇒ Object
- #document ⇒ Object
- #endpoint_names ⇒ Object
- #endpoints ⇒ Object
- #group_names ⇒ Object
- #groups ⇒ Object
- #highlighted_html ⇒ Object
-
#http_blocks ⇒ Object
Extra / process HTTP blocks to get extra information.
- #http_requests ⇒ Object
- #http_responses ⇒ Object
- #short_name ⇒ Object
- #title ⇒ Object
- #to_path_name ⇒ Object
- #toc_html ⇒ Object
Instance Attribute Details
#content ⇒ Object
Returns the value of attribute content
105 106 107 |
# File 'lib/emmett/document.rb', line 105 def content @content end |
#file_name ⇒ Object
Returns the value of attribute file_name
105 106 107 |
# File 'lib/emmett/document.rb', line 105 def file_name @file_name end |
#type ⇒ Object
Returns the value of attribute type
105 106 107 |
# File 'lib/emmett/document.rb', line 105 def type @type end |
Class Method Details
.from_path(path, type = :normal) ⇒ Object
107 108 109 |
# File 'lib/emmett/document.rb', line 107 def self.from_path(path, type = :normal) Document.new path, GitHub::Markup.render(path, File.read(path)), type end |
Instance Method Details
#code_blocks ⇒ Object
193 194 195 196 197 198 199 200 201 202 203 204 205 206 |
# File 'lib/emmett/document.rb', line 193 def code_blocks @code_blocks ||= begin last_header = nil blocks = [] document.css('h2, pre[lang]').each do |d| if d.name == 'h2' last_header = d.text else blocks << [d[:lang], d.at_css('code').text, last_header] end end blocks end end |
#document ⇒ Object
141 142 143 |
# File 'lib/emmett/document.rb', line 141 def document @document ||= Nokogiri::HTML(content) end |
#endpoint_names ⇒ Object
115 116 117 |
# File 'lib/emmett/document.rb', line 115 def endpoint_names @endpoint_names ||= document.css('h2').map(&:text) end |
#endpoints ⇒ Object
127 128 129 |
# File 'lib/emmett/document.rb', line 127 def endpoints @endpoints ||= endpoint_names.map { |name| Endpoint.new(name, self) } end |
#group_names ⇒ Object
111 112 113 |
# File 'lib/emmett/document.rb', line 111 def group_names @group_name ||= document.css('h1').map(&:text) end |
#groups ⇒ Object
119 120 121 122 123 124 125 |
# File 'lib/emmett/document.rb', line 119 def groups @groups ||= group_names.map do |name| group = Group.new(name, self) group.endpoints = endpoints group end end |
#highlighted_html ⇒ Object
149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 |
# File 'lib/emmett/document.rb', line 149 def highlighted_html @highlighted_html ||= begin doc = document.clone doc.css('pre[lang]').each do |block| inner = block.at_css('code') highlighted = Pygments.highlight(inner.inner_html, options: {encoding: 'utf-8'}, lexer: block[:lang]) highlighted_fragment = Nokogiri::HTML::DocumentFragment.parse highlighted highlighted_fragment["data-code-lang"] = block[:lang] block.replace highlighted_fragment end mapping = endpoints.inject({}) { |acc, e| acc[e.name] = e.slug; acc } doc.css('h2').each do |header| if (identifier = mapping[header.text]) header[:id] = identifier end end unless short_name == 'index' # Now, insert an endpoints content before the start of it. toc = Nokogiri::HTML::DocumentFragment.parse toc_html doc.at_css('h2').add_previous_sibling toc end doc.css('body').inner_html end end |
#http_blocks ⇒ Object
Extra / process HTTP blocks to get extra information.
210 211 212 |
# File 'lib/emmett/document.rb', line 210 def http_blocks @http_blocks ||= code_blocks.select { |r| r.first == "http" }.map { |r| r[1..-1] } end |
#http_requests ⇒ Object
214 215 216 217 218 219 |
# File 'lib/emmett/document.rb', line 214 def http_requests @http_requests ||= http_blocks.select do |cb| first_line = cb[0].lines.first.strip first_line =~ /\A[A-Z]+ (\S+) HTTP\/1\.1\Z/ end.map { |r| HTTPRequestProcessor.new(*r) } end |
#http_responses ⇒ Object
221 222 223 224 225 226 |
# File 'lib/emmett/document.rb', line 221 def http_responses @http_responses ||= http_blocks.select do |cb| first_line = cb.lines.first.strip first_line =~ /\AHTTP\/1\.1 (\d+) (\w+)\Z/ end end |
#short_name ⇒ Object
131 132 133 134 135 136 137 138 139 |
# File 'lib/emmett/document.rb', line 131 def short_name @short_name ||= begin if type == :index "index" else File.basename(file_name).split(".")[0..-2].join(".") end end end |
#title ⇒ Object
145 146 147 |
# File 'lib/emmett/document.rb', line 145 def title @title ||= group_names.first end |
#to_path_name ⇒ Object
189 190 191 |
# File 'lib/emmett/document.rb', line 189 def to_path_name "#{short_name}.html" end |
#toc_html ⇒ Object
177 178 179 180 181 182 183 184 185 186 187 |
# File 'lib/emmett/document.rb', line 177 def toc_html [].tap do |html| html << "<h2>Endpoints</h2>" html << "<ul id='endpoints'>" endpoints.each do |endpoint| html << "<li><a href='##{endpoint.slug}'>#{endpoint.name}</a></li>" end html << "</ul>" end.join("") end |