Skip to content

Commit 9cc11ca

Browse files
committed
auto merge of #9577 : alexcrichton/rust/rustdoc, r=cmr
They're getting smaller each time though! The highlight of this round is source files in documentation. Still trying to figure out the best syntax-highlighting solution.
2 parents c635fba + 88866a4 commit 9cc11ca

File tree

10 files changed

+313
-136
lines changed

10 files changed

+313
-136
lines changed

mk/docs.mk

-5
Original file line numberDiff line numberDiff line change
@@ -217,10 +217,6 @@ endif
217217
# Rustdoc (libstd/extra)
218218
######################################################################
219219

220-
ifeq ($(CFG_PANDOC),)
221-
$(info cfg: no pandoc found, omitting library doc build)
222-
else
223-
224220
# The rustdoc executable
225221
RUSTDOC = $(HBIN2_H_$(CFG_BUILD_TRIPLE))/rustdoc$(X_$(CFG_BUILD_TRIPLE))
226222

@@ -238,7 +234,6 @@ endef
238234

239235
$(eval $(call libdoc,std,$(STDLIB_CRATE),$(CFG_BUILD_TRIPLE)))
240236
$(eval $(call libdoc,extra,$(EXTRALIB_CRATE),$(CFG_BUILD_TRIPLE)))
241-
endif
242237

243238

244239
ifdef CFG_DISABLE_DOCS

src/librustdoc/clean.rs

