Module: Octopress::EscapeCode

Extended by:
EscapeCode
Included in:
EscapeCode
Defined in:
lib/octopress-escape-code.rb,
lib/octopress-escape-code/version.rb

Constant Summary collapse

VERSION =
"2.1.1"

Instance Method Summary collapse

Instance Method Details

#escape(page) ⇒ Object



43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
# File 'lib/octopress-escape-code.rb', line 43

def escape(page)
  ext = page.ext.downcase
  content = page.content.encode!("UTF-8")
  md_ext = %w{.markdown .mdown .mkdn .md .mkd .mdwn .mdtxt .mdtext}

  # Escape markdown style code blocks
  if md_ext.include?(ext)

    # Escape four tab or space indented code blocks
    content = content.gsub /^((\t| {4})[^\n].+?)\n($|\S)/m do
      "#{escape_brackets $1}\n#{$3}"
    end

    # Escape in-line code backticks
    content = content.gsub /(`[^`\n]+?`)/ do
      "#{escape_brackets $1}"
    end

    # Escape in-line code double backticks
    content = content.gsub /(``[^\n]+?``)/ do
      escape_brackets $1
    end

  end

  # Escape highlight and codeblock tag contents
  content = content.gsub /^({%\s*(codeblock|highlight).+?%})(.+?){%\s*end(codeblock|highlight)\s*%}/m do
    "#{$1}{% raw %}#{unescape_brackets $3}{% endraw %}{% end#{$4} %}"
  end

  # Escape codefenced codeblocks
  content = content.gsub /^(`{3}.+?`{3})/m do
    
    # Replace any raw/endraw tags inside of codefence block
    # as some of the regex above may have escaped contents
    # of the codefence block
    #
    code = unescape_brackets($1).gsub(/{% (end)?raw %}/, '')

    # Wrap codefence content in raw tags
    "{% raw %}\n#{code}\n{% endraw %}"
  end

  content
end

#escape_brackets(content) ⇒ Object



33
34
35
# File 'lib/octopress-escape-code.rb', line 33

def escape_brackets(content)
  content.gsub(/{/,'{').gsub(/}/, '}')
end

#escape_enabled?(page) ⇒ Boolean

Returns:

  • (Boolean)


19
20
21
# File 'lib/octopress-escape-code.rb', line 19

def escape_enabled?(page)
  get_config(page, 'escape_code', false)
end

#get_config(page, config, default) ⇒ Object



23
24
25
26
27
28
29
30
31
# File 'lib/octopress-escape-code.rb', line 23

def get_config(page, config, default)
  site_config = page.site.config[config]
  site_config = default if site_config.nil?

  page_config = page.data[config]
  page_config = site_config if page_config.nil?

  page_config
end

#unescape_brackets(content) ⇒ Object



37
38
39
40
41
# File 'lib/octopress-escape-code.rb', line 37

def unescape_brackets(content)
  content.gsub!(/&(amp;)?#x7b;/, '{')
  content.gsub!(/&(amp;)?#x7d;/, '}')
  content
end