From 9c39b5e2528fb7f76a6ae0dd4df9049143f769ae Mon Sep 17 00:00:00 2001 From: Iulian Barbu Date: Fri, 1 Nov 2024 21:05:51 +0200 Subject: [PATCH 1/5] fix: docify embed adds ignore lang in md files Signed-off-by: Iulian Barbu --- macros/src/lib.rs | 56 ++++++++++++++++++++++++++++++++++++----------- 1 file changed, 43 insertions(+), 13 deletions(-) diff --git a/macros/src/lib.rs b/macros/src/lib.rs index f50ac8c..a8b2ae2 100644 --- a/macros/src/lib.rs +++ b/macros/src/lib.rs @@ -10,6 +10,7 @@ use regex::Regex; use std::{ cmp::min, collections::HashMap, + fmt::Display, fs::{self, OpenOptions}, io::Write, path::{Path, PathBuf}, @@ -521,7 +522,10 @@ fn export_internal( /// Output should match `rustfmt` output exactly. #[proc_macro] pub fn embed(tokens: TokenStream) -> TokenStream { - match embed_internal(tokens, MarkdownLanguage::Ignore) { + match embed_internal( + tokens, + vec![MarkdownLanguage::Rust, MarkdownLanguage::Ignore], + ) { Ok(tokens) => tokens.into(), Err(err) => err.to_compile_error().into(), } @@ -534,7 +538,7 @@ pub fn embed(tokens: TokenStream) -> TokenStream { /// [`docify::embed!(..)`](`macro@embed`) also apply to this macro. #[proc_macro] pub fn embed_run(tokens: TokenStream) -> TokenStream { - match embed_internal(tokens, MarkdownLanguage::Blank) { + match embed_internal(tokens, vec![MarkdownLanguage::Blank]) { Ok(tokens) => tokens.into(), Err(err) => err.to_compile_error().into(), } @@ -591,17 +595,34 @@ enum MarkdownLanguage { Blank, } +impl Display for MarkdownLanguage { + fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { + match self { + MarkdownLanguage::Ignore => write!(f, "{}", "ignore"), + MarkdownLanguage::Rust => write!(f, "{}", "rust"), + MarkdownLanguage::Blank => write(f, "", ""), + } + } +} + /// Converts a source string to a codeblocks wrapped example -fn into_example(st: &str, lang: MarkdownLanguage) -> String { +fn into_example(st: &str, langs: Vec) -> String { let mut lines: Vec = Vec::new(); - match lang { - MarkdownLanguage::Ignore => lines.push(String::from("```ignore")), - MarkdownLanguage::Rust => lines.push(String::from("```rust")), - MarkdownLanguage::Blank => lines.push(String::from("```")), + + // Add the markdown languages (can be more than one, or none) + lines.push(String::from("```")); + let mut langs_iter = langs.iter(); + langs_iter + .next() + .map(|first_lang| lines.push(first_lang.to_string())); + for lang in langs_iter { + lines.push(format!(",{}", lang.to_string())); } + for line in st.lines() { lines.push(String::from(line)); } + lines.push(String::from("```")); lines.join("\n") } @@ -941,7 +962,10 @@ fn source_excerpt<'a, T: ToTokens>( } /// Inner version of [`embed_internal`] that just returns the result as a [`String`]. -fn embed_internal_str(tokens: impl Into, lang: MarkdownLanguage) -> Result { +fn embed_internal_str( + tokens: impl Into, + langs: Vec, +) -> Result { let args = parse2::(tokens.into())?; // return blank result if we can't properly resolve `caller_crate_root` let Some(root) = caller_crate_root() else { @@ -983,19 +1007,22 @@ fn embed_internal_str(tokens: impl Into, lang: MarkdownLanguage) - for (item, style) in visitor.results { let excerpt = source_excerpt(&source_code, &item, style)?; let formatted = fix_indentation(excerpt); - let example = into_example(formatted.as_str(), lang); + let example = into_example(formatted.as_str(), langs); results.push(example); } results.join("\n") } else { - into_example(source_code.as_str(), lang) + into_example(source_code.as_str(), langs) }; Ok(output) } /// Internal implementation behind [`macro@embed`]. -fn embed_internal(tokens: impl Into, lang: MarkdownLanguage) -> Result { - let output = embed_internal_str(tokens, lang)?; +fn embed_internal( + tokens: impl Into, + langs: Vec, +) -> Result { + let output = embed_internal_str(tokens, langs)?; Ok(quote!(#output)) } @@ -1211,7 +1238,10 @@ fn compile_markdown_source>(source: S) -> Result { let comment = &orig_comment[4..(orig_comment.len() - 3)].trim(); if comment.starts_with("docify") { let args = parse2::(comment.parse()?)?.args; - let compiled = embed_internal_str(args.to_token_stream(), MarkdownLanguage::Rust)?; + let compiled = embed_internal_str( + args.to_token_stream(), + vec![MarkdownLanguage::Rust, MarkdownLanguage::Ignore], + )?; output.push(compiled); } else { output.push(String::from(orig_comment)); From b4f346ccef7326f89ca382a70c69de50e069e70d Mon Sep 17 00:00:00 2001 From: Iulian Barbu Date: Fri, 1 Nov 2024 21:16:22 +0200 Subject: [PATCH 2/5] fixed compilation Signed-off-by: Iulian Barbu --- macros/src/lib.rs | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/macros/src/lib.rs b/macros/src/lib.rs index a8b2ae2..8cd9782 100644 --- a/macros/src/lib.rs +++ b/macros/src/lib.rs @@ -600,16 +600,16 @@ impl Display for MarkdownLanguage { match self { MarkdownLanguage::Ignore => write!(f, "{}", "ignore"), MarkdownLanguage::Rust => write!(f, "{}", "rust"), - MarkdownLanguage::Blank => write(f, "", ""), + MarkdownLanguage::Blank => write!(f, "{}", ""), } } } /// Converts a source string to a codeblocks wrapped example -fn into_example(st: &str, langs: Vec) -> String { +fn into_example(st: &str, langs: &Vec) -> String { let mut lines: Vec = Vec::new(); - // Add the markdown languages (can be more than one, or none) + lines.push(String::from("```")); let mut langs_iter = langs.iter(); langs_iter @@ -1007,12 +1007,12 @@ fn embed_internal_str( for (item, style) in visitor.results { let excerpt = source_excerpt(&source_code, &item, style)?; let formatted = fix_indentation(excerpt); - let example = into_example(formatted.as_str(), langs); + let example = into_example(formatted.as_str(), &langs); results.push(example); } results.join("\n") } else { - into_example(source_code.as_str(), langs) + into_example(source_code.as_str(), &langs) }; Ok(output) } From fdef70369d306bac8520c917732fb61874cb72ab Mon Sep 17 00:00:00 2001 From: Iulian Barbu Date: Fri, 1 Nov 2024 21:36:39 +0200 Subject: [PATCH 3/5] fixed lang line creation Signed-off-by: Iulian Barbu --- macros/src/lib.rs | 20 +++++++++++--------- 1 file changed, 11 insertions(+), 9 deletions(-) diff --git a/macros/src/lib.rs b/macros/src/lib.rs index 8cd9782..16731f2 100644 --- a/macros/src/lib.rs +++ b/macros/src/lib.rs @@ -608,16 +608,18 @@ impl Display for MarkdownLanguage { /// Converts a source string to a codeblocks wrapped example fn into_example(st: &str, langs: &Vec) -> String { let mut lines: Vec = Vec::new(); - // Add the markdown languages (can be more than one, or none) - lines.push(String::from("```")); - let mut langs_iter = langs.iter(); - langs_iter - .next() - .map(|first_lang| lines.push(first_lang.to_string())); - for lang in langs_iter { - lines.push(format!(",{}", lang.to_string())); - } + // Add the markdown languages (can be more than one, or none) + let mut lang_line = String::from("```"); + lang_line.push_str( + langs + .iter() + .map(|lang| lang.to_string()) + .collect::>() + .join(",") + .as_str(), + ); + lines.push(lang_line); for line in st.lines() { lines.push(String::from(line)); From ed3a62d8a41141855d5b24853ad235463e4d64b6 Mon Sep 17 00:00:00 2001 From: Iulian Barbu Date: Fri, 1 Nov 2024 22:03:08 +0200 Subject: [PATCH 4/5] update readme Signed-off-by: Iulian Barbu --- README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/README.md b/README.md index de809d9..1ace8d8 100644 --- a/README.md +++ b/README.md @@ -83,7 +83,7 @@ of files or individual files. In fact, this `README.md` file is automatically compiled whenever `cargo doc` is run on this crate, resulting in the following codeblock to populate dynamically: -```rust +```rust,ignore fn some_example() { assert_eq!(2 + 2, 4); assert_eq!(2 + 3, 5); From 19edb6669d6b776bfc0ee9b8a2646433d4eb72e5 Mon Sep 17 00:00:00 2001 From: Iulian Barbu Date: Sun, 3 Nov 2024 00:21:23 +0200 Subject: [PATCH 5/5] fix tests Signed-off-by: Iulian Barbu --- macros/src/tests.rs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/macros/src/tests.rs b/macros/src/tests.rs index d7b7029..9b89c1f 100644 --- a/macros/src/tests.rs +++ b/macros/src/tests.rs @@ -66,7 +66,7 @@ fn test_compile_markdown_valid() { .to_string(), "\"# This is a markdown file\\n\\n```rust\\nstruct \ Something;\\n```\\n\\n\\n`\ - ``rust\\nfn some_fn() {\\n println!(\\\"foo\\\");\ + ``rust,ignore\\nfn some_fn() {\\n println!(\\\"foo\\\");\ \\n}\\n```\\n\\nSome text this is some text\\n\"" ); } @@ -96,7 +96,7 @@ fn test_compile_markdown_source_valid() { "this is some markdown\n\ this is some more markdown\n\ # this is a title\n\ - ```rust\n\ + ```rust,ignore\n\ fn some_fn() {\n \ println!(\"foo\");\n\ }\n\