Skip to content

Commit ff8c3cc

Browse files
committed
Add example and use it in the dartdoc package.
1 parent 102090e commit ff8c3cc

File tree

5 files changed

+66
-10
lines changed

5 files changed

+66
-10
lines changed

README.md

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -163,6 +163,19 @@ Unrecognized options will be ignored. Supported options:
163163
No branch is considered to be "stable".
164164
* `%n%`: The name of this package, as defined in pubspec.yaml.
165165
* `%v%`: The version of this package as defined in pubspec.yaml.
166+
* **linkToSource**: Generate links to a source code repository based on given templates and
167+
revision information.
168+
* **excludes**: A list of directories to exclude from processing source links.
169+
* **root**: The directory to consider the 'root' for inserting relative paths into the template.
170+
Source code outside the root directory will not be linked.
171+
* **uriTemplate**: A template to substitute revision and file path information. If revision
172+
is present in the template but not specified, or if root is not specified, dartdoc will
173+
throw an exception. To hard-code a revision, don't specify it with `%r%`.
174+
175+
The following strings will be substituted in to complete the URL:
176+
* `%f%`: Relative path of file to the repository root
177+
* `%r%`: Revision
178+
* `%l%`: Line number
166179
* **warnings**: Specify otherwise ignored or set-to-error warnings to simply warn. See the lists
167180
of valid warnings in the command line help for `--errors`, `--warnings`, and `--ignore`.
168181

@@ -391,6 +404,35 @@ other elements on the page, since those may change in future versions of Dartdoc
391404
If `--auto-include-dependencies` flag is provided, dartdoc tries to automatically add
392405
all the used libraries, even from other packages, to the list of the documented libraries.
393406

407+
### Using link-to-source
408+
409+
The source linking feature in dartdoc is a little tricky to use, since pub packages do not actually
410+
include enough information to link back to source code and that's the context in which documentation
411+
is generated for the pub site. This means that for now, it must be manually specified in
412+
dartdoc_options.yaml what revision to use. It is currently recommended practice to
413+
specify a revision in dartdoc_options.yaml that points to the same revision as your public package.
414+
If you're using a documentation staging system outside of Dart's pub site, override the template and
415+
revision on the command line with the head revision number. You can use the branch name,
416+
but generated docs will generate locations that may start drifting with further changes to the branch.
417+
418+
Example dartdoc_options.yaml:
419+
```yaml
420+
link-to-source:
421+
root: '.'
422+
uriTemplate: 'https://github.com/dart-lang/dartdoc/blob/v0.28.0/%f%#L%l%'
423+
```
424+
425+
Example staging command line:
426+
```bash
427+
pub global run dartdoc --link-to-source-root '.' --link-to-source-revision 6fac6f770d271312c88e8ae881861702a9a605be --link-to-source-uri-template 'https://github.com/dart-lang/dartdoc/blob/%r%/%f#L%l%'
428+
```
429+
430+
This gets more complicated with `--auto-include-dependencies` as these command line flags
431+
will override all settings from individual packages. In that case, to preserve
432+
source links from third party packages it may be necessary to generate
433+
dartdoc_options.yaml options for each package you are intending to add source links
434+
to yourself.
435+
394436
## Issues and bugs
395437

396438
Please file reports on the [GitHub Issue Tracker][]. Issues are labeled with

dartdoc_options.yaml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
dartdoc:
2+
linkToSource:
3+
root: '.'
4+
uriTemplate: 'https://github.com/dart-lang/dartdoc/blob/v0.28.0/%f%#L%l%'

lib/src/source_linker.dart

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -13,18 +13,18 @@ final uriTemplateRegexp = new RegExp(r'(%[frl]%)');
1313

1414
abstract class SourceLinkerOptionContext implements DartdocOptionContextBase {
1515
List<String> get linkToSourceExcludes =>
16-
optionSet['link-to-source']['excludes'].valueAt(context);
16+
optionSet['linkToSource']['excludes'].valueAt(context);
1717
String get linkToSourceRevision =>
18-
optionSet['link-to-source']['revision'].valueAt(context);
18+
optionSet['linkToSource']['revision'].valueAt(context);
1919
String get linkToSourceRoot =>
20-
optionSet['link-to-source']['root'].valueAt(context);
20+
optionSet['linkToSource']['root'].valueAt(context);
2121
String get linkToSourceUriTemplate =>
22-
optionSet['link-to-source']['uriTemplate'].valueAt(context);
22+
optionSet['linkToSource']['uriTemplate'].valueAt(context);
2323
}
2424

2525
Future<List<DartdocOption>> createSourceLinkerOptions() async {
2626
return <DartdocOption>[
27-
new DartdocOptionSet('link-to-source')
27+
new DartdocOptionSet('linkToSource')
2828
..addAll([
2929
new DartdocOptionArgFile<List<String>>('excludes', [],
3030
isDir: true,
@@ -65,11 +65,11 @@ class SourceLinker {
6565
String this.revision,
6666
String this.root,
6767
String this.uriTemplate}) {
68-
assert(excludes != null, 'link-to-source-excludes can not be null');
68+
assert(excludes != null, 'linkToSource excludes can not be null');
6969
if (revision != null || root != null || uriTemplate != null) {
7070
if (root == null || uriTemplate == null) {
7171
throw DartdocOptionError(
72-
'link-to-source root and uriTemplate must both be specified to generate repository links');
72+
'linkToSource root and uriTemplate must both be specified to generate repository links');
7373
}
7474
if (uriTemplate.contains('%r%') && revision == null) {
7575
throw DartdocOptionError(

testing/test_package/dartdoc_options.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,6 @@ dartdoc:
1616
linux: ['/bin/sh', '-c', 'echo']
1717
windows: ['C:\\Windows\\System32\\cmd.exe', '/c', 'echo']
1818
description: 'Works on everything'
19-
link-to-source:
19+
linkToSource:
2020
root: '.'
2121
uriTemplate: 'https://github.com/dart-lang/dartdoc/blob/master/testing/test_package/%f%#L%l%'

tool/grind.dart

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -779,10 +779,21 @@ Future<void> build() async {
779779
var launcher = new SubprocessLauncher('build');
780780
await launcher.runStreamed(sdkBin('pub'),
781781
['run', 'build_runner', 'build', '--delete-conflicting-outputs']);
782+
783+
// TODO(jcollins-g): port to build system?
784+
String version = _getPackageVersion();
785+
File dartdoc_options = new File('dartdoc_options.yaml');
786+
await dartdoc_options.writeAsString(
787+
'''dartdoc:
788+
linkToSource:
789+
root: '.'
790+
uriTemplate: 'https://github.com/dart-lang/dartdoc/blob/v${version}/%f%#L%l%'
791+
''');
782792
}
783793

784794
/// Paths in this list are relative to lib/.
785795
final _generated_files_list = <String>[
796+
'../dartdoc_options.yaml',
786797
'src/html/resources.g.dart',
787798
'src/version.dart',
788799
].map((s) => pathLib.joinAll(pathLib.posix.split(s)));
@@ -803,8 +814,7 @@ Future<void> checkBuild() async {
803814
}
804815
}
805816

806-
await launcher.runStreamed(sdkBin('pub'),
807-
['run', 'build_runner', 'build', '--delete-conflicting-outputs']);
817+
await build();
808818
for (String relPath in _generated_files_list) {
809819
File newVersion = new File(pathLib.join('lib', relPath));
810820
if (!await newVersion.exists()) {

0 commit comments

Comments
 (0)