Skip to content

Commit

Permalink
add the option of specifying verbatim fields
Browse files Browse the repository at this point in the history
  • Loading branch information
mutluyuz committed May 7, 2022
1 parent 95cb918 commit f83aec7
Show file tree
Hide file tree
Showing 12 changed files with 215 additions and 79 deletions.
34 changes: 15 additions & 19 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ edition = "2021"
[dependencies]
csv = "^1.1"
serde = "^1"
biblatex = "^0.5"
biblatex = "^0.6.1"
regex = "^1.5"
clap = { version = "^3.1", features = ["cargo"] }
anyhow = "^1.0"
Expand Down
32 changes: 18 additions & 14 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,24 +11,23 @@ mutluyuz
USAGE:
csv2bibtex [FLAGS] [OPTIONS] <INPUT> <OUTPUT>
FLAGS:
--biblatex Print output in BibLaTeX mode (default)
--bibtex Print output in BibTeX mode
-h, --help Prints help information
-l, --lazy Try to recover from as much errors as possible.
--no-defaults Don't add default field mappings.
-V, --version Prints version information
OPTIONS:
-d, --delimiter <DELIMITER> Delimiter between cells in CSV file
-f, --field-mapping <FIELD>... Assignment of csv fields to bibtex fields
-v, --verbosity <LEVEL> Verbosity level, either DEBUG, INFO, WARN, or ERROR
csv2bibtex [OPTIONS] <INPUT> <OUTPUT>
ARGS:
<INPUT> Input file to use
<OUTPUT> Output file to use
OPTIONS:
--biblatex Print output in BibLaTeX mode (default)
--bibtex Print output in BibTeX mode
-d, --delimiter <DELIMITER> Delimiter between cells in CSV file
-f, --field-mapping <FIELD> Assignment of csv fields to bibtex fields
-h, --help Print help information
-l, --lazy Try to recover from as much errors as possible.
--no-defaults Don't add default field mappings and verbatim fields.
-v, --verbosity <LEVEL> Verbosity level, either DEBUG, INFO, WARN, or ERROR
-V, --version Print version information
--verbatim-field <FIELD> Bib(La)TeX verbatim fields, like url, file or doi
```

Usage is really intuitive: `csv2bibtex INPUTFILE OUTPUTFILE`. CSV fields can be
Expand Down Expand Up @@ -59,6 +58,11 @@ any other field (see above). In addition, there are some default field mappings
set (like `title=[[titles]]`, use `--no-defaults` to prevent this).
The field mapping argument can be given multiple times to map multiple fields.

The `--verbatim-field` argument can be used to not escape a certain field
(e.g., `file`, `doi`, or `url`). This means that for example an url like
"https://www.example.com/?1234%56" stays this way and is not changed to
"https://www.example.com/?1234\%56".


## Installation

Expand Down
19 changes: 18 additions & 1 deletion src/args.rs
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ pub struct Config {
pub log_level: log::LevelFilter,
pub output_type: OutputType,
pub mapping_defaults: bool,
pub verbatim_fields: Vec<String>,
}

impl Default for Config {
Expand All @@ -42,6 +43,7 @@ impl Default for Config {
log_level: log::LevelFilter::Info,
output_type: OutputType::default(),
mapping_defaults: true,
verbatim_fields: std::vec::Vec::new(),
}
}
}
Expand Down Expand Up @@ -111,7 +113,7 @@ impl Config {
)
.arg(
clap::Arg::new("no-defaults")
.help("Don't add default field mappings.")
.help("Don't add default field mappings and verbatim fields.")
.long("no-defaults")
.takes_value(false),
)
Expand All @@ -125,6 +127,15 @@ impl Config {
.number_of_values(1)
.value_name("FIELD"),
)
.arg(
clap::Arg::new("verbatim-field")
.help("Bib(La)TeX verbatim fields, like url, file or doi")
.long("verbatim-field")
.takes_value(true)
.multiple_occurrences(true)
.number_of_values(1)
.value_name("FIELD"),
)
.get_matches();

// get defaults
Expand Down Expand Up @@ -152,6 +163,12 @@ impl Config {
}
}

if let Some(x) = matches.values_of("verbatim-field") {
for field in x {
ret.verbatim_fields.push(field.to_string());
}
}

// csv options
if let Some(x) = matches.value_of("csv-delimiter") {
ret.csv_delimiter = String::from(x)
Expand Down
3 changes: 2 additions & 1 deletion src/bibwriter.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
use anyhow::Context;
use anyhow::anyhow;

/// BibWriter Trait
pub trait BibWrite {
Expand Down Expand Up @@ -45,7 +46,7 @@ impl<W: std::io::Write> BibtexWriter<W> {

impl<W: std::io::Write> BibWrite for BibtexWriter<W> {
fn write(&mut self, entry: &biblatex::Entry) -> Result<(), anyhow::Error> {
write!(self.writer, "{}\n\n", entry.to_bibtex_string())
write!(self.writer, "{}\n\n", entry.to_bibtex_string().map_err(|e| anyhow!("TypeError: {}", e))?)
.context("Could not write entry to file")?;
self.counter += 1;

Expand Down
Loading

0 comments on commit f83aec7

Please sign in to comment.