Skip to content

Commit 5f58834

Browse files
Provide helper for synthesizing paths with resource suffix
1 parent 0ad789a commit 5f58834

File tree

1 file changed

+40
-29
lines changed

1 file changed

+40
-29
lines changed

src/librustdoc/html/render.rs

Lines changed: 40 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -212,6 +212,26 @@ crate struct SharedContext {
212212
playground: Option<markdown::Playground>,
213213
}
214214

215+
impl Context {
216+
fn path(&self, filename: &str) -> PathBuf {
217+
// We use splitn vs Path::extension here because we might get a filename
218+
// like `style.min.css` and we want to process that into
219+
// `style-suffix.min.css`. Path::extension would just return `css`
220+
// which would result in `style.min-suffix.css` which isn't what we
221+
// want.
222+
let mut iter = filename.splitn(2, '.');
223+
let base = iter.next().unwrap();
224+
let ext = iter.next().unwrap();
225+
let filename = format!(
226+
"{}{}.{}",
227+
base,
228+
self.shared.resource_suffix,
229+
ext,
230+
);
231+
self.dst.join(&filename)
232+
}
233+
}
234+
215235
impl SharedContext {
216236
crate fn ensure_dir(&self, dst: &Path) -> Result<(), Error> {
217237
let mut dirs = self.created_dirs.borrow_mut();
@@ -530,13 +550,13 @@ fn write_shared(
530550
// Add all the static files. These may already exist, but we just
531551
// overwrite them anyway to make sure that they're fresh and up-to-date.
532552

533-
write_minify(&cx.shared.fs, cx.dst.join(&format!("rustdoc{}.css", cx.shared.resource_suffix)),
553+
write_minify(&cx.shared.fs, cx.path("rustdoc.css"),
534554
static_files::RUSTDOC_CSS,
535555
options.enable_minification)?;
536-
write_minify(&cx.shared.fs, cx.dst.join(&format!("settings{}.css", cx.shared.resource_suffix)),
556+
write_minify(&cx.shared.fs, cx.path("settings.css"),
537557
static_files::SETTINGS_CSS,
538558
options.enable_minification)?;
539-
write_minify(&cx.shared.fs, cx.dst.join(&format!("noscript{}.css", cx.shared.resource_suffix)),
559+
write_minify(&cx.shared.fs, cx.path("noscript.css"),
540560
static_files::NOSCRIPT_CSS,
541561
options.enable_minification)?;
542562

@@ -548,34 +568,25 @@ fn write_shared(
548568
let content = try_err!(fs::read(&entry), &entry);
549569
let theme = try_none!(try_none!(entry.file_stem(), &entry).to_str(), &entry);
550570
let extension = try_none!(try_none!(entry.extension(), &entry).to_str(), &entry);
551-
cx.shared.fs.write(
552-
cx.dst.join(format!("{}{}.{}", theme, cx.shared.resource_suffix, extension)),
553-
content.as_slice())?;
571+
cx.shared.fs.write(cx.path(&format!("{}.{}", theme, extension)), content.as_slice())?;
554572
themes.insert(theme.to_owned());
555573
}
556574

557575
let write = |p, c| { cx.shared.fs.write(p, c) };
558576
if (*cx.shared).layout.logo.is_empty() {
559-
write(cx.dst.join(&format!("rust-logo{}.png", cx.shared.resource_suffix)),
560-
static_files::RUST_LOGO)?;
577+
write(cx.path("rust-log.png"), static_files::RUST_LOGO)?;
561578
}
562579
if (*cx.shared).layout.favicon.is_empty() {
563-
write(cx.dst.join(&format!("favicon{}.ico", cx.shared.resource_suffix)),
564-
static_files::RUST_FAVICON)?;
565-
}
566-
write(cx.dst.join(&format!("brush{}.svg", cx.shared.resource_suffix)),
567-
static_files::BRUSH_SVG)?;
568-
write(cx.dst.join(&format!("wheel{}.svg", cx.shared.resource_suffix)),
569-
static_files::WHEEL_SVG)?;
570-
write(cx.dst.join(&format!("down-arrow{}.svg", cx.shared.resource_suffix)),
571-
static_files::DOWN_ARROW_SVG)?;
572-
write_minify(&cx.shared.fs, cx.dst.join(&format!("light{}.css", cx.shared.resource_suffix)),
573-
static_files::themes::LIGHT,
574-
options.enable_minification)?;
580+
write(cx.path("favicon.ico"), static_files::RUST_FAVICON)?;
581+
}
582+
write(cx.path("brush.svg"), static_files::BRUSH_SVG)?;
583+
write(cx.path("wheel.svg"), static_files::WHEEL_SVG)?;
584+
write(cx.path("down-arrow.svg"), static_files::DOWN_ARROW_SVG)?;
585+
write_minify(&cx.shared.fs,
586+
cx.path("light.css"), static_files::themes::LIGHT, options.enable_minification)?;
575587
themes.insert("light".to_owned());
576-
write_minify(&cx.shared.fs, cx.dst.join(&format!("dark{}.css", cx.shared.resource_suffix)),
577-
static_files::themes::DARK,
578-
options.enable_minification)?;
588+
write_minify(&cx.shared.fs,
589+
cx.path("dark.css"), static_files::themes::DARK, options.enable_minification)?;
579590
themes.insert("dark".to_owned());
580591

581592
let mut themes: Vec<&String> = themes.iter().collect();
@@ -638,40 +649,40 @@ themePicker.onblur = handleThemeButtonsBlur;
638649
theme_js.as_bytes()
639650
)?;
640651

641-
write_minify(&cx.shared.fs, cx.dst.join(&format!("main{}.js", cx.shared.resource_suffix)),
652+
write_minify(&cx.shared.fs, cx.path("main.js"),
642653
static_files::MAIN_JS,
643654
options.enable_minification)?;
644-
write_minify(&cx.shared.fs, cx.dst.join(&format!("settings{}.js", cx.shared.resource_suffix)),
655+
write_minify(&cx.shared.fs, cx.path("settings.js"),
645656
static_files::SETTINGS_JS,
646657
options.enable_minification)?;
647658
if cx.shared.include_sources {
648659
write_minify(
649660
&cx.shared.fs,
650-
cx.dst.join(&format!("source-script{}.js", cx.shared.resource_suffix)),
661+
cx.path("source-script.js"),
651662
static_files::sidebar::SOURCE_SCRIPT,
652663
options.enable_minification)?;
653664
}
654665

655666
{
656667
write_minify(
657668
&cx.shared.fs,
658-
cx.dst.join(&format!("storage{}.js", cx.shared.resource_suffix)),
669+
cx.path("storage.js"),
659670
&format!("var resourcesSuffix = \"{}\";{}",
660671
cx.shared.resource_suffix,
661672
static_files::STORAGE_JS),
662673
options.enable_minification)?;
663674
}
664675

665676
if let Some(ref css) = cx.shared.layout.css_file_extension {
666-
let out = cx.dst.join(&format!("theme{}.css", cx.shared.resource_suffix));
677+
let out = cx.path("theme.css");
667678
let buffer = try_err!(fs::read_to_string(css), css);
668679
if !options.enable_minification {
669680
cx.shared.fs.write(&out, &buffer)?;
670681
} else {
671682
write_minify(&cx.shared.fs, out, &buffer, options.enable_minification)?;
672683
}
673684
}
674-
write_minify(&cx.shared.fs, cx.dst.join(&format!("normalize{}.css", cx.shared.resource_suffix)),
685+
write_minify(&cx.shared.fs, cx.path("normalize.css"),
675686
static_files::NORMALIZE_CSS,
676687
options.enable_minification)?;
677688
write(cx.dst.join("FiraSans-Regular.woff"),

0 commit comments

Comments
 (0)