From 51fd3673a014fdfa59e4e9bd117bb90b61a86fa0 Mon Sep 17 00:00:00 2001 From: DarkWiiPlayer Date: Wed, 1 Feb 2023 19:07:44 +0100 Subject: [PATCH] Add csv reader using streamcsv --- glass-dev-1.rockspec | 1 + glass/csv.lua | 15 +++++++++++++++ readme.md | 1 + 3 files changed, 17 insertions(+) create mode 100644 glass/csv.lua diff --git a/glass-dev-1.rockspec b/glass-dev-1.rockspec index 34135f6..418d182 100644 --- a/glass-dev-1.rockspec +++ b/glass-dev-1.rockspec @@ -15,6 +15,7 @@ build = { modules = { glass = "glass.lua", ["glass.cosmo"] = "glass/cosmo.lua", + ["glass.csv"] = "glass/csv.lua", ["glass.discount"] = "glass/discount.lua", ["glass.environment"] = "glass/environment.lua", ["glass.error"] = "glass/error.lua", diff --git a/glass/csv.lua b/glass/csv.lua new file mode 100644 index 0000000..4000fa2 --- /dev/null +++ b/glass/csv.lua @@ -0,0 +1,15 @@ +--- Loader for CSV-Data using streamcsv +-- @module glass.csv + +local csv = require 'streamcsv' + +--- Loads a CSV-File and returns a corresponding Lua table. +-- CSV file is assumed to have a header row. +-- @treturn table CSV-Data +-- @function load +return function(file) + local file = io.open(file..'.csv') + if file then + return csv.file(file) + end +end diff --git a/readme.md b/readme.md index 8a25d2a..d86f659 100644 --- a/readme.md +++ b/readme.md @@ -28,6 +28,7 @@ A glass loader is initialised with a list of loaders and will try each of them i ## Loaders Glass offers the following loaders out of the box: * `cosmo` loads cosmo templates +* `csv` loads csv files (with header line) * `discount` loads markdown files\*. * `environment` loads environment variables. * `json` loads a JSON file as a Lua table. (requires `cjson`)