Skip to content

Commit

Permalink
separate examples in chunks
Browse files Browse the repository at this point in the history
  • Loading branch information
daniellga committed May 3, 2024
1 parent c62a8bd commit aaec9c3
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 16 deletions.
2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "rdocs"
version = "0.1.41"
version = "0.1.42"
edition = "2021"
repository = "https://github.com/daniellga/rdocs/"

Expand Down
2 changes: 1 addition & 1 deletion rdocs/DESCRIPTION
Original file line number Diff line number Diff line change
@@ -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", , "[email protected]", role = c("aut", "cre"))
Description: Generate R documentation in Quarto format based on comments in code files.
Expand Down
31 changes: 17 additions & 14 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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());
Expand Down Expand Up @@ -202,19 +202,22 @@ fn eval_examples(mut examples: Vec<String>) {
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
);
}
}
}

Expand Down

0 comments on commit aaec9c3

Please sign in to comment.