Method: TermColor::RuleSet#apply
- Defined in:
- lib/term_color/rule_set.rb
#apply(text) ⇒ String
Apply styling to string using rule set
108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 |
# File 'lib/term_color/rule_set.rb', line 108 def apply(text) raw = process_text(text) rule_stack = [] str = '' rule_names = @rules.keys raw.each do |r| if r.is_a?(Symbol) # Part is a rule dprint "\tRule Symbol #{r}\n" if r == :close && rule_stack.length >= 1 # Rule close with 1+ opened rules opened = rule_stack.pop opened_after = @rules[opened].codes(Rule::Parts[:after]) dprint "\t\tClose, opened rule '#{opened}'\n" dprint "\t\t\tClosing rule '#{opened}' with After\n" dprint 4,"After: #{opened_after.inspect}\n" str.concat(opened_after) unless rule_stack.length == 0 rule_stack.each do |outer| outer_inside = @rules[outer].codes(Rule::Parts[:inside]) # Closed rule was nested in another open rule dprint 3, "Outer rule '#{outer}' still open. Restoring Inside\n" dprint 4, "Inside: #{outer_inside.inspect}\n}" str.concat(outer_inside) end end # binding.pry # outer = rule_stack[-1] # outer_inside = @rules[outer].codes(Rule::Parts[:inside]) # # Closed rule was nested in another open rule # dprint 3, "Outer rule '#{outer}' still open. Restoring Inside\n" # dprint 4, "Inside: #{outer_inside.inspect}\n}" # str.concat(outer_inside) # # binding.pry # end elsif r == :reset && rule_stack.length == 0 # no opened outer rules, reset symbol given dprint "\t\tReset, no opened rule\n" str.concat(@rules[r].codes(Rule::Parts[:after])) elsif rule_names.include?(r) # New rule to apply dprint "\t\tApplying new rule '#{r}'\n" dprint 3, "Previous active rule `#{rule_stack[-1]}`\n" rule_stack.push r str.concat(@rules[r].codes(Rule::Parts[:inside])) end else # Part is text str.concat(r) end end str end |