Class: Nesta::ContentFocus::HTMLRenderer

Inherits:
Kramdown::Document
  • Object
show all
Defined in:
lib/nesta-contentfocus-extensions/renderer.rb

Instance Method Summary collapse

Instance Method Details

#block_code(code, language) ⇒ Object



10
11
12
13
14
15
# File 'lib/nesta-contentfocus-extensions/renderer.rb', line 10

def block_code(code, language)
  @code_count ||= 0
  @code_count += 1
  code_block_id = "code-example-#{@code_count}"
  syntax_highlight(code, language, code_block_id)
end

#escape_example_codeblock(code) ⇒ Object



29
30
31
# File 'lib/nesta-contentfocus-extensions/renderer.rb', line 29

def escape_example_codeblock(code)
  code.gsub(/^\\```/m, '```')
end

#escape_example_footnote(code) ⇒ Object



33
34
35
# File 'lib/nesta-contentfocus-extensions/renderer.rb', line 33

def escape_example_footnote(code)
  code.gsub(/^\\\[([a-z]+)/im, '[\1')
end

#preprocess(document) ⇒ Object



6
7
8
# File 'lib/nesta-contentfocus-extensions/renderer.rb', line 6

def preprocess(document)
  @document = document
end

#syntax_highlight(code, language, id) ⇒ Object



37
38
39
40
41
42
# File 'lib/nesta-contentfocus-extensions/renderer.rb', line 37

def syntax_highlight(code, language, id)
  options = syntax_highlight_options(language, id)
  code = escape_example_codeblock(code)
  code = escape_example_footnote(code)
  Pygments.highlight(code, options)
end

#syntax_highlight_options(language, id) ⇒ Object



17
18
19
20
21
22
23
24
25
26
27
# File 'lib/nesta-contentfocus-extensions/renderer.rb', line 17

def syntax_highlight_options(language, id)
  options = { options: {
    linenos: true,
    cssclass: 'hll',
    lineanchors: id,
    linespans: id,
    anchorlinenos: true
  } }
  #options.merge!(lexer: language) if LANGUAGES.include? language
  options
end