Skip to content

Commit c11ef44

Browse files
committed
support multiple headers
1 parent ce296b7 commit c11ef44

File tree

4 files changed

+42
-33
lines changed

4 files changed

+42
-33
lines changed

bin/dartdoc.dart

Lines changed: 22 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -74,15 +74,21 @@ main(List<String> arguments) async {
7474
args['include'] == null ? [] : args['include'].split(',');
7575

7676
String url = args['hosted-url'];
77-
String footerFilePath = _resolveTildePath(args['footer']);
78-
if (footerFilePath != null && !new File(footerFilePath).existsSync()) {
79-
print("Error: unable to locate the file with footer at ${footerFilePath}.");
80-
exit(1);
77+
List<String> footerFilePaths = args['footer'].map(_resolveTildePath).toList();
78+
print(footerFilePaths);
79+
for (String footerFilePath in footerFilePaths) {
80+
if (!new File(footerFilePath).existsSync()) {
81+
print("Error: unable to locate footer file: ${footerFilePath}.");
82+
exit(1);
83+
}
8184
}
82-
String headerFilePath = _resolveTildePath(args['header']);
83-
if (headerFilePath != null && !new File(headerFilePath).existsSync()) {
84-
print("Error: unable to locate the file with header at ${headerFilePath}.");
85-
exit(1);
85+
List<String> headerFilePaths = args['header'].map(_resolveTildePath).toList();
86+
print(headerFilePaths);
87+
for (String headerFilePath in footerFilePaths) {
88+
if (!new File(headerFilePath).existsSync()) {
89+
print("Error: unable to locate header file: ${headerFilePath}.");
90+
exit(1);
91+
}
8692
}
8793

8894
Directory outputDir =
@@ -112,7 +118,7 @@ main(List<String> arguments) async {
112118
print('');
113119

114120
var generators = await initGenerators(
115-
url, headerFilePath, footerFilePath, args['rel-canonical-prefix']);
121+
url, headerFilePaths, footerFilePaths, args['rel-canonical-prefix']);
116122

117123
for (var generator in generators) {
118124
generator.onFileCreated.listen(_onProgress);
@@ -163,7 +169,7 @@ ArgParser _createArgsParser() {
163169
parser.addFlag('version',
164170
help: 'Display the version for $name.', negatable: false);
165171
parser.addFlag('add-crossdart',
166-
help: 'Add Crossdart links to the source code pieces',
172+
help: 'Add Crossdart links to the source code pieces.',
167173
negatable: false,
168174
defaultsTo: false);
169175
parser.addOption('dart-sdk',
@@ -181,11 +187,11 @@ ArgParser _createArgsParser() {
181187
parser.addOption('output',
182188
help: 'Path to output directory.', defaultsTo: defaultOutDir);
183189
parser.addOption('header',
184-
help:
185-
'path to file containing HTML text, inserted into the header of every page.');
190+
allowMultiple: true,
191+
help: 'path to file containing HTML text.');
186192
parser.addOption('footer',
187-
help:
188-
'path to file containing HTML text, inserted into the footer of every page.');
193+
allowMultiple: true,
194+
help: 'path to file containing HTML text.');
189195
parser.addOption('exclude',
190196
help: 'Comma-separated list of library names to ignore.');
191197
parser.addOption('include',
@@ -194,9 +200,9 @@ ArgParser _createArgsParser() {
194200
help:
195201
'URL where the docs will be hosted (used to generate the sitemap).');
196202
parser.addOption('rel-canonical-prefix',
197-
help: 'If provided, add a rel="canonical" prefixed with provided value. '
203+
help: 'If provided, add a rel="canonical" prefixed with provided value. \n'
198204
'Consider using if building many versions of the docs for public SEO. '
199-
'Learn more at https://goo.gl/gktN6F');
205+
'Learn more at https://goo.gl/gktN6F.');
200206
parser.addFlag('include-source',
201207
help: 'If source code blocks should be shown, if they exist.',
202208
negatable: true,

lib/dartdoc.dart

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -44,13 +44,13 @@ const String version = '0.9.2';
4444
final String defaultOutDir = p.join('doc', 'api');
4545

4646
/// Initialize and setup the generators.
47-
Future<List<Generator>> initGenerators(String url, String headerFilePath,
48-
String footerFilePath, String relCanonicalPrefix) async {
47+
Future<List<Generator>> initGenerators(String url, List<String> headerFilePaths,
48+
List<String> footerFilePaths, String relCanonicalPrefix) async {
4949
return [
5050
await HtmlGenerator.create(
5151
url: url,
52-
header: headerFilePath,
53-
footer: footerFilePath,
52+
headers: headerFilePaths,
53+
footers: footerFilePaths,
5454
relCanonicalPrefix: relCanonicalPrefix,
5555
toolVersion: version)
5656
];

lib/src/html/html_generator.dart

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -48,12 +48,12 @@ class HtmlGenerator extends Generator {
4848
/// [url] - optional URL for where the docs will be hosted.
4949
static Future<HtmlGenerator> create(
5050
{String url,
51-
String header,
52-
String footer,
51+
List<String> headers,
52+
List<String> footers,
5353
String relCanonicalPrefix,
5454
String toolVersion}) async {
5555
var templates =
56-
await Templates.create(headerPath: header, footerPath: footer);
56+
await Templates.create(headerPaths: headers, footerPaths: footers);
5757

5858
if (toolVersion == null) {
5959
toolVersion = 'unknown';

lib/src/html/templates.dart

Lines changed: 13 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -32,22 +32,25 @@ const _partials = const <String>[
3232
];
3333

3434
Future<Map<String, String>> _loadPartials(
35-
String headerPath, String footerPath) async {
35+
List<String> headerPaths, List<String> footerPaths) async {
36+
headerPaths ??= [];
37+
footerPaths ??= [];
38+
3639
var partials = <String, String>{};
3740

3841
Future<String> _loadPartial(String templatePath) async {
3942
String template = await _getTemplateFile(templatePath);
40-
// TODO: revisit, not sure this is the right place for this logic
41-
if (templatePath.contains('_footer') && footerPath != null) {
42-
String footerValue = await new File(footerPath).readAsString();
43+
if (templatePath.contains('_head') && headerPaths.isNotEmpty) {
44+
String headerValue = headerPaths.map((path) => new File(path).readAsStringSync()).join('\n');
4345
template =
44-
template.replaceAll('<!-- Footer Placeholder -->', footerValue);
46+
template.replaceAll('<!-- Header Placeholder -->', headerValue);
4547
}
46-
if (templatePath.contains('_head') && headerPath != null) {
47-
String headerValue = await new File(headerPath).readAsString();
48+
if (templatePath.contains('_footer') && footerPaths.isNotEmpty) {
49+
String footerValue = footerPaths.map((path) => new File(path).readAsStringSync()).join('\n');
4850
template =
49-
template.replaceAll('<!-- Header Placeholder -->', headerValue);
51+
template.replaceAll('<!-- Footer Placeholder -->', footerValue);
5052
}
53+
template = template.replaceAll(' <!-- Do not remove placeholder -->\n', '');
5154
return template;
5255
}
5356

@@ -75,8 +78,8 @@ class Templates {
7578
final TemplateRenderer typeDefTemplate;
7679

7780
static Future<Templates> create(
78-
{String headerPath, String footerPath}) async {
79-
var partials = await _loadPartials(headerPath, footerPath);
81+
{List<String> headerPaths, List<String> footerPaths}) async {
82+
var partials = await _loadPartials(headerPaths, footerPaths);
8083

8184
String _partial(String name) {
8285
String partial = partials[name];

0 commit comments

Comments
 (0)