app/helpers/site/rendering_helper.rb
# Discovers and highlights any code blocks using the Rouge gem
#
# === Parameters
#
# [html] _(String, required)_ the HTML to process
Discovers and highlights any code blocks using the Rouge gem
Parameters

[html] _(String, required)_ the HTML to process
Discovers and highlights any code blocks using the Rouge gem Parameters [ html ] _ ( String required ) _ the HTML to process
def highlight_code_blocks( html )
    doc = Nokogiri::HTML.fragment( html )
    doc.css( 'pre code' ).each do |block|
      next unless block.attributes.key?( 'data-language' )
      content = block.inner_text
      language = block.attributes['data-language'].value
      rendered = render( partial: 'site/base/code-block', locals: {
        language: language,
        content: Rouge.highlight( content, language, 'html' ).lines
      }).strip
      Nokogiri::HTML.fragment( rendered ).children.each{ |c| block.parent.previous = c }
      block.parent.remove
    end
    doc.to_html.gsub( /(<div class=\"code-block__line\">)\n/, '\1' ).html_safe
  end
def highlight_code_blocks ( html ) doc = Nokogiri :: HTML . fragment ( html ) doc . css ( 'pre code' ) . each do | block | next unless block . attributes . key? ( 'data-language' ) content = block . inner_text language = block . attributes [ 'data-language' ] . value rendered = render ( partial : 'site/base/code-block' , locals : { language : language , content : Rouge . highlight ( content , language , 'html' ) . lines } ) . strip Nokogiri :: HTML . fragment ( rendered ) . children . each { | c | block . parent . previous = c } block . parent . remove end doc . to_html . gsub ( / (<div class= \" code-block__line \" >) \n / , '\1' ) . html_safe end
Discovers and highlights any code blocks using the Rouge gem
 Parameters
Discovers and highlights any code blocks using the Rouge gem Parameters
# load into nokogiri # process all blocks # get information # render it out # update the DOM + remove the old PRE tag # return # Note: gsub is here otherwise Nokogiri seems want to add newlines where it shouldn’t 🙄
html