Add template for print_item

Add print_item.html and the code in print_item.rs to use it.
This commit is contained in:
Jacob Hoffman-Andrews 2021-10-08 21:57:44 -07:00
parent f8751436ff
commit 7a938005e1
4 changed files with 96 additions and 46 deletions

View File

@ -33,7 +33,7 @@
use crate::html::escape::Escape;
use crate::html::format::Buffer;
use crate::html::markdown::{self, plain_text_summary, ErrorCodes, IdMap};
use crate::html::static_files::PAGE;
use crate::html::static_files::{PAGE, PRINT_ITEM};
use crate::html::{layout, sources};
/// Major driving force in all rustdoc rendering. This contains information
@ -225,7 +225,7 @@ fn render_item(&self, it: &clean::Item, is_module: bool) -> String {
&self.shared.layout,
&page,
|buf: &mut _| print_sidebar(self, it, buf),
|buf: &mut _| print_item(self, it, buf, &page),
|buf: &mut _| print_item(self, &self.shared.templates, it, buf, &page),
&self.shared.style_files,
)
} else {
@ -422,6 +422,10 @@ fn init(
file: "page.html".into(),
error: format!("{}: {}", e, e.source().map(|e| e.to_string()).unwrap_or_default()),
})?;
templates.add_raw_template("print_item.html", PRINT_ITEM).map_err(|e| Error {
file: "print_item.html".into(),
error: format!("{}: {}", e, e.source().map(|e| e.to_string()).unwrap_or_default()),
})?;
// Crawl the crate attributes looking for attributes which control how we're
// going to emit HTML

View File

