lib/hamlit/concerns/indentable.rb in hamlit-0.3.3 vs lib/hamlit/concerns/indentable.rb in hamlit-0.3.4

- old
+ new

@@ -46,8 +46,43 @@ def same_indent?(line) return false unless line count_indent(line) == @current_indent end + + # Validate the template is using consitent indentation, 2 spaces or a tab. + def validate_indentation!(template) + last_indent = '' + + indents = template.scan(/^[ \t]+/) + indents.each do |indent| + if last_indent.include?(' ') && indent.include?("\t") || + last_indent.include?("\t") && indent.include?(' ') + syntax_error!(%Q{Inconsistent indentation: #{indent_label(indent)} used for indentation, but the rest of the document was indented using #{indent_label(last_indent)}.}) + end + + last_indent = indent + end + end + + def indent_label(indent) + return %Q{"#{indent}"} if indent.include?(' ') && indent.include?("\t") + + label = indent.include?(' ') ? 'space' : 'tab' + length = indent.match(/[ \t]+/).to_s.length + + "#{length} #{label}#{'s' if length > 1}" + end + + # Replace hard tabs into 2 spaces + def replace_hard_tabs(template) + lines = [] + template.each_line do |line| + lines << line.gsub(/^\t+/) do |match| + ' ' * (match.length * 2) + end + end + lines.join + end end end end