Class: Grandfather::Code

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

Instance Method Summary collapse

Constructor Details

#initializeCode

Returns a new instance of Code.



6
7
8
# File 'lib/grandfather/code.rb', line 6

def initialize
  @code_blocks = []
end

Instance Method Details

#extract(data) ⇒ Object



10
11
12
13
14
15
16
# File 'lib/grandfather/code.rb', line 10

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)
  end
  data
end

#process(data) ⇒ Object



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

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