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



105
106
107
# File 'lib/emmett/document.rb', line 105

def content
  @content
end

#file_nameObject

Returns the value of attribute file_name

Returns:

  • (Object)

    the current value of file_name



105
106
107
# File 'lib/emmett/document.rb', line 105

def file_name
  @file_name
end

#typeObject

Returns the value of attribute type

Returns:

  • (Object)

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



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

#documentObject



141
142
143
# File 'lib/emmett/document.rb', line 141

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

#endpoint_namesObject



115
116
117
# File 'lib/emmett/document.rb', line 115

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

#endpointsObject



127
128
129
# File 'lib/emmett/document.rb', line 127

def endpoints
  @endpoints ||= endpoint_names.map { |name| Endpoint.new(name, self) }
end

#group_namesObject



111
112
113
# File 'lib/emmett/document.rb', line 111

def group_names
  @group_name ||= document.css('h1').map(&:text)
end

#groupsObject



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_htmlObject



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_blocksObject

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_requestsObject



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_responsesObject



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_nameObject



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

#titleObject



145
146
147
# File 'lib/emmett/document.rb', line 145

def title
  @title ||= group_names.first
end

#to_path_nameObject



189
190
191
# File 'lib/emmett/document.rb', line 189

def to_path_name
  "#{short_name}.html"
end

#toc_htmlObject



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