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

Allow to escape newline with \ like in markdown #3228

Closed
wants to merge 3 commits into from

Conversation

I60R
Copy link

@I60R I60R commented Dec 28, 2021

Edit: the behavior was changed as I've described here — backslash escapes newline as described in CommonMark spec.


Syn transforms comments into raw string literals before parsing, so \n appears as \\n and propagates in help=... argument. This PR fixes that.

Related to #3198 and #1810.

Probably shouldn't be merged in its current form and it's not clear yet what side-effects it might cause...

@pksunkara
Copy link
Member

Don't think this PR should be merged because of #2389. I would give it an okay if this happens when verbatim_doc_comment is present though.

@I60R
Copy link
Author

I60R commented Dec 28, 2021

It seems that #2389 will add two newlines while this will add only one. I don't see any conflict here.

With verbatim_doc_comment \n will be rendered as \n (verbatim) as we don't need any special symbols to insert newlines, so IMO it has sense only without verbatim_doc_comment.

@I60R
Copy link
Author

I60R commented Dec 28, 2021

Note: this PR doesn't allow to escape slash, so \\n will be rendered as \{newline}

@pksunkara
Copy link
Member

I don't see any conflict here.

There's a conflict because single new lines should be ignored when verbatim_doc_comment is not present as described in that issue. What you are doing in this PR is not ignoring them..

@@ -103,5 +103,5 @@ fn is_blank(s: &str) -> bool {
}

fn merge_lines(lines: &[&str]) -> String {
lines.iter().map(|s| s.trim()).collect::<Vec<_>>().join(" ")
lines.iter().map(|s| s.trim().replace("\\n", "\n")).collect::<Vec<_>>().join(" ")
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Note: this PR doesn't allow to escape slash, so \n will be rendered as {newline}

I'm always a bit cautious of doing our own escaping. We should be matching Rust's rules for escape sequences if we do this.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fixed in next commit: now \\\\ will be rendered as \\ and \\n will be rendered as \n.

Although, I still don't recommend to merge this PR

@I60R I60R changed the title display \n as newline without verbatim_doc_comment Allow to escape newline with \ like in markdown Dec 29, 2021
fn process_paragraph(lines: &[&str]) -> String {
lines
.iter()
.map(|ln| ln.trim())
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe I'm misreading the commonmark spec but it doesn't sound like there can be spaces between \\ and \n which a trim would allow.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Just checked the commonmark preview. one space after leaves a \\. More then one space after is treated as \\\n because of another hard break rule.

} else {
None
}
})
.collect()
}

fn process_paragraph(lines: &[&str]) -> String {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If we go this route, I'd like to see test cases for this to show we are matching expected commonmark behavior

let mut chars = ln.chars();
let mut ln = String::with_capacity(expected_len);
while let Some(c) = chars.next() {
if c == '\\' {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Without code fence support, we could accidentally create a hard break when we shouldn't and people can end up relying on that.

@epage
Copy link
Member

epage commented Nov 8, 2022

Closing out due to conflicts and lack of activity.

@epage epage closed this Nov 8, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

3 participants