Skip to content

Commit 32074e7

Browse files
authored
Merge pull request #409 from pietroalbini/global-alert
Add global alert to notify users about upcoming breaking changes
2 parents ff27e25 + b4e734e commit 32074e7

6 files changed

+56
-0
lines changed

src/lib.rs

+11
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,17 @@ pub mod utils;
5353
mod docbuilder;
5454
mod web;
5555

56+
use web::page::GlobalAlert;
57+
58+
59+
// Warning message shown in the navigation bar of every page. Set to `None` to hide it.
60+
pub(crate) static GLOBAL_ALERT: Option<GlobalAlert> = Some(GlobalAlert {
61+
url: "https://blog.rust-lang.org/2019/09/18/upcoming-docsrs-changes.html",
62+
text: "Upcoming docs.rs breaking changes!",
63+
css_class: "error",
64+
fa_icon: "warning",
65+
});
66+
5667

5768
/// Version string generated at build time contains last git
5869
/// commit hash and build date

src/web/page.rs

+24
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,25 @@ use iron::response::Response;
77
use handlebars_iron::Template;
88

99

10+
pub(crate) struct GlobalAlert {
11+
pub(crate) url: &'static str,
12+
pub(crate) text: &'static str,
13+
pub(crate) css_class: &'static str,
14+
pub(crate) fa_icon: &'static str,
15+
}
16+
17+
impl ToJson for GlobalAlert {
18+
fn to_json(&self) -> Json {
19+
let mut map = BTreeMap::new();
20+
map.insert("url".to_string(), self.url.to_json());
21+
map.insert("text".to_string(), self.text.to_json());
22+
map.insert("css_class".to_string(), self.css_class.to_json());
23+
map.insert("fa_icon".to_string(), self.fa_icon.to_json());
24+
Json::Object(map)
25+
}
26+
}
27+
28+
1029
pub struct Page<T: ToJson> {
1130
title: Option<String>,
1231
content: T,
@@ -89,6 +108,11 @@ impl<T: ToJson> ToJson for Page<T> {
89108
tree.insert("title".to_owned(), title.to_json());
90109
}
91110

111+
tree.insert("has_global_alert".to_owned(), ::GLOBAL_ALERT.is_some().to_json());
112+
if let Some(ref global_alert) = ::GLOBAL_ALERT {
113+
tree.insert("global_alert".to_owned(), global_alert.to_json());
114+
}
115+
92116
tree.insert("content".to_owned(), self.content.to_json());
93117
tree.insert("cratesfyi_version".to_owned(), ::BUILD_VERSION.to_json());
94118
tree.insert("cratesfyi_version_safe".to_owned(),

templates/navigation.hbs

+1
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
<li class="pure-menu-item"><a href="/about" class="pure-menu-link">About Docs.rs</a></li>
2222
</ul>
2323
</li>
24+
{{> navigation_global_alert}}
2425
</ul>
2526
</form>
2627
</div>

templates/navigation_global_alert.hbs

+9
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
{{#if ../has_global_alert}}
2+
<li class="pure-menu-item">
3+
<a href="{{../global_alert.url}}" class="pure-menu-link {{../global_alert.css_class}}">
4+
<i class="fa fa-fw fa-{{../global_alert.fa_icon}}"></i>
5+
{{../global_alert.text}}
6+
</a>
7+
</li>
8+
{{/if}}
9+

templates/navigation_rustdoc.hbs

+1
Original file line numberDiff line numberDiff line change
@@ -101,6 +101,7 @@
101101
</ul>
102102
</li>
103103
{{/with}}
104+
{{> navigation_global_alert}}
104105
</ul>
105106
</form>
106107
</div>

templates/style.scss

+10
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ $color-lifetime-incode: #B76514; // orangish
1919
$color-comment-in-code: #8E908C; // light gray
2020
$color-background-code: #F5F5F5; // lighter gray
2121
$color-border: #ddd; // gray
22+
$color-red: #d93d3d; // red
2223
$top-navbar-height: 32px; // height of the floating top navbar
2324

2425

@@ -214,6 +215,15 @@ div.nav-container {
214215
color: darken($color-type, 10%);
215216
}
216217

218+
// used for global alerts
219+
.error {
220+
color: $color-red;
221+
222+
&:hover {
223+
color: darken($color-red, 10%);
224+
}
225+
}
226+
217227
div.rustdoc-navigation {
218228
span.title {
219229
display: none;

0 commit comments

Comments
 (0)