Module: JekyllIncludePlugin::TextUtils
Instance Method Summary collapse
- #blank_line?(line) ⇒ Boolean
- #pick_snippet(text, snippet_name) ⇒ Object
- #remove_all_snippets(text) ⇒ Object
- #remove_excessive_indentation(text) ⇒ Object
- #remove_excessive_newlines(text) ⇒ Object
- #render_comments(text, lang) ⇒ Object
- #wrap_in_codeblock(text, syntax) ⇒ Object
Methods included from Utils
Instance Method Details
#blank_line?(line) ⇒ Boolean
112 113 114 |
# File 'lib/jekyll_include_plugin/utils.rb', line 112 def blank_line?(line) return %r!^\s*$!.match?(line) end |
#pick_snippet(text, 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 |
# File 'lib/jekyll_include_plugin/utils.rb', line 19 def pick_snippet(text, 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 end |
#remove_all_snippets(text) ⇒ Object
48 49 50 51 52 53 54 55 56 57 58 59 60 |
# File 'lib/jekyll_include_plugin/utils.rb', line 48 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
83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 |
# File 'lib/jekyll_include_plugin/utils.rb', line 83 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
79 80 81 |
# File 'lib/jekyll_include_plugin/utils.rb', line 79 def remove_excessive_newlines(text) return text.sub(/^(\s*\R)*/, "").rstrip() end |
#render_comments(text, lang) ⇒ Object
62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 |
# File 'lib/jekyll_include_plugin/utils.rb', line 62 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
108 109 110 |
# File 'lib/jekyll_include_plugin/utils.rb', line 108 def wrap_in_codeblock(text, syntax) return "```#{syntax}\n#{text}\n```" end |