Class: Hiptest::HandlebarsHelper
- Inherits:
-
Object
- Object
- Hiptest::HandlebarsHelper
- Defined in:
- lib/hiptest-publisher/handlebars_helper.rb
Class Method Summary collapse
Instance Method Summary collapse
- #hh_clear_empty_lines(context, block) ⇒ Object
- #hh_close_curly(context, block) ⇒ Object
- #hh_comment(context, commenter, block) ⇒ Object
- #hh_curly(context, block) ⇒ Object
- #hh_debug(context, block) ⇒ Object
- #hh_escape_backslashes_and_double_quotes(context, s, block) ⇒ Object
- #hh_escape_double_quotes(context, s, block) ⇒ Object
-
#hh_escape_quotes(context, s, block) ⇒ Object
kept for backward compatibility of customized templates.
- #hh_escape_single_quotes(context, s, block) ⇒ Object
- #hh_indent(context, block) ⇒ Object
- #hh_join(context, items, joiner, block, else_block = nil) ⇒ Object
- #hh_open_curly(context, block) ⇒ Object
- #hh_relative_path(context, filename, path_prefix = nil, block) ⇒ Object
- #hh_remove_double_quotes(context, s, block) ⇒ Object
-
#hh_remove_quotes(context, s, block) ⇒ Object
kept for backward compatibility of customized templates.
- #hh_remove_single_quotes(context, s, block) ⇒ Object
- #hh_tab(context, block) ⇒ Object
- #hh_to_string(context, value, block) ⇒ Object
-
#initialize(handlebars, context) ⇒ HandlebarsHelper
constructor
A new instance of HandlebarsHelper.
- #register_custom_helpers ⇒ Object
- #register_string_helpers ⇒ Object
Constructor Details
#initialize(handlebars, context) ⇒ HandlebarsHelper
Returns a new instance of HandlebarsHelper.
9 10 11 12 |
# File 'lib/hiptest-publisher/handlebars_helper.rb', line 9 def initialize(, context) @handlebars = @context = context end |
Class Method Details
.register_helpers(handlebars, context) ⇒ Object
3 4 5 6 7 |
# File 'lib/hiptest-publisher/handlebars_helper.rb', line 3 def self.register_helpers(, context) instance = Hiptest::HandlebarsHelper.new(, context) instance.register_string_helpers instance.register_custom_helpers end |
Instance Method Details
#hh_clear_empty_lines(context, block) ⇒ Object
79 80 81 82 83 |
# File 'lib/hiptest-publisher/handlebars_helper.rb', line 79 def hh_clear_empty_lines(context, block) block.fn(context).split("\n").map do |line| line unless line.strip.empty? end.compact.join("\n") end |
#hh_close_curly(context, block) ⇒ Object
135 136 137 |
# File 'lib/hiptest-publisher/handlebars_helper.rb', line 135 def hh_close_curly (context, block) "}" end |
#hh_comment(context, commenter, block) ⇒ Object
121 122 123 124 125 |
# File 'lib/hiptest-publisher/handlebars_helper.rb', line 121 def hh_comment (context, commenter, block) block.fn(context).split("\n").map do |line| "#{commenter} #{line}" end.join("\n") end |
#hh_curly(context, block) ⇒ Object
127 128 129 |
# File 'lib/hiptest-publisher/handlebars_helper.rb', line 127 def hh_curly (context, block) "{#{block.fn(context)}}" end |
#hh_debug(context, block) ⇒ Object
156 157 158 159 160 |
# File 'lib/hiptest-publisher/handlebars_helper.rb', line 156 def hh_debug(context, block) require 'pry' binding.pry "" end |
#hh_escape_backslashes_and_double_quotes(context, s, block) ⇒ Object
107 108 109 110 111 112 113 114 |
# File 'lib/hiptest-publisher/handlebars_helper.rb', line 107 def hh_escape_backslashes_and_double_quotes (context, s, block) if s s.gsub('\\') { |c| c*2 }. gsub('"', '\\"') else "" end end |
#hh_escape_double_quotes(context, s, block) ⇒ Object
103 104 105 |
# File 'lib/hiptest-publisher/handlebars_helper.rb', line 103 def hh_escape_double_quotes (context, s, block) s ? s.gsub('"', '\\"') : "" end |
#hh_escape_quotes(context, s, block) ⇒ Object
kept for backward compatibility of customized templates
99 100 101 |
# File 'lib/hiptest-publisher/handlebars_helper.rb', line 99 def hh_escape_quotes (context, s, block) hh_escape_double_quotes(context, s, block) end |
#hh_escape_single_quotes(context, s, block) ⇒ Object
116 117 118 119 |
# File 'lib/hiptest-publisher/handlebars_helper.rb', line 116 def hh_escape_single_quotes (context, s, block) # weird \\\\, see http://stackoverflow.com/questions/7074337/why-does-stringgsub-double-content s ? s.gsub('\'', "\\\\'") : "" end |
#hh_indent(context, block) ⇒ Object
68 69 70 71 72 73 74 75 76 77 |
# File 'lib/hiptest-publisher/handlebars_helper.rb', line 68 def hh_indent(context, block) indentation = @context[:indentation] || ' ' indentation = "\t" if indentation == '\t' block.fn(context).split("\n").map do |line| indented = "#{indentation}#{line}" indented = "" if indented.strip.empty? indented end.join("\n") end |
#hh_join(context, items, joiner, block, else_block = nil) ⇒ Object
47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 |
# File 'lib/hiptest-publisher/handlebars_helper.rb', line 47 def hh_join(context, items, joiner, block, else_block = nil) joiner = "\t" if joiner == '\t' if block.nil? || block.items.empty? "#{items.join(joiner)}" else if items.empty? && else_block return else_block.fn(context) end current_this = context.get('this') result = items.map do |item| context.add_item(:this, item) block.fn(context) end.join(joiner) context.add_item(:this, current_this) result end end |
#hh_open_curly(context, block) ⇒ Object
131 132 133 |
# File 'lib/hiptest-publisher/handlebars_helper.rb', line 131 def hh_open_curly (context, block) "{" end |
#hh_relative_path(context, filename, path_prefix = nil, block) ⇒ Object
143 144 145 146 147 148 149 150 151 152 153 154 |
# File 'lib/hiptest-publisher/handlebars_helper.rb', line 143 def hh_relative_path(context, filename, path_prefix = nil, block) levels_count = context.get('context.relative_path').count('/') name = "" name << path_prefix if path_prefix if levels_count == 0 name << filename else name << "../" * levels_count name << filename.to_s.gsub(/\A\.\//, '') end name end |
#hh_remove_double_quotes(context, s, block) ⇒ Object
90 91 92 |
# File 'lib/hiptest-publisher/handlebars_helper.rb', line 90 def hh_remove_double_quotes (context, s, block) s ? s.gsub('"', '') : "" end |
#hh_remove_quotes(context, s, block) ⇒ Object
kept for backward compatibility of customized templates
86 87 88 |
# File 'lib/hiptest-publisher/handlebars_helper.rb', line 86 def hh_remove_quotes (context, s, block) hh_remove_double_quotes(context, s, block) end |
#hh_remove_single_quotes(context, s, block) ⇒ Object
94 95 96 |
# File 'lib/hiptest-publisher/handlebars_helper.rb', line 94 def hh_remove_single_quotes (context, s, block) s ? s.gsub('\'', '') : "" end |
#hh_tab(context, block) ⇒ Object
139 140 141 |
# File 'lib/hiptest-publisher/handlebars_helper.rb', line 139 def hh_tab (context, block) "\t" end |
#hh_to_string(context, value, block) ⇒ Object
43 44 45 |
# File 'lib/hiptest-publisher/handlebars_helper.rb', line 43 def hh_to_string(context, value, block) "#{value.to_s}" end |
#register_custom_helpers ⇒ Object
34 35 36 37 38 39 40 41 |
# File 'lib/hiptest-publisher/handlebars_helper.rb', line 34 def register_custom_helpers self.class.instance_methods.each do |method_name| next unless method_name.to_s.start_with? 'hh_' @handlebars.register_helper(method_name.to_s.gsub(/hh_/, '')) do |*args| send(method_name, *args) end end end |
#register_string_helpers ⇒ Object
14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 |
# File 'lib/hiptest-publisher/handlebars_helper.rb', line 14 def register_string_helpers string_helpers = [ :literate, :normalize, :normalize_lower, :underscore, :camelize, :camelize_lower, :camelize_upper, :clear_extension, :downcase ] string_helpers.each do |helper| @handlebars.register_helper(helper) do |context, value| "#{value.to_s.send(helper)}" end end end |