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

rustdoc: Add support for local resources #107640

Open
wants to merge 7 commits into
base: master
Choose a base branch
from

Conversation

GuillaumeGomez
Copy link
Member

@GuillaumeGomez GuillaumeGomez commented Feb 3, 2023

Fixes #32104.

This PR adds the support for "local resources". What it means exactly: if a local item is referenced in the markdown, rustdoc will copy it into {LOCAL_RESOURCES_FOLDER_NAME}/{crate_name} and then refer to it.

To do so, I originally tried to do it directly when we generated the markdown, but I ended with a pretty bad code. You can see this (unfinished) version here.

So how is it implemented? First, we go through a new pass which will gather the local resources from the markdown and store them into two maps: one map to keep the correspondance between the item and its "local location", and the other contains all the stored items to prevent duplications. This second is also used to copy the files into the static folder once everything has been generated.

Then, when we generate the markdown content, we replace the links to these items if we can find them in the correspondance map.

Best review it commit by commit.

RFC: rust-lang/rfcs#3397
r? @notriddle

@rustbot rustbot added S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. T-rustdoc Relevant to the rustdoc team, which will review and decide on the PR/issue. labels Feb 3, 2023
@GuillaumeGomez
Copy link
Member Author

Forgot to update librustdoc inner tests. Should be solved now.

Copy link
Contributor

@notriddle notriddle left a comment

Choose a reason for hiding this comment

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

This should also go through an RFC, since it's adding a new API to rustdoc (like intra-doc links did).

self.cx.tcx.sess.struct_span_err(
item.attr_span(self.cx.tcx),
&format!("`{ori_path}`: No such file"),
).emit();
Copy link
Contributor

Choose a reason for hiding this comment

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

This should probably emit a warning, at least for awhile, because someone could've made a home-grown build system that already did the resource copying.

Copy link
Member Author

Choose a reason for hiding this comment

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

Just a warning forever should be enough indeed.

if !path.is_file() {
self.cx.tcx.sess.struct_span_err(
item.attr_span(self.cx.tcx),
&format!("`{ori_path}`: No such file (expanded into `{}`)", path.display()),
Copy link
Contributor

Choose a reason for hiding this comment

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

This should probably emit a warning, at least for awhile, because someone could've made a home-grown build system that already did the resource copying.

pub(crate) error_codes: ErrorCodes,
pub(crate) edition: Edition,
pub(crate) playground: &'a Option<Playground>,
}
Copy link
Contributor

Choose a reason for hiding this comment

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

Because I'm afraid that this PR might take awhile to approve, can you please separate 62e7207 into its own PR?

Copy link
Member Author

Choose a reason for hiding this comment

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

Sure!


// @has local_resources/foo/0.svg
// @has foo/struct.Enum.html
// @has - '//img[@src="../local_resources/foo/0.svg"]' ''
Copy link
Contributor

Choose a reason for hiding this comment

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

Can we please change this so that it attempts to preserve, at least partially, the original filename?

  • Pure numbers are annoying for anyone who wants to download the file (right-click, Save As) or image search engines that use the filename to guess at the subject matter.
  • Even if everything is sorted so that the numbering is technically deterministic, it still means minor changes to the docs can cause images to swap filenames. This will be very confusing when these URLs are shared elsewhere.
  • It would be better if this filename had a file hash in it, and local_resources ought to be named local.files, for consistency with static files and so that the CDN can be configured the same way.

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 problem with keeping the original filename is that we can potentially end up with multiple files with the same name in the static files folder. I suppose it'll be debated in the RFC.

Copy link
Contributor

Choose a reason for hiding this comment

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

Add a hash to the file name. If two files have the same name and the same hash, then that’s not actually a problem.

Copy link
Member Author

Choose a reason for hiding this comment

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

Currently the key is the absolute file path, which is how we know if a file was already linked to.

// @has - '//img[@src="../../local_resources/foo/0.svg"]' ''
/// test!
///
/// ![yep](../../src/librustdoc/html/static/images/rust-logo.svg)
Copy link
Contributor

Choose a reason for hiding this comment

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

Please also add a test case that combines this feature with cross-crate inlining?

matthiaskrgr added a commit to matthiaskrgr/rust that referenced this pull request Feb 6, 2023
…c-into-struct, r=notriddle

Turn MarkdownWithToc into a struct with named fields

Extracted the commit from rust-lang#107640.

r? `@notriddle`
@bors
Copy link
Contributor

bors commented Feb 7, 2023

☔ The latest upstream changes (presumably #107738) made this pull request unmergeable. Please resolve the merge conflicts.

Comment on lines +526 to +527
/// This will be used when generating the HTML, once everything is generated, we copy these
/// files into the static folder.
Copy link
Contributor

Choose a reason for hiding this comment

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

We shouldn't put per-crate files into static.files/, which is used for toolchain-specific static files. If we go this route, there should be a separate directory for invocation-specific files.

Copy link
Member Author

Choose a reason for hiding this comment

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

Absolutely. The RFC mentions that there won't be a crate folder. ;)

@GuillaumeGomez GuillaumeGomez added the needs-rfc This change is large or controversial enough that it should have an RFC accepted before doing it. label Feb 14, 2023
@GuillaumeGomez
Copy link
Member Author

Opened RFC: rust-lang/rfcs#3397

@anden3 anden3 added S-waiting-on-author Status: This is awaiting some action (such as code changes or more information) from the author. and removed S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. labels May 14, 2023
@anden3
Copy link
Contributor

anden3 commented May 14, 2023

Hello @GuillaumeGomez! Noticed this PR has some merge conflicts, other than that it looks like it's waiting on the RFC :)

@GuillaumeGomez GuillaumeGomez added the S-blocked Status: Blocked on something else such as an RFC or other implementation work. label May 14, 2023
@GuillaumeGomez
Copy link
Member Author

I added the blocked label.

@Dylan-DPC Dylan-DPC removed the S-waiting-on-author Status: This is awaiting some action (such as code changes or more information) from the author. label May 16, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
needs-rfc This change is large or controversial enough that it should have an RFC accepted before doing it. S-blocked Status: Blocked on something else such as an RFC or other implementation work. T-rustdoc Relevant to the rustdoc team, which will review and decide on the PR/issue.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Include images in rustdoc output
7 participants