Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 5 additions & 9 deletions bin/dartdoc.dart
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,8 @@ void main(List<String> arguments) {
if (results['help']) {
_printUsageAndExit(parser);
}
List<String> excludeLibraries = results['exclude'] == null ?
[] : results['exclude'].split(',');
List<String> excludeLibraries =
results['exclude'] == null ? [] : results['exclude'].split(',');

String url = results['url'];
var currentDir = Directory.current;
Expand All @@ -36,15 +36,11 @@ void _printUsageAndExit(ArgParser parser) {
ArgParser _createArgsParser() {
// TODO: more options to be added
var parser = new ArgParser();
parser.addOption(
'exclude',
parser.addOption('exclude',
help: 'a comma-separated list of library names to ignore');
parser.addOption(
'url',
parser.addOption('url',
help: 'the url where the docs will be hosted (used to generate the sitemap)');
parser.addFlag('help',
abbr: 'h',
negatable: false,
help: 'show command help');
abbr: 'h', negatable: false, help: 'show command help');
return parser;
}
19 changes: 12 additions & 7 deletions lib/dartdoc.dart
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,6 @@ const String DEFAULT_OUTPUT_DIRECTORY = 'docs';
/// Generates Dart documentation for all public Dart libraries in the given
/// directory.
class DartDoc {

List<String> _excludes;
Directory _rootDir;
String _url;
Expand Down Expand Up @@ -55,25 +54,32 @@ class DartDoc {
out.createSync(recursive: true);
}

generator = new HtmlGenerator(new Package(libraries, _rootDir.path), out, _url);
generator = new HtmlGenerator(
new Package(libraries, _rootDir.path), out, _url);
// generate the docs
generator.generate();

double seconds = stopwatch.elapsedMilliseconds / 1000.0;
print('');
print("Documented ${libraries.length} " "librar${libraries.length == 1 ? 'y' : 'ies'} in " "${seconds.toStringAsFixed(1)} seconds.");
print(
"Documented ${libraries.length} " "librar${libraries.length == 1 ? 'y' : 'ies'} in " "${seconds.toStringAsFixed(1)} seconds.");
}

List<LibraryElement> parseLibraries(List<String> files) {
DartSdk sdk = new DirectoryBasedDartSdk(new JavaFile(_getSdkDir().path));

ContentCache contentCache = new ContentCache();
List<UriResolver> resolvers = [new DartUriResolver(sdk), new FileUriResolver()];
JavaFile packagesDir = new JavaFile.relative(new JavaFile(_rootDir.path), 'packages');
List<UriResolver> resolvers = [
new DartUriResolver(sdk),
new FileUriResolver()
];
JavaFile packagesDir = new JavaFile.relative(
new JavaFile(_rootDir.path), 'packages');
if (packagesDir.exists()) {
resolvers.add(new PackageUriResolver([packagesDir]));
}
SourceFactory sourceFactory = new SourceFactory(/*contentCache,*/ resolvers);
SourceFactory sourceFactory = new SourceFactory(
/*contentCache,*/ resolvers);
AnalysisContext context = AnalysisEngine.instance.createAnalysisContext();
context.sourceFactory = sourceFactory;

Expand Down Expand Up @@ -104,5 +110,4 @@ class DartDoc {
// Look relative to the dart executable.
return new File(Platform.executable).parent.parent;
}

}
8 changes: 4 additions & 4 deletions lib/src/css.dart
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,9 @@ class CSS {
String getCssName() => 'bootstrap.css';

String getCssContent() {
var script = new File(Platform.script.toFilePath());
var cssFile = new File('${script.parent.path}$cssFilePath');
String text = cssFile.readAsStringSync(encoding: ASCII);
return text;
var script = new File(Platform.script.toFilePath());
var cssFile = new File('${script.parent.path}$cssFilePath');
String text = cssFile.readAsStringSync(encoding: ASCII);
return text;
}
}
20 changes: 12 additions & 8 deletions lib/src/generator.dart
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ body {
HtmlHelper html = new HtmlHelper();
CSS css = new CSS();
HtmlGeneratorHelper helper;
List<String> htmlFiles =[];
List<String> htmlFiles = [];
String url;

HtmlGenerator(this.package, this.out, this.url) {
Expand Down Expand Up @@ -57,7 +57,9 @@ body {
htmlFiles.add(fileName);
print('generating ${f.path}');

html.start(title: 'Package ${packageName}', cssRef: css.getCssName(),
html.start(
title: 'Package ${packageName}',
cssRef: css.getCssName(),
inlineStyle: bootstrapOverrides);
html.generateHeader();
html.startTag('div', attributes: "class='container'", newLine: false);
Expand All @@ -67,7 +69,8 @@ body {
html.startTag('div', attributes: "class='col-md-3'");
html.startTag('ul', attributes: 'class="nav nav-pills nav-stacked"');
html.startTag('li', attributes: 'class="active"', newLine: false);
html.write('<a href="${packageName}">' '<i class="chevron-nav icon-white icon-chevron-right"></i> ' '${packageName}-${packageVersion}</a>');
html.write(
'<a href="${packageName}">' '<i class="chevron-nav icon-white icon-chevron-right"></i> ' '${packageName}-${packageVersion}</a>');
html.endTag(); //li
html.endTag(); //ul
html.endTag();
Expand Down Expand Up @@ -97,7 +100,9 @@ body {
print('generating ${f.path}');
htmlFiles.add(fileName);
html = new HtmlHelper();
html.start(title: 'Library ${library.name}', cssRef: css.getCssName(),
html.start(
title: 'Library ${library.name}',
cssRef: css.getCssName(),
inlineStyle: bootstrapOverrides);

html.generateHeader();
Expand All @@ -111,7 +116,8 @@ body {
html.startTag('div', attributes: "class='col-md-3'");
html.startTag('ul', attributes: 'class="nav nav-pills nav-stacked"');
html.startTag('li', attributes: 'class="active"', newLine: false);
html.write('<a href="${_getFileNameFor(library)}">' '<i class="chevron-nav icon-white icon-chevron-right"></i> ' '${library.name}</a>');
html.write(
'<a href="${_getFileNameFor(library)}">' '<i class="chevron-nav icon-white icon-chevron-right"></i> ' '${library.name}</a>');
html.endTag(); // li
html.endTag(); // ul.nav
html.endTag(); // div.col-md-3
Expand Down Expand Up @@ -194,7 +200,6 @@ body {
}

void generateClass(Class cls) {

html.write(createAnchor(cls));
html.writeln('<hr>');
html.startTag('h4');
Expand Down Expand Up @@ -444,7 +449,7 @@ body {
var tmpl = tmplFile.readAsStringSync();
// TODO: adjust urls
List names = htmlFiles.map((f) => {'name': '$f'}).toList();
var content = render(tmpl, {'url': url, 'links' : names});
var content = render(tmpl, {'url': url, 'links': names});
f.writeAsStringSync(content);
}
}
Expand All @@ -454,7 +459,6 @@ String _getFileNameFor(Library library) {
}

class HtmlGeneratorHelper extends Helper {

Package package;

HtmlGeneratorHelper(this.package);
Expand Down
3 changes: 2 additions & 1 deletion lib/src/html_gen.dart
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,8 @@ class HtmlHelper {
writeln();
startTag('head');
writeln('<meta charset="utf-8">');
writeln('<meta name="viewport" content="width=device-width, initial-scale=1.0">');
writeln(
'<meta name="viewport" content="width=device-width, initial-scale=1.0">');
if (title != null) {
writeln('<title>${title}</title>');
}
Expand Down
17 changes: 13 additions & 4 deletions lib/src/html_utils.dart
Original file line number Diff line number Diff line change
Expand Up @@ -5,15 +5,22 @@
library dartdoc.html_utils;

String htmlEscape(String text) {
return text.replaceAll('&', '&amp;').replaceAll('>', '&gt;').replaceAll('<', '&lt;');
return text
.replaceAll('&', '&amp;')
.replaceAll('>', '&gt;')
.replaceAll('<', '&lt;');
}

String escapeBrackets(String text) {
return text.replaceAll('>', '_').replaceAll('<', '_');
}

String stringEscape(String text, String quoteType) {
return text.replaceAll(quoteType, "\\${quoteType}").replaceAll('\n', '\\n').replaceAll('\r', '\\r').replaceAll('\t', '\\t');
return text
.replaceAll(quoteType, "\\${quoteType}")
.replaceAll('\n', '\\n')
.replaceAll('\r', '\\r')
.replaceAll('\t', '\\t');
}

abstract class CodeResolver {
Expand Down Expand Up @@ -52,7 +59,8 @@ String prettifyDocs(CodeResolver resolver, String docs) {
buf.write('${line}\n');
}
} else if (inList) {
buf.write('<li>${_processMarkdown(resolver, line.trim().substring(2))}</li>');
buf.write(
'<li>${_processMarkdown(resolver, line.trim().substring(2))}</li>');
} else if (line.trim().length == 0) {
buf.write('</p>\n<p>');
} else {
Expand Down Expand Up @@ -127,7 +135,8 @@ String _processMarkdown(CodeResolver resolver, String line) {
return line;
}

String replaceAll(String str, List<String> matchChars, {String htmlEntity, var replaceFunction}) {
String replaceAll(String str, List<String> matchChars,
{String htmlEntity, var replaceFunction}) {
int lastWritten = 0;
int index = str.indexOf(matchChars[0]);
StringBuffer buf = new StringBuffer();
Expand Down
60 changes: 31 additions & 29 deletions lib/src/io_utils.dart
Original file line number Diff line number Diff line change
Expand Up @@ -16,16 +16,16 @@ import 'package:path/path.dart' as path;
/// Excludes files and directories beginning with `.`
///
/// The returned paths are guaranteed to begin with [dir].
List<String> _listDir(String dir, {bool recursive: false,
List<FileSystemEntity> listDir(Directory dir)}) {
List<String> _listDir(String dir,
{bool recursive: false, List<FileSystemEntity> listDir(Directory dir)}) {
if (listDir == null) listDir = (Directory dir) => dir.listSync();

return _doList(dir, new Set<String>(), recursive, listDir);
}

File joinFile(Directory dir, List<String> files) {
String pathFragment = files.join(Platform.pathSeparator);
return new File("${dir.path}${Platform.pathSeparator}${pathFragment}");
String pathFragment = files.join(Platform.pathSeparator);
return new File("${dir.path}${Platform.pathSeparator}${pathFragment}");
}

List<String> _doList(String dir, Set<String> listedDirectories, bool recurse,
Expand All @@ -49,8 +49,8 @@ List<String> _doList(String dir, Set<String> listedDirectories, bool recurse,
contents.add(entity.path);
if (entity is Directory) {
if (recurse) {
children.addAll(_doList(entity.path, listedDirectories, recurse,
listDir));
children.addAll(
_doList(entity.path, listedDirectories, recurse, listDir));
}
}
}
Expand All @@ -67,32 +67,32 @@ List<String> findFilesToDocumentInPackage(String packageDir) {
// containing '/packages' will be added. The only exception is if the file
// to analyze already has a '/package' in its path.
var files = _listDir(packageDir, recursive: true, listDir: _packageDirList)
.where((f) => f.endsWith('.dart') &&
(!f.contains('${path.separator}packages') ||
packageDir.contains('${path.separator}packages')))
.toList();
.where((f) => f.endsWith('.dart') &&
(!f.contains('${path.separator}packages') ||
packageDir.contains('${path.separator}packages')))
.toList();

var packageLibDir = path.join(packageDir, 'lib');
var packageLibSrcDir = path.join(packageLibDir, 'src');

files.forEach((String lib) {
// Only include libraries within the lib dir that are not in lib/src
if (path.isWithin(packageLibDir, lib) &&
!path.isWithin(packageLibSrcDir, lib)) {
// Only add the file if it does not contain 'part of'
// TODO(janicejl): Remove when Issue(12406) is resolved.
var contents = new File(lib).readAsStringSync();

if (contents.contains(new RegExp('\npart of ')) ||
contents.startsWith(new RegExp('part of '))) {
// Only include libraries within the lib dir that are not in lib/src
if (path.isWithin(packageLibDir, lib) &&
!path.isWithin(packageLibSrcDir, lib)) {
// Only add the file if it does not contain 'part of'
// TODO(janicejl): Remove when Issue(12406) is resolved.
var contents = new File(lib).readAsStringSync();

if (contents.contains(new RegExp('\npart of ')) ||
contents.startsWith(new RegExp('part of '))) {
// logger.warning('Skipping part "$lib". '
// 'Part files should be in "lib/src".');
} else {
var uri = new Uri.file(path.normalize(path.absolute(lib)));
libraries.add(uri.path);
} else {
var uri = new Uri.file(path.normalize(path.absolute(lib)));
libraries.add(uri.path);
// logger.info('Added to libraries: $lib');
}
}
}
});
return libraries;
}
Expand All @@ -104,15 +104,17 @@ List<String> findFilesToDocumentInPackage(String packageDir) {
List<FileSystemEntity> _packageDirList(Directory dir) {
var entities = dir.listSync();

var pubspec = entities.firstWhere((e) => e is File &&
path.basename(e.path) == 'pubspec.yaml', orElse: () => null);
var pubspec = entities.firstWhere(
(e) => e is File && path.basename(e.path) == 'pubspec.yaml',
orElse: () => null);

var libDir = entities.firstWhere((e) => e is Directory &&
path.basename(e.path) == 'lib', orElse: () => null);
var libDir = entities.firstWhere(
(e) => e is Directory && path.basename(e.path) == 'lib',
orElse: () => null);

if (pubspec != null && libDir != null) {
return [libDir];
return [libDir];
} else {
return entities;
return entities;
}
}
Loading