diff options
| author | Alex Crichton <alex@alexcrichton.com> | 2014-05-02 17:08:11 -0700 |
|---|---|---|
| committer | Alex Crichton <alex@alexcrichton.com> | 2014-05-03 02:09:30 -0700 |
| commit | eb084b1818bb220928d1c99e241192f2cd98b069 (patch) | |
| tree | f517d6f0244e7740e0128bf2146168d8a197b092 | |
| parent | 71a52a2edc76527e3bba92378da633bb1fde3743 (diff) | |
| download | rust-eb084b1818bb220928d1c99e241192f2cd98b069.tar.gz rust-eb084b1818bb220928d1c99e241192f2cd98b069.zip | |
rustdoc: Make static initalizers prettier
Previously, if an initializer took multiple lines or was just large in general, it was pretty poorly rendered [1] [2]. This alters the logic to just link back to the source for any multi-line static, with a placeholder of "[definition]". This should make reading statics a little easier on the eyes. All single-line statics are still inlined in the documentation. Closes #13198 [1] - http://static.rust-lang.org/doc/master/sync/mutex/index.html#statics [2] - http://static.rust-lang.org/doc/master/std/sync/atomics/index.html#statics
| -rw-r--r-- | src/librustdoc/html/render.rs | 53 |
1 files changed, 29 insertions, 24 deletions
diff --git a/src/librustdoc/html/render.rs b/src/librustdoc/html/render.rs index a95d85741e4..504be2cd8ae 100644 --- a/src/librustdoc/html/render.rs +++ b/src/librustdoc/html/render.rs @@ -930,6 +930,23 @@ impl<'a> Item<'a> { clean::ModuleItem(..) => true, _ => false } } + + fn link(&self) -> ~str { + let mut path = Vec::new(); + clean_srcpath(self.item.source.filename.as_bytes(), |component| { + path.push(component.to_owned()); + }); + let href = if self.item.source.loline == self.item.source.hiline { + format!("{}", self.item.source.loline) + } else { + format!("{}-{}", self.item.source.loline, self.item.source.hiline) + }; + format!("{root}src/{krate}/{path}.html\\#{href}", + root = self.cx.root_path, + krate = self.cx.layout.krate, + path = path.connect("/"), + href = href) + } } impl<'a> fmt::Show for Item<'a> { @@ -977,23 +994,8 @@ impl<'a> fmt::Show for Item<'a> { // Write `src` tag if self.cx.include_sources { - let mut path = Vec::new(); - clean_srcpath(self.item.source.filename.as_bytes(), |component| { - path.push(component.to_owned()); - }); - let href = if self.item.source.loline == self.item.source.hiline { - format!("{}", self.item.source.loline) - } else { - format!("{}-{}", self.item.source.loline, self.item.source.hiline) - }; - try!(write!(fmt.buf, - "<a class='source' \ - href='{root}src/{krate}/{path}.html\\#{href}'>\ - [src]</a>", - root = self.cx.root_path, - krate = self.cx.layout.krate, - path = path.connect("/"), - href = href)); + try!(write!(fmt.buf, "<a class='source' href='{}'>[src]</a>", + self.link())); } try!(write!(fmt.buf, "</h1>\n")); @@ -1138,16 +1140,19 @@ fn item_module(w: &mut Writer, cx: &Context, match myitem.inner { clean::StaticItem(ref s) | clean::ForeignStaticItem(ref s) => { - struct Initializer<'a>(&'a str); + struct Initializer<'a>(&'a str, Item<'a>); impl<'a> fmt::Show for Initializer<'a> { fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { - let Initializer(s) = *self; + let Initializer(s, item) = *self; if s.len() == 0 { return Ok(()); } try!(write!(f.buf, "<code> = </code>")); - let tag = if s.contains("\n") { "pre" } else { "code" }; - try!(write!(f.buf, "<{tag}>{}</{tag}>", - s.as_slice(), tag=tag)); - Ok(()) + if s.contains("\n") { + write!(f.buf, + "<a href='{}'>[definition]</a>", + item.link()) + } else { + write!(f.buf, "<code>{}</code>", s.as_slice()) + } } } @@ -1160,7 +1165,7 @@ fn item_module(w: &mut Writer, cx: &Context, VisSpace(myitem.visibility), *myitem.name.get_ref(), s.type_, - Initializer(s.expr), + Initializer(s.expr, Item { cx: cx, item: myitem }), Markdown(blank(myitem.doc_value())))); } |
