Skip to content

Commit

Permalink
refactor: new, more composable API, split out optional ignore paths
Browse files Browse the repository at this point in the history
  • Loading branch information
Firgrep committed Aug 31, 2024
1 parent 6452316 commit 44d12c2
Show file tree
Hide file tree
Showing 8 changed files with 157 additions and 73 deletions.
2 changes: 1 addition & 1 deletion 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 @@ -7,7 +7,7 @@ repository = "https://github.com/systemphil/prepyrus"
readme = "README.md"
categories = ["database", "parser-implementations", "text-processing"]
keywords = ["bibtex", "biblatex", "mdx", "parser", "citation"]
version = "0.1.2"
version = "0.2.0"
edition = "2021"

[dependencies]
Expand Down
61 changes: 37 additions & 24 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,53 +6,68 @@
Prepyrus is a tool for verifying and processing MDX files
that contain citations in Chicago author-date style and certain metadata.

⚠️ This tool is still in early development and API may frequently change.

## Usage

Add the crate to your `Cargo.toml` and use it as shown below:

```toml
[dependencies]
prepyrus = "0.1"
prepyrus = "0.2"
```

Main API interface is the `run_prepyrus` function. Example usage:
Main API interface is the `Prepyrus` impl. Example usage:

```rust
use prepyrus::run_prepyrus;
use prepyrus::Prepyrus;

fn main() {
let args: Vec<String> = std::env::args().collect();
if args.len() < 4 {
eprintln!(
"Expected more args. Usage: prepyrus <bibliography.bib> <target_dir_or_file> <mode>"
);
std::process::exit(1);
}
if let Err(e) = run_prepyrus(&args[1], &args[2], &args[3]) {
let args = vec![
"_program_index".to_string(),
"tests/mocks/test.bib".to_string(), // bibliography file
"tests/mocks/data".to_string(), // target directory or .mdx file
"verify".to_string(), // mode
"tests/mocks/data/development.mdx".to_string(), // optional ignore paths, separate with commas if multiple
];

let _ = run(args).unwrap_or_else(|e| {
eprintln!("Error: {}", e);
std::process::exit(1);
}
});

println!("===Prepyrus completed successfully!");
}
```

