Skip to content

Commit

Permalink
Allow Configurable Converters on CSV
Browse files Browse the repository at this point in the history
Lets the user specify which converters to pass to `CSV`, defaulting to `nil` if not present in config. This is specifically so that numeric values in a CSV file can be parsed as something other than strings.
  • Loading branch information
MichaelCordingley committed Oct 29, 2021
1 parent 10a7359 commit b394f21
Showing 1 changed file with 19 additions and 7 deletions.
26 changes: 19 additions & 7 deletions lib/jekyll/readers/data_reader.rb
Original file line number Diff line number Diff line change
Expand Up @@ -58,14 +58,9 @@ def read_data_file(path)

case File.extname(path).downcase
when ".csv"
CSV.read(path,
:headers => true,
:encoding => site.config["encoding"]).map(&:to_hash)
convert_csv(path)
when ".tsv"
CSV.read(path,
:col_sep => "\t",
:headers => true,
:encoding => site.config["encoding"]).map(&:to_hash)
convert_tsv(path)
else
SafeYAML.load_file(path)
end
Expand All @@ -75,5 +70,22 @@ def sanitize_filename(name)
name.gsub(%r![^\w\s-]+|(?<=^|\b\s)\s+(?=$|\s?\b)!, "")
.gsub(%r!\s+!, "_")
end

private

def convert_csv(path)
CSV.read(path,
:converters => site.config["csv_converters"]&.map(&:to_sym),
:headers => true,
:encoding => site.config["encoding"]).map(&:to_hash)
end

def convert_tsv(path)
CSV.read(path,
:col_sep => "\t",
:converters => site.config["csv_converters"]&.map(&:to_sym),
:headers => true,
:encoding => site.config["encoding"]).map(&:to_hash)
end
end
end

0 comments on commit b394f21

Please sign in to comment.