Class: Gimli::Markup::Code

Inherits:
Object
  • Object
show all
Defined in:
lib/gimli/markup/code.rb

Overview

Class that knows how to extract code blocks and render them with syntax highlightning

Instance Method Summary collapse

Constructor Details

#initializeCode

Returns a new instance of Code.



10
11
12
# File 'lib/gimli/markup/code.rb', line 10

def initialize
  @code_blocks = []
end

Instance Method Details

#extract(data) ⇒ String

Extract all code blocks into the codemap and replace with placeholders.

Returns:

  • (String)

    Returns the placeholder’d String data.



17
18
19
20
21
22
23
24
# File 'lib/gimli/markup/code.rb', line 17

def extract(data)
  data.gsub!(/^``` ?([^\r\n]+)?\r?\n(.+?)\r?\n```\r?$/m) do
    id = Digest::SHA1.hexdigest($2)
    @code_blocks << CodeBlock.new(id, $1, $2)
    id
  end
  data
end

#process(data) ⇒ String

Process all code from the codemap and replace the placeholders with the final HTML.

Returns:

  • (String)

    Returns the marked up String data.



30
31
32
33
34
35
36
# File 'lib/gimli/markup/code.rb', line 30

def process(data)
  return data if data.nil? || data.size.zero? || @code_blocks.size.zero?
  @code_blocks.each do |block|
    data.gsub!(block.id, block.highlighted)
  end
  data
end