The function takes three arguments: `<bibliography.bib> <target_dir_or_file> <mode>`
fn run(args: Vec<String>) -> Result<(), Box<dyn std::error::Error>> {
let config = Prepyrus::build_config(&args, None)?;
let all_entries = Prepyrus::get_all_bib_entries(&config.bib_file).unwrap();
let mdx_paths =
Prepyrus::get_mdx_paths(&config.target_path, Some(config.settings.ignore_paths))?;

// Phase 1: Verify MDX files
let articles_file_data = Prepyrus::verify(mdx_paths, &all_entries)?;

- a bibliography file (.bib),
- a target directory or .mdx file,
- and a mode (either `verify` or `process`).
// Phase 2: Process MDX files (requires mode to be set to "process")
if config.mode == "process" {
Prepyrus::process(articles_file_data);
}

Ok(())
}
```

`verify` mode only verifies the citations in the MDX files against the bibliography.

**⚠️ NOTE: `process` mode modifies the MDX files.**
`process` mode _additionally_ processes the MDX files by injecting bibliography and other HTML details into the MDX files.
**⚠️ NOTE: This mode modifies the MDX files.**

`process` mode _additionally_ processes the MDX files by injecting bibliography and other details into the MDX files.

## Description

The tool is designed to work with MDX files that contain citations in Chicago author-date style. Examples:

```markdown
"...nowhere on heaven or on earth is there anything which does not contain both being and nothing in itself" (Hegel 2010, 61).
```
> "...nowhere on heaven or on earth is there anything which does not contain both being and nothing in itself" (Hegel 2010, 61).
The tool parses and verifies the citations in the MDX files against a
bibliography file in BibTeX format (using Biblatex).
Expand All @@ -64,9 +79,7 @@ Finally, it also adds a notes heading at the end if footnotes are present in the
## Limitations

The tool currently only supports citations in Chicago author-date style.

Only book entries are currently supported (plans to support more types in the future).

Only the following metadata fields are supported:

- author
Expand Down
3 changes: 3 additions & 0 deletions prepyrus_settings.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"ignore_paths": []
}
60 changes: 37 additions & 23 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,40 +2,56 @@
Prepyrus is a tool for verifying and processing MDX files
that contain citations in Chicago author-date style and certain metadata.
⚠️ This tool is still in early development and API may frequently change.
## Usage
Add the crate to your `Cargo.toml` and use it as shown below:
```toml
[dependencies]
prepyrus = "0.1"
prepyrus = "0.2"
```
Main API interface is the `run_prepyrus` function. Example usage:
Main API interface is the `Prepyrus` impl. Example usage:
```rust,ignore
use prepyrus::run_prepyrus;
```rust
use prepyrus::Prepyrus;
fn main() {
let args: Vec<String> = std::env::args().collect();
if args.len() < 4 {
eprintln!(
"Expected more args. Usage: prepyrus <bibliography.bib> <target_dir_or_file> <mode>"
);
std::process::exit(1);
}
if let Err(e) = run_prepyrus(&args[1], &args[2], &args[3]) {
let args = vec![
"_program_index".to_string(),
"tests/mocks/test.bib".to_string(), // bibliography file
"tests/mocks/data".to_string(), // target directory or .mdx file
"verify".to_string(), // mode
"tests/mocks/data/development.mdx".to_string(), // optional ignore paths, separate with commas if multiple
];
let _ = run(args).unwrap_or_else(|e| {
eprintln!("Error: {}", e);
std::process::exit(1);
}
});
println!("===Prepyrus completed successfully!");
}
```
The function takes three arguments: `<bibliography.bib> <target_dir_or_file> <mode>`
- a bibliography file (.bib),
- a target directory or .mdx file,
- and a mode (either `verify` or `process`).
fn run(args: Vec<String>) -> Result<(), Box<dyn std::error::Error>> {
let config = Prepyrus::build_config(&args, None)?;
let all_entries = Prepyrus::get_all_bib_entries(&config.bib_file).unwrap();
let mdx_paths =
Prepyrus::get_mdx_paths(&config.target_path, Some(config.settings.ignore_paths))?;
// Phase 1: Verify MDX files
let articles_file_data = Prepyrus::verify(mdx_paths, &all_entries)?;
// Phase 2: Process MDX files (requires mode to be set to "process")
if config.mode == "process" {
Prepyrus::process(articles_file_data);
}
Ok(())
}
```
`verify` mode only verifies the citations in the MDX files against the bibliography.
Expand All @@ -47,9 +63,7 @@ The function takes three arguments: `<bibliography.bib> <target_dir_or_file> <mo
The tool is designed to work with MDX files that contain citations in Chicago author-date style. Examples:
```markdown
"...nowhere on heaven or on earth is there anything which does not contain both being and nothing in itself" (Hegel 2010, 61).
```
> "...nowhere on heaven or on earth is there anything which does not contain both being and nothing in itself" (Hegel 2010, 61).
The tool parses and verifies the citations in the MDX files against a
bibliography file in BibTeX format (using Biblatex).
Expand Down Expand Up @@ -94,11 +108,11 @@ use validator::ArticleFileData;
pub struct Prepyrus {}

