Skip to content

Commit 9be97ae

Browse files
authored
Rollup merge of #129796 - GuillaumeGomez:unify-code-examples, r=notriddle
Unify scraped examples with other code examples Fixes#129763. This first PR both fixes#129763 but also unifies buttons display for code examples: ![image](https://github.com/user-attachments/assets/c8475945-dcc3-4c25-8d7d-1659f85301c8) You can test it [here](https://rustdoc.crud.net/imperio/unify-code-examples/doc/scrape_examples/fn.test.html) and [here](https://rustdoc.crud.net/imperio/unify-code-examples/doc/scrape_examples/fn.test_many.html). I'm planning to send a follow-up to make the buttons generated in JS directly (or I can do it in this PR directly if you prefer). cc ```@willcrichton``` r? ```@notriddle```
2 parents 15e7a67 + 7157f98 commit 9be97ae

14 files changed

Lines changed: 380 additions & 156 deletions

‎src/librustdoc/html/render/mod.rs‎

Lines changed: 9 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -2506,28 +2506,6 @@ fn render_call_locations<W: fmt::Write>(mut w: W, cx: &mut Context<'_>, item: &c
25062506
let needs_expansion = line_max - line_min > NUM_VISIBLE_LINES;
25072507
let locations_encoded = serde_json::to_string(&line_ranges).unwrap();
25082508

2509-
write!(
2510-
&mut w,
2511-
"<div class=\"scraped-example {expanded_cls}\" data-locs=\"{locations}\">\
2512-
<div class=\"scraped-example-title\">\
2513-
{name} (<a href=\"{url}\">{title}</a>)\
2514-
</div>\
2515-
<div class=\"code-wrapper\">",
2516-
expanded_cls = if needs_expansion {""} else {"expanded"},
2517-
name = call_data.display_name,
2518-
url = init_url,
2519-
title = init_title,
2520-
// The locations are encoded as a data attribute, so they can be read
2521-
// later by the JS for interactions.
2522-
locations = Escape(&locations_encoded)
2523-
)
2524-
.unwrap();
2525-
2526-
if line_ranges.len() > 1{
2527-
w.write_str(r#"<button class="prev">&pr;</button> <button class="next">&sc;</button>"#)
2528-
.unwrap();
2529-
}
2530-
25312509
// Look for the example file in the source map if it exists, otherwise return a dummy span
25322510
let file_span = (|| {
25332511
let source_map = tcx.sess.source_map();
@@ -2558,9 +2536,16 @@ fn render_call_locations<W: fmt::Write>(mut w: W, cx: &mut Context<'_>, item: &c
25582536
cx,
25592537
&cx.root_path(),
25602538
highlight::DecorationInfo(decoration_info),
2561-
sources::SourceContext::Embedded{offset: line_min, needs_expansion },
2539+
sources::SourceContext::Embedded(sources::ScrapedInfo{
2540+
needs_prev_next_buttons: line_ranges.len() > 1,
2541+
needs_expansion,
2542+
offset: line_min,
2543+
name:&call_data.display_name,
2544+
url: init_url,
2545+
title: init_title,
2546+
locations: locations_encoded,
2547+
}),
25622548
);
2563-
w.write_str("</div></div>").unwrap();
25642549

25652550
true
25662551
};

‎src/librustdoc/html/sources.rs‎

Lines changed: 38 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -290,9 +290,34 @@ where
290290
}
291291
}
292292

293-
pub(crate)enumSourceContext{
293+
pub(crate)structScrapedInfo<'a>{
294+
pub(crate)offset:usize,
295+
pub(crate)needs_prev_next_buttons:bool,
296+
pub(crate)name:&'astr,
297+
pub(crate)url:&'astr,
298+
pub(crate)title:&'astr,
299+
pub(crate)locations:String,
300+
pub(crate)needs_expansion:bool,
301+
}
302+
303+
#[derive(Template)]
304+
#[template(path = "scraped_source.html")]
305+
structScrapedSource<'a,Code: std::fmt::Display>{
306+
info:ScrapedInfo<'a>,
307+
lines:RangeInclusive<usize>,
308+
code_html:Code,
309+
}
310+
311+
#[derive(Template)]
312+
#[template(path = "source.html")]
313+
structSource<Code: std::fmt::Display>{
314+
lines:RangeInclusive<usize>,
315+
code_html:Code,
316+
}
317+
318+
pub(crate)enumSourceContext<'a>{
294319
Standalone,
295-
Embedded{offset:usize,needs_expansion:bool},
320+
Embedded(ScrapedInfo<'a>),
296321
}
297322

298323
/// Wrapper struct to render the source code of a file. This will do things like
@@ -304,23 +329,8 @@ pub(crate) fn print_src(
304329
context:&Context<'_>,
305330
root_path:&str,
306331
decoration_info: highlight::DecorationInfo,
307-
source_context:SourceContext,
332+
source_context:SourceContext<'_>,
308333
){
309-
#[derive(Template)]
310-
#[template(path = "source.html")]
311-
structSource<Code: std::fmt::Display>{
312-
embedded:bool,
313-
needs_expansion:bool,
314-
lines:RangeInclusive<usize>,
315-
code_html:Code,
316-
}
317-
let lines = s.lines().count();
318-
let(embedded, needs_expansion, lines) = match source_context {
319-
SourceContext::Standalone => (false,false,1..=lines),
320-
SourceContext::Embedded{ offset, needs_expansion } => {
321-
(true, needs_expansion,(1 + offset)..=(lines + offset))
322-
}
323-
};
324334
let current_href = context
325335
.href_from_span(clean::Span::new(file_span),false)
326336
.expect("only local crates should have sources emitted");
@@ -333,5 +343,14 @@ pub(crate) fn print_src(
333343
);
334344
Ok(())
335345
});
336-
Source{ embedded, needs_expansion, lines,code_html: code }.render_into(&mut writer).unwrap();
346+
let lines = s.lines().count();
347+
match source_context {
348+
SourceContext::Standalone => {
349+
Source{lines:(1..=lines),code_html: code }.render_into(&mut writer).unwrap()
350+
}
351+
SourceContext::Embedded(info) => {
352+
let lines = (1 + info.offset)..=(lines + info.offset);
353+
ScrapedSource{ info, lines,code_html: code }.render_into(&mut writer).unwrap();
354+
}
355+
};
337356
}

‎src/librustdoc/html/static/css/noscript.css‎

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,8 @@ nav.sub {
5959
--copy-path-button-color:#999;
6060
--copy-path-img-filter:invert(50%);
6161
--copy-path-img-hover-filter:invert(35%);
62+
--code-example-button-color:#7f7f7f;
63+
--code-example-button-hover-color:#595959;
6264
--codeblock-error-hover-color:rgb(255,0,0);
6365
--codeblock-error-color:rgba(255,0,0,.5);
6466
--codeblock-ignore-hover-color:rgb(255,142,0);
@@ -162,6 +164,8 @@ nav.sub {
162164
--copy-path-button-color:#999;
163165
--copy-path-img-filter:invert(50%);
164166
--copy-path-img-hover-filter:invert(65%);
167+
--code-example-button-color:#7f7f7f;
168+
--code-example-button-hover-color:#a5a5a5;
165169
--codeblock-error-hover-color:rgb(255,0,0);
166170
--codeblock-error-color:rgba(255,0,0,.5);
167171
--codeblock-ignore-hover-color:rgb(255,142,0);

0 commit comments

Comments
 (0)