|
| 1 | +// Copyright (c) 2019, the Dart project authors. Please see the AUTHORS file |
| 2 | +// for details. All rights reserved. Use of this source code is governed by a |
| 3 | +// BSD-style license that can be found in the LICENSE file. |
| 4 | + |
| 5 | +/// A library for getting external source code links for Dartdoc. |
| 6 | +library dartdoc.source_linker; |
| 7 | + |
| 8 | +import 'package:dartdoc/src/dartdoc_options.dart'; |
| 9 | +import 'package:dartdoc/src/model.dart'; |
| 10 | +import 'package:path/path.dart' as pathLib; |
| 11 | + |
| 12 | +final uriTemplateRegexp = new RegExp(r'(%[frl]%)'); |
| 13 | + |
| 14 | +abstract class SourceLinkerOptionContext implements DartdocOptionContextBase { |
| 15 | + List<String> get linkToSourceExcludes => |
| 16 | + optionSet['linkToSource']['excludes'].valueAt(context); |
| 17 | + String get linkToSourceRevision => |
| 18 | + optionSet['linkToSource']['revision'].valueAt(context); |
| 19 | + String get linkToSourceRoot => |
| 20 | + optionSet['linkToSource']['root'].valueAt(context); |
| 21 | + String get linkToSourceUriTemplate => |
| 22 | + optionSet['linkToSource']['uriTemplate'].valueAt(context); |
| 23 | +} |
| 24 | + |
| 25 | +Future<List<DartdocOption>> createSourceLinkerOptions() async { |
| 26 | + return <DartdocOption>[ |
| 27 | + new DartdocOptionSet('linkToSource') |
| 28 | + ..addAll([ |
| 29 | + new DartdocOptionArgFile<List<String>>('excludes', [], |
| 30 | + isDir: true, |
| 31 | + help: |
| 32 | + 'A list of directories to exclude from linking to a source code repository.'), |
| 33 | + // TODO(jcollins-g): Use [DartdocOptionArgSynth], possibly in combination with a repository type and the root directory, and get revision number automatically |
| 34 | + new DartdocOptionArgOnly<String>('revision', null, |
| 35 | + help: 'Revision number to insert into the URI.'), |
| 36 | + new DartdocOptionArgFile<String>('root', null, |
| 37 | + isDir: true, |
| 38 | + help: |
| 39 | + 'Path to a local directory that is the root of the repository we link to. All source code files under this directory will be linked.'), |
| 40 | + new DartdocOptionArgFile<String>('uriTemplate', null, |
| 41 | + help: |
| 42 | + '''Substitute into this template to generate a uri for an element's source code. |
| 43 | + Dartdoc dynamically substitutes the following fields into the template: |
| 44 | + %f%: Relative path of file to the repository root |
| 45 | + %r%: Revision number |
| 46 | + %l%: Line number'''), |
| 47 | + ]) |
| 48 | + ]; |
| 49 | +} |
| 50 | + |
| 51 | +class SourceLinker { |
| 52 | + final List<String> excludes; |
| 53 | + final int lineNumber; |
| 54 | + final String sourceFileName; |
| 55 | + final String revision; |
| 56 | + final String root; |
| 57 | + final String uriTemplate; |
| 58 | + |
| 59 | + /// Most users of this class should use the [SourceLinker.fromElement] factory |
| 60 | + /// instead. This constructor is public for testing. |
| 61 | + SourceLinker( |
| 62 | + {List<String> this.excludes, |
| 63 | + int this.lineNumber, |
| 64 | + String this.sourceFileName, |
| 65 | + String this.revision, |
| 66 | + String this.root, |
| 67 | + String this.uriTemplate}) { |
| 68 | + assert(excludes != null, 'linkToSource excludes can not be null'); |
| 69 | + if (revision != null || root != null || uriTemplate != null) { |
| 70 | + if (root == null || uriTemplate == null) { |
| 71 | + throw DartdocOptionError( |
| 72 | + 'linkToSource root and uriTemplate must both be specified to generate repository links'); |
| 73 | + } |
| 74 | + if (uriTemplate.contains('%r%') && revision == null) { |
| 75 | + throw DartdocOptionError( |
| 76 | + r'%r% specified in uriTemplate, but no revision available'); |
| 77 | + } |
| 78 | + } |
| 79 | + } |
| 80 | + |
| 81 | + /// Build a SourceLinker from a ModelElement. |
| 82 | + factory SourceLinker.fromElement(ModelElement element) { |
| 83 | + SourceLinkerOptionContext config = element.config; |
| 84 | + return new SourceLinker( |
| 85 | + excludes: config.linkToSourceExcludes, |
| 86 | + // TODO(jcollins-g): disallow defaulting? Some elements come back without |
| 87 | + // a line number right now. |
| 88 | + lineNumber: element.lineAndColumn?.item1 ?? 1, |
| 89 | + sourceFileName: element.sourceFileName, |
| 90 | + revision: config.linkToSourceRevision, |
| 91 | + root: config.linkToSourceRoot, |
| 92 | + uriTemplate: config.linkToSourceUriTemplate, |
| 93 | + ); |
| 94 | + } |
| 95 | + |
| 96 | + String href() { |
| 97 | + if (sourceFileName == null || root == null || uriTemplate == null) |
| 98 | + return ''; |
| 99 | + if (!pathLib.isWithin(root, sourceFileName) || |
| 100 | + excludes |
| 101 | + .any((String exclude) => pathLib.isWithin(exclude, sourceFileName))) |
| 102 | + return ''; |
| 103 | + return uriTemplate.replaceAllMapped(uriTemplateRegexp, (match) { |
| 104 | + switch (match[1]) { |
| 105 | + case '%f%': |
| 106 | + var urlContext = new pathLib.Context(style: pathLib.Style.url); |
| 107 | + return urlContext.joinAll( |
| 108 | + pathLib.split(pathLib.relative(sourceFileName, from: root))); |
| 109 | + break; |
| 110 | + case '%r%': |
| 111 | + return revision; |
| 112 | + break; |
| 113 | + case '%l%': |
| 114 | + return lineNumber.toString(); |
| 115 | + break; |
| 116 | + } |
| 117 | + }); |
| 118 | + } |
| 119 | +} |
0 commit comments