Skip to content

Commit b078972

Browse files
committed
rustdoc: implement and use print_plain
1 parent b5ba3c9 commit b078972

File tree

3 files changed

+41
-54
lines changed

3 files changed

+41
-54
lines changed

src/librustdoc/html/format.rs

+24-37
Original file line numberDiff line numberDiff line change
@@ -1169,6 +1169,14 @@ impl clean::Type {
11691169
) -> impl fmt::Display + 'b + Captures<'tcx> {
11701170
display_fn(move |f| fmt_type(self, f, false, cx))
11711171
}
1172+
1173+
/// Emit this type in plain text form (no HTML).
1174+
pub(crate) fn print_plain<'b, 'a: 'b, 'tcx: 'a>(
1175+
&'a self,
1176+
cx: &'a Context<'tcx>,
1177+
) -> impl fmt::Display + 'b + Captures<'tcx> {
1178+
Plain(self.print(cx))
1179+
}
11721180
}
11731181

11741182
impl clean::Path {
@@ -1178,6 +1186,14 @@ impl clean::Path {
11781186
) -> impl fmt::Display + 'b + Captures<'tcx> {
11791187
display_fn(move |f| resolved_path(f, self.def_id(), self, false, false, cx))
11801188
}
1189+
1190+
/// Emit this type in plain text form (no HTML).
1191+
pub(crate) fn print_plain<'b, 'a: 'b, 'tcx: 'a>(
1192+
&'a self,
1193+
cx: &'a Context<'tcx>,
1194+
) -> impl fmt::Display + 'b + Captures<'tcx> {
1195+
Plain(self.print(cx))
1196+
}
11811197
}
11821198

