Skip to content

Add a template for sitemap generation #29

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Dec 11, 2014
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
27 changes: 24 additions & 3 deletions lib/src/generator.dart
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand All @@ -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<String> htmlFiles =[];

HtmlGenerator(this.package, this.out) {
helper = new HtmlGeneratorHelper(package);
Expand All @@ -31,14 +37,17 @@ class HtmlGenerator {
// copy the css resource into 'out'
File f = joinFile(new Directory(out.path), [css.getCssName()]);
f.writeAsStringSync(css.getCssContent());
generateSiteMap();
}

void generatePackage() {
var packageName = package.name;
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());
Expand Down Expand Up @@ -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());

Expand Down Expand Up @@ -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) {
Expand Down
2 changes: 2 additions & 0 deletions pubspec.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -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
8 changes: 8 additions & 0 deletions templates/sitemap.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
<?xml version="1.0" encoding="UTF-8"?>
<urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9">
{{#links}}
<url>
<loc>{{name}}</loc>
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

loc needs to be a fully qualified URL, beginning with http or https

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, have added a TODO in the dart code to fix that. Was not sure what the url would be.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe pass it in from command line?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

So generate sitemap only when the option is passed in?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That's probably the easiest way to handle this for now.

</url>
{{/links}}
</urlset>
7 changes: 7 additions & 0 deletions test/all.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
library dartdoc.all_tests;

import 'template_test.dart' as template_tests;

main() {
template_tests.tests();
}
49 changes: 49 additions & 0 deletions test/template_test.dart
Original file line number Diff line number Diff line change
@@ -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'}]}),
'''
<?xml version="1.0" encoding="UTF-8"?>
<urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9">
<url>
<loc>somefile.html</loc>
</url>
</urlset>
''');
});

test('substitute multiple links', () {
expect(sitemap({'links' : [{'name': 'somefile.html'}, {'name': 'asecondfile.html'}]}),
'''
<?xml version="1.0" encoding="UTF-8"?>
<urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9">
<url>
<loc>somefile.html</loc>
</url>
<url>
<loc>asecondfile.html</loc>
</url>
</urlset>
''');
});

});
}
8 changes: 3 additions & 5 deletions tool/travis.sh
Original file line number Diff line number Diff line change
Expand Up @@ -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