layout::render takes Print instead of fmt::Display

This commit is contained in:
Mark Rousskov 2019-08-31 12:18:25 -04:00
parent d5f147086b
commit 3f0e77f19c
4 changed files with 15 additions and 11 deletions

View File

@ -99,7 +99,11 @@ impl Buffer {
self.into_inner()
}
crate fn display<T: fmt::Display>(&mut self, t: T) {
crate fn with_formatter<T: FnOnce(&mut fmt::Formatter<'_>) -> fmt::Result>(&mut self, t: T) {
self.from_display(display_fn(move |f| (t)(f)));
}
crate fn from_display<T: std::fmt::Display>(&mut self, t: T) {
if self.for_html {
write!(self, "{}", t);
} else {

View File

@ -1,4 +1,3 @@
use std::fmt;
use std::path::PathBuf;
use crate::externalfiles::ExternalHtml;
@ -31,11 +30,11 @@ pub struct Page<'a> {
pub static_extra_scripts: &'a [&'a str],
}
pub fn render<T: fmt::Display, S: Print>(
pub fn render<T: Print, S: Print>(
layout: &Layout,
page: &Page<'_>,
sidebar: S,
t: &T,
t: T,
themes: &[PathBuf],
) -> String {
let static_root_path = page.static_root_path.unwrap_or(page.root_path);
@ -175,7 +174,7 @@ pub fn render<T: fmt::Display, S: Print>(
} else {
String::new()
},
content = *t,
content = Buffer::html().to_display(t),
static_root_path = static_root_path,
root_path = page.root_path,
css_class = page.css_class,

View File

@ -65,7 +65,7 @@ use crate::docfs::{DocFS, ErrorStorage, PathError};
use crate::doctree;
use crate::fold::DocFolder;
use crate::html::escape::Escape;
use crate::html::format::{Print, Buffer, AsyncSpace, ConstnessSpace};
use crate::html::format::{Buffer, AsyncSpace, ConstnessSpace};
use crate::html::format::{GenericBounds, WhereClause, href, AbiSpace, DefaultSpace};
use crate::html::format::{VisSpace, Function, UnsafetySpace, MutableSpace};
use crate::html::format::fmt_impl_for_trait_page;
@ -1172,7 +1172,7 @@ themePicker.onblur = handleThemeButtonsBlur;
})
.collect::<String>());
let v = layout::render(&cx.shared.layout,
&page, "", &content,
&page, "", content,
&cx.shared.themes);
cx.shared.fs.write(&dst, v.as_bytes())?;
}
@ -1919,7 +1919,7 @@ impl Context {
String::new()
};
let v = layout::render(&self.shared.layout,
&page, sidebar, &all,
&page, sidebar, |buf: &mut Buffer| buf.from_display(all),
&self.shared.themes);
self.shared.fs.write(&final_file, v.as_bytes())?;
@ -1935,7 +1935,7 @@ impl Context {
themes.push(PathBuf::from("settings.css"));
let v = layout::render(
&self.shared.layout,
&page, sidebar, &settings,
&page, sidebar, |buf: &mut Buffer| buf.from_display(settings),
&themes);
self.shared.fs.write(&settings_file, v.as_bytes())?;
@ -1993,7 +1993,7 @@ impl Context {
if !self.render_redirect_pages {
layout::render(&self.shared.layout, &page,
|buf: &mut _| print_sidebar(self, it, buf),
&Item{ cx: self, item: it },
|buf: &mut Buffer| buf.from_display(Item { cx: self, item: it }),
&self.shared.themes)
} else {
let mut url = self.root_path();

View File

@ -4,6 +4,7 @@ use crate::fold::DocFolder;
use crate::html::layout;
use crate::html::render::{Error, SharedContext, BASIC_KEYWORDS};
use crate::html::highlight;
use crate::html::format::Buffer;
use std::ffi::OsStr;
use std::fs;
use std::path::{Component, Path, PathBuf};
@ -120,7 +121,7 @@ impl<'a> SourceCollector<'a> {
static_extra_scripts: &[&format!("source-script{}", self.scx.resource_suffix)],
};
let v = layout::render(&self.scx.layout,
&page, "", &Source(contents),
&page, "", |buf: &mut Buffer| buf.from_display(Source(&contents)),
&self.scx.themes);
self.scx.fs.write(&cur, v.as_bytes())?;
self.scx.local_sources.insert(p.clone(), href);