+30-42
Original file line numberDiff line numberDiff line change
@@ -84,7 +84,7 @@ impl Clean<Crate> for visit_ast::RustdocVisitor {
8484
#[deriving(Clone, Encodable, Decodable)]
8585
pub struct Item {
8686
/// Stringified span
87-
source: ~str,
87+
source: Span,
8888
/// Not everything has a name. E.g., impls
8989
name: Option<~str>,
9090
attrs: ~[Attribute],
@@ -539,9 +539,11 @@ impl Clean<TraitMethod> for ast::trait_method {
539539
#[deriving(Clone, Encodable, Decodable)]
540540
pub enum Type {
541541
/// structs/enums/traits (anything that'd be an ast::ty_path)
542-
ResolvedPath { path: Path, typarams: Option<~[TyParamBound]>, id: ast::NodeId },
543-
/// Reference to an item in an external crate (fully qualified path)
544-
External(~str, ~str),
542+
ResolvedPath {
543+
path: Path,
544+
typarams: Option<~[TyParamBound]>,
545+
did: ast::DefId
546+
},
545547
// I have no idea how to usefully use this.
546548
TyParamBinder(ast::NodeId),
547549
/// For parameterized types, so the consumer of the JSON don't go looking
@@ -736,10 +738,28 @@ impl Clean<VariantKind> for ast::variant_kind {
736738
}
737739
}
738740

739-
impl Clean<~str> for syntax::codemap::Span {
740-
fn clean(&self) -> ~str {
741-
let cm = local_data::get(super::ctxtkey, |x| x.unwrap().clone()).sess.codemap;
742-
cm.span_to_str(*self)
741+
#[deriving(Clone, Encodable, Decodable)]
742+
pub struct Span {
743+
filename: ~str,
744+
loline: uint,
745+
locol: uint,
746+
hiline: uint,
747+
hicol: uint,
748+
}
749+
750+
impl Clean<Span> for syntax::codemap::Span {
751+
fn clean(&self) -> Span {
752+
let cm = local_data::get(super::ctxtkey, |x| *x.unwrap()).sess.codemap;
753+
let filename = cm.span_to_filename(*self);
754+
let lo = cm.lookup_char_pos(self.lo);
755+
let hi = cm.lookup_char_pos(self.hi);
756+
Span {
757+
filename: filename.to_owned(),
758+
loline: lo.line,
759+
locol: *lo.col,
760+
hiline: hi.line,
761+
hicol: *hi.col,
762+
}
743763
}
744764
}
745765

@@ -1033,7 +1053,7 @@ trait ToSource {
10331053

10341054
impl ToSource for syntax::codemap::Span {
10351055
fn to_src(&self) -> ~str {
1036-
debug!("converting span %s to snippet", self.clean());
1056+
debug!("converting span %? to snippet", self.clean());
10371057
let cm = local_data::get(super::ctxtkey, |x| x.unwrap().clone()).sess.codemap.clone();
10381058
let sn = match cm.span_to_snippet(*self) {
10391059
Some(x) => x,
@@ -1129,39 +1149,7 @@ fn resolve_type(path: Path, tpbs: Option<~[TyParamBound]>,
11291149
},
11301150
x => fail!("resolved type maps to a weird def %?", x),
11311151
};
1132-
1133-
if def_id.crate != ast::CRATE_NODE_ID {
1134-
use rustc::metadata::decoder::*;
1135-
1136-
let sess = local_data::get(super::ctxtkey, |x| *x.unwrap()).sess;
1137-
let cratedata = ::rustc::metadata::cstore::get_crate_data(sess.cstore, def_id.crate);
1138-
let doc = lookup_item(def_id.node, cratedata.data);
1139-
let path = syntax::ast_map::path_to_str_with_sep(item_path(doc), "::", sess.intr());
1140-
let ty = match def_like_to_def(item_to_def_like(doc, def_id, def_id.crate)) {
1141-
DefFn(*) => ~"fn",
1142-
DefTy(*) => ~"enum",
1143-
DefTrait(*) => ~"trait",
1144-
DefPrimTy(p) => match p {
1145-
ty_str => ~"str",
1146-
ty_bool => ~"bool",
1147-
ty_int(t) => match t.to_str() {
1148-
~"" => ~"i",
1149-
s => s
1150-
},
1151-
ty_uint(t) => t.to_str(),
1152-
ty_float(t) => t.to_str(),
1153-
ty_char => ~"char",
1154-
},
1155-
DefTyParam(*) => ~"generic",
1156-
DefStruct(*) => ~"struct",
1157-
DefTyParamBinder(*) => ~"typaram_binder",
1158-
x => fail!("resolved external maps to a weird def %?", x),
1159-
};
1160-
let cname = cratedata.name.to_owned();
1161-
External(cname + "::" + path, ty)
1162-
} else {
1163-
ResolvedPath {path: path.clone(), typarams: tpbs, id: def_id.node}
1164-
}
1152+
ResolvedPath{ path: path, typarams: tpbs, did: def_id }
11651153
}
11661154

11671155
fn resolve_use_source(path: Path, id: ast::NodeId) -> ImportSource {

src/librustdoc/html/escape.rs

+44
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
// Copyright 2013 The Rust Project Developers. See the COPYRIGHT
2+
// file at the top-level directory of this distribution and at
3+
// http://rust-lang.org/COPYRIGHT.
4+
//
5+
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
6+
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
7+
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
8+
// option. This file may not be copied, modified, or distributed
9+
// except according to those terms.
10+
11+
use std::fmt;
12+
13+
pub struct Escape<'self>(&'self str);
14+
15+
impl<'self> fmt::Default for Escape<'self> {
16+
fn fmt(s: &Escape<'self>, fmt: &mut fmt::Formatter) {
17+
// Because the internet is always right, turns out there's not that many
18+
// characters to escape: http://stackoverflow.com/questions/7381974
19+
let pile_o_bits = s.as_slice();
20+
let mut last = 0;
21+
for (i, ch) in s.byte_iter().enumerate() {
22+
match ch as char {
23+
'<' | '>' | '&' | '\'' | '"' => {
24+
fmt.buf.write(pile_o_bits.slice(last, i).as_bytes());
25+
let s = match ch as char {
26+
'>' => "&gt;",
27+
'<' => "&lt;",
28+
'&' => "&amp;",
29+
'\'' => "&#39;",
30+
'"' => "&quot;",
31+
_ => unreachable!()
32+
};
33+
fmt.buf.write(s.as_bytes());
34+
last = i + 1;
35+
}
36+
_ => {}
37+
}
38+
}
39+
40+
if last < s.len() {
41+
fmt.buf.write(pile_o_bits.slice_from(last).as_bytes());
42+
}
43+
}
44+
}

src/librustdoc/html/format.rs

+11-14
Original file line numberDiff line numberDiff line change
@@ -97,7 +97,7 @@ impl fmt::Default for clean::Path {
9797
}
9898
}
9999

100-
fn resolved_path(w: &mut io::Writer, id: ast::NodeId,
100+
fn resolved_path(w: &mut io::Writer, did: ast::DefId,
101101
path: &clean::Path, print_all: bool) {
102102
// The generics will get written to both the title and link
103103
let mut generics = ~"";
@@ -144,9 +144,10 @@ fn resolved_path(w: &mut io::Writer, id: ast::NodeId,
144144

145145
do local_data::get(cache_key) |cache| {
146146
do cache.unwrap().read |cache| {
147-
match cache.paths.find(&id) {
147+
match cache.paths.find(&did.node) {
148148
// This is a documented path, link to it!
149-
Some(&(ref fqp, shortty)) => {
149+
// FIXME(#9539): this is_local check should not exist
150+
Some(&(ref fqp, shortty)) if ast_util::is_local(did) => {
150151
let fqn = fqp.connect("::");
151152
let same = loc.iter().zip(fqp.iter())
152153
.take_while(|&(a, b)| *a == *b).len();
@@ -180,7 +181,7 @@ fn resolved_path(w: &mut io::Writer, id: ast::NodeId,
180181
write!(w, "<a class='{}' href='{}' title='{}'>{}</a>{}",
181182
shortty, url, fqn, last.name, generics);
182183
}
183-
None => {
184+
_ => {
184185
if print_all {
185186
let amt = path.segments.len() - 1;
186187
for seg in path.segments.iter().take(amt) {
@@ -205,8 +206,8 @@ impl fmt::Default for clean::Type {
205206
}
206207
}
207208
}
208-
clean::ResolvedPath{id, typarams: ref typarams, path: ref path} => {
209-
resolved_path(f.buf, id, path, false);
209+
clean::ResolvedPath{did, typarams: ref typarams, path: ref path} => {
210+
resolved_path(f.buf, did, path, false);
210211
match *typarams {
211212
Some(ref params) => {
212213
f.buf.write("&lt;".as_bytes());
@@ -219,10 +220,6 @@ impl fmt::Default for clean::Type {
219220
None => {}
220221
}
221222
}
222-
// XXX: this should be a link
223-
clean::External(ref a, _) => {
224-
write!(f.buf, "{}", *a);
225-
}
226223
clean::Self(*) => f.buf.write("Self".as_bytes()),
227224
clean::Primitive(prim) => {
228225
let s = match prim {
@@ -421,8 +418,8 @@ impl fmt::Default for clean::ViewPath {
421418
impl fmt::Default for clean::ImportSource {
422419
fn fmt(v: &clean::ImportSource, f: &mut fmt::Formatter) {
423420
match v.did {
424-
Some(did) if ast_util::is_local(did) => {
425-
resolved_path(f.buf, did.node, &v.path, true);
421+
Some(did) => {
422+
resolved_path(f.buf, did, &v.path, true);
426423
}
427424
_ => {
428425
for (i, seg) in v.path.segments.iter().enumerate() {
@@ -437,7 +434,7 @@ impl fmt::Default for clean::ImportSource {
437434
impl fmt::Default for clean::ViewListIdent {
438435
fn fmt(v: &clean::ViewListIdent, f: &mut fmt::Formatter) {
439436
match v.source {
440-
Some(did) if ast_util::is_local(did) => {
437+
Some(did) => {
441438
let path = clean::Path {
442439
global: false,
443440
segments: ~[clean::PathSegment {
@@ -446,7 +443,7 @@ impl fmt::Default for clean::ViewListIdent {
446443
types: ~[],
447444
}]
448445
};
449-
resolved_path(f.buf, did.node, &path, false);
446+
resolved_path(f.buf, did, &path, false);
450447
}
451448
_ => write!(f.buf, "{}", v.name),
452449
}

src/librustdoc/html/layout.rs

+2-1
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,8 @@ pub fn render<T: fmt::Default, S: fmt::Default>(
6666
</form>
6767
</nav>
6868
69-
<section class=\"content {ty}\">{content}</section>
69+
<section id='main' class=\"content {ty}\">{content}</section>
70+
<section id='search' class=\"content hidden\"></section>
7071
7172
<section class=\"footer\"></section>
7273

0 commit comments

Comments
 (0)