impl Prepyrus {
pub fn verify_config(
pub fn build_config(
args: &Vec<String>,
test_mode: Option<LoadOrCreateSettingsTestMode>,
) -> Result<Config, &'static str> {
Utils::verify_config(args, test_mode)
Utils::build_config(args, test_mode)
}

pub fn get_all_bib_entries(bib_file: &str) -> Result<Vec<biblatex::Entry>, BibliographyError> {
Expand Down
3 changes: 2 additions & 1 deletion src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,9 @@ fn main() {
println!("===Prepyrus completed successfully!");
}

/// Run all the methods of prepyrus
fn run(args: Vec<String>) -> Result<(), Box<dyn std::error::Error>> {
let config = Prepyrus::verify_config(&args, None)?;
let config = Prepyrus::build_config(&args, None)?;
let all_entries = Prepyrus::get_all_bib_entries(&config.bib_file).unwrap();
let mdx_paths =
Prepyrus::get_mdx_paths(&config.target_path, Some(config.settings.ignore_paths))?;
Expand Down
44 changes: 25 additions & 19 deletions src/utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,7 @@ impl BiblatexUtils {
}
}

#[derive(Debug, Serialize, Deserialize)]
pub struct Config {
pub bib_file: String,
pub target_path: String,
Expand Down Expand Up @@ -129,38 +130,47 @@ impl Utils {
Ok(settings)
}

pub fn extract_paths(path: &str, exceptions: Option<Vec<String>>) -> io::Result<Vec<String>> {
let exceptions = exceptions.unwrap_or_else(|| Vec::new());
pub fn extract_paths(path: &str, ignore_paths: Option<Vec<String>>) -> io::Result<Vec<String>> {
let exceptions = ignore_paths.unwrap_or_else(|| Vec::new());
let mdx_paths_raw = Self::extract_mdx_paths(path).unwrap();
let mdx_paths = Self::filter_mdx_paths_for_exceptions(mdx_paths_raw, exceptions);

Ok(mdx_paths)
}

pub fn verify_config(
pub fn build_config(
args: &Vec<String>,
test_mode: Option<LoadOrCreateSettingsTestMode>,
) -> Result<Config, &'static str> {
if args.len() < 3 {
if args.len() < 4 {
return Err("Arguments missing: <bibliography.bib> <target_dir_or_file> <mode>");
}
if !args[0].ends_with(".bib") {
if !args[1].ends_with(".bib") {
return Err("Invalid file format. Please provide a file with .bib extension.");
}
let target_arg = &args[1];
let target_arg = &args[2];
if !Path::new(target_arg).is_dir() && !target_arg.ends_with(".mdx") {
return Err("Invalid target. Please provide a directory or a single MDX file.");
}
if !args[2].eq("verify") && !args[2].eq("process") {
if !args[3].eq("verify") && !args[3].eq("process") {
return Err("Invalid mode. Please provide either 'verify' or 'process'.");
}

let settings = Self::load_or_create_settings("prepyrus_settings.json", test_mode).unwrap();
let settings: Settings;
if args.len() == 5 {
let ignore_parts_vector: Vec<String> =
args[4].split(',').map(|s| s.to_string()).collect();
settings = Settings {
ignore_paths: ignore_parts_vector,
};
} else {
settings = Self::load_or_create_settings("prepyrus_settings.json", test_mode).unwrap();
}

let config = Config {
bib_file: args[0].clone(),
target_path: args[1].clone(),
mode: args[2].clone(),
bib_file: args[1].clone(),
target_path: args[2].clone(),
mode: args[3].clone(),
settings,
};

Expand All @@ -185,9 +195,6 @@ impl Utils {
let path = entry.path();

if path.is_dir() {
if path.file_name() == Some(std::ffi::OsStr::new("contributing")) {
continue; // Skip the "contributing" folder
}
let sub_paths = Self::extract_mdx_paths(path.to_str().unwrap())?;
mdx_paths.extend(sub_paths);
} else if path.is_file() && path.extension() == Some(std::ffi::OsStr::new("mdx")) {
Expand All @@ -207,12 +214,11 @@ impl Utils {
mdx_paths: Vec<String>,
exceptions: Vec<String>,
) -> Vec<String> {
let mut filtered_paths = Vec::new();
for path in mdx_paths {
if !exceptions.contains(&path) {
filtered_paths.push(path);
}
let mut filtered_paths = mdx_paths.clone();
if exceptions.is_empty() {
return filtered_paths;
}
filtered_paths.retain(|path| !exceptions.iter().any(|exception| path.contains(exception)));
filtered_paths
}
}
Expand Down
Loading

0 comments on commit 44d12c2

Please sign in to comment.