Skip to content

Commit

Permalink
derive: feature gate "code-in-doc"
Browse files Browse the repository at this point in the history
  • Loading branch information
Kijewski committed Aug 13, 2024
1 parent 989de93 commit 695bd53
Show file tree
Hide file tree
Showing 7 changed files with 19 additions and 6 deletions.
1 change: 1 addition & 0 deletions rinja/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ maintenance = { status = "actively-developed" }

[features]
default = ["config", "humansize", "num-traits", "urlencode"]
code-in-doc = ["rinja_derive/code-in-doc"]
config = ["rinja_derive/config"]
humansize = ["rinja_derive/humansize", "dep:humansize"]
num-traits = ["rinja_derive/num-traits", "dep:num-traits"]
Expand Down
3 changes: 2 additions & 1 deletion rinja_derive/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ rust-version = "1.71"
proc-macro = true

[features]
code-in-doc = ["dep:pulldown-cmark"]
config = ["dep:serde", "dep:basic-toml"]
humansize = []
urlencode = []
Expand All @@ -33,7 +34,7 @@ mime = "0.3"
mime_guess = "2"
once_map = "0.4.18"
proc-macro2 = "1"
pulldown-cmark = { version = "0.11.0", default-features = false }
pulldown-cmark = { version = "0.11.0", optional = true, default-features = false }
quote = "1"
serde = { version = "1.0", optional = true, features = ["derive"] }
syn = "2.0.3"
Expand Down
8 changes: 7 additions & 1 deletion rinja_derive/src/input.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ use mime::Mime;
use once_map::OnceMap;
use parser::{Node, Parsed};
use proc_macro2::Span;
use pulldown_cmark::{CodeBlockKind, Event, Parser, Tag, TagEnd};
use syn::punctuated::Punctuated;
use syn::spanned::Spanned;

Expand Down Expand Up @@ -406,6 +405,7 @@ impl TemplateArgs {
}
}

#[cfg(feature = "code-in-doc")]
if args.source.is_none() {
args.source = source_from_docs(ast);
}
Expand All @@ -426,6 +426,7 @@ impl TemplateArgs {
}
}

#[cfg(feature = "code-in-doc")]
/// Try to find the souce in the comment, in a "```rinja```" block
///
/// This is only done if no path or source was given in the `#[template]` attribute.
Expand All @@ -436,6 +437,7 @@ fn source_from_docs(ast: &syn::DeriveInput) -> Option<(Source, Option<Span>)> {
Some((source, span))
}

#[cfg(feature = "code-in-doc")]
fn collect_comment_blocks(ast: &syn::DeriveInput) -> Option<(Option<Span>, String)> {
let mut span: Option<Span> = None;
let mut assign_span = |kv: &syn::MetaNameValue| {
Expand Down Expand Up @@ -485,6 +487,7 @@ fn collect_comment_blocks(ast: &syn::DeriveInput) -> Option<(Option<Span>, Strin
Some((span, source))
}

#[cfg(feature = "code-in-doc")]
fn strip_common_ws_prefix(source: String) -> Option<String> {
let mut common_prefix_iter = source
.lines()
Expand Down Expand Up @@ -514,7 +517,10 @@ fn strip_common_ws_prefix(source: String) -> Option<String> {
)
}

#[cfg(feature = "code-in-doc")]
fn collect_rinja_code_blocks(source: String) -> Option<Source> {
use pulldown_cmark::{CodeBlockKind, Event, Parser, Tag, TagEnd};

let mut tmpl_source = String::new();
let mut in_rinja_code = false;
let mut had_rinja_code = false;
Expand Down
4 changes: 3 additions & 1 deletion rinja_derive_standalone/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@ publish = false
[features]
default = ["__standalone"]
__standalone = []

code-in-doc = ["dep:pulldown-cmark"]
config = ["dep:serde", "dep:basic-toml"]
humansize = []
urlencode = []
Expand All @@ -32,7 +34,7 @@ mime = "0.3"
mime_guess = "2"
once_map = "0.4.18"
proc-macro2 = "1"
pulldown-cmark = { version = "0.11.0", default-features = false }
pulldown-cmark = { version = "0.11.0", optional = true, default-features = false }
quote = "1"
serde = { version = "1.0", optional = true, features = ["derive"] }
syn = "2"
Expand Down
5 changes: 3 additions & 2 deletions testing/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -8,15 +8,16 @@ rust-version = "1.71"
publish = false

[features]
default = ["serde_json"]
default = ["code-in-doc", "serde_json"]
code-in-doc = ["rinja/code-in-doc"]
serde_json = ["dep:serde_json", "rinja/serde_json"]

[dependencies]
rinja = { path = "../rinja", version = "0.3.0" }
serde_json = { version = "1.0", optional = true }

[dev-dependencies]
rinja = { path = "../rinja", version = "0.3.0", features = ["serde_json"] }
rinja = { path = "../rinja", version = "0.3.0", features = ["code-in-doc", "serde_json"] }
criterion = "0.5"
phf = { version = "0.11", features = ["macros" ]}
trybuild = "1.0.76"
Expand Down
2 changes: 1 addition & 1 deletion testing/tests/filter_block.rs
Original file line number Diff line number Diff line change
Expand Up @@ -233,7 +233,7 @@ fn filter_block_include() {
<body class=""><h1>Metadata</h1>
99</body>
104</body>
</html>"#
);
}
Expand Down
2 changes: 2 additions & 0 deletions testing/tests/source-in-code.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
#![cfg(feature = "code-in-doc")]

// Regarding `#[rustfmt::skip]`: `cargo fmt` strips extraneous newlines in code block,
// but we want to test if `rinja_derive` works with extraneous newlines.

Expand Down

0 comments on commit 695bd53

Please sign in to comment.