-
Notifications
You must be signed in to change notification settings - Fork 13.2k
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
GuillaumeGomez
wants to merge
7
commits into
rust-lang:master
Choose a base branch
from
GuillaumeGomez:rustdoc-local-resources-2
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
d23042d
Copy local resources into doc generated folder
GuillaumeGomez 62e7207
Turn MarkdownWithToc into a struct with named fields
GuillaumeGomez 3d28a2d
Integrate LocalResources into markdown generation
GuillaumeGomez 618edd1
Add test for local resources
GuillaumeGomez 6713e68
Update rustdoc-ui test output
GuillaumeGomez 15f3fc6
Add rustdoc UI test for not found local resources
GuillaumeGomez bd298a3
Update librustdoc inner tests
GuillaumeGomez File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -20,6 +20,8 @@ | |
//! edition: Edition::Edition2015, | ||
//! playground: &None, | ||
//! heading_offset: HeadingOffset::H2, | ||
//! depth: 0, | ||
//! local_resources: None, | ||
//! }; | ||
//! let html = md.into_string(); | ||
//! // ... something using html | ||
|
@@ -42,6 +44,7 @@ use std::str; | |
|
||
use crate::clean::RenderedLink; | ||
use crate::doctest; | ||
use crate::formats::cache::LocalResources; | ||
use crate::html::escape::Escape; | ||
use crate::html::format::Buffer; | ||
use crate::html::highlight; | ||
|
@@ -101,21 +104,60 @@ pub struct Markdown<'a> { | |
/// Offset at which we render headings. | ||
/// E.g. if `heading_offset: HeadingOffset::H2`, then `# something` renders an `<h2>`. | ||
pub heading_offset: HeadingOffset, | ||
pub depth: usize, | ||
pub local_resources: Option<&'a LocalResources>, | ||
} | ||
/// A tuple struct like `Markdown` that renders the markdown with a table of contents. | ||
pub(crate) struct MarkdownWithToc<'a>( | ||
pub(crate) &'a str, | ||
pub(crate) &'a mut IdMap, | ||
pub(crate) ErrorCodes, | ||
pub(crate) Edition, | ||
pub(crate) &'a Option<Playground>, | ||
); | ||
pub(crate) struct MarkdownWithToc<'a> { | ||
pub(crate) content: &'a str, | ||
pub(crate) ids: &'a mut IdMap, | ||
pub(crate) error_codes: ErrorCodes, | ||
pub(crate) edition: Edition, | ||
pub(crate) playground: &'a Option<Playground>, | ||
pub(crate) depth: usize, | ||
pub(crate) local_resources: Option<&'a LocalResources>, | ||
} | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Sure! |
||
/// A tuple struct like `Markdown` that renders the markdown escaping HTML tags | ||
/// and includes no paragraph tags. | ||
pub(crate) struct MarkdownItemInfo<'a>(pub(crate) &'a str, pub(crate) &'a mut IdMap); | ||
/// A tuple struct like `Markdown` that renders only the first paragraph. | ||
pub(crate) struct MarkdownSummaryLine<'a>(pub &'a str, pub &'a [RenderedLink]); | ||
|
||
struct LocalResourcesReplacer<'b, I> { | ||
inner: I, | ||
local_resources: Option<&'b LocalResources>, | ||
depth: usize, | ||
} | ||
|
||
impl<'b, I> LocalResourcesReplacer<'b, I> { | ||
fn new(iter: I, local_resources: Option<&'b LocalResources>, depth: usize) -> Self { | ||
Self { inner: iter, local_resources, depth } | ||
} | ||
} | ||
|
||
impl<'a, 'b, I: Iterator<Item = Event<'a>>> Iterator for LocalResourcesReplacer<'b, I> { | ||
type Item = Event<'a>; | ||
|
||
fn next(&mut self) -> Option<Self::Item> { | ||
let event = self.inner.next()?; | ||
// We only modify | ||
if let Event::Start(Tag::Image(type_, ref path, ref title)) = event && | ||
!path.starts_with("http://") && | ||
!path.starts_with("https://") && | ||
let Some(local_resources) = &self.local_resources && | ||
let Some(correspondance) = local_resources.get_at_depth(self.depth, &*path) | ||
{ | ||
Some(Event::Start(Tag::Image( | ||
type_, | ||
CowStr::Boxed(correspondance.clone().into_boxed_str()), | ||
title.clone(), | ||
))) | ||
} else { | ||
Some(event) | ||
} | ||
} | ||
} | ||
|
||
#[derive(Copy, Clone, PartialEq, Debug)] | ||
pub enum ErrorCodes { | ||
Yes, | ||
|
@@ -1017,6 +1059,8 @@ impl Markdown<'_> { | |
edition, | ||
playground, | ||
heading_offset, | ||
depth, | ||
local_resources, | ||
} = self; | ||
|
||
// This is actually common enough to special-case | ||
|
@@ -1038,6 +1082,7 @@ impl Markdown<'_> { | |
let p = HeadingLinks::new(p, None, ids, heading_offset); | ||
let p = Footnotes::new(p); | ||
let p = LinkReplacer::new(p.map(|(ev, _)| ev), links); | ||
let p = LocalResourcesReplacer::new(p, local_resources, depth); | ||
let p = TableWrapper::new(p); | ||
let p = CodeBlocks::new(p, codes, edition, playground); | ||
html::push_html(&mut s, p); | ||
|
@@ -1048,7 +1093,15 @@ impl Markdown<'_> { | |
|
||
impl MarkdownWithToc<'_> { | ||
pub(crate) fn into_string(self) -> String { | ||
let MarkdownWithToc(md, ids, codes, edition, playground) = self; | ||
let MarkdownWithToc { | ||
content: md, | ||
ids, | ||
error_codes: codes, | ||
edition, | ||
playground, | ||
depth, | ||
local_resources, | ||
} = self; | ||
|
||
let p = Parser::new_ext(md, main_body_opts()).into_offset_iter(); | ||
|
||
|
@@ -1059,7 +1112,8 @@ impl MarkdownWithToc<'_> { | |
{ | ||
let p = HeadingLinks::new(p, Some(&mut toc), ids, HeadingOffset::H1); | ||
let p = Footnotes::new(p); | ||
let p = TableWrapper::new(p.map(|(ev, _)| ev)); | ||
let p = LocalResourcesReplacer::new(p.map(|(ev, _)| ev), local_resources, depth); | ||
let p = TableWrapper::new(p); | ||
let p = CodeBlocks::new(p, codes, edition, playground); | ||
html::push_html(&mut s, p); | ||
} | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
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.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.
Absolutely. The RFC mentions that there won't be a crate folder. ;)