OLD | NEW |
| (Empty) |
1 // Copyright (c) 2011, the Dart project authors. Please see the AUTHORS file | |
2 // for details. All rights reserved. Use of this source code is governed by a | |
3 // BSD-style license that can be found in the LICENSE file. | |
4 | |
5 // Functions for working with files and paths. | |
6 | |
7 /** The path to the file currently being written to, relative to [outdir]. */ | |
8 String _filePath; | |
9 | |
10 /** The file currently being written to. */ | |
11 StringBuffer _file; | |
12 | |
13 /** Path to generate HTML files into. */ | |
14 final _outdir = 'docs'; | |
15 | |
16 startFile(String path) { | |
17 _filePath = path; | |
18 _file = new StringBuffer(); | |
19 } | |
20 | |
21 write(String s) { | |
22 _file.add(s); | |
23 } | |
24 | |
25 writeln(String s) { | |
26 write(s); | |
27 write('\n'); | |
28 } | |
29 | |
30 endFile() { | |
31 String outPath = '$_outdir/$_filePath'; | |
32 world.files.createDirectory(dirname(outPath), recursive: true); | |
33 | |
34 world.files.writeString(outPath, _file.toString()); | |
35 _filePath = null; | |
36 _file = null; | |
37 } | |
38 | |
39 /** | |
40 * Converts [fullPath] which is understood to be a full path from the root of | |
41 * the generated docs to one relative to the current file. | |
42 */ | |
43 String relativePath(String fullPath) { | |
44 // Don't make it relative if it's an absolute path. | |
45 if (isAbsolute(fullPath)) return fullPath; | |
46 | |
47 // TODO(rnystrom): Walks all the way up to root each time. Shouldn't do this | |
48 // if the paths overlap. | |
49 return repeat('../', countOccurrences(_filePath, '/')) + fullPath; | |
50 } | |
51 | |
52 /** Gets whether or not the given URL is absolute or relative. */ | |
53 bool isAbsolute(String url) { | |
54 // TODO(rnystrom): This is a bit hackish. We consider any URL that lacks | |
55 // a scheme to be relative. | |
56 return const RegExp(@'^\w+:').hasMatch(url); | |
57 } | |
58 | |
59 /** Gets the URL to the documentation for [library]. */ | |
60 libraryUrl(Library library) => '${sanitize(library.name)}.html'; | |
61 | |
62 /** Gets the URL for the documentation for [type]. */ | |
63 typeUrl(Type type) { | |
64 if (type.isTop) return '${sanitize(type.library.name)}.html'; | |
65 // Always get the generic type to strip off any type parameters or arguments. | |
66 // If the type isn't generic, genericType returns `this`, so it works for | |
67 // non-generic types too. | |
68 return '${sanitize(type.library.name)}/${type.genericType.name}.html'; | |
69 } | |
70 | |
71 /** Gets the URL for the documentation for [member]. */ | |
72 memberUrl(Member member) { | |
73 final typeUrl = typeUrl(member.declaringType); | |
74 if (!member.isConstructor) return '$typeUrl#${member.name}'; | |
75 if (member.constructorName == '') return '$typeUrl#new:${member.name}'; | |
76 return '$typeUrl#new:${member.name}.${member.constructorName}'; | |
77 } | |
78 | |
79 /** Gets the anchor id for the document for [member]. */ | |
80 memberAnchor(Member member) => '${member.name}'; | |
OLD | NEW |