@ -32,16 +32,41 @@
use crate::html::layout::Page;
use crate::html::markdown::{HeadingOffset, MarkdownSummaryLine};
use serde::Serialize;
const ITEM_TABLE_OPEN: &'static str = "<div class=\"item-table\">";
const ITEM_TABLE_CLOSE: &'static str = "</div>";
const ITEM_TABLE_ROW_OPEN: &'static str = "<div class=\"item-row\">";
const ITEM_TABLE_ROW_CLOSE: &'static str = "</div>";
pub(super) fn print_item(cx: &Context<'_>, item: &clean::Item, buf: &mut Buffer, page: &Page<'_>) {
// A component in a `use` path, like `string` in std::string::ToString
#[derive(Serialize)]
struct PathComponent<'a> {
path: String,
name: &'a str,
}
#[derive(Serialize)]
struct ItemVars<'a> {
page: &'a Page<'a>,
static_root_path: &'a str,
typ: &'a str,
name: &'a str,
item_type: &'a str,
path_components: Vec<PathComponent<'a>>,
stability_since_raw: &'a str,
src_href: Option<&'a str>,
}
pub(super) fn print_item(
cx: &Context<'_>,
templates: &tera::Tera,
item: &clean::Item,
buf: &mut Buffer,
page: &Page<'_>,
) {
debug_assert!(!item.is_stripped());
// Write the breadcrumb trail header for the top
buf.write_str("<h1 class=\"fqn\"><span class=\"in-band\">");
let name = match *item.kind {
let typ = match *item.kind {
clean::ModuleItem(_) => {
if item.is_crate() {
"Crate "
@ -73,48 +98,15 @@ pub(super) fn print_item(cx: &Context<'_>, item: &clean::Item, buf: &mut Buffer,
unreachable!();
}
};
buf.write_str(name);
if !item.is_primitive() && !item.is_keyword() {
let cur = &cx.current;
let amt = if item.is_mod() { cur.len() - 1 } else { cur.len() };
for (i, component) in cur.iter().enumerate().take(amt) {
write!(
buf,
"<a href=\"{}index.html\">{}</a>::<wbr>",
"../".repeat(cur.len() - i - 1),
component
);
}
}
write!(buf, "<a class=\"{}\" href=\"#\">{}</a>", item.type_(), item.name.as_ref().unwrap());
write!(
buf,
"<button id=\"copy-path\" onclick=\"copy_path(this)\" title=\"Copy item path to clipboard\">\
<img src=\"{static_root_path}clipboard{suffix}.svg\" \
width=\"19\" height=\"18\" \
alt=\"Copy item path\">\
</button>",
static_root_path = page.get_static_root_path(),
suffix = page.resource_suffix,
);
buf.write_str("</span>"); // in-band
buf.write_str("<span class=\"out-of-band\">");
let mut stability_since_raw = Buffer::new();
render_stability_since_raw(
buf,
&mut stability_since_raw,
item.stable_since(cx.tcx()).as_deref(),
item.const_stability(cx.tcx()),
None,
None,
);
buf.write_str(
"<span id=\"render-detail\">\
<a id=\"toggle-all-docs\" href=\"javascript:void(0)\" \
title=\"collapse all docs\">\
[<span class=\"inner\">&#x2212;</span>]\
</a>\
</span>",
);
let stability_since_raw: String = stability_since_raw.into_inner();
// Write `src` tag
//
@ -122,11 +114,38 @@ pub(super) fn print_item(cx: &Context<'_>, item: &clean::Item, buf: &mut Buffer,
// [src] link in the downstream documentation will actually come back to
// this page, and this link will be auto-clicked. The `id` attribute is
// used to find the link to auto-click.
if cx.include_sources && !item.is_primitive() {
write_srclink(cx, item, buf);
}
let src_href =
if cx.include_sources && !item.is_primitive() { cx.src_href(item) } else { None };
buf.write_str("</span></h1>"); // out-of-band
let path_components = if item.is_primitive() || item.is_keyword() {
vec![]
} else {
let cur = &cx.current;
let amt = if item.is_mod() { cur.len() - 1 } else { cur.len() };
cur.iter()
.enumerate()
.take(amt)
.map(|(i, component)| PathComponent {
path: "../".repeat(cur.len() - i - 1),
name: component,
})
.collect()
};
let item_vars = ItemVars {
page: page,
static_root_path: page.get_static_root_path(),
typ: typ,
name: &item.name.as_ref().unwrap().as_str(),
item_type: &item.type_().to_string(),
path_components: path_components,
stability_since_raw: &stability_since_raw,
src_href: src_href.as_deref(),
};
let teractx = tera::Context::from_serialize(item_vars).unwrap();
let heading = templates.render("print_item.html", &teractx).unwrap();
buf.write_str(&heading);
match *item.kind {
clean::ModuleItem(ref m) => item_module(buf, cx, item, &m.items),

View File

@ -71,6 +71,7 @@
crate static RUST_FAVICON_PNG_32: &[u8] = include_bytes!("static/images/favicon-32x32.png");
crate static PAGE: &str = include_str!("templates/page.html");
crate static PRINT_ITEM: &str = include_str!("templates/print_item.html");
/// The built-in themes given to every documentation site.
crate mod themes {

View File

@ -0,0 +1,26 @@
<h1 class="fqn"> {#- -#}
<span class="in-band"> {#- -#}
{{-typ-}}
{#- The breadcrumbs of the item path, like std::string -#}
{%- for component in path_components -%}
<a href="{{component.path | safe}}index.html">{{component.name}}</a>::<wbr>
{%- endfor -%}
<a class="{{item_type}}" href="#">{{name}}</a> {#- -#}
<button id="copy-path" onclick="copy_path(this)" title="Copy item path to clipboard"> {#- -#}
<img src="{{static_root_path | safe}}clipboard{{page.resource_suffix}}.svg"
width="19" height="18"
alt="Copy item path">
</button> {#- -#}
</span> {#- -#}
<span class="out-of-band"> {#- -#}
{{- stability_since_raw | safe -}}
<span id="render-detail"> {#- -#}
<a id="toggle-all-docs" href="javascript:void(0)" title="collapse all docs"> {#- -#}
[<span class="inner">&#x2212;</span>] {#- -#}
</a> {#- -#}
</span> {#- -#}
{%- if src_href -%}
<a class="srclink" href="{{src_href | safe}}" title="goto source code">[src]</a>
{%- endif -%}
</span> {#- -#}
</h1> {#- -#}