Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Render diagrams on inner doc-comments (when custom_inner_attribute lands) #5

Open
joseluis opened this issue Jan 24, 2021 · 11 comments
Labels
enhancement New feature or request

Comments

@joseluis
Copy link

Currently is not possible to have diagrams at the inner doc comments, e.g.:

lib.rs:

#[cfg_attr(doc, aquamarine::aquamarine)]
//! demo
//!
//! ```mermaid
//! graph LR
//!     s([Source]) --> a[[aquamarine]]
//!     r[[rustdoc]] --> f([Docs w/ Mermaid!])
//!     subgraph rustc[Rust Compiler]
//!     a -. inject mermaid.js .-> r
//!     end
//! ```

since there can't be anything before the module-level doc-comment block...

@mersinvald
Copy link
Owner

I'm not sure that's even possible to do with attribute macros. Maybe with build.rs.

@mersinvald mersinvald added the enhancement New feature or request label Jan 27, 2021
@zbraniecki
Copy link

Is there a workaround for this issue? I have a use case for a diagram that only makes sense on the entry point of the documentation.

@mersinvald
Copy link
Owner

I did investigate a bit, trying to produce a prototype implementation for generating inner attributes, but it doesn't seem to be possible at all on the current Rust.

custom_inner_attribute isn't stable yet, but that's only half of the problem. Even on nightly compiler cannot handle proc-macro-produced inner attributes at all at this time, and treats them as they were preceded by some non-existent outer attribute.

@zbraniecki the only workaround I can think of is to do what the macro does by hand, but I wouldn't recommend that.

@mersinvald mersinvald changed the title To render diagrams on inner doc-comments To render diagrams on inner doc-comments (when custom_inner_attribute lands) Apr 9, 2021
@mersinvald mersinvald changed the title To render diagrams on inner doc-comments (when custom_inner_attribute lands) Render diagrams on inner doc-comments (when custom_inner_attribute lands) Apr 9, 2021
@mbergkvist
Copy link

Is there a workaround for this issue? I have a use case for a diagram that only makes sense on the entry point of the documentation.

@zbraniecki Did you find a suitable workaround?

@kjvalencik
Copy link

kjvalencik commented Jun 28, 2022

One workaround is that it's possible to document modules in two different places:

  1. At the top of the file with //!
  2. On the pub mod ...; line

The 2nd option works; however, it requires the unstable #![feature(proc_macro_hygiene)] if the module isn't inline. It also only works for modules, not crate level docs. You can either enable that with the doc feature flag or you can define the module internally and then create an inline wrapper for it that re-exports.

#[cfg_attr(doc, feature(proc_macro_hygiene))]

#[cfg_attr(doc, aquamarine::aquamarine)]
/// The configuration module.
///
/// ```mermaid
/// graph LR
///     s([Source]) --> a[[aquamarine]]
///     r[[rustdoc]] --> f([Docs w/ Mermaid!])
///     subgraph rustc[Rust Compiler]
///     a -. inject mermaid.js .-> r
///     end
/// ```
pub mod config;

Or with the re-export workaround:

mod config_internal;

#[cfg_attr(doc, aquamarine::aquamarine)]
/// The configuration module.
///
/// ```mermaid
/// graph LR
///     s([Source]) --> a[[aquamarine]]
///     r[[rustdoc]] --> f([Docs w/ Mermaid!])
///     subgraph rustc[Rust Compiler]
///     a -. inject mermaid.js .-> r
///     end
/// ```
pub mod config {
    pub use crate::config_internal::*;
}

Unfortunately this can be a bit messy if there are a lot of modules because all of their docs live in the same file. I'm not aware of any other workaround.

Edit: I found one more workaround that allow separating the module docs into separate files. I like this one because it doesn't require re-naming modules:

lib.rs

mod config_impl;

pub use config_impl::exports as config;

config_impl.rs

#[cfg_attr(doc, aquamarine::aquamarine)]
/// The configuration module.
///
/// ```mermaid
/// graph LR
///     s([Source]) --> a[[aquamarine]]
///     r[[rustdoc]] --> f([Docs w/ Mermaid!])
///     subgraph rustc[Rust Compiler]
///     a -. inject mermaid.js .-> r
///     end
/// ```
pub mod exports {
    pub use super::*;
}

/// This is a config object
pub struct Config;

The idea is that each public module is actually private and has a public wrapper that provides documentation.

@frehberg
Copy link
Collaborator

frehberg commented Dec 9, 2022

support inner doc would be really nice

@joeyame
Copy link

joeyame commented Oct 7, 2023

For anyone else who needs mermaid in the top level, no compromise, you can check out the crate simple_mermaid: https://github.com/glueball/simple-mermaid

It supports top-level mermaid diagrams with the only disadvantage being needing to make the diagrams separate files. Perhaps this project could adopt a similar approach to allow in-file top level diagrams? For now though, I am going to have to use that crate because it better supports my needs.

@mbergkvist
Copy link

For anyone else who needs mermaid in the top level, no compromise, you can check out the crate simple_mermaid

@joeyame How do you accomplish mermaid in the top level with simple_mermaid?

@joeyame
Copy link

joeyame commented Oct 19, 2023

For anyone else who needs mermaid in the top level, no compromise, you can check out the crate simple_mermaid

@joeyame How do you accomplish mermaid in the top level with simple_mermaid?

Just like this:

//! # Brief Overview 
//! The following diagram is a simple overview of how Parrhesia is used. 
#![ doc=mermaid!( "diagrams/overall_form.mmd" ) ] 

Then, for non - top level, remove the exclamation

@mbergkvist
Copy link

Thanks! I was doing

//! # Brief Overview 
//! The following diagram is a simple overview of how Parrhesia is used. 
#[ doc=mermaid!( "diagrams/overall_form.mmd" ) ] 

i.e. no ! after the # 🤦

@mersinvald
Copy link
Owner

mersinvald commented Dec 14, 2023

@joeyame great little crate, I love the simplicity. Hopefully one day I'll be able to support inner docs too, hehe.

Thanks for pointing out the aquamarine deficiency regarding filesystem-stored diagrams in your readme, btw, I've fixed it in 0.4.0.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

No branches or pull requests

7 participants