Method: Emmett::Document#highlighted_html

Defined in:
lib/emmett/document.rb

#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