From 96440178a1e8ec0ee44eaaa51fd0a9db69a10a78 Mon Sep 17 00:00:00 2001 From: keertip Date: Thu, 11 Dec 2014 14:50:24 -0800 Subject: [PATCH 1/2] add template for site map --- lib/src/generator.dart | 27 ++++++++++++++++++++--- pubspec.yaml | 2 ++ templates/sitemap.xml | 8 +++++++ test/all.dart | 7 ++++++ test/template_test.dart | 49 +++++++++++++++++++++++++++++++++++++++++ tool/travis.sh | 8 +++---- 6 files changed, 93 insertions(+), 8 deletions(-) create mode 100644 templates/sitemap.xml create mode 100644 test/all.dart create mode 100644 test/template_test.dart diff --git a/lib/src/generator.dart b/lib/src/generator.dart index d02200e2af..01b715b792 100644 --- a/lib/src/generator.dart +++ b/lib/src/generator.dart @@ -6,6 +6,8 @@ library dartdoc.generator; import 'dart:io'; +import 'package:mustache4dart/mustache4dart.dart'; + import 'css.dart'; import 'html_gen.dart'; import 'html_utils.dart'; @@ -14,12 +16,16 @@ import 'model.dart'; /// Generates the HTML files class HtmlGenerator { + + // The sitemap template file + final String siteMapTemplate = '/templates/sitemap.xml'; Directory out; Package package; HtmlHelper html = new HtmlHelper(); CSS css = new CSS(); HtmlGeneratorHelper helper; + List htmlFiles =[]; HtmlGenerator(this.package, this.out) { helper = new HtmlGeneratorHelper(package); @@ -31,6 +37,7 @@ class HtmlGenerator { // copy the css resource into 'out' File f = joinFile(new Directory(out.path), [css.getCssName()]); f.writeAsStringSync(css.getCssContent()); + generateSiteMap(); } void generatePackage() { @@ -38,7 +45,9 @@ class HtmlGenerator { var packageDesc = package.description; var packageVersion = package.version; if (packageName.isNotEmpty) { - File f = joinFile(new Directory(out.path), ['${packageName}_package.html']); + var fileName = '${packageName}_package.html'; + File f = joinFile(new Directory(out.path), [fileName]); + htmlFiles.add(fileName); print('generating ${f.path}'); html.start(title: 'Package ${packageName}', cssRef: css.getCssName()); @@ -75,8 +84,10 @@ class HtmlGenerator { } void generateLibrary(Library library) { - File f = joinFile(new Directory(out.path), [_getFileNameFor(library)]); + var fileName = _getFileNameFor(library); + File f = joinFile(new Directory(out.path), [fileName]); print('generating ${f.path}'); + htmlFiles.add(fileName); html = new HtmlHelper(); html.start(title: 'Library ${library.name}', cssRef: css.getCssName()); @@ -418,7 +429,17 @@ class HtmlGenerator { } } - + void generateSiteMap() { + print('generating sitemap.xml'); + File f = joinFile(new Directory(out.path), ['sitemap.xml']); + var script = new File(Platform.script.toFilePath()); + File tmplFile = new File('${script.parent.parent.path}$siteMapTemplate'); + var tmpl = tmplFile.readAsStringSync(); + // TODO: adjust urls + List names = htmlFiles.map((f) => {'name': '$f'}).toList(); + var content = render(tmpl, {'links' : names}); + f.writeAsStringSync(content); + } } String _getFileNameFor(Library library) { diff --git a/pubspec.yaml b/pubspec.yaml index be538730ce..b1643d5213 100644 --- a/pubspec.yaml +++ b/pubspec.yaml @@ -8,7 +8,9 @@ dependencies: args: any bootstrap: any logging: any + mustache4dart: '>= 1.0.0 < 2.0.0' path: any + unittest: any yaml: any executables: dartdoc: null diff --git a/templates/sitemap.xml b/templates/sitemap.xml new file mode 100644 index 0000000000..3a35c2c402 --- /dev/null +++ b/templates/sitemap.xml @@ -0,0 +1,8 @@ + + +{{#links}} + + {{name}} + +{{/links}} + diff --git a/test/all.dart b/test/all.dart new file mode 100644 index 0000000000..1b59435ed6 --- /dev/null +++ b/test/all.dart @@ -0,0 +1,7 @@ +library dartdoc.all_tests; + +import 'template_test.dart' as template_tests; + +main() { + template_tests.tests(); +} \ No newline at end of file diff --git a/test/template_test.dart b/test/template_test.dart new file mode 100644 index 0000000000..4561f01b88 --- /dev/null +++ b/test/template_test.dart @@ -0,0 +1,49 @@ +library dartdoc.template_test; + +import 'dart:io'; + +import 'package:mustache4dart/mustache4dart.dart'; +import 'package:unittest/unittest.dart'; + + +tests() { + group('template', () { + var script = new File(Platform.script.toFilePath()); + File tmplFile = new File('${script.parent.parent.path}/templates/sitemap.xml'); + + test('sitemap template exists', () { + tmplFile.exists().then((t) => expect(t, true)); + }); + + var siteMapTmpl = tmplFile.readAsStringSync(); + var sitemap = compile(siteMapTmpl); + + test('render', () { + expect(sitemap({'links' : [{'name': 'somefile.html'}]}), + ''' + + + + somefile.html + + +'''); + }); + + test('substitute multiple links', () { + expect(sitemap({'links' : [{'name': 'somefile.html'}, {'name': 'asecondfile.html'}]}), + ''' + + + + somefile.html + + + asecondfile.html + + +'''); + }); + + }); +} \ No newline at end of file diff --git a/tool/travis.sh b/tool/travis.sh index 7a21b5cb51..12536b4893 100755 --- a/tool/travis.sh +++ b/tool/travis.sh @@ -26,10 +26,8 @@ pub get # Verify that the libraries are error free. dartanalyzer --fatal-warnings \ bin/dartdoc.dart \ - lib/dartdoc.dart -# TODO: -# test/all.dart + lib/dartdoc.dart \ + test/all.dart # Run the tests. -# TODO: -#dart test/all.dart +dart test/all.dart From e9797e8d11ef3bb168b2d491e758ba38c5fb7dde Mon Sep 17 00:00:00 2001 From: keertip Date: Thu, 11 Dec 2014 14:53:31 -0800 Subject: [PATCH 2/2] fix whitespace --- test/all.dart | 2 +- test/template_test.dart | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/test/all.dart b/test/all.dart index 1b59435ed6..a808d7f918 100644 --- a/test/all.dart +++ b/test/all.dart @@ -4,4 +4,4 @@ import 'template_test.dart' as template_tests; main() { template_tests.tests(); -} \ No newline at end of file +} diff --git a/test/template_test.dart b/test/template_test.dart index 4561f01b88..f62913154d 100644 --- a/test/template_test.dart +++ b/test/template_test.dart @@ -46,4 +46,4 @@ tests() { }); }); -} \ No newline at end of file +}