Auto merge of #89695 - jsha:more-templates, r=GuillaumeGomez · rust-lang/rust@c1cb974 (original) (raw)

`@@ -32,16 +32,41 @@ use crate::html::highlight;

`

32

32

`use crate::html::layout::Page;

`

33

33

`use crate::html::markdown::{HeadingOffset, MarkdownSummaryLine};

`

34

34

``

``

35

`+

use serde::Serialize;

`

``

36

+

35

37

`const ITEM_TABLE_OPEN: &'static str = "<div class="item-table">";

`

36

38

`const ITEM_TABLE_CLOSE: &'static str = "";

`

37

39

`const ITEM_TABLE_ROW_OPEN: &'static str = "<div class="item-row">";

`

38

40

`const ITEM_TABLE_ROW_CLOSE: &'static str = "";

`

39

41

``

40

``

`-

pub(super) fn print_item(cx: &Context<'_>, item: &clean::Item, buf: &mut Buffer, page: &Page<'_>) {

`

``

42

`` +

// A component in a use path, like string in std:🧵:ToString

``

``

43

`+

#[derive(Serialize)]

`

``

44

`+

struct PathComponent<'a> {

`

``

45

`+

path: String,

`

``

46

`+

name: &'a str,

`

``

47

`+

}

`

``

48

+

``

49

`+

#[derive(Serialize)]

`

``

50

`+

struct ItemVars<'a> {

`

``

51

`+

page: &'a Page<'a>,

`

``

52

`+

static_root_path: &'a str,

`

``

53

`+

typ: &'a str,

`

``

54

`+

name: &'a str,

`

``

55

`+

item_type: &'a str,

`

``

56

`+

path_components: Vec<PathComponent<'a>>,

`

``

57

`+

stability_since_raw: &'a str,

`

``

58

`+

src_href: Option<&'a str>,

`

``

59

`+

}

`

``

60

+

``

61

`+

pub(super) fn print_item(

`

``

62

`+

cx: &Context<'_>,

`

``

63

`+

templates: &tera::Tera,

`

``

64

`+

item: &clean::Item,

`

``

65

`+

buf: &mut Buffer,

`

``

66

`+

page: &Page<'_>,

`

``

67

`+

) {

`

41

68

`debug_assert!(!item.is_stripped());

`

42

``

`-

// Write the breadcrumb trail header for the top

`

43

``

`-

buf.write_str("<h1 class="fqn"><span class="in-band">");

`

44

``

`-

let name = match *item.kind {

`

``

69

`+

let typ = match *item.kind {

`

45

70

` clean::ModuleItem(_) => {

`

46

71

`if item.is_crate() {

`

47

72

`"Crate "

`

`@@ -73,60 +98,54 @@ pub(super) fn print_item(cx: &Context<'_>, item: &clean::Item, buf: &mut Buffer,

`

73

98

`unreachable!();

`

74

99

`}

`

75

100

`};

`

76

``

`-

buf.write_str(name);

`

77

``

`-

if !item.is_primitive() && !item.is_keyword() {

`

78

``

`-

let cur = &cx.current;

`

79

``

`-

let amt = if item.is_mod() { cur.len() - 1 } else { cur.len() };

`

80

``

`-

for (i, component) in cur.iter().enumerate().take(amt) {

`

81

``

`-

write!(

`

82

``

`-

buf,

`

83

``

`-

"<a href="{}index.html">{}::",

`

84

``

`-

"../".repeat(cur.len() - i - 1),

`

85

``

`-

component

`

86

``

`-

);

`

87

``

`-

}

`

88

``

`-

}

`

89

``

`-

write!(buf, "<a class="{}" href="#">{}", item.type_(), item.name.as_ref().unwrap());

`

90

``

`-

write!(

`

91

``

`-

buf,

`

92

``

`-

"<button id="copy-path" onclick="copy_path(this)" title="Copy item path to clipboard">\

`

93

``

`-

<img src="{static_root_path}clipboard{suffix}.svg" \

`

94

``

`-

width="19" height="18" \

`

95

``

`-

alt="Copy item path">\

`

96

``

`-

",

`

97

``

`-

static_root_path = page.get_static_root_path(),

`

98

``

`-

suffix = page.resource_suffix,

`

99

``

`-

);

`

100

``

-

101

``

`-

buf.write_str(""); // in-band

`

102

``

`-

buf.write_str("<span class="out-of-band">");

`

``

101

`+

let mut stability_since_raw = Buffer::new();

`

103

102

`render_stability_since_raw(

`

104

``

`-

buf,

`

``

103

`+

&mut stability_since_raw,

`

105

104

` item.stable_since(cx.tcx()).as_deref(),

`

106

105

` item.const_stability(cx.tcx()),

`

107

106

`None,

`

108

107

`None,

`

109

108

`);

`

110

``

`-

buf.write_str(

`

111

``

`-

"<span id="render-detail">\

`

112

``

`-

<a id="toggle-all-docs" href="javascript:void(0)" \

`

113

``

`-

title="collapse all docs">\

`

114

``

`-

[<span class="inner">−]\

`

115

``

`-

\

`

116

``

`-

",

`

117

``

`-

);

`

``

109

`+

let stability_since_raw: String = stability_since_raw.into_inner();

`

118

110

``

119

111

`` // Write src tag

``

120

112

`//

`

121

113

`` // When this item is part of a crate use in a downstream crate, the

``

122

114

`// [src] link in the downstream documentation will actually come back to

`

123

115

`` // this page, and this link will be auto-clicked. The id attribute is

``

124

116

`// used to find the link to auto-click.

`

125

``

`-

if cx.include_sources && !item.is_primitive() {

`

126

``

`-

write_srclink(cx, item, buf);

`

127

``

`-

}

`

``

117

`+

let src_href =

`

``

118

`+

if cx.include_sources && !item.is_primitive() { cx.src_href(item) } else { None };

`

``

119

+

``

120

`+

let path_components = if item.is_primitive() || item.is_keyword() {

`

``

121

`+

vec![]

`

``

122

`+

} else {

`

``

123

`+

let cur = &cx.current;

`

``

124

`+

let amt = if item.is_mod() { cur.len() - 1 } else { cur.len() };

`

``

125

`+

cur.iter()

`

``

126

`+

.enumerate()

`

``

127

`+

.take(amt)

`

``

128

`+

.map(|(i, component)| PathComponent {

`

``

129

`+

path: "../".repeat(cur.len() - i - 1),

`

``

130

`+

name: component,

`

``

131

`+

})

`

``

132

`+

.collect()

`

``

133

`+

};

`

``

134

+

``

135

`+

let item_vars = ItemVars {

`

``

136

`+

page: page,

`

``

137

`+

static_root_path: page.get_static_root_path(),

`

``

138

`+

typ: typ,

`

``

139

`+

name: &item.name.as_ref().unwrap().as_str(),

`

``

140

`+

item_type: &item.type_().to_string(),

`

``

141

`+

path_components: path_components,

`

``

142

`+

stability_since_raw: &stability_since_raw,

`

``

143

`+

src_href: src_href.as_deref(),

`

``

144

`+

};

`

128

145

``

129

``

`-

buf.write_str(""); // out-of-band

`

``

146

`+

let teractx = tera::Context::from_serialize(item_vars).unwrap();

`

``

147

`+

let heading = templates.render("print_item.html", &teractx).unwrap();

`

``

148

`+

buf.write_str(&heading);

`

130

149

``

131

150

`match *item.kind {

`

132

151

` clean::ModuleItem(ref m) => item_module(buf, cx, item, &m.items),

`