1 // Copyright 2013-2014 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.
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.
11 //! Rustdoc's HTML Rendering module
13 //! This modules contains the bulk of the logic necessary for rendering a
14 //! rustdoc `clean::Crate` instance to a set of static HTML pages. This
15 //! rendering process is largely driven by the `format!` syntax extension to
16 //! perform all I/O into files and streams.
18 //! The rendering process is largely driven by the `Context` and `Cache`
19 //! structures. The cache is pre-populated by crawling the crate in question,
20 //! and then it is shared among the various rendering tasks. The cache is meant
21 //! to be a fairly large structure not implementing `Clone` (because it's shared
22 //! among tasks). The context, however, should be a lightweight structure. This
23 //! is cloned per-task and contains information about what is currently being
26 //! In order to speed up rendering (mostly because of markdown rendering), the
27 //! rendering process has been parallelized. This parallelization is only
28 //! exposed through the `crate` method on the context, and then also from the
29 //! fact that the shared cache is stored in TLS (and must be accessed as such).
31 //! In addition to rendering the crate itself, this module is also responsible
32 //! for creating the corresponding search index and source file renderings.
33 //! These tasks are not parallelized (they haven't been a bottleneck yet), and
34 //! both occur before the crate is rendered.
36 use collections::{HashMap, HashSet};
38 use std::io::{fs, File, BufferedWriter, MemWriter, BufferedReader};
41 use std::string::String;
44 use serialize::json::ToJson;
48 use syntax::parse::token::InternedString;
49 use rustc::util::nodemap::NodeSet;
54 use html::format::{VisSpace, Method, FnStyleSpace};
56 use html::item_type::{ItemType, shortty};
59 use html::markdown::Markdown;
62 /// Major driving force in all rustdoc rendering. This contains information
63 /// about where in the tree-like hierarchy rendering is occurring and controls
64 /// how the current page is being rendered.
66 /// It is intended that this context is a lightweight object which can be fairly
67 /// easily cloned because it is cloned per work-job (about once per item in the
71 /// Current hierarchy of components leading down to what's currently being
73 pub current: Vec<String> ,
74 /// String representation of how to get back to the root path of the 'doc/'
75 /// folder in terms of a relative URL.
76 pub root_path: String,
77 /// The current destination folder of where HTML artifacts should be placed.
78 /// This changes as the context descends into the module hierarchy.
80 /// This describes the layout of each page, and is not modified after
81 /// creation of the context (contains info like the favicon)
82 pub layout: layout::Layout,
83 /// This map is a list of what should be displayed on the sidebar of the
84 /// current page. The key is the section header (traits, modules,
85 /// functions), and the value is the list of containers belonging to this
86 /// header. This map will change depending on the surrounding context of the
88 pub sidebar: HashMap<String, Vec<String>>,
89 /// This flag indicates whether [src] links should be generated or not. If
90 /// the source files are present in the html rendering, then this will be
92 pub include_sources: bool,
95 /// Indicates where an external crate can be found.
96 pub enum ExternalLocation {
97 /// Remote URL root of the external crate
99 /// This external crate can be found in the local doc/ folder
101 /// The external crate could not be found.
105 /// Metadata about an implementor of a trait.
106 pub struct Implementor {
107 generics: clean::Generics,
112 /// This cache is used to store information about the `clean::Crate` being
113 /// rendered in order to provide more useful documentation. This contains
114 /// information like all implementors of a trait, all traits a type implements,
115 /// documentation for all known traits, etc.
117 /// This structure purposefully does not implement `Clone` because it's intended
118 /// to be a fairly large and expensive structure to clone. Instead this adheres
119 /// to `Send` so it may be stored in a `Arc` instance and shared among the various
122 /// Mapping of typaram ids to the name of the type parameter. This is used
123 /// when pretty-printing a type (so pretty printing doesn't have to
124 /// painfully maintain a context like this)
125 pub typarams: HashMap<ast::DefId, String>,
127 /// Maps a type id to all known implementations for that type. This is only
128 /// recognized for intra-crate `ResolvedPath` types, and is used to print
129 /// out extra documentation on the page of an enum/struct.
131 /// The values of the map are a list of implementations and documentation
132 /// found on that implementation.
133 pub impls: HashMap<ast::DefId, Vec<(clean::Impl, Option<String>)>>,
135 /// Maintains a mapping of local crate node ids to the fully qualified name
136 /// and "short type description" of that node. This is used when generating
137 /// URLs when a type is being linked to. External paths are not located in
138 /// this map because the `External` type itself has all the information
140 pub paths: HashMap<ast::DefId, (Vec<String>, ItemType)>,
142 /// Similar to `paths`, but only holds external paths. This is only used for
143 /// generating explicit hyperlinks to other crates.
144 pub external_paths: HashMap<ast::DefId, Vec<String>>,
146 /// This map contains information about all known traits of this crate.
147 /// Implementations of a crate should inherit the documentation of the
148 /// parent trait if no extra documentation is specified, and default methods
149 /// should show up in documentation about trait implementations.
150 pub traits: HashMap<ast::DefId, clean::Trait>,
152 /// When rendering traits, it's often useful to be able to list all
153 /// implementors of the trait, and this mapping is exactly, that: a mapping
154 /// of trait ids to the list of known implementors of the trait
155 pub implementors: HashMap<ast::DefId, Vec<Implementor>>,
157 /// Cache of where external crate documentation can be found.
158 pub extern_locations: HashMap<ast::CrateNum, ExternalLocation>,
160 /// Cache of where documentation for primitives can be found.
161 pub primitive_locations: HashMap<clean::Primitive, ast::CrateNum>,
163 /// Set of definitions which have been inlined from external crates.
164 pub inlined: HashSet<ast::DefId>,
166 // Private fields only used when initially crawling a crate to build a cache
169 parent_stack: Vec<ast::DefId>,
170 search_index: Vec<IndexItem>,
172 public_items: NodeSet,
174 // In rare case where a structure is defined in one module but implemented
175 // in another, if the implementing module is parsed before defining module,
176 // then the fully qualified name of the structure isn't presented in `paths`
177 // yet when its implementation methods are being indexed. Caches such methods
178 // and their parent id here and indexes them at the end of crate parsing.
179 orphan_methods: Vec<(ast::NodeId, clean::Item)>,
182 /// Helper struct to render all source code to HTML pages
183 struct SourceCollector<'a> {
186 /// Processed source-file paths
187 seen: HashSet<String>,
188 /// Root destination to place all HTML output into
192 /// Wrapper struct to render the source code of a file. This will do things like
193 /// adding line numbers to the left-hand side.
194 struct Source<'a>(&'a str);
196 // Helper structs for rendering items/sidebars and carrying along contextual
199 struct Item<'a> { cx: &'a Context, item: &'a clean::Item, }
200 struct Sidebar<'a> { cx: &'a Context, item: &'a clean::Item, }
202 /// Struct representing one entry in the JS search index. These are all emitted
203 /// by hand to a large JS file at the end of cache-creation.
209 parent: Option<ast::DefId>,
212 // TLS keys used to carry information around during rendering.
214 local_data_key!(pub cache_key: Arc<Cache>)
215 local_data_key!(pub current_location_key: Vec<String> )
217 /// Generates the documentation for `crate` into the directory `dst`
218 pub fn run(mut krate: clean::Crate, dst: Path) -> io::IoResult<()> {
219 let mut cx = Context {
222 root_path: String::new(),
223 sidebar: HashMap::new(),
224 layout: layout::Layout {
225 logo: "".to_string(),
226 favicon: "".to_string(),
227 krate: krate.name.clone(),
229 include_sources: true,
231 try!(mkdir(&cx.dst));
233 // Crawl the crate attributes looking for attributes which control how we're
234 // going to emit HTML
235 match krate.module.as_ref().map(|m| m.doc_list().unwrap_or(&[])) {
237 for attr in attrs.iter() {
239 clean::NameValue(ref x, ref s)
240 if "html_favicon_url" == x.as_slice() => {
241 cx.layout.favicon = s.to_string();
243 clean::NameValue(ref x, ref s)
244 if "html_logo_url" == x.as_slice() => {
245 cx.layout.logo = s.to_string();
248 if "html_no_source" == x.as_slice() => {
249 cx.include_sources = false;
258 // Crawl the crate to build various caches used for the output
259 let analysis = ::analysiskey.get();
260 let public_items = analysis.as_ref().map(|a| a.public_items.clone());
261 let public_items = public_items.unwrap_or(NodeSet::new());
262 let paths: HashMap<ast::DefId, (Vec<String>, ItemType)> =
263 analysis.as_ref().map(|a| {
264 let paths = a.external_paths.borrow_mut().take_unwrap();
265 paths.move_iter().map(|(k, (v, t))| {
267 clean::TypeStruct => item_type::Struct,
268 clean::TypeEnum => item_type::Enum,
269 clean::TypeFunction => item_type::Function,
270 clean::TypeTrait => item_type::Trait,
271 clean::TypeModule => item_type::Module,
272 clean::TypeStatic => item_type::Static,
273 clean::TypeVariant => item_type::Variant,
276 }).unwrap_or(HashMap::new());
277 let mut cache = Cache {
278 impls: HashMap::new(),
279 external_paths: paths.iter().map(|(&k, &(ref v, _))| (k, v.clone()))
282 implementors: HashMap::new(),
284 parent_stack: Vec::new(),
285 search_index: Vec::new(),
286 extern_locations: HashMap::new(),
287 primitive_locations: HashMap::new(),
289 public_items: public_items,
290 orphan_methods: Vec::new(),
291 traits: analysis.as_ref().map(|a| {
292 a.external_traits.borrow_mut().take_unwrap()
293 }).unwrap_or(HashMap::new()),
294 typarams: analysis.as_ref().map(|a| {
295 a.external_typarams.borrow_mut().take_unwrap()
296 }).unwrap_or(HashMap::new()),
297 inlined: analysis.as_ref().map(|a| {
298 a.inlined.borrow_mut().take_unwrap()
299 }).unwrap_or(HashSet::new()),
301 cache.stack.push(krate.name.clone());
302 krate = cache.fold_crate(krate);
304 // Cache where all our extern crates are located
305 for &(n, ref e) in krate.externs.iter() {
306 cache.extern_locations.insert(n, extern_location(e, &cx.dst));
307 let did = ast::DefId { krate: n, node: ast::CRATE_NODE_ID };
308 cache.paths.insert(did, (vec![e.name.to_string()], item_type::Module));
311 // Cache where all known primitives have their documentation located.
313 // Favor linking to as local extern as possible, so iterate all crates in
314 // reverse topological order.
315 for &(n, ref e) in krate.externs.iter().rev() {
316 for &prim in e.primitives.iter() {
317 cache.primitive_locations.insert(prim, n);
320 for &prim in krate.primitives.iter() {
321 cache.primitive_locations.insert(prim, ast::LOCAL_CRATE);
324 // Build our search index
325 let index = try!(build_index(&krate, &mut cache));
327 // Freeze the cache now that the index has been built. Put an Arc into TLS
328 // for future parallelization opportunities
329 let cache = Arc::new(cache);
330 cache_key.replace(Some(cache.clone()));
331 current_location_key.replace(Some(Vec::new()));
333 try!(write_shared(&cx, &krate, &*cache, index));
334 let krate = try!(render_sources(&mut cx, krate));
336 // And finally render the whole crate's documentation
340 fn build_index(krate: &clean::Crate, cache: &mut Cache) -> io::IoResult<String> {
341 // Build the search index from the collected metadata
342 let mut nodeid_to_pathid = HashMap::new();
343 let mut pathid_to_nodeid = Vec::new();
345 let Cache { ref mut search_index,
347 ref mut paths, .. } = *cache;
349 // Attach all orphan methods to the type's definition if the type
350 // has since been learned.
351 for &(pid, ref item) in orphan_methods.iter() {
352 let did = ast_util::local_def(pid);
353 match paths.find(&did) {
354 Some(&(ref fqp, _)) => {
355 search_index.push(IndexItem {
357 name: item.name.clone().unwrap(),
358 path: fqp.slice_to(fqp.len() - 1).connect("::")
360 desc: shorter(item.doc_value()).to_string(),
368 // Reduce `NodeId` in paths into smaller sequential numbers,
369 // and prune the paths that do not appear in the index.
370 for item in search_index.iter() {
373 if !nodeid_to_pathid.contains_key(&nodeid) {
374 let pathid = pathid_to_nodeid.len();
375 nodeid_to_pathid.insert(nodeid, pathid);
376 pathid_to_nodeid.push(nodeid);
382 assert_eq!(nodeid_to_pathid.len(), pathid_to_nodeid.len());
385 // Collect the index into a string
386 let mut w = MemWriter::new();
387 try!(write!(&mut w, r#"searchIndex['{}'] = \{"items":["#, krate.name));
389 let mut lastpath = "".to_string();
390 for (i, item) in cache.search_index.iter().enumerate() {
391 // Omit the path if it is same to that of the prior item.
393 if lastpath.as_slice() == item.path.as_slice() {
396 lastpath = item.path.to_string();
397 path = item.path.as_slice();
401 try!(write!(&mut w, ","));
403 try!(write!(&mut w, r#"[{:u},"{}","{}",{}"#,
404 item.ty, item.name, path,
405 item.desc.to_json().to_str()));
408 let pathid = *nodeid_to_pathid.find(&nodeid).unwrap();
409 try!(write!(&mut w, ",{}", pathid));
413 try!(write!(&mut w, "]"));
416 try!(write!(&mut w, r#"],"paths":["#));
418 for (i, &did) in pathid_to_nodeid.iter().enumerate() {
419 let &(ref fqp, short) = cache.paths.find(&did).unwrap();
421 try!(write!(&mut w, ","));
423 try!(write!(&mut w, r#"[{:u},"{}"]"#,
424 short, *fqp.last().unwrap()));
427 try!(write!(&mut w, r"]\};"));
429 Ok(str::from_utf8(w.unwrap().as_slice()).unwrap().to_string())
432 fn write_shared(cx: &Context,
433 krate: &clean::Crate,
435 search_index: String) -> io::IoResult<()> {
436 // Write out the shared files. Note that these are shared among all rustdoc
437 // docs placed in the output directory, so this needs to be a synchronized
438 // operation with respect to all other rustdocs running around.
439 try!(mkdir(&cx.dst));
440 let _lock = ::flock::Lock::new(&cx.dst.join(".lock"));
442 // Add all the static files. These may already exist, but we just
443 // overwrite them anyway to make sure that they're fresh and up-to-date.
444 try!(write(cx.dst.join("jquery.js"),
445 include_bin!("static/jquery-2.1.0.min.js")));
446 try!(write(cx.dst.join("main.js"), include_bin!("static/main.js")));
447 try!(write(cx.dst.join("main.css"), include_bin!("static/main.css")));
448 try!(write(cx.dst.join("normalize.css"),
449 include_bin!("static/normalize.css")));
450 try!(write(cx.dst.join("FiraSans-Regular.woff"),
451 include_bin!("static/FiraSans-Regular.woff")));
452 try!(write(cx.dst.join("FiraSans-Medium.woff"),
453 include_bin!("static/FiraSans-Medium.woff")));
454 try!(write(cx.dst.join("Heuristica-Regular.woff"),
455 include_bin!("static/Heuristica-Regular.woff")));
456 try!(write(cx.dst.join("Heuristica-Italic.woff"),
457 include_bin!("static/Heuristica-Italic.woff")));
458 try!(write(cx.dst.join("Heuristica-Bold.woff"),
459 include_bin!("static/Heuristica-Bold.woff")));
461 fn collect(path: &Path, krate: &str,
462 key: &str) -> io::IoResult<Vec<String>> {
463 let mut ret = Vec::new();
465 for line in BufferedReader::new(File::open(path)).lines() {
466 let line = try!(line);
467 if !line.as_slice().starts_with(key) {
470 if line.as_slice().starts_with(
471 format!("{}['{}']", key, krate).as_slice()) {
474 ret.push(line.to_string());
480 // Update the search index
481 let dst = cx.dst.join("search-index.js");
482 let all_indexes = try!(collect(&dst, krate.name.as_slice(),
484 let mut w = try!(File::create(&dst));
485 try!(writeln!(&mut w, r"var searchIndex = \{\};"));
486 try!(writeln!(&mut w, "{}", search_index));
487 for index in all_indexes.iter() {
488 try!(writeln!(&mut w, "{}", *index));
490 try!(writeln!(&mut w, "initSearch(searchIndex);"));
492 // Update the list of all implementors for traits
493 let dst = cx.dst.join("implementors");
495 for (&did, imps) in cache.implementors.iter() {
496 let &(ref remote_path, remote_item_type) = cache.paths.get(&did);
498 let mut mydst = dst.clone();
499 for part in remote_path.slice_to(remote_path.len() - 1).iter() {
500 mydst.push(part.as_slice());
503 mydst.push(format!("{}.{}.js",
504 remote_item_type.to_static_str(),
505 *remote_path.get(remote_path.len() - 1)));
506 let all_implementors = try!(collect(&mydst, krate.name.as_slice(),
509 try!(mkdir(&mydst.dir_path()));
510 let mut f = BufferedWriter::new(try!(File::create(&mydst)));
511 try!(writeln!(&mut f, r"(function() \{var implementors = \{\};"));
513 for implementor in all_implementors.iter() {
514 try!(write!(&mut f, "{}", *implementor));
517 try!(write!(&mut f, r"implementors['{}'] = [", krate.name));
518 for imp in imps.iter() {
519 try!(write!(&mut f, r#""impl{} {} for {}","#,
520 imp.generics, imp.trait_, imp.for_));
522 try!(writeln!(&mut f, r"];"));
523 try!(writeln!(&mut f, "{}", r"
524 if (window.register_implementors) {
525 window.register_implementors(implementors);
527 window.pending_implementors = implementors;
530 try!(writeln!(&mut f, r"\})()"));
535 fn render_sources(cx: &mut Context,
536 krate: clean::Crate) -> io::IoResult<clean::Crate> {
537 info!("emitting source files");
538 let dst = cx.dst.join("src");
540 let dst = dst.join(krate.name.as_slice());
542 let mut folder = SourceCollector {
544 seen: HashSet::new(),
547 // skip all invalid spans
548 folder.seen.insert("".to_string());
549 Ok(folder.fold_crate(krate))
552 /// Writes the entire contents of a string to a destination, not attempting to
553 /// catch any errors.
554 fn write(dst: Path, contents: &[u8]) -> io::IoResult<()> {
555 File::create(&dst).write(contents)
558 /// Makes a directory on the filesystem, failing the task if an error occurs and
559 /// skipping if the directory already exists.
560 fn mkdir(path: &Path) -> io::IoResult<()> {
562 fs::mkdir(path, io::UserRWX)
568 /// Takes a path to a source file and cleans the path to it. This canonicalizes
569 /// things like ".." to components which preserve the "top down" hierarchy of a
570 /// static HTML tree.
571 // FIXME (#9639): The closure should deal with &[u8] instead of &str
572 fn clean_srcpath(src: &[u8], f: |&str|) {
573 let p = Path::new(src);
574 if p.as_vec() != bytes!(".") {
575 for c in p.str_components().map(|x|x.unwrap()) {
585 /// Attempts to find where an external crate is located, given that we're
586 /// rendering in to the specified source destination.
587 fn extern_location(e: &clean::ExternalCrate, dst: &Path) -> ExternalLocation {
588 // See if there's documentation generated into the local directory
589 let local_location = dst.join(e.name.as_slice());
590 if local_location.is_dir() {
594 // Failing that, see if there's an attribute specifying where to find this
596 for attr in e.attrs.iter() {
598 clean::List(ref x, ref list) if "doc" == x.as_slice() => {
599 for attr in list.iter() {
601 clean::NameValue(ref x, ref s)
602 if "html_root_url" == x.as_slice() => {
603 if s.as_slice().ends_with("/") {
604 return Remote(s.to_string());
606 return Remote(format!("{}/", s));
616 // Well, at least we tried.
620 impl<'a> DocFolder for SourceCollector<'a> {
621 fn fold_item(&mut self, item: clean::Item) -> Option<clean::Item> {
622 // If we're including source files, and we haven't seen this file yet,
623 // then we need to render it out to the filesystem
624 if self.cx.include_sources && !self.seen.contains(&item.source.filename) {
626 // If it turns out that we couldn't read this file, then we probably
627 // can't read any of the files (generating html output from json or
628 // something like that), so just don't include sources for the
629 // entire crate. The other option is maintaining this mapping on a
630 // per-file basis, but that's probably not worth it...
632 .include_sources = match self.emit_source(item.source
637 println!("warning: source code was requested to be rendered, \
638 but processing `{}` had an error: {}",
639 item.source.filename, e);
640 println!(" skipping rendering of source code");
644 self.seen.insert(item.source.filename.clone());
647 self.fold_item_recur(item)
651 impl<'a> SourceCollector<'a> {
652 /// Renders the given filename into its corresponding HTML source file.
653 fn emit_source(&mut self, filename: &str) -> io::IoResult<()> {
654 let p = Path::new(filename);
656 // If we couldn't open this file, then just returns because it
657 // probably means that it's some standard library macro thing and we
658 // can't have the source to it anyway.
659 let contents = match File::open(&p).read_to_end() {
661 // macros from other libraries get special filenames which we can
663 Err(..) if filename.starts_with("<") &&
664 filename.ends_with("macros>") => return Ok(()),
665 Err(e) => return Err(e)
667 let contents = str::from_utf8(contents.as_slice()).unwrap();
669 // Remove the utf-8 BOM if any
670 let contents = if contents.starts_with("\ufeff") {
671 contents.as_slice().slice_from(3)
676 // Create the intermediate directories
677 let mut cur = self.dst.clone();
678 let mut root_path = String::from_str("../../");
679 clean_srcpath(p.dirname(), |component| {
681 mkdir(&cur).unwrap();
682 root_path.push_str("../");
685 cur.push(Vec::from_slice(p.filename().expect("source has no filename"))
686 .append(bytes!(".html")));
687 let mut w = BufferedWriter::new(try!(File::create(&cur)));
689 let title = format!("{} -- source", cur.filename_display());
690 let page = layout::Page {
691 title: title.as_slice(),
693 root_path: root_path.as_slice(),
695 try!(layout::render(&mut w as &mut Writer, &self.cx.layout,
696 &page, &(""), &Source(contents)));
702 impl DocFolder for Cache {
703 fn fold_item(&mut self, item: clean::Item) -> Option<clean::Item> {
704 // If this is a private module, we don't want it in the search index.
705 let orig_privmod = match item.inner {
706 clean::ModuleItem(..) => {
707 let prev = self.privmod;
708 self.privmod = prev || item.visibility != Some(ast::Public);
714 // Register any generics to their corresponding string. This is used
715 // when pretty-printing types
717 clean::StructItem(ref s) => self.generics(&s.generics),
718 clean::EnumItem(ref e) => self.generics(&e.generics),
719 clean::FunctionItem(ref f) => self.generics(&f.generics),
720 clean::TypedefItem(ref t) => self.generics(&t.generics),
721 clean::TraitItem(ref t) => self.generics(&t.generics),
722 clean::ImplItem(ref i) => self.generics(&i.generics),
723 clean::TyMethodItem(ref i) => self.generics(&i.generics),
724 clean::MethodItem(ref i) => self.generics(&i.generics),
725 clean::ForeignFunctionItem(ref f) => self.generics(&f.generics),
729 // Propagate a trait methods' documentation to all implementors of the
732 clean::TraitItem(ref t) => {
733 self.traits.insert(item.def_id, t.clone());
738 // Collect all the implementors of traits.
740 clean::ImplItem(ref i) => {
742 Some(clean::ResolvedPath{ did, .. }) => {
743 let v = self.implementors.find_or_insert_with(did, |_| {
747 generics: i.generics.clone(),
748 trait_: i.trait_.get_ref().clone(),
749 for_: i.for_.clone(),
752 Some(..) | None => {}
758 // Index this method for searching later on
761 let parent = match item.inner {
762 clean::TyMethodItem(..) |
763 clean::StructFieldItem(..) |
764 clean::VariantItem(..) => {
765 (Some(*self.parent_stack.last().unwrap()),
766 Some(self.stack.slice_to(self.stack.len() - 1)))
768 clean::MethodItem(..) => {
769 if self.parent_stack.len() == 0 {
772 let last = self.parent_stack.last().unwrap();
774 let path = match self.paths.find(&did) {
775 Some(&(_, item_type::Trait)) =>
776 Some(self.stack.slice_to(self.stack.len() - 1)),
777 // The current stack not necessarily has correlation for
778 // where the type was defined. On the other hand,
779 // `paths` always has the right information if present.
780 Some(&(ref fqp, item_type::Struct)) |
781 Some(&(ref fqp, item_type::Enum)) =>
782 Some(fqp.slice_to(fqp.len() - 1)),
783 Some(..) => Some(self.stack.as_slice()),
789 _ => (None, Some(self.stack.as_slice()))
792 (parent, Some(path)) if !self.privmod => {
793 self.search_index.push(IndexItem {
796 path: path.connect("::").to_string(),
797 desc: shorter(item.doc_value()).to_string(),
801 (Some(parent), None) if !self.privmod => {
802 if ast_util::is_local(parent) {
803 // We have a parent, but we don't know where they're
804 // defined yet. Wait for later to index this item.
805 self.orphan_methods.push((parent.node, item.clone()))
814 // Keep track of the fully qualified path for this item.
815 let pushed = if item.name.is_some() {
816 let n = item.name.get_ref();
818 self.stack.push(n.to_string());
823 clean::StructItem(..) | clean::EnumItem(..) |
824 clean::TypedefItem(..) | clean::TraitItem(..) |
825 clean::FunctionItem(..) | clean::ModuleItem(..) |
826 clean::ForeignFunctionItem(..) => {
827 // Reexported items mean that the same id can show up twice
828 // in the rustdoc ast that we're looking at. We know,
829 // however, that a reexported item doesn't show up in the
830 // `public_items` map, so we can skip inserting into the
831 // paths map if there was already an entry present and we're
832 // not a public item.
833 let id = item.def_id.node;
834 if !self.paths.contains_key(&item.def_id) ||
835 !ast_util::is_local(item.def_id) ||
836 self.public_items.contains(&id) {
837 self.paths.insert(item.def_id,
838 (self.stack.clone(), shortty(&item)));
841 // link variants to their parent enum because pages aren't emitted
843 clean::VariantItem(..) => {
844 let mut stack = self.stack.clone();
846 self.paths.insert(item.def_id, (stack, item_type::Enum));
851 // Maintain the parent stack
852 let parent_pushed = match item.inner {
853 clean::TraitItem(..) | clean::EnumItem(..) | clean::StructItem(..) => {
854 self.parent_stack.push(item.def_id);
857 clean::ImplItem(ref i) => {
859 clean::ResolvedPath{ did, .. } => {
860 self.parent_stack.push(did);
869 // Once we've recursively found all the generics, then hoard off all the
870 // implementations elsewhere
871 let ret = match self.fold_item_recur(item) {
874 clean::Item{ attrs, inner: clean::ImplItem(i), .. } => {
875 use clean::{Primitive, Vector, ResolvedPath, BorrowedRef};
876 use clean::{FixedVector, Slice, Tuple, PrimitiveTuple};
878 // extract relevant documentation for this impl
879 let dox = match attrs.move_iter().find(|a| {
881 clean::NameValue(ref x, _)
882 if "doc" == x.as_slice() => {
888 Some(clean::NameValue(_, dox)) => Some(dox),
889 Some(..) | None => None,
892 // Figure out the id of this impl. This may map to a
893 // primitive rather than always to a struct/enum.
894 let did = match i.for_ {
895 ResolvedPath { did, .. } => Some(did),
897 // References to primitives are picked up as well to
898 // recognize implementations for &str, this may not
899 // be necessary in a DST world.
901 BorrowedRef { type_: box Primitive(p), ..} =>
903 Some(ast_util::local_def(p.to_node_id()))
906 // In a DST world, we may only need
907 // Vector/FixedVector, but for now we also pick up
908 // borrowed references
909 Vector(..) | FixedVector(..) |
910 BorrowedRef{ type_: box Vector(..), .. } |
911 BorrowedRef{ type_: box FixedVector(..), .. } =>
913 Some(ast_util::local_def(Slice.to_node_id()))
917 let id = PrimitiveTuple.to_node_id();
918 Some(ast_util::local_def(id))
926 let v = self.impls.find_or_insert_with(did, |_| {
935 // Private modules may survive the strip-private pass if
936 // they contain impls for public types, but those will get
938 clean::Item { inner: clean::ModuleItem(ref m),
940 if (m.items.len() == 0 &&
941 item.doc_value().is_none()) ||
942 visibility != Some(ast::Public) => None,
950 if pushed { self.stack.pop().unwrap(); }
951 if parent_pushed { self.parent_stack.pop().unwrap(); }
952 self.privmod = orig_privmod;
958 fn generics(&mut self, generics: &clean::Generics) {
959 for typ in generics.type_params.iter() {
960 self.typarams.insert(typ.did, typ.name.clone());
966 /// Recurse in the directory structure and change the "root path" to make
967 /// sure it always points to the top (relatively)
968 fn recurse<T>(&mut self, s: String, f: |&mut Context| -> T) -> T {
970 fail!("what {:?}", self);
972 let prev = self.dst.clone();
973 self.dst.push(s.as_slice());
974 self.root_path.push_str("../");
975 self.current.push(s);
977 info!("Recursing into {}", self.dst.display());
979 mkdir(&self.dst).unwrap();
982 info!("Recursed; leaving {}", self.dst.display());
984 // Go back to where we were at
986 let len = self.root_path.len();
987 self.root_path.truncate(len - 3);
988 self.current.pop().unwrap();
993 /// Main method for rendering a crate.
995 /// This currently isn't parallelized, but it'd be pretty easy to add
996 /// parallelization to this function.
997 fn krate(self, mut krate: clean::Crate) -> io::IoResult<()> {
998 let mut item = match krate.module.take() {
1000 None => return Ok(())
1002 item.name = Some(krate.name);
1004 let mut work = vec!((self, item));
1007 Some((mut cx, item)) => try!(cx.item(item, |cx, item| {
1008 work.push((cx.clone(), item));
1016 /// Non-parellelized version of rendering an item. This will take the input
1017 /// item, render its contents, and then invoke the specified closure with
1018 /// all sub-items which need to be rendered.
1020 /// The rendering driver uses this closure to queue up more work.
1021 fn item(&mut self, item: clean::Item,
1022 f: |&mut Context, clean::Item|) -> io::IoResult<()> {
1023 fn render(w: io::File, cx: &mut Context, it: &clean::Item,
1024 pushname: bool) -> io::IoResult<()> {
1025 info!("Rendering an item to {}", w.path().display());
1026 // A little unfortunate that this is done like this, but it sure
1027 // does make formatting *a lot* nicer.
1028 current_location_key.replace(Some(cx.current.clone()));
1030 let mut title = cx.current.connect("::");
1032 if title.len() > 0 {
1033 title.push_str("::");
1035 title.push_str(it.name.get_ref().as_slice());
1037 title.push_str(" - Rust");
1038 let page = layout::Page {
1039 ty: shortty(it).to_static_str(),
1040 root_path: cx.root_path.as_slice(),
1041 title: title.as_slice(),
1044 markdown::reset_headers();
1046 // We have a huge number of calls to write, so try to alleviate some
1047 // of the pain by using a buffered writer instead of invoking the
1048 // write sycall all the time.
1049 let mut writer = BufferedWriter::new(w);
1050 try!(layout::render(&mut writer as &mut Writer, &cx.layout, &page,
1051 &Sidebar{ cx: cx, item: it },
1052 &Item{ cx: cx, item: it }));
1057 // modules are special because they add a namespace. We also need to
1058 // recurse into the items of the module as well.
1059 clean::ModuleItem(..) => {
1060 let name = item.name.get_ref().to_string();
1061 let mut item = Some(item);
1062 self.recurse(name, |this| {
1063 let item = item.take_unwrap();
1064 let dst = this.dst.join("index.html");
1065 let dst = try!(File::create(&dst));
1066 try!(render(dst, this, &item, false));
1068 let m = match item.inner {
1069 clean::ModuleItem(m) => m,
1072 this.sidebar = build_sidebar(&m);
1073 for item in m.items.move_iter() {
1080 // Things which don't have names (like impls) don't get special
1081 // pages dedicated to them.
1082 _ if item.name.is_some() => {
1083 let dst = self.dst.join(item_path(&item));
1084 let dst = try!(File::create(&dst));
1085 render(dst, self, &item, true)
1094 fn ismodule(&self) -> bool {
1095 match self.item.inner {
1096 clean::ModuleItem(..) => true, _ => false
1100 /// Generate a url appropriate for an `href` attribute back to the source of
1103 /// The url generated, when clicked, will redirect the browser back to the
1104 /// original source code.
1106 /// If `None` is returned, then a source link couldn't be generated. This
1107 /// may happen, for example, with externally inlined items where the source
1108 /// of their crate documentation isn't known.
1109 fn href(&self) -> Option<String> {
1110 // If this item is part of the local crate, then we're guaranteed to
1111 // know the span, so we plow forward and generate a proper url. The url
1112 // has anchors for the line numbers that we're linking to.
1113 if ast_util::is_local(self.item.def_id) {
1114 let mut path = Vec::new();
1115 clean_srcpath(self.item.source.filename.as_bytes(), |component| {
1116 path.push(component.to_string());
1118 let href = if self.item.source.loline == self.item.source.hiline {
1119 format!("{}", self.item.source.loline)
1122 self.item.source.loline,
1123 self.item.source.hiline)
1125 Some(format!("{root}src/{krate}/{path}.html\\#{href}",
1126 root = self.cx.root_path,
1127 krate = self.cx.layout.krate,
1128 path = path.connect("/"),
1131 // If this item is not part of the local crate, then things get a little
1132 // trickier. We don't actually know the span of the external item, but
1133 // we know that the documentation on the other end knows the span!
1135 // In this case, we generate a link to the *documentation* for this type
1136 // in the original crate. There's an extra URL parameter which says that
1137 // we want to go somewhere else, and the JS on the destination page will
1138 // pick it up and instantly redirect the browser to the source code.
1140 // If we don't know where the external documentation for this crate is
1141 // located, then we return `None`.
1143 let cache = cache_key.get().unwrap();
1144 let path = cache.external_paths.get(&self.item.def_id);
1145 let root = match *cache.extern_locations.get(&self.item.def_id.krate) {
1146 Remote(ref s) => s.to_string(),
1147 Local => self.cx.root_path.clone(),
1148 Unknown => return None,
1150 Some(format!("{root}{path}/{file}?gotosrc={goto}",
1152 path = path.slice_to(path.len() - 1).connect("/"),
1153 file = item_path(self.item),
1154 goto = self.item.def_id.node))
1159 impl<'a> fmt::Show for Item<'a> {
1160 fn fmt(&self, fmt: &mut fmt::Formatter) -> fmt::Result {
1161 // Write the breadcrumb trail header for the top
1162 try!(write!(fmt, "\n<h1 class='fqn'>"));
1163 match self.item.inner {
1164 clean::ModuleItem(ref m) => if m.is_crate {
1165 try!(write!(fmt, "Crate "));
1167 try!(write!(fmt, "Module "));
1169 clean::FunctionItem(..) => try!(write!(fmt, "Function ")),
1170 clean::TraitItem(..) => try!(write!(fmt, "Trait ")),
1171 clean::StructItem(..) => try!(write!(fmt, "Struct ")),
1172 clean::EnumItem(..) => try!(write!(fmt, "Enum ")),
1173 clean::PrimitiveItem(..) => try!(write!(fmt, "Primitive Type ")),
1176 let is_primitive = match self.item.inner {
1177 clean::PrimitiveItem(..) => true,
1181 let cur = self.cx.current.as_slice();
1182 let amt = if self.ismodule() { cur.len() - 1 } else { cur.len() };
1183 for (i, component) in cur.iter().enumerate().take(amt) {
1184 try!(write!(fmt, "<a href='{}index.html'>{}</a>::",
1185 "../".repeat(cur.len() - i - 1),
1186 component.as_slice()));
1189 try!(write!(fmt, "<a class='{}' href=''>{}</a>",
1190 shortty(self.item), self.item.name.get_ref().as_slice()));
1192 // Write stability attributes
1193 match attr::find_stability_generic(self.item.attrs.iter()) {
1194 Some((ref stability, _)) => {
1196 "<a class='stability {lvl}' title='{reason}'>{lvl}</a>",
1197 lvl = stability.level.to_str(),
1198 reason = match stability.text {
1199 Some(ref s) => (*s).clone(),
1200 None => InternedString::new(""),
1208 // When this item is part of a `pub use` in a downstream crate, the
1209 // [src] link in the downstream documentation will actually come back to
1210 // this page, and this link will be auto-clicked. The `id` attribute is
1211 // used to find the link to auto-click.
1212 if self.cx.include_sources && !is_primitive {
1216 "<a class='source' id='src-{}' \
1217 href='{}'>[src]</a>",
1218 self.item.def_id.node, l));
1223 try!(write!(fmt, "</h1>\n"));
1225 match self.item.inner {
1226 clean::ModuleItem(ref m) => {
1227 item_module(fmt, self.cx, self.item, m.items.as_slice())
1229 clean::FunctionItem(ref f) | clean::ForeignFunctionItem(ref f) =>
1230 item_function(fmt, self.item, f),
1231 clean::TraitItem(ref t) => item_trait(fmt, self.cx, self.item, t),
1232 clean::StructItem(ref s) => item_struct(fmt, self.item, s),
1233 clean::EnumItem(ref e) => item_enum(fmt, self.item, e),
1234 clean::TypedefItem(ref t) => item_typedef(fmt, self.item, t),
1235 clean::MacroItem(ref m) => item_macro(fmt, self.item, m),
1236 clean::PrimitiveItem(ref p) => item_primitive(fmt, self.item, p),
1242 fn item_path(item: &clean::Item) -> String {
1244 clean::ModuleItem(..) => {
1245 format!("{}/index.html", item.name.get_ref())
1248 format!("{}.{}.html",
1249 shortty(item).to_static_str(),
1250 *item.name.get_ref())
1255 fn full_path(cx: &Context, item: &clean::Item) -> String {
1256 let mut s = cx.current.connect("::");
1258 s.push_str(item.name.get_ref().as_slice());
1262 fn blank<'a>(s: Option<&'a str>) -> &'a str {
1269 fn shorter<'a>(s: Option<&'a str>) -> &'a str {
1271 Some(s) => match s.find_str("\n\n") {
1272 Some(pos) => s.slice_to(pos),
1279 fn document(w: &mut fmt::Formatter, item: &clean::Item) -> fmt::Result {
1280 match item.doc_value() {
1282 try!(write!(w, "<div class='docblock'>{}</div>", Markdown(s)));
1289 fn item_module(w: &mut fmt::Formatter, cx: &Context,
1290 item: &clean::Item, items: &[clean::Item]) -> fmt::Result {
1291 try!(document(w, item));
1292 debug!("{:?}", items);
1293 let mut indices = Vec::from_fn(items.len(), |i| i);
1295 fn cmp(i1: &clean::Item, i2: &clean::Item, idx1: uint, idx2: uint) -> Ordering {
1296 if shortty(i1) == shortty(i2) {
1297 return i1.name.cmp(&i2.name);
1299 match (&i1.inner, &i2.inner) {
1300 (&clean::ViewItemItem(ref a), &clean::ViewItemItem(ref b)) => {
1301 match (&a.inner, &b.inner) {
1302 (&clean::ExternCrate(..), _) => Less,
1303 (_, &clean::ExternCrate(..)) => Greater,
1304 _ => idx1.cmp(&idx2),
1307 (&clean::ViewItemItem(..), _) => Less,
1308 (_, &clean::ViewItemItem(..)) => Greater,
1309 (&clean::PrimitiveItem(..), _) => Less,
1310 (_, &clean::PrimitiveItem(..)) => Greater,
1311 (&clean::ModuleItem(..), _) => Less,
1312 (_, &clean::ModuleItem(..)) => Greater,
1313 (&clean::MacroItem(..), _) => Less,
1314 (_, &clean::MacroItem(..)) => Greater,
1315 (&clean::StructItem(..), _) => Less,
1316 (_, &clean::StructItem(..)) => Greater,
1317 (&clean::EnumItem(..), _) => Less,
1318 (_, &clean::EnumItem(..)) => Greater,
1319 (&clean::StaticItem(..), _) => Less,
1320 (_, &clean::StaticItem(..)) => Greater,
1321 (&clean::ForeignFunctionItem(..), _) => Less,
1322 (_, &clean::ForeignFunctionItem(..)) => Greater,
1323 (&clean::ForeignStaticItem(..), _) => Less,
1324 (_, &clean::ForeignStaticItem(..)) => Greater,
1325 (&clean::TraitItem(..), _) => Less,
1326 (_, &clean::TraitItem(..)) => Greater,
1327 (&clean::FunctionItem(..), _) => Less,
1328 (_, &clean::FunctionItem(..)) => Greater,
1329 (&clean::TypedefItem(..), _) => Less,
1330 (_, &clean::TypedefItem(..)) => Greater,
1331 _ => idx1.cmp(&idx2),
1335 debug!("{:?}", indices);
1336 indices.sort_by(|&i1, &i2| cmp(&items[i1], &items[i2], i1, i2));
1338 debug!("{:?}", indices);
1339 let mut curty = None;
1340 for &idx in indices.iter() {
1341 let myitem = &items[idx];
1343 let myty = Some(shortty(myitem));
1345 if curty.is_some() {
1346 try!(write!(w, "</table>"));
1349 let (short, name) = match myitem.inner {
1350 clean::ModuleItem(..) => ("modules", "Modules"),
1351 clean::StructItem(..) => ("structs", "Structs"),
1352 clean::EnumItem(..) => ("enums", "Enums"),
1353 clean::FunctionItem(..) => ("functions", "Functions"),
1354 clean::TypedefItem(..) => ("types", "Type Definitions"),
1355 clean::StaticItem(..) => ("statics", "Statics"),
1356 clean::TraitItem(..) => ("traits", "Traits"),
1357 clean::ImplItem(..) => ("impls", "Implementations"),
1358 clean::ViewItemItem(..) => ("reexports", "Reexports"),
1359 clean::TyMethodItem(..) => ("tymethods", "Type Methods"),
1360 clean::MethodItem(..) => ("methods", "Methods"),
1361 clean::StructFieldItem(..) => ("fields", "Struct Fields"),
1362 clean::VariantItem(..) => ("variants", "Variants"),
1363 clean::ForeignFunctionItem(..) => ("ffi-fns", "Foreign Functions"),
1364 clean::ForeignStaticItem(..) => ("ffi-statics", "Foreign Statics"),
1365 clean::MacroItem(..) => ("macros", "Macros"),
1366 clean::PrimitiveItem(..) => ("primitives", "Primitive Types"),
1369 "<h2 id='{id}' class='section-header'>\
1370 <a href=\"\\#{id}\">{name}</a></h2>\n<table>",
1371 id = short, name = name));
1374 match myitem.inner {
1375 clean::StaticItem(ref s) | clean::ForeignStaticItem(ref s) => {
1376 struct Initializer<'a>(&'a str, Item<'a>);
1377 impl<'a> fmt::Show for Initializer<'a> {
1378 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
1379 let Initializer(s, item) = *self;
1380 if s.len() == 0 { return Ok(()); }
1381 try!(write!(f, "<code> = </code>"));
1382 if s.contains("\n") {
1385 write!(f, "<a href='{}'>[definition]</a>",
1391 write!(f, "<code>{}</code>", s.as_slice())
1398 <td><code>{}static {}: {}</code>{}</td>
1399 <td class='docblock'>{} </td>
1402 VisSpace(myitem.visibility),
1403 *myitem.name.get_ref(),
1405 Initializer(s.expr.as_slice(), Item { cx: cx, item: myitem }),
1406 Markdown(blank(myitem.doc_value()))));
1409 clean::ViewItemItem(ref item) => {
1411 clean::ExternCrate(ref name, ref src, _) => {
1412 try!(write!(w, "<tr><td><code>extern crate {}",
1415 Some(ref src) => try!(write!(w, " = \"{}\"",
1419 try!(write!(w, ";</code></td></tr>"));
1422 clean::Import(ref import) => {
1423 try!(write!(w, "<tr><td><code>{}{}</code></td></tr>",
1424 VisSpace(myitem.visibility),
1432 if myitem.name.is_none() { continue }
1435 <td><a class='{class}' href='{href}'
1436 title='{title}'>{}</a></td>
1437 <td class='docblock short'>{}</td>
1440 *myitem.name.get_ref(),
1441 Markdown(shorter(myitem.doc_value())),
1442 class = shortty(myitem),
1443 href = item_path(myitem),
1444 title = full_path(cx, myitem)));
1448 write!(w, "</table>")
1451 fn item_function(w: &mut fmt::Formatter, it: &clean::Item,
1452 f: &clean::Function) -> fmt::Result {
1453 try!(write!(w, "<pre class='rust fn'>{vis}{fn_style}fn \
1454 {name}{generics}{decl}</pre>",
1455 vis = VisSpace(it.visibility),
1456 fn_style = FnStyleSpace(f.fn_style),
1457 name = it.name.get_ref().as_slice(),
1458 generics = f.generics,
1463 fn item_trait(w: &mut fmt::Formatter, cx: &Context, it: &clean::Item,
1464 t: &clean::Trait) -> fmt::Result {
1465 let mut parents = String::new();
1466 if t.parents.len() > 0 {
1467 parents.push_str(": ");
1468 for (i, p) in t.parents.iter().enumerate() {
1469 if i > 0 { parents.push_str(" + "); }
1470 parents.push_str(format!("{}", *p).as_slice());
1474 // Output the trait definition
1475 try!(write!(w, "<pre class='rust trait'>{}trait {}{}{} ",
1476 VisSpace(it.visibility),
1477 it.name.get_ref().as_slice(),
1480 let required = t.methods.iter().filter(|m| m.is_req()).collect::<Vec<&clean::TraitMethod>>();
1481 let provided = t.methods.iter().filter(|m| !m.is_req()).collect::<Vec<&clean::TraitMethod>>();
1483 if t.methods.len() == 0 {
1484 try!(write!(w, "\\{ \\}"));
1486 try!(write!(w, "\\{\n"));
1487 for m in required.iter() {
1488 try!(write!(w, " "));
1489 try!(render_method(w, m.item()));
1490 try!(write!(w, ";\n"));
1492 if required.len() > 0 && provided.len() > 0 {
1493 try!(w.write("\n".as_bytes()));
1495 for m in provided.iter() {
1496 try!(write!(w, " "));
1497 try!(render_method(w, m.item()));
1498 try!(write!(w, " \\{ ... \\}\n"));
1500 try!(write!(w, "\\}"));
1502 try!(write!(w, "</pre>"));
1504 // Trait documentation
1505 try!(document(w, it));
1507 fn meth(w: &mut fmt::Formatter, m: &clean::TraitMethod) -> fmt::Result {
1508 try!(write!(w, "<h3 id='{}.{}' class='method'><code>",
1510 *m.item().name.get_ref()));
1511 try!(render_method(w, m.item()));
1512 try!(write!(w, "</code></h3>"));
1513 try!(document(w, m.item()));
1517 // Output the documentation for each function individually
1518 if required.len() > 0 {
1520 <h2 id='required-methods'>Required Methods</h2>
1521 <div class='methods'>
1523 for m in required.iter() {
1526 try!(write!(w, "</div>"));
1528 if provided.len() > 0 {
1530 <h2 id='provided-methods'>Provided Methods</h2>
1531 <div class='methods'>
1533 for m in provided.iter() {
1536 try!(write!(w, "</div>"));
1539 let cache = cache_key.get().unwrap();
1541 <h2 id='implementors'>Implementors</h2>
1542 <ul class='item-list' id='implementors-list'>
1544 match cache.implementors.find(&it.def_id) {
1545 Some(implementors) => {
1546 for i in implementors.iter() {
1547 try!(writeln!(w, "<li><code>impl{} {} for {}</code></li>",
1548 i.generics, i.trait_, i.for_));
1553 try!(write!(w, "</ul>"));
1554 try!(write!(w, r#"<script type="text/javascript" async
1555 src="{root_path}/implementors/{path}/{ty}.{name}.js">
1557 root_path = Vec::from_elem(cx.current.len(), "..").connect("/"),
1558 path = if ast_util::is_local(it.def_id) {
1559 cx.current.connect("/")
1561 let path = cache.external_paths.get(&it.def_id);
1562 path.slice_to(path.len() - 1).connect("/")
1564 ty = shortty(it).to_static_str(),
1565 name = *it.name.get_ref()));
1569 fn render_method(w: &mut fmt::Formatter, meth: &clean::Item) -> fmt::Result {
1570 fn fun(w: &mut fmt::Formatter, it: &clean::Item, fn_style: ast::FnStyle,
1571 g: &clean::Generics, selfty: &clean::SelfTy,
1572 d: &clean::FnDecl) -> fmt::Result {
1573 write!(w, "{}fn <a href='\\#{ty}.{name}' class='fnname'>{name}</a>\
1576 ast::UnsafeFn => "unsafe ",
1580 name = it.name.get_ref().as_slice(),
1582 decl = Method(selfty, d))
1585 clean::TyMethodItem(ref m) => {
1586 fun(w, meth, m.fn_style, &m.generics, &m.self_, &m.decl)
1588 clean::MethodItem(ref m) => {
1589 fun(w, meth, m.fn_style, &m.generics, &m.self_, &m.decl)
1595 fn item_struct(w: &mut fmt::Formatter, it: &clean::Item,
1596 s: &clean::Struct) -> fmt::Result {
1597 try!(write!(w, "<pre class='rust struct'>"));
1598 try!(render_struct(w,
1602 s.fields.as_slice(),
1605 try!(write!(w, "</pre>"));
1607 try!(document(w, it));
1608 let mut fields = s.fields.iter().filter(|f| {
1610 clean::StructFieldItem(clean::HiddenStructField) => false,
1611 clean::StructFieldItem(clean::TypedStructField(..)) => true,
1615 match s.struct_type {
1616 doctree::Plain if fields.peek().is_some() => {
1617 try!(write!(w, "<h2 class='fields'>Fields</h2>\n<table>"));
1618 for field in fields {
1619 try!(write!(w, "<tr><td id='structfield.{name}'>\
1620 <code>{name}</code></td><td>",
1621 name = field.name.get_ref().as_slice()));
1622 try!(document(w, field));
1623 try!(write!(w, "</td></tr>"));
1625 try!(write!(w, "</table>"));
1629 render_methods(w, it)
1632 fn item_enum(w: &mut fmt::Formatter, it: &clean::Item,
1633 e: &clean::Enum) -> fmt::Result {
1634 try!(write!(w, "<pre class='rust enum'>{}enum {}{}",
1635 VisSpace(it.visibility),
1636 it.name.get_ref().as_slice(),
1638 if e.variants.len() == 0 && !e.variants_stripped {
1639 try!(write!(w, " \\{\\}"));
1641 try!(write!(w, " \\{\n"));
1642 for v in e.variants.iter() {
1643 try!(write!(w, " "));
1644 let name = v.name.get_ref().as_slice();
1646 clean::VariantItem(ref var) => {
1648 clean::CLikeVariant => try!(write!(w, "{}", name)),
1649 clean::TupleVariant(ref tys) => {
1650 try!(write!(w, "{}(", name));
1651 for (i, ty) in tys.iter().enumerate() {
1653 try!(write!(w, ", "))
1655 try!(write!(w, "{}", *ty));
1657 try!(write!(w, ")"));
1659 clean::StructVariant(ref s) => {
1660 try!(render_struct(w,
1664 s.fields.as_slice(),
1672 try!(write!(w, ",\n"));
1675 if e.variants_stripped {
1676 try!(write!(w, " // some variants omitted\n"));
1678 try!(write!(w, "\\}"));
1680 try!(write!(w, "</pre>"));
1682 try!(document(w, it));
1683 if e.variants.len() > 0 {
1684 try!(write!(w, "<h2 class='variants'>Variants</h2>\n<table>"));
1685 for variant in e.variants.iter() {
1686 try!(write!(w, "<tr><td id='variant.{name}'><code>{name}</code></td><td>",
1687 name = variant.name.get_ref().as_slice()));
1688 try!(document(w, variant));
1689 match variant.inner {
1690 clean::VariantItem(ref var) => {
1692 clean::StructVariant(ref s) => {
1693 let mut fields = s.fields.iter().filter(|f| {
1695 clean::StructFieldItem(ref t) => match *t {
1696 clean::HiddenStructField => false,
1697 clean::TypedStructField(..) => true,
1702 try!(write!(w, "<h3 class='fields'>Fields</h3>\n
1704 for field in fields {
1705 try!(write!(w, "<tr><td \
1706 id='variant.{v}.field.{f}'>\
1707 <code>{f}</code></td><td>",
1708 v = variant.name.get_ref().as_slice(),
1709 f = field.name.get_ref().as_slice()));
1710 try!(document(w, field));
1711 try!(write!(w, "</td></tr>"));
1713 try!(write!(w, "</table>"));
1720 try!(write!(w, "</td></tr>"));
1722 try!(write!(w, "</table>"));
1725 try!(render_methods(w, it));
1729 fn render_struct(w: &mut fmt::Formatter, it: &clean::Item,
1730 g: Option<&clean::Generics>,
1731 ty: doctree::StructType,
1732 fields: &[clean::Item],
1734 structhead: bool) -> fmt::Result {
1735 try!(write!(w, "{}{}{}",
1736 VisSpace(it.visibility),
1737 if structhead {"struct "} else {""},
1738 it.name.get_ref().as_slice()));
1740 Some(g) => try!(write!(w, "{}", *g)),
1745 try!(write!(w, " \\{\n{}", tab));
1746 let mut fields_stripped = false;
1747 for field in fields.iter() {
1749 clean::StructFieldItem(clean::HiddenStructField) => {
1750 fields_stripped = true;
1752 clean::StructFieldItem(clean::TypedStructField(ref ty)) => {
1753 try!(write!(w, " {}{}: {},\n{}",
1754 VisSpace(field.visibility),
1755 field.name.get_ref().as_slice(),
1759 _ => unreachable!(),
1763 if fields_stripped {
1764 try!(write!(w, " // some fields omitted\n{}", tab));
1766 try!(write!(w, "\\}"));
1768 doctree::Tuple | doctree::Newtype => {
1769 try!(write!(w, "("));
1770 for (i, field) in fields.iter().enumerate() {
1772 try!(write!(w, ", "));
1775 clean::StructFieldItem(clean::HiddenStructField) => {
1776 try!(write!(w, "_"))
1778 clean::StructFieldItem(clean::TypedStructField(ref ty)) => {
1779 try!(write!(w, "{}{}", VisSpace(field.visibility), *ty))
1784 try!(write!(w, ");"));
1787 try!(write!(w, ";"));
1793 fn render_methods(w: &mut fmt::Formatter, it: &clean::Item) -> fmt::Result {
1794 match cache_key.get().unwrap().impls.find(&it.def_id) {
1796 let mut non_trait = v.iter().filter(|p| {
1797 p.ref0().trait_.is_none()
1799 let non_trait = non_trait.collect::<Vec<&(clean::Impl, Option<String>)>>();
1800 let mut traits = v.iter().filter(|p| {
1801 p.ref0().trait_.is_some()
1803 let traits = traits.collect::<Vec<&(clean::Impl, Option<String>)>>();
1805 if non_trait.len() > 0 {
1806 try!(write!(w, "<h2 id='methods'>Methods</h2>"));
1807 for &(ref i, ref dox) in non_trait.move_iter() {
1808 try!(render_impl(w, i, dox));
1811 if traits.len() > 0 {
1812 try!(write!(w, "<h2 id='implementations'>Trait \
1813 Implementations</h2>"));
1814 let mut any_derived = false;
1815 for & &(ref i, ref dox) in traits.iter() {
1817 try!(render_impl(w, i, dox));
1823 try!(write!(w, "<h3 id='derived_implementations'>Derived Implementations \
1825 for &(ref i, ref dox) in traits.move_iter() {
1827 try!(render_impl(w, i, dox));
1838 fn render_impl(w: &mut fmt::Formatter, i: &clean::Impl,
1839 dox: &Option<String>) -> fmt::Result {
1840 try!(write!(w, "<h3 class='impl'><code>impl{} ", i.generics));
1842 Some(ref ty) => try!(write!(w, "{} for ", *ty)),
1845 try!(write!(w, "{}</code></h3>", i.for_));
1848 try!(write!(w, "<div class='docblock'>{}</div>",
1849 Markdown(dox.as_slice())));
1854 fn docmeth(w: &mut fmt::Formatter, item: &clean::Item,
1855 dox: bool) -> fmt::Result {
1856 try!(write!(w, "<h4 id='method.{}' class='method'><code>",
1857 *item.name.get_ref()));
1858 try!(render_method(w, item));
1859 try!(write!(w, "</code></h4>\n"));
1860 match item.doc_value() {
1862 try!(write!(w, "<div class='docblock'>{}</div>", Markdown(s)));
1865 Some(..) | None => Ok(())
1869 try!(write!(w, "<div class='methods'>"));
1870 for meth in i.methods.iter() {
1871 try!(docmeth(w, meth, true));
1874 fn render_default_methods(w: &mut fmt::Formatter,
1876 i: &clean::Impl) -> fmt::Result {
1877 for method in t.methods.iter() {
1878 let n = method.item().name.clone();
1879 match i.methods.iter().find(|m| { m.name == n }) {
1880 Some(..) => continue,
1884 try!(docmeth(w, method.item(), false));
1889 // If we've implemented a trait, then also emit documentation for all
1890 // default methods which weren't overridden in the implementation block.
1892 Some(clean::ResolvedPath { did, .. }) => {
1894 match cache_key.get().unwrap().traits.find(&did) {
1895 Some(t) => try!(render_default_methods(w, t, i)),
1901 Some(..) | None => {}
1903 try!(write!(w, "</div>"));
1907 fn item_typedef(w: &mut fmt::Formatter, it: &clean::Item,
1908 t: &clean::Typedef) -> fmt::Result {
1909 try!(write!(w, "<pre class='rust typedef'>type {}{} = {};</pre>",
1910 it.name.get_ref().as_slice(),
1917 impl<'a> fmt::Show for Sidebar<'a> {
1918 fn fmt(&self, fmt: &mut fmt::Formatter) -> fmt::Result {
1921 try!(write!(fmt, "<p class='location'>"));
1922 let len = cx.current.len() - if it.is_mod() {1} else {0};
1923 for (i, name) in cx.current.iter().take(len).enumerate() {
1925 try!(write!(fmt, "&\\#8203;::"));
1927 try!(write!(fmt, "<a href='{}index.html'>{}</a>",
1930 .slice_to((cx.current.len() - i - 1) * 3),
1933 try!(write!(fmt, "</p>"));
1935 fn block(w: &mut fmt::Formatter, short: &str, longty: &str,
1936 cur: &clean::Item, cx: &Context) -> fmt::Result {
1937 let items = match cx.sidebar.find_equiv(&short) {
1938 Some(items) => items.as_slice(),
1939 None => return Ok(())
1941 try!(write!(w, "<div class='block {}'><h2>{}</h2>", short, longty));
1942 for item in items.iter() {
1943 let curty = shortty(cur).to_static_str();
1944 let class = if cur.name.get_ref() == item && short == curty {
1949 try!(write!(w, "<a class='{ty} {class}' href='{curty, select,
1953 mod{{name}/index.html}
1954 other{#.{name}.html}
1955 }'>{name}</a><br/>",
1960 name = item.as_slice()));
1962 try!(write!(w, "</div>"));
1966 try!(block(fmt, "mod", "Modules", it, cx));
1967 try!(block(fmt, "struct", "Structs", it, cx));
1968 try!(block(fmt, "enum", "Enums", it, cx));
1969 try!(block(fmt, "trait", "Traits", it, cx));
1970 try!(block(fmt, "fn", "Functions", it, cx));
1971 try!(block(fmt, "macro", "Macros", it, cx));
1976 fn build_sidebar(m: &clean::Module) -> HashMap<String, Vec<String>> {
1977 let mut map = HashMap::new();
1978 for item in m.items.iter() {
1979 let short = shortty(item).to_static_str();
1980 let myname = match item.name {
1982 Some(ref s) => s.to_string(),
1984 let v = map.find_or_insert_with(short.to_string(), |_| Vec::new());
1988 for (_, items) in map.mut_iter() {
1989 items.as_mut_slice().sort();
1994 impl<'a> fmt::Show for Source<'a> {
1995 fn fmt(&self, fmt: &mut fmt::Formatter) -> fmt::Result {
1996 let Source(s) = *self;
1997 let lines = s.lines().len();
1999 let mut tmp = lines;
2004 try!(write!(fmt, "<pre class='line-numbers'>"));
2005 for i in range(1, lines + 1) {
2006 try!(write!(fmt, "<span id='{0:u}'>{0:1$u}</span>\n", i, cols));
2008 try!(write!(fmt, "</pre>"));
2009 try!(write!(fmt, "{}", highlight::highlight(s.as_slice(), None)));
2014 fn item_macro(w: &mut fmt::Formatter, it: &clean::Item,
2015 t: &clean::Macro) -> fmt::Result {
2016 try!(w.write(highlight::highlight(t.source.as_slice(), Some("macro")).as_bytes()));
2020 fn item_primitive(w: &mut fmt::Formatter,
2022 _p: &clean::Primitive) -> fmt::Result {
2023 try!(document(w, it));
2024 render_methods(w, it)