-
-
Notifications
You must be signed in to change notification settings - Fork 1.1k
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
Preserve newlines in Doc comments #2389 #2401
Preserve newlines in Doc comments #2389 #2401
Conversation
Yeah looks like. You would need to get the CI to pass though. |
Yeah, I'm gonna take care of it tomorrow. Just wanted a double check of the fix before I start working on the tests. |
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.
Looking at the test case you fixed, I remembered why we didn't do it in the first place. Doc comments are generally parsed to only care about newlines when there are \n\n
.
I think we might need a little bit of research and design first here before deciding on the implementation.
Can you please upload a picture of how cargo doc
generates the doc comment the original code had?
Hello,
You mean what the output of
Before with
After with
Is this what you requested? |
No, what |
Okay alright. Edit: Just double checked, and the change has no effect on the |
I think we need some kind of markdown to text converter library. As of right now, this would be a wrong implementation. |
Right now I don't understand why this is a wrong implementation, wouldn't that mean that the code right now is lacking that feature aswell? Because as i said i double checked the current code and the output of |
So, the doc comments are markdown. Which means two lines which are not separated by an empty line should be treated as a single line of text without any The issue now comes up when the lines are in list format I think we also need to take notice of how textwrap dep might interact with these preserved lines. In fact, I am now thinking, this might be a good feature for textwrap itself rather than clap. @mgeisler What do you think? |
I'm not 100% up to speed this feature, but I would be happy to make textwrap useful for things like Markdown. Infact, I happen to have added a function in the last release which can re-wrap blocks of text in some simple cases: As for newlines, textwrap simply preserves them. So println!("{}", textwrap::fill("foo bar baz...\nxxx yyy zzz...", width)); is the same as println!("{}", textwrap::fill("foo bar baz...", width));
println!("{}", textwrap::fill("xxx yyy zzz...", width)); Now, you say that the docstrings are in Markdown format. Is this a requirement because of how they're handed to Clap by the thing that parses the Rust code and extracts the string? I would intuitively have assumed that the string was just passed as-in to Clap. If it is passed as-is, then I would argue that it should not be treated as Markdown since, well, the terminal isn't a Markdown interpreter. However, even if the string is passed literally to Clap, I guess users will run into all sorts of problems with Related, I once made a little demo of word-wrapping Markdown text for the terminal. It sounds like this might do more or less what you ask: mgeisler/textwrap#140 (comment). |
I might reply with more later, but we want to consider the docstrings as markdown when parsed by clap because we want to play nice with |
Let's see some examples:
If we send this to textwrap without removing the newlines:
For lists:
Same issue if we don't remove newlines
For blockquotes:
When wrapped:
And I haven't even gone into lists in blockquotes, nested blockquotes etc.. (We don't need to support them because that's too much for a CLI tool) Ideally, we can convert the markdown into text before sending to textwrap. But the blockquote scenario makes me think that having markdown care in textwrap is better. |
Let me just say that I haven't forgotten about this, I'm just a little busy at the moment so please don't expect a lot of movement on this right now. There might be better and existing solutions of you want full-blown Markdown in the terminal: https://github.com/Canop/termimad I don't know the library yet, but it promise to do more or less what you want. I don't know if it can be used in a simple "here is some Markdown, please format as text" fashion or if it wants full control over the terminal. I would also look at https://github.com/kivikakk/comrak, which is know can reformat Markdown to accommodate for different line widths — again similar to what you're trying to do here. Just some notes for now... My feeling is that textwrap ought to be used by the higher level libraries above, not the other way around (maybe they already do, haven't checked yet). |
I am going to close this PR because this needs more design. |
Hello,
I am no expert. But isn't this already the fix you'd like to have to close #2389 ?
If i run your example code
I get the following output: