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

Add support for image, rules and footnotes #40919

Merged
merged 5 commits into from
Apr 3, 2017

Conversation

GuillaumeGomez
Copy link
Member

Part of #40912.

r? @rust-lang/docs

PS: the footnotes are waiting for pulldown-cmark/pulldown-cmark#21 to be merged to be fully working.

/// [link]: https://example.com "this is a title"
///
/// hard break:
/// after hard break

Choose a reason for hiding this comment

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

I believe this generates a sequence like:

[..., Text("hard break:"), SoftBreak, Text("after hard break"), ...]

To generate a HardBreak in place of SoftBreak, there needs to be at least 2 trailing spaces right before the newline character:

"hard break  \nafter hard break"

ref

Copy link
Member Author

Choose a reason for hiding this comment

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

The issue is even before the markdown parsing, a trim is being done somewhere so the text the parser receives never has lines ending with whitespaces.

Copy link
Member Author

Choose a reason for hiding this comment

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

I added a test for it. Should be fully working now.

Copy link

@critiqjo critiqjo left a comment

Choose a reason for hiding this comment

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

Footnote implementation is incomplete.

let cur_len = parser.footnotes.len() + 1;
parser.footnotes.push(format!("<li id=\"ref{}\">{}<a href=\"#supref{0}\" \
rev=\"footnote\">↩</a></li>",
cur_len, content));

Choose a reason for hiding this comment

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

This is wrong. For example:

foot[^note1], foot[^note2]

[^note1]: text1

[^note2]: text2

Will give the following event sequence:

[
    Start(Paragraph),
    Text("foot"),
    FootnoteReference("note1"),
    Text(", foot"),
    FootnoteReference("note2"),
    End(Paragraph),
    Start(FootnoteDefinition("note1")),
    Start(Paragraph),
    Text("text1"),
    End(Paragraph),
    End(FootnoteDefinition("note1"))
    Start(FootnoteDefinition("note2")),
    Start(Paragraph),
    Text("text2"),
    End(Paragraph),
    End(FootnoteDefinition("note2"))
]

Correct me if I am wrong, but according to this code snippet, both footnote definitions will have the same id cur_len.

Copy link
Member Author

Choose a reason for hiding this comment

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

Oh absolutely! Thanks for notifying it!

Event::FootnoteReference(_) => {
buffer.push_str(&format!("<sup id=\"supref{0}\"><a href=\"#ref{0}\">{0}</a>\
</sup>",
parser.get_next_footnote_id()));

Choose a reason for hiding this comment

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

Footnote ids cannot be this simple either. The same footnote can be referenced from multiple places. So you can't simply keep incrementing the id whenever a footnote reference is found. You'd need to use a map.

Copy link
Member Author

Choose a reason for hiding this comment

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

Indeed, I'll update to this.

id);
let cur_len = parser.footnotes.len() + 1;
parser.footnotes.push(format!("<li id=\"ref{}\">{}<a href=\"#supref{0}\" \
rev=\"footnote\">↩</a></li>",

Choose a reason for hiding this comment

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

Where is parent ul generated?

Copy link
Member Author

Choose a reason for hiding this comment

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

At the end.

&content[..content.len() - 4]
} else {
&content
}));

Choose a reason for hiding this comment

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

There's one more issue here, though it would be very rarely encountered, if at all. When there are multiple footnote references, there needs to be multiple backreferences from the definition too, to be 100% proper. (For a nice reference implementation, see Wikipedia.)

Copy link
Member Author

Choose a reason for hiding this comment

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

Well, for now let's keep it aside. This PR is already big enough.

Copy link
Member Author

Choose a reason for hiding this comment

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

