Skip to content

Commit d8d5e4d

Browse files
committed
Auto merge of #20221 - liigo:rustdoc-sidebar-tooltips-v3, r=alexcrichton
This pull request add tooltips to most links of sidebar. The tooltips display "summary line" of items' document. Some lengthy/annoying raw markdown code are eliminated, such as links and headers. - `[Rust](http://rust-lang.org)` displays as `Rust` (no URLs) - `# header` displays as `header` (no `#`s) Some inline spans, e.g. ``` `code` ``` and ```*emphasis*```, are kept as they are, for better readable. I've make sure `&` `'` `"` `<` and `>` are properly displayed in tooltips, for example, `&'a Option<T>`. Online preview: http://liigo.com/tmp/tooltips/std/index.html @alexcrichton @steveklabnik since you have reviewed my previous ([v1](https://github.com/rust-lang/rust/pull/13014),[v2](https://github.com/rust-lang/rust/pull/16448)) PRs of this serise, which have been closed for technical reasons. Thank you.
2 parents 4874ca3 + 2b11a80 commit d8d5e4d

File tree

3 files changed

+130
-15
lines changed

3 files changed

+130
-15
lines changed

src/librustdoc/html/markdown.rs

+97-4
Original file line numberDiff line numberDiff line change
@@ -72,16 +72,40 @@ type blockcodefn = extern "C" fn(*mut hoedown_buffer, *const hoedown_buffer,
7272
type headerfn = extern "C" fn(*mut hoedown_buffer, *const hoedown_buffer,
7373
libc::c_int, *mut libc::c_void);
7474

75+
type linkfn = extern "C" fn (*mut hoedown_buffer, *const hoedown_buffer,
76+
*const hoedown_buffer, *const hoedown_buffer,
77+
*mut libc::c_void) -> libc::c_int;
78+
79+
type normaltextfn = extern "C" fn(*mut hoedown_buffer, *const hoedown_buffer,
80+
*mut libc::c_void);
81+
7582
#[repr(C)]
7683
struct hoedown_renderer {
77-
opaque: *mut hoedown_html_renderer_state,
84+
opaque: *mut libc::c_void,
85+
7886
blockcode: Option<blockcodefn>,
7987
blockquote: Option<extern "C" fn(*mut hoedown_buffer, *const hoedown_buffer,
8088
*mut libc::c_void)>,
8189
blockhtml: Option<extern "C" fn(*mut hoedown_buffer, *const hoedown_buffer,
8290
*mut libc::c_void)>,
8391
header: Option<headerfn>,
84-
other: [libc::size_t; 28],
92+
93+
other_block_level_callbacks: [libc::size_t; 9],
94+
95+
/* span level callbacks - NULL or return 0 prints the span verbatim */
96+
other_span_level_callbacks_1: [libc::size_t; 9],
97+
link: Option<linkfn>,
98+
other_span_level_callbacks_2: [libc::size_t; 5],
99+
// hoedown will add `math` callback here, but we use an old version of it.
100+
101+
/* low level callbacks - NULL copies input directly into the output */
102+
entity: Option<extern "C" fn(*mut hoedown_buffer, *const hoedown_buffer,
103+
*mut libc::c_void)>,
104+
normal_text: Option<normaltextfn>,
105+
106+
/* header and footer */
107+
doc_header: Option<extern "C" fn(*mut hoedown_buffer, *mut libc::c_void)>,
108+
doc_footer: Option<extern "C" fn(*mut hoedown_buffer, *mut libc::c_void)>,
85109
}
86110

87111
#[repr(C)]
@@ -134,6 +158,8 @@ extern {
134158
fn hoedown_document_free(md: *mut hoedown_document);
135159

136160
fn hoedown_buffer_new(unit: libc::size_t) -> *mut hoedown_buffer;
161+
fn hoedown_buffer_put(b: *mut hoedown_buffer, c: *const libc::c_char,
162+
n: libc::size_t);
137163
fn hoedown_buffer_puts(b: *mut hoedown_buffer, c: *const libc::c_char);
138164
fn hoedown_buffer_free(b: *mut hoedown_buffer);
139165

@@ -279,7 +305,8 @@ pub fn render(w: &mut fmt::Formatter, s: &str, print_toc: bool) -> fmt::Result {
279305
dfltblk: (*renderer).blockcode.unwrap(),
280306
toc_builder: if print_toc {Some(TocBuilder::new())} else {None}
281307
};
282-
(*(*renderer).opaque).opaque = &mut opaque as *mut _ as *mut libc::c_void;
308+
(*((*renderer).opaque as *mut hoedown_html_renderer_state)).opaque
309+
= &mut opaque as *mut _ as *mut libc::c_void;
283310
(*renderer).blockcode = Some(block as blockcodefn);
284311
(*renderer).header = Some(header as headerfn);
285312

@@ -355,7 +382,8 @@ pub fn find_testable_code(doc: &str, tests: &mut ::test::Collector) {
355382
let renderer = hoedown_html_renderer_new(0, 0);
356383
(*renderer).blockcode = Some(block as blockcodefn);
357384
(*renderer).header = Some(header as headerfn);
358-
(*(*renderer).opaque).opaque = tests as *mut _ as *mut libc::c_void;
385+
(*((*renderer).opaque as *mut hoedown_html_renderer_state)).opaque
386+
= tests as *mut _ as *mut libc::c_void;
359387

360388
let document = hoedown_document_new(renderer, HOEDOWN_EXTENSIONS, 16);
361389
hoedown_document_render(document, ob, doc.as_ptr(),
@@ -442,9 +470,60 @@ impl<'a> fmt::Display for MarkdownWithToc<'a> {
442470
}
443471
}
444472

473+
pub fn plain_summary_line(md: &str) -> String {
474+
extern fn link(_ob: *mut hoedown_buffer,
475+
_link: *const hoedown_buffer,
476+
_title: *const hoedown_buffer,
477+
content: *const hoedown_buffer,
478+
opaque: *mut libc::c_void) -> libc::c_int
479+
{
480+
unsafe {
481+
if !content.is_null() && (*content).size > 0 {
482+
let ob = opaque as *mut hoedown_buffer;
483+
hoedown_buffer_put(ob, (*content).data as *const libc::c_char,
484+
(*content).size);
485+
}
486+
}
487+
1
488+
}
489+
490+
extern fn normal_text(_ob: *mut hoedown_buffer,
491+
text: *const hoedown_buffer,
492+
opaque: *mut libc::c_void)
493+
{
494+
unsafe {
495+
let ob = opaque as *mut hoedown_buffer;
496+
hoedown_buffer_put(ob, (*text).data as *const libc::c_char,
497+
(*text).size);
498+
}
499+
}
500+
501+
unsafe {
502+
let ob = hoedown_buffer_new(DEF_OUNIT);
503+
let mut plain_renderer: hoedown_renderer = ::std::mem::zeroed();
504+
let renderer = &mut plain_renderer as *mut hoedown_renderer;
505+
(*renderer).opaque = ob as *mut libc::c_void;
506+
(*renderer).link = Some(link as linkfn);
507+
(*renderer).normal_text = Some(normal_text as normaltextfn);
508+
509+
let document = hoedown_document_new(renderer, HOEDOWN_EXTENSIONS, 16);
510+
hoedown_document_render(document, ob, md.as_ptr(),
511+
md.len() as libc::size_t);
512+
hoedown_document_free(document);
513+
let plain_slice = slice::from_raw_buf(&(*ob).data, (*ob).size as uint);
514+
let plain = match str::from_utf8(plain_slice) {
515+
Ok(s) => s.to_string(),
516+
Err(_) => "".to_string(),
517+
};
518+
hoedown_buffer_free(ob);
519+
plain
520+
}
521+
}
522+
445523
#[cfg(test)]
446524
mod tests {
447525
use super::{LangString, Markdown};
526+
use super::plain_summary_line;
448527

449528
#[test]
450529
fn test_lang_string_parse() {
@@ -478,4 +557,18 @@ mod tests {
478557
let markdown = "# title";
479558
format!("{}", Markdown(markdown.as_slice()));
480559
}
560+
561+
#[test]
562+
fn test_plain_summary_line() {
563+
fn t(input: &str, expect: &str) {
564+
let output = plain_summary_line(input);
565+
assert_eq!(output, expect);
566+
}
567+
568+
t("hello [Rust](http://rust-lang.org) :)", "hello Rust :)");
569+
t("code `let x = i32;` ...", "code `let x = i32;` ...");
570+
t("type `Type<'static>` ...", "type `Type<'static>` ...");
571+
t("# top header", "top header");
572+
t("## header", "header");
573+
}
481574
}

src/librustdoc/html/render.rs

+21-10
Original file line numberDiff line numberDiff line change
@@ -64,8 +64,13 @@ use html::item_type::ItemType;
6464
use html::layout;
6565
use html::markdown::Markdown;
6666
use html::markdown;
67+
use html::escape::Escape;
6768
use stability_summary;
6869

70+
/// A pair of name and its optional document.
71+
#[derive(Clone, Eq, Ord, PartialEq, PartialOrd)]
72+
pub struct NameDoc(String, Option<String>);
73+
6974
/// Major driving force in all rustdoc rendering. This contains information
7075
/// about where in the tree-like hierarchy rendering is occurring and controls
7176
/// how the current page is being rendered.
@@ -95,7 +100,7 @@ pub struct Context {
95100
/// functions), and the value is the list of containers belonging to this
96101
/// header. This map will change depending on the surrounding context of the
97102
/// page.
98-
pub sidebar: HashMap<String, Vec<String>>,
103+
pub sidebar: HashMap<String, Vec<NameDoc>>,
99104
/// This flag indicates whether [src] links should be generated or not. If
100105
/// the source files are present in the html rendering, then this will be
101106
/// `true`.
@@ -1245,7 +1250,7 @@ impl Context {
12451250
}
12461251
}
12471252

1248-
fn build_sidebar(&self, m: &clean::Module) -> HashMap<String, Vec<String>> {
1253+
fn build_sidebar(&self, m: &clean::Module) -> HashMap<String, Vec<NameDoc>> {
12491254
let mut map = HashMap::new();
12501255
for item in m.items.iter() {
12511256
if self.ignore_private_item(item) { continue }
@@ -1262,7 +1267,7 @@ impl Context {
12621267
let short = short.to_string();
12631268
let v = map.entry(short).get().unwrap_or_else(
12641269
|vacant_entry| vacant_entry.insert(Vec::with_capacity(1)));
1265-
v.push(myname);
1270+
v.push(NameDoc(myname, Some(shorter_line(item.doc_value()))));
12661271
}
12671272

12681273
for (_, items) in map.iter_mut() {
@@ -1476,6 +1481,11 @@ fn shorter<'a>(s: Option<&'a str>) -> &'a str {
14761481
}
14771482
}
14781483

1484+
#[inline]
1485+
fn shorter_line(s: Option<&str>) -> String {
1486+
shorter(s).replace("\n", " ")
1487+
}
1488+
14791489
fn document(w: &mut fmt::Formatter, item: &clean::Item) -> fmt::Result {
14801490
match item.doc_value() {
14811491
Some(s) => {
@@ -2201,21 +2211,22 @@ impl<'a> fmt::Display for Sidebar<'a> {
22012211
None => return Ok(())
22022212
};
22032213
try!(write!(w, "<div class='block {}'><h2>{}</h2>", short, longty));
2204-
for item in items.iter() {
2214+
for &NameDoc(ref name, ref doc) in items.iter() {
22052215
let curty = shortty(cur).to_static_str();
2206-
let class = if cur.name.as_ref().unwrap() == item &&
2216+
let class = if cur.name.as_ref().unwrap() == name &&
22072217
short == curty { "current" } else { "" };
2208-
try!(write!(w, "<a class='{ty} {class}' href='{href}{path}'>\
2209-
{name}</a>",
2218+
try!(write!(w, "<a class='{ty} {class}' href='{href}{path}' \
2219+
title='{title}'>{name}</a>",
22102220
ty = short,
22112221
class = class,
22122222
href = if curty == "mod" {"../"} else {""},
22132223
path = if short == "mod" {
2214-
format!("{}/index.html", item.as_slice())
2224+
format!("{}/index.html", name.as_slice())
22152225
} else {
2216-
format!("{}.{}.html", short, item.as_slice())
2226+
format!("{}.{}.html", short, name.as_slice())
22172227
},
2218-
name = item.as_slice()));
2228+
title = Escape(doc.as_ref().unwrap().as_slice()),
2229+
name = name.as_slice()));
22192230
}
22202231
try!(write!(w, "</div>"));
22212232
Ok(())

src/librustdoc/html/static/main.js

+12-1
Original file line numberDiff line numberDiff line change
@@ -668,6 +668,15 @@
668668
search();
669669
}
670670

671+
function plainSummaryLine(markdown) {
672+
var str = markdown.replace(/\n/g, ' ')
673+
str = str.replace(/'/g, "\'")
674+
str = str.replace(/^#+? (.+?)/, "$1")
675+
str = str.replace(/\[(.*?)\]\(.*?\)/g, "$1")
676+
str = str.replace(/\[(.*?)\]\[.*?\]/g, "$1")
677+
return str;
678+
}
679+
671680
index = buildIndex(rawSearchIndex);
672681
startSearch();
673682

@@ -688,8 +697,10 @@
688697
if (crates[i] == window.currentCrate) {
689698
klass += ' current';
690699
}
700+
var desc = rawSearchIndex[crates[i]].items[0][3];
691701
div.append($('<a>', {'href': '../' + crates[i] + '/index.html',
692-
'class': klass}).text(crates[i]));
702+
'title': plainSummaryLine(desc),
703+
'class': klass}).text(crates[i]));
693704
}
694705
sidebar.append(div);
695706
}

0 commit comments

Comments
 (0)