Skip to content

Commit

Permalink
Merge pull request #19 from sam0x17/fix-nested-exporting
Browse files Browse the repository at this point in the history
Fix nested exporting #18
  • Loading branch information
sam0x17 authored Oct 27, 2023
2 parents e003e6f + 38016ee commit 27e1c3a
Show file tree
Hide file tree
Showing 3 changed files with 54 additions and 11 deletions.
9 changes: 9 additions & 0 deletions examples/integration.rs
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,15 @@ pub struct ExportContentFn;
#[doc = docify::embed!("examples/samples.rs", MY_CONST)]
pub struct ExportContentConst;

/// This tests that `#[docify::export]` can appear inside of things that have
/// `#[docify::export]` on them.
#[doc = docify::embed!("examples/samples.rs", outer_mod)]
#[doc = docify::embed!("examples/samples.rs", outer_foo)]
#[doc = docify::embed!("examples/samples.rs", inner_mod)]
#[doc = docify::embed!("examples/samples.rs", inner_inner_bar)]
#[doc = docify::embed!("examples/samples.rs", inner_mod2)]
pub struct NestedExports;

/// This will compile all markdown files in the `markdown_source` directory to `markdown_bin`
/// when `cargo doc` is run, handling any doc embed calls as it goes
#[cfg(doc)]
Expand Down
36 changes: 36 additions & 0 deletions examples/samples.rs
Original file line number Diff line number Diff line change
Expand Up @@ -185,3 +185,39 @@ pub fn some_other_fn(x: i32, y: i32) -> Result<i32, i32> {

#[docify::export_content]
const MY_CONST: &'static str = "hello world";

#[docify::export]
pub mod outer_mod {

pub fn hello() {
println!("hello");
}

#[docify::export]
pub fn outer_foo() {
println!("foo!");
}

#[docify::export]
pub mod inner_mod {
const SOMETHING: i32 = 55;

#[docify::export]
pub fn inner_inner_bar() {
println!("bar!");
}

#[docify::export_content]
pub fn inner_inner_fizz() {
println!("fizz!");
}
}

#[docify::export_content]
pub mod inner_mod2 {
#[docify::export]
pub fn inner_inner_wiz() {
println!("wiz!");
}
}
}
20 changes: 9 additions & 11 deletions macros/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -687,7 +687,7 @@ impl<'ast> SupportedVisitItem<'ast> for ItemVisitor {
}
}

#[derive(Copy, Clone, Debug, PartialEq, Eq)]
#[derive(Copy, Clone, PartialEq, Eq)]
enum ResultStyle {
Export,
ExportContent,
Expand Down Expand Up @@ -778,8 +778,11 @@ impl CompressedString {
}
}

static DOCIFY_ATTRIBUTES: Lazy<Regex> =
Lazy::new(|| Regex::new(r"\#\[(?:\w+::)*export(?:\s*\(\s*(\w+)\s*\))?\]").unwrap());
static DOCIFY_ATTRIBUTES: Lazy<Regex> = Lazy::new(|| {
Regex::new(r"\n?\#\[(?:\w+\s*::\s*)*(?:export|export_content)(?:\s*\(\s*(\w+)\s*\))?\]\n?")
.unwrap()
});

static DOC_COMMENT: Lazy<Regex> = Lazy::new(|| Regex::new(r"///.*").unwrap());
static DOC_COMMENT_ATTR: Lazy<Regex> =
Lazy::new(|| Regex::new(r#"#\[doc\s*=\s*".*"\s*]"#).unwrap());
Expand Down Expand Up @@ -905,7 +908,8 @@ fn source_excerpt<'a, T: ToTokens>(
item: &'a T,
style: ResultStyle,
) -> Result<String> {
// note: can't rely on span locations because this requires nightly and/or is otherwise bugged
// note: can't rely on span locations because this requires nightly and/or is otherwise
// bugged
let compressed_source = CompressedString::from(source);
let item_tokens = match style {
ResultStyle::Export => item.to_token_stream(),
Expand All @@ -931,13 +935,7 @@ fn source_excerpt<'a, T: ToTokens>(
let final_excerpt = &source[start_pos..min(end_pos + 1, source.len())];
Ok(final_excerpt
.lines()
.map(|line| {
if DOCIFY_ATTRIBUTES.is_match(line) && !line.trim().starts_with("//") {
"\n"
} else {
line
}
})
.filter(|line| !(DOCIFY_ATTRIBUTES.is_match(line) && !line.trim().starts_with("//")))
.collect::<Vec<&str>>()
.join("\n"))
}
Expand Down

0 comments on commit 27e1c3a

Please sign in to comment.