Class: Markover::Markup::Code
- Inherits:
-
Object
- Object
- Markover::Markup::Code
- Defined in:
- lib/markover/markup/code.rb
Overview
Class that knows how to extract code blocks and render them with syntax highlightning
Instance Method Summary collapse
-
#extract(data) ⇒ String
Extract all code blocks into the codemap and replace with placeholders.
-
#initialize ⇒ Code
constructor
A new instance of Code.
-
#process(data) ⇒ String
Process all code from the codemap and replace the placeholders with the final HTML.
Constructor Details
#initialize ⇒ Code
Returns a new instance of Code.
10 11 12 |
# File 'lib/markover/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.
17 18 19 20 21 22 23 24 |
# File 'lib/markover/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.
30 31 32 33 34 35 36 |
# File 'lib/markover/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 |