Skip to content

Commit

Permalink
Gracefully handle encoding mishaps
Browse files Browse the repository at this point in the history
We are getting reports like in #14 where we're not selecting the best
encoding for a file in some cases. While this is usually user error, we
can try to fix things by attempting to change the encoding to UTF-8 as
that's usually what is intended.

Piggy-back on the Temple library's `Encoding` filter to clean this up
for us.
  • Loading branch information
sds committed Jun 23, 2015
1 parent 2578189 commit f747591
Showing 1 changed file with 9 additions and 0 deletions.
9 changes: 9 additions & 0 deletions lib/slim_lint/document.rb
Original file line number Diff line number Diff line change
Expand Up @@ -37,13 +37,22 @@ def initialize(source, options)
# @param source [String] Slim code to parse
# @raise [SlimLint::Exceptions::ParseError] if there was a problem parsing the document
def process_source(source)
@source = process_encoding(source)
@source = strip_frontmatter(source)
@source_lines = @source.split("\n")

engine = SlimLint::Engine.new(file: @file)
@sexp = engine.parse(source)
end

# Ensure the string's encoding is valid.
#
# @param source [String]
# @return [String] source encoded in a valid encoding
def process_encoding(source)
::Temple::Filters::Encoding.new.call(source)
end

# Removes YAML frontmatter
def strip_frontmatter(source)
if config['skip_frontmatter'] &&
Expand Down

0 comments on commit f747591

Please sign in to comment.