(But it's a great suggestion!)

Copy link
Member

Choose a reason for hiding this comment

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

Yeah, the current footnote implementation does this. I think @GuillaumeGomez is right though, we can add that back in in another PR; it's not as big of a deal as this stuff is.

@GuillaumeGomez
Copy link
Member Author

cc @alexcrichton @brson

Copy link
Member

@steveklabnik steveklabnik left a comment

Choose a reason for hiding this comment

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

I think this looks good. Let's see what @frewsxcv says.

@@ -115,15 +116,19 @@ macro_rules! event_loop_break {
match event {
$($end_event)|* => break,
Event::Text(ref s) => {
debug!("Text");
Copy link
Member

Choose a reason for hiding this comment

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

was this meant to be left in?

Copy link
Member

Choose a reason for hiding this comment

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

I'm not opposed to keeping the debugging statements, but it'd be nicer if there were a little more descriptive, maybe something like debug!("markdown parser encountered 'Text'"), whatever works though.

Event::SoftBreak | Event::HardBreak if !$buf.is_empty() => {
$buf.push(' ');
Event::SoftBreak => {
debug!("SoftBreak");
Copy link
Member

Choose a reason for hiding this comment

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

same here

pub fn render(w: &mut fmt::Formatter,
s: &str,
print_toc: bool,
shorter: MarkdownOutputStyle) -> fmt::Result {
fn code_block(parser: &mut Parser, buffer: &mut String, lang: &str) {
fn code_block(parser: &mut ParserWrapper, buffer: &mut String, lang: &str) {
debug!("CodeBlock");
Copy link
Member

Choose a reason for hiding this comment

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

I won't comment on all of these 😄

&content[..content.len() - 4]
} else {
&content
}));
Copy link
Member

Choose a reason for hiding this comment

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

Yeah, the current footnote implementation does this. I think @GuillaumeGomez is right though, we can add that back in in another PR; it's not as big of a deal as this stuff is.


// @has foo/fn.f.html
// @has - '<p>hard break:<br>after hard break</p>'
/// hard break:
Copy link
Member

Choose a reason for hiding this comment

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

just a review note; this works because there are the two trailing spaces. I was confused at first 😄

Copy link
Member Author

Choose a reason for hiding this comment

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

That's the point of hard break. :p

Copy link
Member

Choose a reason for hiding this comment

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

Yeah, I just meant it's hard to see in this diff.

@GuillaumeGomez
Copy link
Member Author

This PR is now ready.

@frewsxcv
Copy link
Member

frewsxcv commented Apr 1, 2017

Looks great! r=me

Not going to r+ right now in case you want to change/remove the debug logging statements.

@GuillaumeGomez
Copy link
Member Author

As I said to @steveklabnik, it'll certainly be helpful in a close future. Once this is all done, we can remove them.

@bors: r+

@bors
Copy link
Contributor

bors commented Apr 2, 2017

📌 Commit ef01ae7 has been approved by GuillaumeGomez

@frewsxcv
Copy link
Member

frewsxcv commented Apr 2, 2017

@bors r=frewsxcv,steveklabnik

@bors
Copy link
Contributor

bors commented Apr 2, 2017

💡 This pull request was already approved, no need to approve it again.

@bors
Copy link
Contributor

bors commented Apr 2, 2017

📌 Commit ef01ae7 has been approved by frewsxcv,steveklabnik

@bors
Copy link
Contributor

bors commented Apr 2, 2017

⌛ Testing commit ef01ae7 with merge 72ecd79...

bors added a commit that referenced this pull request Apr 2, 2017
…veklabnik

Add support for image, rules and footnotes

Part of #40912.

r? @rust-lang/docs

PS: the footnotes are waiting for pulldown-cmark/pulldown-cmark#21 to be merged to be fully working.
@bors
Copy link
Contributor

bors commented Apr 3, 2017

☀️ Test successful - status-appveyor, status-travis
Approved by: frewsxcv,steveklabnik
Pushing 72ecd79 to master...

@bors bors merged commit ef01ae7 into rust-lang:master Apr 3, 2017
@GuillaumeGomez GuillaumeGomez deleted the fix-new-rustdoc branch April 3, 2017 14:15
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.

5 participants