Skip to content

Commit bb37b60

Browse files
committed
Migrate document_item_info to templates
1 parent 39f2657 commit bb37b60

File tree

3 files changed

+84
-43
lines changed

3 files changed

+84
-43
lines changed

src/librustdoc/html/render/mod.rs

+54-43
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,7 @@ use std::rc::Rc;
4646
use std::str;
4747
use std::string::ToString;
4848

49+
use askama::Template;
4950
use rustc_ast_pretty::pprust;
5051
use rustc_attr::{ConstStability, Deprecation, StabilityLevel};
5152
use rustc_data_structures::fx::{FxHashMap, FxHashSet};
@@ -417,7 +418,7 @@ fn document(
417418
if let Some(ref name) = item.name {
418419
info!("Documenting {}", name);
419420
}
420-
document_item_info(w, cx, item, parent);
421+
document_item_info(cx, item, parent).render_into(w).unwrap();
421422
if parent.is_none() {
422423
document_full_collapsible(w, item, cx, heading_offset);
423424
} else {
@@ -459,7 +460,7 @@ fn document_short(
459460
parent: &clean::Item,
460461
show_def_docs: bool,
461462
) {
462-
document_item_info(w, cx, item, Some(parent));
463+
document_item_info(cx, item, Some(parent)).render_into(w).unwrap();
463464
if !show_def_docs {
464465
return;
465466
}
@@ -531,25 +532,23 @@ fn document_full_inner(
531532
}
532533
}
533534

535+
#[derive(Template)]
536+
#[template(path = "item_info.html")]
537+
struct ItemInfo {
538+
items: Vec<ShortItemInfo>,
539+
}
534540
/// Add extra information about an item such as:
535541
///
536542
/// * Stability
537543
/// * Deprecated
538544
/// * Required features (through the `doc_cfg` feature)
539545
fn document_item_info(
540-
w: &mut Buffer,
541546
cx: &mut Context<'_>,
542547
item: &clean::Item,
543548
parent: Option<&clean::Item>,
544-
) {
545-
let item_infos = short_item_info(item, cx, parent);
546-
if !item_infos.is_empty() {
547-
w.write_str("<span class=\"item-info\">");
548-
for info in item_infos {
549-
w.write_str(&info);
550-
}
551-
w.write_str("</span>");
552-
}
549+
) -> ItemInfo {
550+
let items = short_item_info(item, cx, parent);
551+
ItemInfo { items }
553552
}
554553

555554
fn portability(item: &clean::Item, parent: Option<&clean::Item>) -> Option<String> {
@@ -567,7 +566,25 @@ fn portability(item: &clean::Item, parent: Option<&clean::Item>) -> Option<Strin
567566
cfg
568567
);
569568

570-
Some(format!("<div class=\"stab portability\">{}</div>", cfg?.render_long_html()))
569+
Some(cfg?.render_long_html())
570+
}
571+
572+
#[derive(Template)]
573+
#[template(path = "short_item_info.html")]
574+
enum ShortItemInfo {
575+
/// A message describing the deprecation of this item
576+
Deprecation {
577+
message: String,
578+
},
579+
/// The feature corresponding to an unstable item, and optionally
580+
/// a tracking issue URL and number.
581+
Unstable {
582+
feature: String,
583+
tracking: Option<(String, u32)>,
584+
},
585+
Portability {
586+
message: String,
587+
},
571588
}
572589

573590
/// Render the stability, deprecation and portability information that is displayed at the top of
@@ -576,7 +593,7 @@ fn short_item_info(
576593
item: &clean::Item,
577594
cx: &mut Context<'_>,
578595
parent: Option<&clean::Item>,
579-
) -> Vec<String> {
596+
) -> Vec<ShortItemInfo> {
580597
let mut extra_info = vec![];
581598

582599
if let Some(depr @ Deprecation { note, since, is_since_rustc_version: _, suggestion: _ }) =
@@ -602,15 +619,10 @@ fn short_item_info(
602619
if let Some(note) = note {
603620
let note = note.as_str();
604621
let html = MarkdownItemInfo(note, &mut cx.id_map);
605-
message.push_str(&format!(": {}", html.into_string()));
622+
message.push_str(": ");
623+
message.push_str(&html.into_string());
606624
}
607-
extra_info.push(format!(
608-
"<div class=\"stab deprecated\">\
609-
<span class=\"emoji\">👎</span>\
610-
<span>{}</span>\
611-
</div>",
612-
message,
613-
));
625+
extra_info.push(ShortItemInfo::Deprecation { message });
614626
}
615627

616628
// Render unstable items. But don't render "rustc_private" crates (internal compiler crates).
@@ -621,26 +633,17 @@ fn short_item_info(
621633
.filter(|stab| stab.feature != sym::rustc_private)
622634
.map(|stab| (stab.level, stab.feature))
623635
{
624-
let mut message = "<span class=\"emoji\">🔬</span>\
625-
<span>This is a nightly-only experimental API."
626-
.to_owned();
627-
628-
let mut feature = format!("<code>{}</code>", Escape(feature.as_str()));
629-
if let (Some(url), Some(issue)) = (&cx.shared.issue_tracker_base_url, issue) {
630-
feature.push_str(&format!(
631-
"&nbsp;<a href=\"{url}{issue}\">#{issue}</a>",
632-
url = url,
633-
issue = issue
634-
));
635-
}
636-
637-
message.push_str(&format!(" ({})</span>", feature));
638-
639-
extra_info.push(format!("<div class=\"stab unstable\">{}</div>", message));
636+
let tracking = if let (Some(url), Some(issue)) = (&cx.shared.issue_tracker_base_url, issue)
637+
{
638+
Some((url.clone(), issue.get()))
639+
} else {
640+
None
641+
};
642+
extra_info.push(ShortItemInfo::Unstable { feature: feature.to_string(), tracking });
640643
}
641644

642-
if let Some(portability) = portability(item, parent) {
643-
extra_info.push(portability);
645+
if let Some(message) = portability(item, parent) {
646+
extra_info.push(ShortItemInfo::Portability { message });
644647
}
645648

646649
extra_info
@@ -1472,7 +1475,9 @@ fn render_impl(
14721475
// We need the stability of the item from the trait
14731476
// because impls can't have a stability.
14741477
if item.doc_value().is_some() {
1475-
document_item_info(&mut info_buffer, cx, it, Some(parent));
1478+
document_item_info(cx, it, Some(parent))
1479+
.render_into(&mut info_buffer)
1480+
.unwrap();
14761481
document_full(&mut doc_buffer, item, cx, HeadingOffset::H5);
14771482
short_documented = false;
14781483
} else {
@@ -1489,7 +1494,9 @@ fn render_impl(
14891494
}
14901495
}
14911496
} else {
1492-
document_item_info(&mut info_buffer, cx, item, Some(parent));
1497+
document_item_info(cx, item, Some(parent))
1498+
.render_into(&mut info_buffer)
1499+
.unwrap();
14931500
if rendering_params.show_def_docs {
14941501
document_full(&mut doc_buffer, item, cx, HeadingOffset::H5);
14951502
short_documented = false;
@@ -1862,7 +1869,11 @@ pub(crate) fn render_impl_summary(
18621869
let is_trait = inner_impl.trait_.is_some();
18631870
if is_trait {
18641871
if let Some(portability) = portability(&i.impl_item, Some(parent)) {
1865-
write!(w, "<span class=\"item-info\">{}</span>", portability);
1872+
write!(
1873+
w,
1874+
"<span class=\"item-info\"><div class=\"stab portability\">{}</div></span>",
1875+
portability
1876+
);
18661877
}
18671878
}
18681879

Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
{% if !items.is_empty() %}
2+
<span class="item-info"> {# #}
3+
{% for item in items %}
4+
{{item|safe}} {# #}
5+
{% endfor %}
6+
</span>
7+
{% endif %}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
{% match self %}
2+
{% when Self::Deprecation with { message } %}
3+
<div class="stab deprecated"> {# #}
4+
<span class="emoji">👎</span> {# #}
5+
<span>{{message}}</span> {# #}
6+
</div> {# #}
7+
{% when Self::Unstable with { feature, tracking } %}
8+
<div class="stab unstable"> {# #}
9+
<span class="emoji">🔬</span> {# #}
10+
<span> {# #}
11+
This is a nightly-only experimental API. ({# #}
12+
<code>{{feature}}</code> {# #}
13+
{% match tracking %}
14+
{% when Some with ((url, num)) %}
15+
&nbsp;<a href="{{url}}{{num}}">#{{num}}</a> {# #}
16+
{% when None %}
17+
{% endmatch %}
18+
) {# #}
19+
</span> {# #}
20+
</div> {# #}
21+
{% when Self::Portability with { message } %}
22+
<div class="stab portability">{{message|safe}}</div> {# #}
23+
{% endmatch %}

0 commit comments

Comments
 (0)