Skip to content

Commit 75ba20e

Browse files
Add expand feature for code blocks
1 parent 08310a5 commit 75ba20e

File tree

11 files changed

+213
-154
lines changed

11 files changed

+213
-154
lines changed

src/librustdoc/html/highlight.rs

+34-25
Original file line numberDiff line numberDiff line change
@@ -82,8 +82,8 @@ crate fn render_with_highlighting<'a>(
8282
}
8383

8484
write_header(out, class, extra_content);
85-
write_code(out, src, edition, context_info);
86-
write_footer(out, playground_button);
85+
let expand = write_code(out, src, edition, context_info);
86+
write_footer(out, playground_button, expand);
8787
}
8888

8989
fn write_header(out: &mut Buffer, class: Option<&str>, extra_content: Option<Buffer>) {
@@ -115,46 +115,55 @@ fn write_code(
115115
src: impl Iterator<Item = Line<'a>>,
116116
edition: Edition,
117117
context_info: Option<ContextInfo<'_, '_, '_>>,
118-
) {
118+
) -> bool {
119119
let mut iter = src.peekable();
120+
let mut expand = false;
120121

121122
// For each `Line`, we replace DOS backlines with '\n'. This replace allows to fix how the code
122123
// source with DOS backline characters is displayed.
123124
while let Some(line) = iter.next() {
124-
match line {
125+
let (before, text, after) = match line {
125126
Line::Hidden(text) => {
126-
write!(
127-
out,
128-
"<span class=\"hidden\">{}{}</span>",
129-
Escape(&text.replace("\r\n", "\n")),
130-
if iter.peek().is_some() && !text.ends_with('\n') { "\n" } else { "" },
131-
);
132-
}
133-
Line::Shown(text) => {
134-
Classifier::new(&text.replace("\r\n", "\n"), edition, context_info.as_ref().map(|c| c.file_span).unwrap_or(DUMMY_SP)).highlight(&mut |highlight| {
135-
match highlight {
136-
Highlight::Token { text, class } => string(out, Escape(text), class),
137-
Highlight::EnterSpan { class } => enter_span(out, class),
138-
Highlight::ExitSpan => exit_span(out),
139-
};
140-
});
141-
if iter.peek().is_some() && !text.ends_with('\n') {
142-
write!(out, "\n");
143-
}
127+
expand = true;
128+
("<span class=\"hidden\">", text.replace("\r\n", "\n"), "</span>")
144129
}
130+
Line::Shown(text) => ("", text.replace("\r\n", "\n"), ""),
131+
};
132+
if !before.is_empty() {
133+
out.push_str(before);
134+
}
135+
Classifier::new(
136+
&text.replace("\r\n", "\n"),
137+
edition,
138+
context_info.as_ref().map(|c| c.file_span).unwrap_or(DUMMY_SP),
139+
)
140+
.highlight(&mut |highlight| {
141+
match highlight {
142+
Highlight::Token { text, class } => string(out, Escape(text), class, context_info),
143+
Highlight::EnterSpan { class } => enter_span(out, class),
144+
Highlight::ExitSpan => exit_span(out),
145+
};
146+
});
147+
if iter.peek().is_some() && !text.ends_with('\n') {
148+
out.push_str("\n");
149+
}
150+
if !after.is_empty() {
151+
out.push_str(after);
145152
}
146153
}
154+
expand
147155
}
148156

149-
fn write_footer(out: &mut Buffer, playground_button: Option<&str>) {
157+
fn write_footer(out: &mut Buffer, playground_button: Option<&str>, expand: bool) {
150158
writeln!(
151159
out,
152160
"</code></pre>\
153161
<div class=\"code-buttons\">\
154-
{}<button class=\"copy-code\" onclick=\"copyCode(this)\"></button>\
162+
{}{}<button class=\"copy-code\" onclick=\"copyCode(this)\"></button>\
155163
</div>\
156164
</div>",
157-
playground_button.unwrap_or_default()
165+
playground_button.unwrap_or_default(),
166+
if expand { "<button class=\"expand\" onclick=\"expandCode(this)\"></button>" } else { "" },
158167
);
159168
}
160169

src/librustdoc/html/render/write_shared.rs

+9
Original file line numberDiff line numberDiff line change
@@ -179,6 +179,7 @@ pub(super) fn write_shared(
179179
cx.write_shared(SharedResource::InvocationSpecific { basename: p }, content, &options.emit)
180180
};
181181

182+
// This is required because the name of the image changes based on the current toolchain.
182183
fn add_background_image_to_css(
183184
cx: &Context<'_>,
184185
css: &mut String,
@@ -218,6 +219,12 @@ pub(super) fn write_shared(
218219

219220
// Add all the static files. These may already exist, but we just
220221
// overwrite them anyway to make sure that they're fresh and up-to-date.
222+
let mut rustdoc_css = static_files::RUSTDOC_CSS.to_owned();
223+
add_background_image_to_css(cx, &mut rustdoc_css, ".copy-code", "clipboard.svg");
224+
add_background_image_to_css(cx, &mut rustdoc_css, ".expand", "eye.svg");
225+
add_background_image_to_css(cx, &mut rustdoc_css, ".collapse", "eye-slash.svg");
226+
write_minify("rustdoc.css", rustdoc_css, cx, options)?;
227+
221228
write_minify("settings.css", static_files::SETTINGS_CSS, cx, options)?;
222229
write_minify("noscript.css", static_files::NOSCRIPT_CSS, cx, options)?;
223230

@@ -259,6 +266,8 @@ pub(super) fn write_shared(
259266
write_toolchain("down-arrow.svg", static_files::DOWN_ARROW_SVG)?;
260267
write_toolchain("toggle-minus.svg", static_files::TOGGLE_MINUS_PNG)?;
261268
write_toolchain("toggle-plus.svg", static_files::TOGGLE_PLUS_PNG)?;
269+
write_toolchain("eye.svg", static_files::EXPAND_SVG)?;
270+
write_toolchain("eye-slash.svg", static_files::COLLAPSE_SVG)?;
262271

263272
let mut themes: Vec<&String> = themes.iter().collect();
264273
themes.sort();

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

+1-1
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ rules.
99
margin-left: 0 !important;
1010
}
1111

12-
#copy-path, .copy-code {
12+
#copy-path, .copy-code, .expand, .collapse {
1313
/* It requires JS to work so no need to display it in this case. */
1414
display: none;
1515
}

src/librustdoc/html/static/css/rustdoc.css

+1-2
Original file line numberDiff line numberDiff line change
@@ -1056,8 +1056,7 @@ a.test-arrow {
10561056
a.test-arrow:hover{
10571057
text-decoration: none;
10581058
}
1059-
.copy-code {
1060-
background-image: url(clipboard1.55.0.svg);
1059+
.copy-code, .expand, .collapse {
10611060
width: 21px;
10621061
background-size: contain;
10631062
background-color: transparent;

src/librustdoc/html/static/css/themes/ayu.css

+2-2
Original file line numberDiff line numberDiff line change
@@ -336,11 +336,11 @@ a.test-arrow:hover {
336336
color: #c5c5c5;
337337
}
338338

339-
.copy-code {
339+
.copy-code, .expand, .collapse {
340340
filter: invert(70%);
341341
color: #fff;
342342
}
343-
.copy-code:hover {
343+
.copy-code:hover, .expand:hover, .collapse:hover {
344344
filter: invert(100%);
345345
}
346346

src/librustdoc/html/static/css/themes/dark.css

+2-2
Original file line numberDiff line numberDiff line change
@@ -190,11 +190,11 @@ body.source .example-wrap pre.rust a {
190190
a.test-arrow {
191191
color: #dedede;
192192
}
193-
.copy-code {
193+
.copy-code, .expand, .collapse {
194194
filter: invert(50%);
195195
color: #999;
196196
}
197-
.copy-code:hover {
197+
.copy-code:hover, .expand:hover, .collapse:hover {
198198
filter: invert(65%);
199199
}
200200

src/librustdoc/html/static/css/themes/light.css

+2-2
Original file line numberDiff line numberDiff line change
@@ -185,11 +185,11 @@ body.source .example-wrap pre.rust a {
185185
a.test-arrow {
186186
color: #f5f5f5;
187187
}
188-
.copy-code {
188+
.copy-code, .expand, .collapse {
189189
filter: invert(50%);
190190
color: #999;
191191
}
192-
.copy-code:hover {
192+
.copy-code:hover, .expand:hover, .collapse:hover {
193193
filter: invert(35%);
194194
}
195195

+1
Loading

src/librustdoc/html/static/eye.svg

+1
Loading

0 commit comments

Comments
 (0)