From aaec9c3fe14eb3a35ec0908a16beaea3f839e40a Mon Sep 17 00:00:00 2001 From: daniellga Date: Thu, 2 May 2024 22:27:07 -0300 Subject: [PATCH] separate examples in chunks --- Cargo.toml | 2 +- rdocs/DESCRIPTION | 2 +- src/main.rs | 31 +++++++++++++++++-------------- 3 files changed, 19 insertions(+), 16 deletions(-) diff --git a/Cargo.toml b/Cargo.toml index 0792753..4a9ea53 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "rdocs" -version = "0.1.41" +version = "0.1.42" edition = "2021" repository = "https://github.com/daniellga/rdocs/" diff --git a/rdocs/DESCRIPTION b/rdocs/DESCRIPTION index a4f33b3..896e025 100644 --- a/rdocs/DESCRIPTION +++ b/rdocs/DESCRIPTION @@ -1,6 +1,6 @@ Package: rdocs Title: Create Quarto documentation for R files from comments -Version: 0.1.41 +Version: 0.1.42 Authors@R: person("Daniel", "Gurgel", , "daniellga@gmail.com", role = c("aut", "cre")) Description: Generate R documentation in Quarto format based on comments in code files. diff --git a/src/main.rs b/src/main.rs index 34ffb04..6eaa605 100644 --- a/src/main.rs +++ b/src/main.rs @@ -105,7 +105,7 @@ fn generate_r_docs( // associate with key in first line of comment chunk. Keys are identifiable by a 1 word line. if !last_line_was_comment { - key = filtered_line.clone(); + key.clone_from(&filtered_line); // key should have only one word if key.contains(' ') { skip_comment_chunk = true; @@ -119,7 +119,7 @@ fn generate_r_docs( if inside_code_chunk { if filtered_line_trimmed == "```" { - examples.push("rm(list = ls())".to_string()); + examples.push("***end_of_example".to_string()); inside_code_chunk = false; } else { examples.push(filtered_line.clone()); @@ -202,19 +202,22 @@ fn eval_examples(mut examples: Vec) { examples.retain(|s| !s.is_empty()); let output_text = examples.join(";"); - let output = Command::new("Rscript") - .args(["--vanilla", "-e", output_text.as_str()]) - .stdout(Stdio::null()) - .stderr(Stdio::piped()) - .output() - .expect("Failed to execute Rscript."); + // Iterate for each example chunk in the file. + for example in output_text.split("***end_of_example;") { + let output = Command::new("Rscript") + .args(["--vanilla", "-e", example]) + .stdout(Stdio::null()) + .stderr(Stdio::piped()) + .output() + .expect("Failed to execute Rscript."); - if !output.status.success() { - let error_message = String::from_utf8_lossy(&output.stderr); - panic!( - "Error running example:\n\n{}\n\n**********\n\nR code executed:\n\n{}", - error_message, output_text - ); + if !output.status.success() { + let error_message = String::from_utf8_lossy(&output.stderr); + panic!( + "Error running example:\n\n{}\n\n**********\n\nR code executed:\n\n{}", + error_message, output_text + ); + } } }