Class: Emmett::Document

Inherits:
Struct
  • Object
show all
Defined in:
lib/emmett/document.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#contentObject

Returns the value of attribute content

Returns:

  • (Object)

    the current value of content



9
10
11
# File 'lib/emmett/document.rb', line 9

def content
  @content
end

#file_nameObject

Returns the value of attribute file_name

Returns:

  • (Object)

    the current value of file_name



9
10
11
# File 'lib/emmett/document.rb', line 9

def file_name
  @file_name
end

#typeObject

Returns the value of attribute type

Returns:

  • (Object)

    the current value of 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_blocksObject



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

#documentObject



25
26
27
# File 'lib/emmett/document.rb', line 25

def document
  @document ||= Nokogiri::HTML(content)
end

#highlighted_htmlObject



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_blocksObject



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_requestsObject



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_responsesObject



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_mappingObject



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_mappingObject



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

#sectionsObject



29
30
31
# File 'lib/emmett/document.rb', line 29

def sections
  @sections ||= document.css('h2').map(&:text)
end

#short_nameObject



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

#titleObject



40
41
42
# File 'lib/emmett/document.rb', line 40

def title
  @title ||= document.at_css('h1').text
end

#to_path_nameObject



88
89
90
# File 'lib/emmett/document.rb', line 88

def to_path_name
  "#{short_name}.html"
end

#toc_htmlObject



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