Module: Jekyll::WebAwesome::CodeBlockTransformer

Defined in:
lib/jekyll/webawesome/code_block_transformer.rb

Overview

Protects markdown fenced code blocks before Markawesome transforms run, and substitutes them with Jekyll ‘highlight %` tags after, so code examples containing `:::`/`^^^`/`@@@` survive intact and language-tagged blocks still render with Jekyll’s syntax highlighter.

Protection/restore is delegated to Markawesome::CodeBlockProtector; the ‘highlight %` substitution at priority 15 is Jekyll-specific and stays here.

Constant Summary collapse

CODE_BLOCK_PATTERN =
Markawesome::CodeBlockProtector::CODE_BLOCK_PATTERN

Class Attribute Summary collapse

Class Method Summary collapse

Class Attribute Details

.protected_blocksObject (readonly)

Returns the value of attribute protected_blocks.



21
22
23
# File 'lib/jekyll/webawesome/code_block_transformer.rb', line 21

def protected_blocks
  @protected_blocks
end

Class Method Details

.clear_protected_blocksObject



23
24
25
# File 'lib/jekyll/webawesome/code_block_transformer.rb', line 23

def clear_protected_blocks
  @protected_blocks.clear
end

.process(content) ⇒ Object



85
86
87
# File 'lib/jekyll/webawesome/code_block_transformer.rb', line 85

def process(content)
  transform_code_blocks(content)
end

.protect_all_code_blocks(content) ⇒ Object

Protect all code blocks by converting them to placeholders via Markawesome::CodeBlockProtector. The resulting token map is merged into @protected_blocks so per-page placeholders survive across the pre_render priority chain (50 → 15 → 10).



47
48
49
50
51
# File 'lib/jekyll/webawesome/code_block_transformer.rb', line 47

def protect_all_code_blocks(content)
  protected_content, tokens = Markawesome::CodeBlockProtector.protect(content)
  @protected_blocks.merge!(tokens)
  protected_content
end

.restore_protected_blocks(content) ⇒ Object

Restore protected code blocks using Markawesome::CodeBlockProtector, then clear the per-page token map so the next page starts fresh. CodeBlockProtector.protect always restarts its counter at 0, so clearing between pages prevents placeholder-id collisions.



79
80
81
82
83
# File 'lib/jekyll/webawesome/code_block_transformer.rb', line 79

def restore_protected_blocks(content)
  restored = Markawesome::CodeBlockProtector.restore(content, @protected_blocks)
  @protected_blocks.clear
  restored
end

.transform_code_blocks(content) ⇒ Object

Rewrite each protected fenced block to a Jekyll ‘highlight %` tag, except for blocks without a language or with `plain` — those are restored verbatim. Called at priority 15, i.e. after Markawesome transformers but before restore.



57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
# File 'lib/jekyll/webawesome/code_block_transformer.rb', line 57

def transform_code_blocks(content)
  @protected_blocks.transform_values! do |match|
    if match =~ CODE_BLOCK_PATTERN
      language = Regexp.last_match(1)
      code_content = Regexp.last_match(2).strip

      if language && language.downcase != 'plain'
        "{% highlight #{language} %}\n#{code_content}\n{% endhighlight %}"
      else
        match
      end
    else
      match
    end
  end
  content
end

.transform_documents_enabled?(site) ⇒ Boolean

Check if documents transformation is enabled

Returns:

  • (Boolean)


36
37
38
39
40
41
# File 'lib/jekyll/webawesome/code_block_transformer.rb', line 36

def transform_documents_enabled?(site)
  return Jekyll::WebAwesome.configuration.transform_documents if Jekyll::WebAwesome.configuration
  return site.config.dig('webawesome', 'transform_documents') if site.config.dig('webawesome', 'transform_documents') != nil

  true
end

.transform_pages_enabled?(site) ⇒ Boolean

Check if pages transformation is enabled

Returns:

  • (Boolean)


28
29
30
31
32
33
# File 'lib/jekyll/webawesome/code_block_transformer.rb', line 28

def transform_pages_enabled?(site)
  return Jekyll::WebAwesome.configuration.transform_pages if Jekyll::WebAwesome.configuration
  return site.config.dig('webawesome', 'transform_pages') if site.config.dig('webawesome', 'transform_pages') != nil

  true
end