-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathescape_code.rb
More file actions
69 lines (55 loc) · 1.97 KB
/
Copy pathescape_code.rb
File metadata and controls
69 lines (55 loc) · 1.97 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
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
require 'jekyll'
module Jekyll
module EscapeCode
Jekyll::Hooks.register [:documents, :pages, :posts], :pre_render do |item|
item.content = Jekyll::EscapeCode.escape(item)
end
Jekyll::Hooks.register [:documents, :pages, :posts], :post_render do |item|
Jekyll::EscapeCode.unescape_brackets(item.output)
end
extend self
def escape_brackets(content)
content.gsub(/{/,'{').gsub(/}/, '}')
end
def unescape_brackets(content)
content.gsub!(/&(amp;)?#x7b;/, '{')
content.gsub!(/&(amp;)?#x7d;/, '}')
content
end
def escape(page)
ext = (page.data["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
end
end