11831199
impl clean::Impl {
@@ -1325,15 +1341,20 @@ impl clean::BareFunctionDecl {
13251341
/// - HTML attributes are quoted with double quotes.
13261342
/// - The only HTML entities used are `&lt;`, `&gt;`, `&amp;`, `&quot`, and `&#39;`
13271343
#[derive(Debug, Clone)]
1328-
struct HtmlRemover<W: fmt::Write> {
1344+
pub(super) struct HtmlRemover<W: fmt::Write> {
13291345
inner: W,
13301346
state: HtmlTextCounterState,
13311347
}
13321348

13331349
impl<W: fmt::Write> HtmlRemover<W> {
1334-
fn new(w: W) -> Self {
1350+
pub(super) fn new(w: W) -> Self {
13351351
HtmlRemover { inner: w, state: HtmlTextCounterState::Text }
13361352
}
1353+
1354+
#[cfg(test)]
1355+
pub(super) fn into_inner(self) -> W {
1356+
self.inner
1357+
}
13371358
}
13381359

13391360
// A state machine that tracks our progress through the HTML.
@@ -1389,7 +1410,7 @@ impl<W: fmt::Write> fmt::Write for HtmlRemover<W> {
13891410
}
13901411

13911412
/// This generates the plain text form of a marked-up HTML input, using HtmlRemover.
1392-
struct Plain<D: fmt::Display>(D);
1413+
pub(super) struct Plain<D: fmt::Display>(pub(super) D);
13931414

13941415
impl<D: fmt::Display> fmt::Display for Plain<D> {
13951416
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
@@ -1801,37 +1822,3 @@ pub(crate) fn display_fn(
18011822

18021823
WithFormatter(Cell::new(Some(f)))
18031824
}
1804-
1805-
#[test]
1806-
fn test_html_remover() {
1807-
use std::fmt::Write;
1808-
1809-
fn assert_removed_eq(input: &str, output: &str) {
1810-
let mut remover = HtmlRemover::new(String::new());
1811-
write!(&mut remover, "{}", input).unwrap();
1812-
assert_eq!(&remover.inner, output);
1813-
}
1814-
1815-
assert_removed_eq("a<a href='https://example.com'>b", "ab");
1816-
assert_removed_eq("alpha &lt;bet&gt;", "alpha <bet>");
1817-
assert_removed_eq("<a href=\"&quot;\">", "");
1818-
assert_removed_eq("<tag>&gt;</tag>text&lt;<tag>", ">text<");
1819-
1820-
let mut remover = HtmlRemover::new(String::new());
1821-
assert!(write!(&mut remover, "&ent;").is_err());
1822-
1823-
let mut remover = HtmlRemover::new(String::new());
1824-
assert!(write!(&mut remover, "&entity").is_err());
1825-
1826-
let mut remover = HtmlRemover::new(String::new());
1827-
assert!(write!(&mut remover, "&&").is_err());
1828-
1829-
let mut remover = HtmlRemover::new(String::new());
1830-
assert!(write!(&mut remover, "<open <tag").is_err());
1831-
}
1832-
1833-
#[test]
1834-
fn test_plain() {
1835-
let d = Plain::new("<strong>alpha</strong> &lt;bet&gt;");
1836-
assert_eq!(&d.to_string(), "alpha <bet>");
1837-
}

src/librustdoc/html/render/mod.rs

+13-13
Original file line numberDiff line numberDiff line change
@@ -1141,8 +1141,10 @@ fn render_assoc_items_inner(
11411141
(RenderMode::Normal, "implementations-list".to_owned())
11421142
}
11431143
AssocItemRender::DerefFor { trait_, type_, deref_mut_ } => {
1144-
let id =
1145-
cx.derive_id(small_url_encode(format!("deref-methods-{:#}", type_.print(cx))));
1144+
let id = cx.derive_id(small_url_encode(format!(
1145+
"deref-methods-{}",
1146+
type_.print_plain(cx)
1147+
)));
11461148
if let Some(def_id) = type_.def_id(cx.cache()) {
11471149
cx.deref_id_map.insert(def_id, id.clone());
11481150
}
@@ -1314,7 +1316,7 @@ pub(crate) fn notable_traits_button(ty: &clean::Type, cx: &mut Context<'_>) -> O
13141316
cx.types_with_notable_traits.insert(ty.clone());
13151317
Some(format!(
13161318
" <a href=\"#\" class=\"tooltip\" data-notable-ty=\"{ty}\">ⓘ</a>",
1317-
ty = Escape(&format!("{:#}", ty.print(cx))),
1319+
ty = Escape(&ty.print_plain(cx).to_string()),
13181320
))
13191321
} else {
13201322
None
@@ -1379,7 +1381,7 @@ fn notable_traits_decl(ty: &clean::Type, cx: &Context<'_>) -> (String, String) {
13791381
write!(&mut out, "</code></pre>",);
13801382
}
13811383

1382-
(format!("{:#}", ty.print(cx)), out.into_inner())
1384+
(ty.print_plain(cx).to_string(), out.into_inner())
13831385
}
13841386

13851387
pub(crate) fn notable_traits_json<'a>(
@@ -1947,20 +1949,18 @@ pub(crate) fn small_url_encode(s: String) -> String {
19471949

19481950
fn get_id_for_impl(for_: &clean::Type, trait_: Option<&clean::Path>, cx: &Context<'_>) -> String {
19491951
match trait_ {
1950-
Some(t) => small_url_encode(format!("impl-{:#}-for-{:#}", t.print(cx), for_.print(cx))),
1951-
None => small_url_encode(format!("impl-{:#}", for_.print(cx))),
1952+
Some(t) => {
1953+
small_url_encode(format!("impl-{}-for-{}", t.print_plain(cx), for_.print_plain(cx)))
1954+
}
1955+
None => small_url_encode(format!("impl-{}", for_.print_plain(cx))),
19521956
}
19531957
}
19541958

19551959
fn extract_for_impl_name(item: &clean::Item, cx: &Context<'_>) -> Option<(String, String)> {
19561960
match *item.kind {
1957-
clean::ItemKind::ImplItem(ref i) => {
1958-
i.trait_.as_ref().map(|trait_| {
1959-
// Alternative format produces no URLs,
1960-
// so this parameter does nothing.
1961-
(format!("{:#}", i.for_.print(cx)), get_id_for_impl(&i.for_, Some(trait_), cx))
1962-
})
1963-
}
1961+
clean::ItemKind::ImplItem(ref i) => i.trait_.as_ref().map(|trait_| {
1962+
(i.for_.print_plain(cx).to_string(), get_id_for_impl(&i.for_, Some(trait_), cx))
1963+
}),
19641964
_ => None,
19651965
}
19661966
}

src/librustdoc/html/render/sidebar.rs

+4-4
Original file line numberDiff line numberDiff line change
@@ -378,9 +378,9 @@ fn sidebar_deref_methods<'a>(
378378
Cow::Borrowed("deref-methods")
379379
};
380380
let title = format!(
381-
"Methods from {:#}<Target={:#}>",
382-
impl_.inner_impl().trait_.as_ref().unwrap().print(cx),
383-
real_target.print(cx),
381+
"Methods from {}<Target={}>",
382+
impl_.inner_impl().trait_.as_ref().unwrap().print_plain(cx),
383+
real_target.print_plain(cx),
384384
);
385385
// We want links' order to be reproducible so we don't use unstable sort.
386386
ret.sort();
@@ -487,7 +487,7 @@ fn sidebar_render_assoc_items(
487487
ty::ImplPolarity::Positive | ty::ImplPolarity::Reservation => "",
488488
ty::ImplPolarity::Negative => "!",
489489
};
490-
let generated = Link::new(encoded, format!("{prefix}{:#}", trait_.print(cx)));
490+
let generated = Link::new(encoded, format!("{prefix}{}", trait_.print_plain(cx)));
491491
if links.insert(generated.clone()) { Some(generated) } else { None }
492492
})
493493
.collect::<Vec<Link<'static>>>();

0 commit comments

Comments
 (0)