-
Notifications
You must be signed in to change notification settings - Fork 1.7k
Recursively apply preprocessor #682
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
Conversation
This seems fairly reasonable. If I'm wanting to include a document it makes sense to also expand any Should we bother to guard against accidental include-cycles? It's certainly an edge case of an edge case, but we'd end up with infinite recursion and a crash due to stack overflow. Which would be kinda annoying. |
I created a book that in chapter_1.md, I {{#include chapter_1.md}} and this was the message:
What this doesn't tell me is what file caused the overflow, so maybe some messaging around it could curtail issues. |
|
81ce2b5
to
b1d6592
Compare
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I really like the error message and stack depth approach taken here. There are only two small knits I could see.
Would you also be able to add a test which contains an include cycle to make sure we don't blow the stack? It should be enough to add a cyclic include to the dummy book in the tests/
directory and then assert the original page's contents are as we expect.
src/preprocess/links.rs
Outdated
} | ||
else { | ||
if let Some(source_path) = source.as_os_str().to_str() { | ||
error!("Stack depth exceeded in {}. Check for cyclic includes", |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I like this error message, it's quite helpful and user friendly.
src/preprocess/links.rs
Outdated
previous_end_index = playpen.end_index; | ||
} | ||
else { | ||
if let Some(source_path) = source.as_os_str().to_str() { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
You can actually remove the need for the if let Some(...)
and two branches by using source.display()
in the error!()
message.
src/preprocess/links.rs
Outdated
@@ -58,8 +59,18 @@ fn replace_all<P: AsRef<Path>>(s: &str, path: P) -> String { | |||
|
|||
match playpen.render_with_path(&path) { | |||
Ok(new_content) => { | |||
replaced.push_str(&new_content); | |||
previous_end_index = playpen.end_index; | |||
if depth < 200 { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This can probably be pulled out into its own private const
(e.g. MAX_NESTED_INCLUDES_DEPTH
) so it's not a magic number.
I highly doubt anyone's going to have more than 2 or 3 nested includes, so we can probably cap it off at 10 instead of 200. It'd be annoying if we actually overflowed the stack before hitting the 200 depth limit.
@@ -0,0 +1,2 @@ | |||
Around the world, around the world |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
So I assume the If I looked at the output from the file vs the output of the file when it was included, they would be different--one would have a broken include and one would do it right. |
Ah I can see how that would happen. As you mentioned, the "current directory" changes when trying to include a link from a different directory. I think how you've done it (new source path = old current directory + new link) is a good solution and should hopefully be intuitive for people. Once the unwraps are swapped with proper error handling I think this PR will be good to merge. I'm assuming hitting one of the unwraps while rendering |
406d786
to
c6fccc0
Compare
Oops, I didn't notice you'd added a commit to resolve those last couple nits. This should be good to merge now. |
Will this make it into v0.1.8? Thanks! |
Sends the content inserted through the links processor.
fixes #678