Module: JekyllIncludePlugin::TextUtils

Includes:
Utils
Included in:
IncludeFileTag
Defined in:
lib/jekyll_include_plugin/utils.rb

Instance Method Summary collapse

Methods included from Utils

#abort, #debug, #info

Instance Method Details

#blank_line?(line) ⇒ Boolean

Returns:

  • (Boolean)


130
131
132
# File 'lib/jekyll_include_plugin/utils.rb', line 130

def blank_line?(line)
  return %r!^\s*$!.match?(line)
end

#pick_snippet(text, snippet_prefix, snippet_name) ⇒ Object



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
# File 'lib/jekyll_include_plugin/utils.rb', line 19

def pick_snippet(text, snippet_prefix, snippet_name)
  snippet_content = ""
  snippet_start_found = false
  snippet_end_found = false
  text.each_line do |line|
    if %r!\[<snippet\s+#{snippet_name}>\]!.match?(line)
      if snippet_start_found
        abort("Snippet '#{snippet_name}' occured twice. Each snippet should have a unique name, same name not allowed.")
      end
      snippet_start_found = true
      debug("Snippet '#{snippet_name}' start matched by line: #{line}")
    elsif %r!\[<endsnippet\s+#{snippet_name}>\]!.match?(line)
      snippet_end_found = true
      debug("Snippet '#{snippet_name}' end matched by line: #{line}")
      break
    elsif %r!\[<(end)?snippet\s+[^>]+>\]!.match?(line)
      debug("Skipping line with non-relevant (end)snippet: #{line}")
      next
    elsif snippet_start_found
      snippet_content += line
    end
  end
  abort("Snippet '#{snippet_name}' has not been found.") unless snippet_start_found
  abort("End of the snippet '#{snippet_name}' has not been found.") unless snippet_end_found
  abort("Snippet '#{snippet_name}' appears to be empty. Fix and retry.") if snippet_content.empty?

  return snippet_content if snippet_prefix.empty?

  first_line_indent = %r!^\s*!.match(snippet_content)[0]
  return "#{first_line_indent}#{snippet_prefix}\n#{snippet_content}"
end

#remove_all_snippets(text) ⇒ Object



66
67
68
69
70
71
72
73
74
75
76
77
78
# File 'lib/jekyll_include_plugin/utils.rb', line 66

def remove_all_snippets(text)
  result_text = ""
  text.each_line do |line|
    if %r!\[<(end)?snippet\s+[^>]+>\]!.match?(line)
      debug("Skipping line with non-relevant (end)snippet: #{line}")
      next
    else
      result_text += line
    end
  end

  return result_text
end

#remove_excessive_indentation(text) ⇒ Object



101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
# File 'lib/jekyll_include_plugin/utils.rb', line 101

def remove_excessive_indentation(text)
  unindented_text = ""

  lowest_indent = nil
  text.each_line do |line|
    if %r!^\s*$!.match?(line)
      next
    else
      cur_indent = %r!^\s*!.match(line)[0].length
      lowest_indent = cur_indent if lowest_indent.nil? || lowest_indent > cur_indent
    end
  end
  return text if lowest_indent.nil?

  text.each_line do |line|
    if blank_line?(line)
      unindented_text += line
    else
      unindented_text += line[lowest_indent..-1]
    end
  end

  return unindented_text
end

#remove_excessive_newlines(text) ⇒ Object



97
98
99
# File 'lib/jekyll_include_plugin/utils.rb', line 97

def remove_excessive_newlines(text)
  return text.sub(/^(\s*\R)*/, "").rstrip()
end

#remove_ignored_lines(text) ⇒ Object



51
52
53
54
55
56
57
58
59
60
61
62
63
64
# File 'lib/jekyll_include_plugin/utils.rb', line 51

def remove_ignored_lines(text)
  ignoring = false
  text.each_line.reject do |line|
    if line =~ /^\s*\/\/\s*\[<ignore>\]/
      ignoring = true
      true
    elsif line =~ /^\s*\/\/\s*\[<endignore>\]/
      ignoring = false
      true
    else
      ignoring
    end
  end.join
end

#render_comments(text, lang) ⇒ Object



80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
# File 'lib/jekyll_include_plugin/utils.rb', line 80

def render_comments(text, lang)
  rendered_file_contents = ""
  text.each_line do |line|
    if %r!\[<#{lang}>\]!.match?(line)
      debug("Matched doc line: #{line}")
      rendered_file_contents += line.sub(/\[<#{lang}>\]\s*/, "")
    elsif %r!\[<\w+>\]!.match?(line)
      debug("Found non-matching doc line, skipping: #{line}")
      next
    else
      rendered_file_contents += line
    end
  end

  return rendered_file_contents
end

#wrap_in_codeblock(text, syntax) ⇒ Object



126
127
128
# File 'lib/jekyll_include_plugin/utils.rb', line 126

def wrap_in_codeblock(text, syntax)
  return "```#{syntax}\n#{text}\n```"
end