Skip to content

Commit

Permalink
auto merge of #9577 : alexcrichton/rust/rustdoc, r=cmr
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
bors committed Sep 28, 2013
2 parents c635fba + 88866a4 commit 9cc11ca
Show file tree
Hide file tree
Showing 10 changed files with 313 additions and 136 deletions.
5 changes: 0 additions & 5 deletions mk/docs.mk
Original file line number Diff line number Diff line change
Expand Up @@ -217,10 +217,6 @@ endif
# Rustdoc (libstd/extra)
######################################################################

ifeq ($(CFG_PANDOC),)
$(info cfg: no pandoc found, omitting library doc build)
else

# The rustdoc executable
RUSTDOC = $(HBIN2_H_$(CFG_BUILD_TRIPLE))/rustdoc$(X_$(CFG_BUILD_TRIPLE))

Expand All @@ -238,7 +234,6 @@ endef

$(eval $(call libdoc,std,$(STDLIB_CRATE),$(CFG_BUILD_TRIPLE)))
$(eval $(call libdoc,extra,$(EXTRALIB_CRATE),$(CFG_BUILD_TRIPLE)))
endif


ifdef CFG_DISABLE_DOCS
Expand Down
72 changes: 30 additions & 42 deletions src/librustdoc/clean.rs
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ impl Clean<Crate> for visit_ast::RustdocVisitor {
#[deriving(Clone, Encodable, Decodable)]
pub struct Item {
/// Stringified span
source: ~str,
source: Span,
/// Not everything has a name. E.g., impls
name: Option<~str>,
attrs: ~[Attribute],
Expand Down Expand Up @@ -539,9 +539,11 @@ impl Clean<TraitMethod> for ast::trait_method {
#[deriving(Clone, Encodable, Decodable)]
pub enum Type {
/// structs/enums/traits (anything that'd be an ast::ty_path)
ResolvedPath { path: Path, typarams: Option<~[TyParamBound]>, id: ast::NodeId },
/// Reference to an item in an external crate (fully qualified path)
External(~str, ~str),
ResolvedPath {
path: Path,
typarams: Option<~[TyParamBound]>,
did: ast::DefId
},
// I have no idea how to usefully use this.
TyParamBinder(ast::NodeId),
/// For parameterized types, so the consumer of the JSON don't go looking
Expand Down Expand Up @@ -736,10 +738,28 @@ impl Clean<VariantKind> for ast::variant_kind {
}
}

impl Clean<~str> for syntax::codemap::Span {
fn clean(&self) -> ~str {
let cm = local_data::get(super::ctxtkey, |x| x.unwrap().clone()).sess.codemap;
cm.span_to_str(*self)
#[deriving(Clone, Encodable, Decodable)]
pub struct Span {
filename: ~str,
loline: uint,
locol: uint,
hiline: uint,
hicol: uint,
}

impl Clean<Span> for syntax::codemap::Span {
fn clean(&self) -> Span {
let cm = local_data::get(super::ctxtkey, |x| *x.unwrap()).sess.codemap;
let filename = cm.span_to_filename(*self);
let lo = cm.lookup_char_pos(self.lo);
let hi = cm.lookup_char_pos(self.hi);
Span {
filename: filename.to_owned(),
loline: lo.line,
locol: *lo.col,
hiline: hi.line,
hicol: *hi.col,
}
}
}

Expand Down Expand Up @@ -1033,7 +1053,7 @@ trait ToSource {

impl ToSource for syntax::codemap::Span {
fn to_src(&self) -> ~str {
debug!("converting span %s to snippet", self.clean());
debug!("converting span %? to snippet", self.clean());
let cm = local_data::get(super::ctxtkey, |x| x.unwrap().clone()).sess.codemap.clone();
let sn = match cm.span_to_snippet(*self) {
Some(x) => x,
Expand Down Expand Up @@ -1129,39 +1149,7 @@ fn resolve_type(path: Path, tpbs: Option<~[TyParamBound]>,
},
x => fail!("resolved type maps to a weird def %?", x),
};

if def_id.crate != ast::CRATE_NODE_ID {
use rustc::metadata::decoder::*;

let sess = local_data::get(super::ctxtkey, |x| *x.unwrap()).sess;
let cratedata = ::rustc::metadata::cstore::get_crate_data(sess.cstore, def_id.crate);
let doc = lookup_item(def_id.node, cratedata.data);
let path = syntax::ast_map::path_to_str_with_sep(item_path(doc), "::", sess.intr());
let ty = match def_like_to_def(item_to_def_like(doc, def_id, def_id.crate)) {
DefFn(*) => ~"fn",
DefTy(*) => ~"enum",
DefTrait(*) => ~"trait",
DefPrimTy(p) => match p {
ty_str => ~"str",
ty_bool => ~"bool",
ty_int(t) => match t.to_str() {
~"" => ~"i",
s => s
},
ty_uint(t) => t.to_str(),
ty_float(t) => t.to_str(),
ty_char => ~"char",
},
DefTyParam(*) => ~"generic",
DefStruct(*) => ~"struct",
DefTyParamBinder(*) => ~"typaram_binder",
x => fail!("resolved external maps to a weird def %?", x),
};
let cname = cratedata.name.to_owned();
External(cname + "::" + path, ty)
} else {
ResolvedPath {path: path.clone(), typarams: tpbs, id: def_id.node}
}
ResolvedPath{ path: path, typarams: tpbs, did: def_id }
}

fn resolve_use_source(path: Path, id: ast::NodeId) -> ImportSource {
Expand Down
44 changes: 44 additions & 0 deletions src/librustdoc/html/escape.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
// Copyright 2013 The Rust Project Developers. See the COPYRIGHT
// file at the top-level directory of this distribution and at
// http://rust-lang.org/COPYRIGHT.
//
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
// option. This file may not be copied, modified, or distributed
// except according to those terms.

use std::fmt;

pub struct Escape<'self>(&'self str);

impl<'self> fmt::Default for Escape<'self> {
fn fmt(s: &Escape<'self>, fmt: &mut fmt::Formatter) {
// Because the internet is always right, turns out there's not that many
// characters to escape: http://stackoverflow.com/questions/7381974
let pile_o_bits = s.as_slice();
let mut last = 0;
for (i, ch) in s.byte_iter().enumerate() {
match ch as char {
'<' | '>' | '&' | '\'' | '"' => {
fmt.buf.write(pile_o_bits.slice(last, i).as_bytes());
let s = match ch as char {
'>' => "&gt;",
'<' => "&lt;",
'&' => "&amp;",
'\'' => "&#39;",
'"' => "&quot;",
_ => unreachable!()
};
fmt.buf.write(s.as_bytes());
last = i + 1;
}
_ => {}
}
}

if last < s.len() {
fmt.buf.write(pile_o_bits.slice_from(last).as_bytes());
}
}
}
25 changes: 11 additions & 14 deletions src/librustdoc/html/format.rs
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,7 @@ impl fmt::Default for clean::Path {
}
}

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

do local_data::get(cache_key) |cache| {
do cache.unwrap().read |cache| {
match cache.paths.find(&id) {
match cache.paths.find(&did.node) {
// This is a documented path, link to it!
Some(&(ref fqp, shortty)) => {
// FIXME(#9539): this is_local check should not exist
Some(&(ref fqp, shortty)) if ast_util::is_local(did) => {
let fqn = fqp.connect("::");
let same = loc.iter().zip(fqp.iter())
.take_while(|&(a, b)| *a == *b).len();
Expand Down Expand Up @@ -180,7 +181,7 @@ fn resolved_path(w: &mut io::Writer, id: ast::NodeId,
write!(w, "<a class='{}' href='{}' title='{}'>{}</a>{}",
shortty, url, fqn, last.name, generics);
}
None => {
_ => {
if print_all {
let amt = path.segments.len() - 1;
for seg in path.segments.iter().take(amt) {
Expand All @@ -205,8 +206,8 @@ impl fmt::Default for clean::Type {
}
}
}
clean::ResolvedPath{id, typarams: ref typarams, path: ref path} => {
resolved_path(f.buf, id, path, false);
clean::ResolvedPath{did, typarams: ref typarams, path: ref path} => {
resolved_path(f.buf, did, path, false);
match *typarams {
Some(ref params) => {
f.buf.write("&lt;".as_bytes());
Expand All @@ -219,10 +220,6 @@ impl fmt::Default for clean::Type {
None => {}
}
}
// XXX: this should be a link
clean::External(ref a, _) => {
write!(f.buf, "{}", *a);
}
clean::Self(*) => f.buf.write("Self".as_bytes()),
clean::Primitive(prim) => {
let s = match prim {
Expand Down Expand Up @@ -421,8 +418,8 @@ impl fmt::Default for clean::ViewPath {
impl fmt::Default for clean::ImportSource {
fn fmt(v: &clean::ImportSource, f: &mut fmt::Formatter) {
match v.did {
Some(did) if ast_util::is_local(did) => {
resolved_path(f.buf, did.node, &v.path, true);
Some(did) => {
resolved_path(f.buf, did, &v.path, true);
}
_ => {
for (i, seg) in v.path.segments.iter().enumerate() {
Expand All @@ -437,7 +434,7 @@ impl fmt::Default for clean::ImportSource {
impl fmt::Default for clean::ViewListIdent {
fn fmt(v: &clean::ViewListIdent, f: &mut fmt::Formatter) {
match v.source {
Some(did) if ast_util::is_local(did) => {
Some(did) => {
let path = clean::Path {
global: false,
segments: ~[clean::PathSegment {
Expand All @@ -446,7 +443,7 @@ impl fmt::Default for clean::ViewListIdent {
types: ~[],
}]
};
resolved_path(f.buf, did.node, &path, false);
resolved_path(f.buf, did, &path, false);
}
_ => write!(f.buf, "{}", v.name),
}
Expand Down
3 changes: 2 additions & 1 deletion src/librustdoc/html/layout.rs
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,8 @@ pub fn render<T: fmt::Default, S: fmt::Default>(
</form>
</nav>
<section class=\"content {ty}\">{content}</section>
<section id='main' class=\"content {ty}\">{content}</section>
<section id='search' class=\"content hidden\"></section>
<section class=\"footer\"></section>
Expand Down
Loading

0 comments on commit 9cc11ca

Please sign in to comment.