Skip to content

Commit 266b5b1

Browse files
chalindevoncarew
authored andcommitted
feat: support --example-path-prefix (#1302)
1 parent 38a7863 commit 266b5b1

File tree

9 files changed

+25
-13
lines changed

9 files changed

+25
-13
lines changed

bin/dartdoc.dart

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -139,6 +139,7 @@ main(List<String> arguments) async {
139139

140140
initializeConfig(
141141
addCrossdart: addCrossdart,
142+
examplePathPrefix: args['example-path-prefix'],
142143
includeSource: includeSource,
143144
inputDir: inputDir,
144145
sdkVersion: sdk.sdkVersion);
@@ -199,6 +200,8 @@ ArgParser _createArgsParser() {
199200
parser.addOption('hosted-url',
200201
help:
201202
'URL where the docs will be hosted (used to generate the sitemap).');
203+
parser.addOption('example-path-prefix',
204+
help: 'Prefix for @example paths. Default is project root.');
202205
parser.addOption('rel-canonical-prefix',
203206
help:
204207
'If provided, add a rel="canonical" prefixed with provided value. \n'

lib/dartdoc.dart

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,11 +65,13 @@ void initializeConfig(
6565
{Directory inputDir,
6666
String sdkVersion,
6767
bool addCrossdart: false,
68+
String examplePathPrefix,
6869
bool includeSource: true}) {
6970
setConfig(
7071
inputDir: inputDir,
7172
sdkVersion: sdkVersion,
7273
addCrossdart: addCrossdart,
74+
examplePathPrefix: examplePathPrefix,
7375
includeSource: includeSource);
7476
}
7577

lib/src/config.dart

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,10 +9,11 @@ import 'dart:io';
99
class Config {
1010
final Directory inputDir;
1111
final bool addCrossdart;
12+
final String examplePathPrefix;
1213
final bool includeSource;
1314
final String sdkVersion;
1415
Config._(
15-
this.inputDir, this.addCrossdart, this.includeSource, this.sdkVersion);
16+
this.inputDir, this.addCrossdart, this.examplePathPrefix, this.includeSource, this.sdkVersion);
1617
}
1718

1819
Config _config;
@@ -22,6 +23,7 @@ void setConfig(
2223
{Directory inputDir,
2324
String sdkVersion,
2425
bool addCrossdart: false,
26+
String examplePathPrefix,
2527
bool includeSource: true}) {
26-
_config = new Config._(inputDir, addCrossdart, includeSource, sdkVersion);
28+
_config = new Config._(inputDir, addCrossdart, examplePathPrefix, includeSource, sdkVersion);
2729
}

lib/src/model.dart

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1793,7 +1793,8 @@ abstract class ModelElement implements Comparable, Nameable, Documentable {
17931793
RegExp keyValueRE = new RegExp('(\\w+)=[\'"]?(\\S*)[\'"]?');
17941794
Iterable<Match> matches = keyValueRE.allMatches(namedArgs);
17951795
matches.forEach((match) {
1796-
args[match[1]] = match[2];
1796+
// Ignore optional quotes
1797+
args[match[1]] = match[2].replaceAll(new RegExp('[\'"]'), '');
17971798
});
17981799

17991800
// Compute 'file'
@@ -1806,7 +1807,7 @@ abstract class ModelElement implements Comparable, Nameable, Documentable {
18061807
var ext = p.extension(src);
18071808
file = p.join(dir, '$basename-$region$ext$fragExtension');
18081809
}
1809-
args['file'] = file;
1810+
args['file'] = config.examplePathPrefix == null ? file : p.join(config.examplePathPrefix, file);
18101811
return args;
18111812
}
18121813
}

test/compare_output_test.dart

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,8 @@ void main() {
5050

5151
var args = <String>[
5252
dartdocBin,
53+
'--example-path-prefix',
54+
'examples',
5355
'--no-include-source',
5456
'--output',
5557
tempDir.path
@@ -133,9 +135,14 @@ void main() {
133135
fail('dartdoc failed');
134136
}
135137

136-
if (!result.stderr
137-
.contains(new RegExp(r'warning:.*file-does-not-exist\.js'))) {
138-
fail('Missing warning for nonexistent @example: \nstdout: ${result.stdout} \nstderr: ${result.stderr}');
138+
// Examples are reported as unfound because we (purposefully)
139+
// did not use --example-path-prefix above.
140+
final sep = '.'; // We don't care what the path separator character is
141+
final firstUnfoundExample = new RegExp('warning: lib${sep}example.dart: '
142+
'@example file not found.*test_package${sep}dog${sep}food.md');
143+
if (!result.stderr.contains(firstUnfoundExample)) {
144+
fail('Should warn about unfound @example files: \n'
145+
'stdout:\n${result.stdout}\nstderr:\n${result.stderr}');
139146
}
140147
});
141148

@@ -156,7 +163,6 @@ void main() {
156163
print(result.stderr);
157164
fail('dartdoc failed');
158165
}
159-
160166
});
161167
}, onPlatform: {'windows': new Skip('Avoiding parsing git output')});
162168
}
File renamed without changes.
File renamed without changes.

testing/test_package/lib/example.dart

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -198,11 +198,10 @@ class ConstantCat implements Cat {
198198

199199
/// implements [Cat], [E]
200200
///
201-
/// {@example examples/dog_food}
202-
/// {@example examples/dog_food.txt region=meat}
201+
/// {@example dog/food}
202+
/// {@example dog/food.txt region=meat}
203203
///
204-
/// {@example examples/test.dart region= lang=html}
205-
/// {@example examples/file-does-not-exist.js}
204+
/// {@example test.dart region= lang=html}
206205
class Dog implements Cat, E {
207206
String name;
208207

testing/test_package_docs/ex/Dog-class.html

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -141,7 +141,6 @@ <h5><a href="ex/ex-library.html">ex</a></h5>
141141
A selection of dog flavors:</p><ul><li>beef</li><li>poultry</li></ul>
142142
<pre class="language-html prettyprint"><code class="language-html">&lt;h1&gt;Hello &lt;b&gt;World&lt;/b&gt;!&lt;/h1&gt;
143143
</code></pre>
144-
<p>{@example examples/file-does-not-exist.js}</p>
145144
</section>
146145

147146
<section>

0 commit comments

Comments
 (0)