Skip to content

Commit

Permalink
refactor: template non-alphanumeric characters in column names ar…
Browse files Browse the repository at this point in the history
…e replaced with _

so they can be easily used in MiniJinja templates
also added link to example template
  • Loading branch information
jqnatividad committed Nov 5, 2024
1 parent 30be8b2 commit 6b54c06
Showing 1 changed file with 12 additions and 1 deletion.
13 changes: 12 additions & 1 deletion src/cmd/template.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ Renders a template using CSV data with the MiniJinja template engine.
https://docs.rs/minijinja/latest/minijinja/
Each CSV row is used to populate the template, with column headers used as variable names.
Non-alphanumeric characters in column headers are replaced with an underscore ("_").
The template syntax follows the Jinja2 template language with additional custom filters
(see bottom of file).
Expand All @@ -12,6 +13,7 @@ Example template:
Status: {% if active|str_to_bool is true %}Active{% else %}Inactive{% endif %}
For examples, see https://github.com/jqnatividad/qsv/blob/master/tests/test_template.rs.
For a relatively complex MiniJinja template, see https://github.com/jqnatividad/qsv/blob/master/scripts/template.tpl
Usage:
qsv template [options] [--template <str> | --template-file <file>] [<input>] [<outdir> | --output <file>]
Expand Down Expand Up @@ -130,7 +132,16 @@ pub fn run(argv: &[&str]) -> CliResult<()> {
let headers = if args.flag_no_headers {
csv::StringRecord::new()
} else {
rdr.headers()?.clone()
let headers = rdr.headers()?.clone();
let sanitized_headers: Vec<String> = headers
.iter()
.map(|h| {
h.chars()
.map(|c| if c.is_alphanumeric() { c } else { '_' })
.collect()
})
.collect();
csv::StringRecord::from(sanitized_headers)
};
let context_capacity = if args.flag_no_headers {
rdr.headers()?.len()
Expand Down

0 comments on commit 6b54c06

Please sign in to comment.