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
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
|
# File 'lib/jekyll-aeo/utils/content_stripper.rb', line 18
def self.strip(content, config = {})
return "" if content.nil? || content.strip.empty?
strip_block_tags = config.fetch("strip_block_tags", true)
protect_indented = config.fetch("protect_indented_code", false)
lines = content.lines
state = :normal
fence_close_pattern = nil
silent_close_pattern = nil
prev_blank = false
result_lines = []
lines.each do |line|
case state
when :in_fence
result_lines << line
if line.rstrip =~ fence_close_pattern
state = :normal
fence_close_pattern = nil
prev_blank = false
end
when :in_raw
if line =~ LIQUID_RAW_CLOSE
result_lines << line.sub(LIQUID_RAW_CLOSE, "")
state = :normal
prev_blank = false
else
result_lines << line
end
when :in_silent_block
if line =~ silent_close_pattern
state = :normal
silent_close_pattern = nil
prev_blank = false
end
when :in_indented_code
if line =~ /\A\s*\n?\z/ || line =~ /\A {4,}/
result_lines << line
else
state = :normal
prev_blank = false
result_lines << strip_line(line)
end
when :normal
if (match = line.match(FENCE_OPEN))
state = :in_fence
char = match[3] ? "`" : "~"
count = (match[3] || match[4]).length
fence_close_pattern = /\A\s{0,3}#{Regexp.escape(char)}{#{count},}\s*\z/
result_lines << line
elsif line =~ LIQUID_RAW_OPEN
if line =~ LIQUID_RAW_ENDRAW
result_lines << line.gsub(LIQUID_RAW_ENDRAW, '\1')
else
state = :in_raw
result_lines << line.sub(LIQUID_RAW_OPEN, "")
end
elsif strip_block_tags && line =~ LIQUID_COMMENT_OPEN
state = :in_silent_block
silent_close_pattern = LIQUID_COMMENT_CLOSE
elsif strip_block_tags && line =~ LIQUID_CAPTURE_OPEN
state = :in_silent_block
silent_close_pattern = LIQUID_CAPTURE_CLOSE
elsif protect_indented && prev_blank && line =~ /\A {4,}/
state = :in_indented_code
result_lines << line
else
result_lines << strip_line(line)
end
prev_blank = line =~ /\A\s*\n?\z/
end
end
result_lines.join
end
|