Module: JekyllAeo::Utils::ContentStripper

Defined in:
lib/jekyll-aeo/utils/content_stripper.rb

Constant Summary collapse

FENCE_OPEN =
/^(\s{0,3})((`{3,})|(~{3,}))/
LIQUID_RAW_ENDRAW =
/\{%-?\s*raw\s*-?%\}(.*?)\{%-?\s*endraw\s*-?%\}/
LIQUID_RAW_OPEN =
/\{%-?\s*raw\s*-?%\}/
LIQUID_RAW_CLOSE =
/\{%-?\s*endraw\s*-?%\}/
LIQUID_COMMENT_OPEN =
/\{%-?\s*comment\s*-?%\}/
LIQUID_COMMENT_CLOSE =
/\{%-?\s*endcomment\s*-?%\}/
LIQUID_CAPTURE_OPEN =
/\{%-?\s*capture\s+\w+\s*-?%\}/
LIQUID_CAPTURE_CLOSE =
/\{%-?\s*endcapture\s*-?%\}/
LIQUID_BLOCK_TAG =
/\{%-?.*?-?%\}/
LIQUID_OUTPUT_TAG =
/\{\{-?.*?-?\}\}/
KRAMDOWN_IAL =
/\s*\{:.*?\}/

Class Method Summary collapse

Class Method Details

.strip(content, config = {}) ⇒ Object



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
      # discard all lines (including closing tag)

    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
      # Check for fenced code block opening
      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

      # Check for {% raw %} opening
      elsif line =~ LIQUID_RAW_OPEN
        if line =~ LIQUID_RAW_ENDRAW
          # Single-line raw/endraw: strip both tags, keep inner content verbatim
          result_lines << line.gsub(LIQUID_RAW_ENDRAW, '\1')
        else
          state = :in_raw
          result_lines << line.sub(LIQUID_RAW_OPEN, "")
        end

      # Check for silent block opening (comment/capture) when strip_block_tags enabled
      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

      # Check for indented code block
      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