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
- #highlighted_html ⇒ Object
- #http_blocks ⇒ Object
- #http_requests ⇒ Object
- #http_responses ⇒ Object
- #iterable_section_mapping ⇒ Object
- #section_mapping ⇒ Object
- #sections ⇒ Object
- #short_name ⇒ Object
- #title ⇒ Object
- #to_path_name ⇒ Object
- #toc_html ⇒ Object
Instance Attribute Details
#content ⇒ Object
Returns the value of attribute content
9 10 11 |
# File 'lib/emmett/document.rb', line 9 def content @content end |
#file_name ⇒ Object
Returns the value of attribute file_name
9 10 11 |
# File 'lib/emmett/document.rb', line 9 def file_name @file_name end |
#type ⇒ Object
Returns the value of attribute type
9 10 11 |
# File 'lib/emmett/document.rb', line 9 def type @type end |
Class Method Details
.from_path(path, type = :normal) ⇒ Object
11 12 13 |
# File 'lib/emmett/document.rb', line 11 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
92 93 94 95 96 97 98 99 100 101 102 103 104 105 |
# File 'lib/emmett/document.rb', line 92 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
25 26 27 |
# File 'lib/emmett/document.rb', line 25 def document @document ||= Nokogiri::HTML(content) end |
#highlighted_html ⇒ Object
44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 |
# File 'lib/emmett/document.rb', line 44 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 = section_mapping 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
107 108 109 |
# File 'lib/emmett/document.rb', line 107 def http_blocks @http_blocks ||= code_blocks.select { |r| r.first == "http" }.map { |r| r[1..-1] } end |
#http_requests ⇒ Object
111 112 113 114 115 116 |
# File 'lib/emmett/document.rb', line 111 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
118 119 120 121 122 123 |
# File 'lib/emmett/document.rb', line 118 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 |
#iterable_section_mapping ⇒ Object
84 85 86 |
# File 'lib/emmett/document.rb', line 84 def iterable_section_mapping section_mapping.map { |(n,v)| {name: n, hash: v} } end |
#section_mapping ⇒ Object
33 34 35 36 37 38 |
# File 'lib/emmett/document.rb', line 33 def section_mapping @section_mapping ||= sections.inject({}) do |acc, current| acc[current] = current.strip.downcase.gsub(/\W+/, '-').gsub(/-+/, '-').gsub(/(^-|-$)/, '') acc end end |
#sections ⇒ Object
29 30 31 |
# File 'lib/emmett/document.rb', line 29 def sections @sections ||= document.css('h2').map(&:text) end |
#short_name ⇒ Object
15 16 17 18 19 20 21 22 23 |
# File 'lib/emmett/document.rb', line 15 def short_name @short_name ||= begin if type == :index "index" else File.basename(file_name).split(".")[0..-2].join(".") end end end |
#title ⇒ Object
40 41 42 |
# File 'lib/emmett/document.rb', line 40 def title @title ||= document.at_css('h1').text end |
#to_path_name ⇒ Object
88 89 90 |
# File 'lib/emmett/document.rb', line 88 def to_path_name "#{short_name}.html" end |
#toc_html ⇒ Object
72 73 74 75 76 77 78 79 80 81 82 |
# File 'lib/emmett/document.rb', line 72 def toc_html [].tap do |html| html << "<h2>Endpoints</h2>" html << "<ul id='endpoints'>" section_mapping.each_pair do |section, slug| html << "<li><a href='##{slug}'>#{section}</a></li>" end html << "</ul>" end.join("") end |